Viam C++ SDK current
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <cstdint>
7
8#include <map>
9#include <memory>
10#include <ostream>
11
12#include <boost/log/attributes/clock.hpp>
13#include <boost/log/expressions/keyword.hpp>
14#include <boost/log/sinks/sync_frontend.hpp>
15#include <boost/log/sinks/text_ostream_backend.hpp>
16#include <boost/log/sources/severity_channel_logger.hpp>
17#include <boost/log/utility/manipulators/add_value.hpp>
18#include <boost/utility/string_view.hpp>
19
20namespace viam {
21namespace sdk {
22
24
30enum class log_level : std::int8_t {
31 trace = -2,
32 debug = -1,
33 info = 0, // default value is info
34 warn = 1,
35 error = 2,
36 fatal = 3,
37};
38
39std::string to_string(log_level);
40
41log_level level_from_string(std::string level);
42
43std::ostream& operator<<(std::ostream&, log_level);
44
50using LogSource = boost::log::sources::severity_channel_logger_mt<log_level>;
51
55
58const char* default_module_name();
59
67 public:
68 struct Filter {
69 const LogManager* parent;
70
71 bool operator()(const boost::log::attribute_value_set&) const;
72 };
73
77 static LogManager& get();
78
80 void set_global_resource_name(const std::string& name);
81
84
91 void set_global_log_level(int argc, char** argv);
92
96 void set_module_name(const std::string& name);
97
100
109 void set_resource_log_level(const std::string& resource, log_level);
110
115
120
121 private:
122 friend class RobotClient;
123 friend class Instance;
124 LogManager() = default;
125
126 LogManager(const LogManager&) = delete;
127 LogManager(LogManager&&) = delete;
128
129 LogManager& operator=(const LogManager&) = delete;
130 LogManager& operator=(LogManager&&) = delete;
131
132 void init_logging();
133 void enable_console_logging();
134 void disable_console_logging();
135
136 LogSource sdk_logger_;
137
138 LogSource module_logger_;
139
140 boost::shared_ptr<boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>>
141 console_sink_;
142
143 log_level global_level_{log_level::info};
144 log_level module_level_{log_level::info};
145
146 std::map<std::string, log_level> resource_levels_;
147};
148
149namespace log_detail {
150
151// Some of the filenames in the SDK are not unique, eg config/resource.hpp, resource/resource.hpp.
152// This function trims a full filename /path/to/some/file.xpp to some/file.xpp
153boost::string_view trim_filename(const char* file);
154
155} // namespace log_detail
156
157BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_channel, "Channel", std::string);
158BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_sev, "Severity", viam::sdk::log_level);
159BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_file, "file", boost::string_view);
160BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_line, "line", unsigned int);
161BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_time,
162 "TimeStamp",
163 boost::log::attributes::local_clock::value_type);
164
165#define VIAM_SDK_LOG_IMPL(lg, level) \
166 BOOST_LOG_SEV((lg), ::viam::sdk::log_level::level) \
167 << ::boost::log::add_value(::viam::sdk::attr_file_type{}, \
168 ::viam::sdk::log_detail::trim_filename(__FILE__)) \
169 << ::boost::log::add_value(::viam::sdk::attr_line_type{}, __LINE__)
170
175#define VIAM_SDK_LOG(level) VIAM_SDK_LOG_IMPL(::viam::sdk::LogManager::get().global_logger(), level)
176
181#define VIAM_MODULE_LOG(level) \
182 VIAM_SDK_LOG_IMPL(::viam::sdk::LogManager::get().module_logger(), level)
183
190#define VIAM_RESOURCE_LOG(level) VIAM_SDK_LOG_IMPL(this->logger_, level)
191
192} // namespace sdk
193} // namespace viam
Instance management for Viam C++ SDK applications. This is a single instance class which is responsib...
Definition instance.hpp:13
Manages the logging infrastructure in the SDDK.
Definition logging.hpp:66
static LogManager & get()
Returns the unique logger instance.
void set_module_log_level(log_level)
Set the module logger severity.
LogSource & module_logger()
Return the SDK module log source.
void set_module_name(const std::string &name)
Set the channel name of log messages to be associated with a C++ module.
void set_global_log_level(log_level)
Set the global logger severity.
void set_resource_log_level(const std::string &resource, log_level)
Set the logger severity for a resource.
LogSource & global_logger()
Return the SDK global log source.
void set_global_log_level(int argc, char **argv)
Set the global logger severity from a command line argument vector.
void set_global_resource_name(const std::string &name)
Override the channel name of general log messages not originating from resources.
gRPC client for a robot, to be used for all interactions with a robot. There are two ways to instanti...
Definition client.hpp:46
log_level
Severity levels for the logger.
Definition logging.hpp:30
const char * default_module_name()
Returns the default "channel name" of log messages related to a Viam C++ module.
boost::log::sources::severity_channel_logger_mt< log_level > LogSource
Type alias for the log source in the C++ SDK.
Definition logging.hpp:50
const char * global_resource_name()
Returns the "channel name" of general log messages related to the Viam C++ SDK.
Definition logging.hpp:68