summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common/logging/backend.cpp2
-rw-r--r--src/common/logging/backend.h2
-rw-r--r--src/common/logging/log.h33
-rw-r--r--src/common/string_util.cpp22
4 files changed, 23 insertions, 36 deletions
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 8f2591d53..04bc3128f 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -120,7 +120,7 @@ private:
120 duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin); 120 duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin);
121 entry.log_class = log_class; 121 entry.log_class = log_class;
122 entry.log_level = log_level; 122 entry.log_level = log_level;
123 entry.filename = Common::TrimSourcePath(filename); 123 entry.filename = filename;
124 entry.line_num = line_nr; 124 entry.line_num = line_nr;
125 entry.function = function; 125 entry.function = function;
126 entry.message = std::move(message); 126 entry.message = std::move(message);
diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h
index fca0267a1..fc338c70d 100644
--- a/src/common/logging/backend.h
+++ b/src/common/logging/backend.h
@@ -23,7 +23,7 @@ struct Entry {
23 std::chrono::microseconds timestamp; 23 std::chrono::microseconds timestamp;
24 Class log_class; 24 Class log_class;
25 Level log_level; 25 Level log_level;
26 std::string filename; 26 const char* filename;
27 unsigned int line_num; 27 unsigned int line_num;
28 std::string function; 28 std::string function;
29 std::string message; 29 std::string message;
diff --git a/src/common/logging/log.h b/src/common/logging/log.h
index 259708116..13a4f1e30 100644
--- a/src/common/logging/log.h
+++ b/src/common/logging/log.h
@@ -9,6 +9,15 @@
9 9
10namespace Log { 10namespace Log {
11 11
12// trims up to and including the last of ../, ..\, src/, src\ in a string
13constexpr const char* TrimSourcePath(std::string_view source) {
14 const auto rfind = [source](const std::string_view match) {
15 return source.rfind(match) == source.npos ? 0 : (source.rfind(match) + match.size());
16 };
17 auto idx = std::max({rfind("src/"), rfind("src\\"), rfind("../"), rfind("..\\")});
18 return source.data() + idx;
19}
20
12/// Specifies the severity or level of detail of the log message. 21/// Specifies the severity or level of detail of the log message.
13enum class Level : u8 { 22enum class Level : u8 {
14 Trace, ///< Extremely detailed and repetitive debugging information that is likely to 23 Trace, ///< Extremely detailed and repetitive debugging information that is likely to
@@ -141,24 +150,24 @@ void FmtLogMessage(Class log_class, Level log_level, const char* filename, unsig
141 150
142#ifdef _DEBUG 151#ifdef _DEBUG
143#define LOG_TRACE(log_class, ...) \ 152#define LOG_TRACE(log_class, ...) \
144 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Trace, __FILE__, __LINE__, \ 153 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Trace, \
145 __func__, __VA_ARGS__) 154 ::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__)
146#else 155#else
147#define LOG_TRACE(log_class, fmt, ...) (void(0)) 156#define LOG_TRACE(log_class, fmt, ...) (void(0))
148#endif 157#endif
149 158
150#define LOG_DEBUG(log_class, ...) \ 159#define LOG_DEBUG(log_class, ...) \
151 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Debug, __FILE__, __LINE__, \ 160 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Debug, \
152 __func__, __VA_ARGS__) 161 ::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__)
153#define LOG_INFO(log_class, ...) \ 162#define LOG_INFO(log_class, ...) \
154 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Info, __FILE__, __LINE__, \ 163 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Info, \
155 __func__, __VA_ARGS__) 164 ::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__)
156#define LOG_WARNING(log_class, ...) \ 165#define LOG_WARNING(log_class, ...) \
157 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Warning, __FILE__, __LINE__, \ 166 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Warning, \
158 __func__, __VA_ARGS__) 167 ::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__)
159#define LOG_ERROR(log_class, ...) \ 168#define LOG_ERROR(log_class, ...) \
160 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Error, __FILE__, __LINE__, \ 169 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Error, \
161 __func__, __VA_ARGS__) 170 ::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__)
162#define LOG_CRITICAL(log_class, ...) \ 171#define LOG_CRITICAL(log_class, ...) \
163 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Critical, __FILE__, __LINE__, \ 172 ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Critical, \
164 __func__, __VA_ARGS__) 173 ::Log::TrimSourcePath(__FILE__), __LINE__, __func__, __VA_ARGS__)
diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp
index 959f278aa..84883a1d3 100644
--- a/src/common/string_util.cpp
+++ b/src/common/string_util.cpp
@@ -223,26 +223,4 @@ std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buff
223 return std::u16string(buffer.begin(), buffer.begin() + len); 223 return std::u16string(buffer.begin(), buffer.begin() + len);
224} 224}
225 225
226const char* TrimSourcePath(const char* path, const char* root) {
227 const char* p = path;
228
229 while (*p != '\0') {
230 const char* next_slash = p;
231 while (*next_slash != '\0' && *next_slash != '/' && *next_slash != '\\') {
232 ++next_slash;
233 }
234
235 bool is_src = Common::ComparePartialString(p, next_slash, root);
236 p = next_slash;
237
238 if (*p != '\0') {
239 ++p;
240 }
241 if (is_src) {
242 path = p;
243 }
244 }
245 return path;
246}
247
248} // namespace Common 226} // namespace Common