Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
rs.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #ifndef LIBREALSENSE_RS2_HPP
5 #define LIBREALSENSE_RS2_HPP
6 
7 #include "rs.h"
8 #include "hpp/rs_types.hpp"
9 #include "hpp/rs_context.hpp"
10 #include "hpp/rs_device.hpp"
11 #include "hpp/rs_frame.hpp"
12 #include "hpp/rs_processing.hpp"
14 #include "hpp/rs_sensor.hpp"
15 #include "hpp/rs_pipeline.hpp"
16 
17 namespace rs2
18 {
19  inline void log_to_console(rs2_log_severity min_severity)
20  {
21  rs2_error* e = nullptr;
22  rs2_log_to_console(min_severity, &e);
23  error::handle(e);
24  }
25 
26  inline void log_to_file(rs2_log_severity min_severity, const char * file_path = nullptr)
27  {
28  rs2_error* e = nullptr;
29  rs2_log_to_file(min_severity, file_path, &e);
30  error::handle(e);
31  }
32 
33  /*
34  Interface to the log message data we expose.
35  */
37  {
38  // Only log_callback should be creating us!
39  template< class T > friend class log_callback;
40 
41  log_message( rs2_log_message const & msg ) : _msg( msg ) {}
42 
43  public:
44  /* Returns the line-number in the file where the LOG() comment was issued */
45  size_t line_number() const
46  {
47  rs2_error* e = nullptr;
48  size_t ln = rs2_get_log_message_line_number( &_msg, &e );
49  error::handle( e );
50  return ln;
51  }
52  /* Returns the file in which the LOG() command was issued */
53  const char * filename() const
54  {
55  rs2_error* e = nullptr;
56  const char * path = rs2_get_log_message_filename( &_msg, &e );
57  error::handle( e );
58  return path;
59  }
60  /* Returns the raw message, as it was passed to LOG(), before any embelishments like level etc. */
61  const char* raw() const
62  {
63  rs2_error* e = nullptr;
64  const char* r = rs2_get_raw_log_message( &_msg, &e );
65  error::handle( e );
66  return r;
67  }
68  /*
69  Returns a complete log message, as defined by librealsense, with level, timestamp, etc.:
70  11/12 13:49:40,153 INFO [10604] (rs.cpp:2271) Framebuffer size changed to 1552 x 919
71  */
72  const char* full() const
73  {
74  rs2_error* e = nullptr;
75  const char* str = rs2_get_full_log_message( &_msg, &e );
76  error::handle( e );
77  return str;
78  }
79 
80  private:
81  rs2_log_message const & _msg;
82  };
83 
84  /*
85  Wrapper around any callback function that is given to log_to_callback.
86  */
87  template<class T>
89  {
90  T on_log_function;
91  public:
92  explicit log_callback( T on_log ) : on_log_function( on_log ) {}
93 
94  void on_log( rs2_log_severity severity, rs2_log_message const & msg ) noexcept override
95  {
96  on_log_function( severity, log_message( msg ));
97  }
98 
99  void release() override { delete this; }
100  };
101 
102  /*
103  Your callback should look like this, for example:
104  void callback( rs2_log_severity severity, rs2::log_message const & msg ) noexcept
105  {
106  std::cout << msg.build() << std::endl;
107  }
108  and, when initializing rs2:
109  rs2::log_to_callback( callback );
110  or:
111  rs2::log_to_callback(
112  []( rs2_log_severity severity, rs2::log_message const & msg ) noexcept
113  {
114  std::cout << msg.build() << std::endl;
115  })
116  */
117  template< typename S >
118  inline void log_to_callback( rs2_log_severity min_severity, S callback )
119  {
120  // We wrap the callback with an interface and pass it to librealsense, who will
121  // now manage its lifetime. Rather than deleting it, though, it will call its
122  // release() function, where (back in our context) it can be safely deleted:
123  rs2_error* e = nullptr;
124  rs2_log_to_callback_cpp( min_severity, new log_callback< S >( std::move( callback )), &e );
125  error::handle( e );
126  }
127 
128  inline void log(rs2_log_severity severity, const char* message)
129  {
130  rs2_error* e = nullptr;
131  rs2_log(severity, message, &e);
132  error::handle(e);
133  }
134 }
135 
136 inline std::ostream & operator << (std::ostream & o, rs2_stream stream) { return o << rs2_stream_to_string(stream); }
137 inline std::ostream & operator << (std::ostream & o, rs2_format format) { return o << rs2_format_to_string(format); }
138 inline std::ostream & operator << (std::ostream & o, rs2_distortion distortion) { return o << rs2_distortion_to_string(distortion); }
139 inline std::ostream & operator << (std::ostream & o, rs2_option option) { return o << rs2_option_to_string(option); } // This function is being deprecated. For existing options it will return option name, but for future API additions the user should call rs2_get_option_name instead.
140 inline std::ostream & operator << (std::ostream & o, rs2_log_severity severity) { return o << rs2_log_severity_to_string(severity); }
141 inline std::ostream & operator << (std::ostream & o, rs2::log_message const & msg ) { return o << msg.raw(); }
142 inline std::ostream & operator << (std::ostream & o, rs2_camera_info camera_info) { return o << rs2_camera_info_to_string(camera_info); }
143 inline std::ostream & operator << (std::ostream & o, rs2_frame_metadata_value metadata) { return o << rs2_frame_metadata_to_string(metadata); }
144 inline std::ostream & operator << (std::ostream & o, rs2_timestamp_domain domain) { return o << rs2_timestamp_domain_to_string(domain); }
145 inline std::ostream & operator << (std::ostream & o, rs2_notification_category notificaton) { return o << rs2_notification_category_to_string(notificaton); }
146 inline std::ostream & operator << (std::ostream & o, rs2_sr300_visual_preset preset) { return o << rs2_sr300_visual_preset_to_string(preset); }
147 inline std::ostream & operator << (std::ostream & o, rs2_exception_type exception_type) { return o << rs2_exception_type_to_string(exception_type); }
148 inline std::ostream & operator << (std::ostream & o, rs2_playback_status status) { return o << rs2_playback_status_to_string(status); }
149 inline std::ostream & operator << (std::ostream & o, rs2_l500_visual_preset preset) {return o << rs2_l500_visual_preset_to_string(preset);}
150 inline std::ostream & operator << (std::ostream & o, rs2_sensor_mode mode) { return o << rs2_sensor_mode_to_string(mode); }
151 
152 #endif // LIBREALSENSE_RS2_HPP
void on_log(rs2_log_severity severity, rs2_log_message const &msg) noexcept override
Definition: rs.hpp:94
const char * rs2_sensor_mode_to_string(rs2_sensor_mode preset)
rs2_camera_info
Read-only strings that can be queried from the device. Not all information attributes are available o...
Definition: rs_sensor.h:22
const char * rs2_format_to_string(rs2_format format)
rs2_exception_type
Exception types are the different categories of errors that RealSense API might return.
Definition: rs_types.h:30
const char * rs2_timestamp_domain_to_string(rs2_timestamp_domain info)
const char * rs2_frame_metadata_to_string(rs2_frame_metadata_value metadata)
void log_to_callback(rs2_log_severity min_severity, S callback)
Definition: rs.hpp:118
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
const char * rs2_get_full_log_message(rs2_log_message const *msg, rs2_error **error)
rs2_distortion
Distortion model: defines how pixel coordinates should be mapped to sensor coordinates.
Definition: rs_types.h:45
rs2_sr300_visual_preset
For SR300 devices: provides optimized settings (presets) for specific types of usage.
Definition: rs_option.h:104
const char * rs2_option_to_string(rs2_option option)
void rs2_log_to_callback_cpp(rs2_log_severity min_severity, rs2_log_callback *callback, rs2_error **error)
const char * full() const
Definition: rs.hpp:72
const char * rs2_distortion_to_string(rs2_distortion distortion)
void rs2_log_to_file(rs2_log_severity min_severity, const char *file_path, rs2_error **error)
const char * rs2_get_log_message_filename(rs2_log_message const *msg, rs2_error **error)
void log(rs2_log_severity severity, const char *message)
Definition: rs.hpp:128
unsigned rs2_get_log_message_line_number(rs2_log_message const *msg, rs2_error **error)
Definition: rs_context.hpp:11
void rs2_log(rs2_log_severity severity, const char *message, rs2_error **error)
const char * raw() const
Definition: rs.hpp:61
const char * filename() const
Definition: rs.hpp:53
const char * rs2_sr300_visual_preset_to_string(rs2_sr300_visual_preset preset)
Exposes librealsense functionality for C compilers.
Definition: rs_types.hpp:55
Definition: rs.hpp:36
const char * rs2_exception_type_to_string(rs2_exception_type type)
void release() override
Definition: rs.hpp:99
Definition: rs.hpp:88
size_t line_number() const
Definition: rs.hpp:45
rs2_playback_status
Definition: rs_record_playback.h:19
struct rs2_log_message rs2_log_message
Definition: rs_types.h:218
rs2_format
A stream&#39;s format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:59
const char * rs2_get_raw_log_message(rs2_log_message const *msg, rs2_error **error)
void log_to_file(rs2_log_severity min_severity, const char *file_path=nullptr)
Definition: rs.hpp:26
static void handle(rs2_error *e)
Definition: rs_types.hpp:137
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:42
rs2_sensor_mode
For setting the camera_mode option.
Definition: rs_option.h:149
void log_to_console(rs2_log_severity min_severity)
Definition: rs.hpp:19
const char * rs2_camera_info_to_string(rs2_camera_info info)
void rs2_log_to_console(rs2_log_severity min_severity, rs2_error **error)
log_callback(T on_log)
Definition: rs.hpp:92
const char * rs2_stream_to_string(rs2_stream stream)
rs2_notification_category
Category of the librealsense notification.
Definition: rs_types.h:17
const char * rs2_playback_status_to_string(rs2_playback_status status)
std::ostream & operator<<(std::ostream &o, rs2_stream stream)
Definition: rs.hpp:136
const char * rs2_notification_category_to_string(rs2_notification_category category)
const char * rs2_log_severity_to_string(rs2_log_severity info)
struct rs2_error rs2_error
Definition: rs_types.h:217
rs2_log_severity
Severity of the librealsense logger.
Definition: rs_types.h:121
const char * rs2_l500_visual_preset_to_string(rs2_l500_visual_preset preset)
rs2_frame_metadata_value
Per-Frame-Metadata is the set of read-only properties that might be exposed for each individual frame...
Definition: rs_frame.h:29
rs2_l500_visual_preset
For L500 devices: provides optimized settings (presets) for specific types of usage.
Definition: rs_option.h:136
rs2_timestamp_domain
Specifies the clock in relation to which the frame timestamp was measured.
Definition: rs_frame.h:19