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/preprocessor/facilities/overload.hpp>
19#include <boost/utility/string_view.hpp>
20
21namespace viam {
22namespace sdk {
23
25
31enum class log_level : std::int8_t {
32 trace = -2,
33 debug = -1,
34 info = 0, // default value is info
35 warn = 1,
36 error = 2,
37 fatal = 3,
38};
39
40std::string to_string(log_level);
41
42log_level level_from_string(std::string level);
43
44std::ostream& operator<<(std::ostream&, log_level);
45
51using LogSource = boost::log::sources::severity_channel_logger_mt<log_level>;
52
56
59const char* default_module_name();
60
61class ResourceConfig;
62
70 public:
71 struct Filter {
72 const LogManager* parent;
73
74 bool operator()(const boost::log::attribute_value_set&) const;
75 };
76
80 static LogManager& get();
81
83 void set_global_resource_name(const std::string& name);
84
87
94 void set_global_log_level(int argc, char** argv);
95
99 void set_module_name(const std::string& name);
100
103
112 void set_resource_log_level(const std::string& resource, log_level);
113
118
123
126 std::string module_channel_for(const ResourceConfig& cfg);
127
128 private:
129 friend class RobotClient;
130 friend class Instance;
131 LogManager() = default;
132
133 LogManager(const LogManager&) = delete;
134 LogManager(LogManager&&) = delete;
135
136 LogManager& operator=(const LogManager&) = delete;
137 LogManager& operator=(LogManager&&) = delete;
138
139 void init_logging();
140 void enable_console_logging();
141 void disable_console_logging();
142
143 LogSource sdk_logger_;
144
145 LogSource module_logger_;
146
147 boost::shared_ptr<boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>>
148 console_sink_;
149
150 log_level global_level_{log_level::info};
151 log_level module_level_{log_level::info};
152
153 std::map<std::string, log_level> resource_levels_;
154};
155
156namespace log_detail {
157
158// Some of the filenames in the SDK are not unique, eg config/resource.hpp, resource/resource.hpp.
159// This function trims a full filename /path/to/some/file.xpp to some/file.xpp
160boost::string_view trim_filename(const char* file);
161
162} // namespace log_detail
163
164BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_channel, "Channel", std::string);
165BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_sev, "Severity", viam::sdk::log_level);
166BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_file, "file", boost::string_view);
167BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_line, "line", unsigned int);
168BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_time,
169 "TimeStamp",
170 boost::log::attributes::utc_clock::value_type);
171
172} // namespace sdk
173} // namespace viam
174
175#define VIAM_SDK_LOG_IMPL(lg, level) \
176 BOOST_LOG_SEV((lg), ::viam::sdk::log_level::level) \
177 << ::boost::log::add_value(::viam::sdk::attr_file_type{}, \
178 ::viam::sdk::log_detail::trim_filename(__FILE__)) \
179 << ::boost::log::add_value(::viam::sdk::attr_line_type{}, __LINE__)
180
187#define VIAM_SDK_LOG(level) VIAM_SDK_LOG_IMPL(::viam::sdk::LogManager::get().global_logger(), level)
188
194#define VIAM_MODULE_LOG(level) \
195 VIAM_SDK_LOG_IMPL(::viam::sdk::LogManager::get().module_logger(), level)
196
197// Single argument overload, used for macro overloading below.
198#define VIAM_RESOURCE_LOG_IMPL_1(level) VIAM_RESOURCE_LOG_IMPL_2(*this, level)
199
200// Two argument overload, used for macro overloading below.
201#define VIAM_RESOURCE_LOG_IMPL_2(resource, level) \
202 VIAM_SDK_LOG_IMPL(::viam::sdk::log_detail::logger_access::logger(resource), level)
203
213#define VIAM_RESOURCE_LOG(...) \
214 BOOST_PP_CAT(BOOST_PP_OVERLOAD(VIAM_RESOURCE_LOG_IMPL_, __VA_ARGS__)(__VA_ARGS__), \
215 BOOST_PP_EMPTY())
Instance management for Viam C++ SDK applications. This is a single instance class which is responsib...
Definition instance.hpp:17
Manages the logging infrastructure in the SDDK.
Definition logging.hpp:69
static LogManager & get()
Returns the unique logger instance.
void set_module_log_level(log_level)
Set the module logger severity.
std::string module_channel_for(const ResourceConfig &cfg)
Return the log channel name for a modular resource: the module channel joined with the resource's ful...
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.
Definition resource.hpp:43
gRPC client for a robot, to be used for all interactions with a robot. There are two ways to instanti...
Definition client.hpp:54
log_level
Severity levels for the logger.
Definition logging.hpp:31
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:51
const char * global_resource_name()
Returns the "channel name" of general log messages related to the Viam C++ SDK.
Definition logging.hpp:71