1+ // / @file log/logger.hpp
2+ // /
3+ // / @brief Defines logging infrastructure
14#pragma once
25
36#include < cstdint>
1619namespace viam {
1720namespace sdk {
1821
22+ // / @defgroup Log Classes related to logging.
23+
24+ // / @brief Severity levels for the logger.
25+ // / @ingroup Log
1926enum class log_level : std::int8_t {
2027 trace = -2 ,
2128 debug = -1 ,
@@ -31,10 +38,23 @@ log_level level_from_string(std::string level);
3138
3239std::ostream& operator <<(std::ostream&, log_level);
3340
41+ // / @brief Type alias for the log source in the C++ SDK.
42+ // / @ingroup Log
43+ // /
44+ // / In the paradigm of Boost.Log the C++ SDK has precisely one logging source, namely the source of
45+ // / messages generated by the user invoking one of the logging macros.
3446using LogSource = boost::log::sources::severity_channel_logger_mt<log_level>;
3547
48+ // / @brief Returns the "channel name" of general log messages not originating from resources.
49+ // / @ingroup Log
3650std::string global_resource_name ();
3751
52+ // / @class Logger logger.hpp "log/logger.hpp"
53+ // / @brief Manages the logging infrastructure in the SDDK.
54+ // / @ingroup Log
55+ // /
56+ // / Handles initialization and bookkeeping for logging infrastructure in the C++ SDK. This includes
57+ // / console logging, if active, and both global and resource-level log filtering.
3858class Logger {
3959 public:
4060 struct Filter {
@@ -43,29 +63,47 @@ class Logger {
4363 bool operator ()(const boost::log::attribute_value_set&);
4464 };
4565
66+ // / @brief Returns the unique logger instance.
67+ // /
68+ // / This is the only way to access the logger.
4669 static Logger& get ();
4770
71+ // / @brief Set the global logger severity.
4872 void set_global_log_level (log_level);
4973
50- // / @brief Set the SDK logger severity from a command line argument vector.
74+ // / @brief Set the global logger severity from a command line argument vector.
5175 // /
52- // / Sets the boost trivial logger's severity to debug if "--log-level=debug" is the
76+ // / Sets severity to debug if "--log-level=debug" is
5377 // / the third argument. Sets severity to info otherwise. Useful for module
5478 // / implementations. See LogLevel documentation in the RDK for more information on
5579 // / how to start modules with a "log-level" commandline argument.
5680 void set_global_log_level (int argc, char ** argv);
5781
82+ // / @brief Set the logger severity for a resource.
83+ // /
84+ // / Logger can maintain separate severity levels for individual resources. For example, you may
85+ // / want to only report "error" messages for global logs and logs for one component of a modular
86+ // / resource, but enable verbose logging for a new component that is still being debugged.
87+ // / @see @ref Resource, which has a set_log_level method which automatically provides its own
88+ // / resource name.
5889 void set_resource_log_level (const std::string& resource, log_level);
5990
60- LogSource& logger () {
61- return sdk_logger_;
62- }
91+ // / @brief Return the SDK global log source.
92+ // /
93+ // / Users should prefer to log messages using the logging macros below.
94+ LogSource& logger ();
6395
6496 private:
6597 friend class RobotClient ;
6698 friend class Instance ;
6799 Logger () = default ;
68100
101+ Logger (const Logger&) = delete ;
102+ Logger (Logger&&) = delete ;
103+
104+ Logger& operator =(const Logger&) = delete ;
105+ Logger& operator =(Logger&&) = delete ;
106+
69107 void init_logging ();
70108 void disable_console_logging ();
71109
@@ -86,14 +124,23 @@ BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_time,
86124 " TimeStamp" ,
87125 boost::log::attributes::local_clock::value_type);
88126
89- // Logging macro for general SDK logs
90127#define VIAM_LOG_IMPL (lg, level ) \
91128 BOOST_LOG_SEV ((lg), ::viam::sdk::log_level::level) \
92129 << ::boost::log::add_value(::viam::sdk::attr_file_type{}, __FILE__) \
93130 << ::boost::log::add_value(::viam::sdk::attr_line_type{}, __LINE__)
94131
132+ // / @brief Log macro for general SDK logs.
133+ // / @ingroup Log
134+ // /
135+ // / Use this macro to generate log messages pertaining to the SDK at large.
95136#define VIAM_LOG (level ) VIAM_LOG_IMPL(::viam::sdk::Logger::get().logger(), level)
96137
138+ // / @brief Log macro for resource-level logs.
139+ // / @ingroup Log
140+ // /
141+ // / This macro can only be called from the definition of a member function of a class inheriting
142+ // / @ref Resource. It will log messages to the log source of that specific resource, allowing
143+ // / resource-level log filtering.
97144#define VIAM_RESOURCE_LOG (level ) VIAM_LOG_IMPL(this ->logger_, level)
98145
99146} // namespace sdk
0 commit comments