Skip to content

Commit f4b0f11

Browse files
committed
fix file logging
1 parent 216df1d commit f4b0f11

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/viam/sdk/log/logger.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,22 @@ void Logger::disable_console_logging() {
137137
boost::log::core::get()->remove_sink(console_sink_);
138138
}
139139

140+
namespace log_detail {
141+
142+
boost::string_view trim_filename(const char* file) {
143+
boost::string_view result(file);
144+
const std::size_t second_last = result //
145+
.substr(0, result.find_last_of('/'))
146+
.find_last_of('/');
147+
148+
if (second_last != boost::string_view::npos) {
149+
return result.substr(second_last + 1, result.size() - second_last);
150+
}
151+
152+
return result;
153+
}
154+
155+
} // namespace log_detail
156+
140157
} // namespace sdk
141158
} // namespace viam

src/viam/sdk/log/logger.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <boost/log/sinks/text_ostream_backend.hpp>
1616
#include <boost/log/sources/severity_channel_logger.hpp>
1717
#include <boost/log/utility/manipulators/add_value.hpp>
18+
#include <boost/utility/string_view.hpp>
1819

1920
namespace viam {
2021
namespace sdk {
@@ -116,17 +117,26 @@ class Logger {
116117
std::map<std::string, log_level> resource_levels_;
117118
};
118119

120+
namespace log_detail {
121+
122+
// Some of the filenames in the SDK are not unique, eg config/resource.hpp, resource/resource.hpp.
123+
// This function trims a full filename /path/to/some/file.xpp to some/file.xpp
124+
boost::string_view trim_filename(const char* file);
125+
126+
} // namespace log_detail
127+
119128
BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_channel, "Channel", std::string);
120129
BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_sev, "Severity", viam::sdk::log_level);
121-
BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_file, "file", const char*);
130+
BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_file, "file", boost::string_view);
122131
BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_line, "line", unsigned int);
123132
BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_time,
124133
"TimeStamp",
125134
boost::log::attributes::local_clock::value_type);
126135

127-
#define VIAM_LOG_IMPL(lg, level) \
128-
BOOST_LOG_SEV((lg), ::viam::sdk::log_level::level) \
129-
<< ::boost::log::add_value(::viam::sdk::attr_file_type{}, __FILE__) \
136+
#define VIAM_LOG_IMPL(lg, level) \
137+
BOOST_LOG_SEV((lg), ::viam::sdk::log_level::level) \
138+
<< ::boost::log::add_value(::viam::sdk::attr_file_type{}, \
139+
::viam::sdk::log_detail::trim_filename(__FILE__)) \
130140
<< ::boost::log::add_value(::viam::sdk::attr_line_type{}, __LINE__)
131141

132142
/// @brief Log macro for general SDK logs.

0 commit comments

Comments
 (0)