M5Utility 0.0.2 git rev:5c1a751
Loading...
Searching...
No Matches
library_log.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UTILITY_LOG_LIBRARY_LOG_HPP
11#define M5_UTILITY_LOG_LIBRARY_LOG_HPP
12
13#include <cstdint>
14#include <chrono>
15
16namespace m5 {
17namespace utility {
18namespace log {
19
25enum class LogLevel : uint8_t {
26 None,
27 Error,
28 Warn,
29 Info,
30 Debug,
31 Verbose,
32};
33using log_level_t = LogLevel;
34
35#if defined(NDEBUG)
36constexpr log_level_t logOutputLevel = log_level_t::None;
37#elif defined(M5_LOG_LEVEL)
38constexpr log_level_t logOutputLevel = static_cast<log_level_t>(M5_LOG_LEVEL);
39#elif defined(CORE_DEBUG_LEVEL)
40constexpr log_level_t logOutputLevel = static_cast<log_level_t>(CORE_DEBUG_LEVEL);
41#else
50constexpr log_level_t logOutputLevel = log_level_t::None;
51#endif
52
54// Does the string contain slash?
55constexpr bool containss_slash(const char* s)
56{
57 return *s ? (*s == '/' ? true : containss_slash(s + 1)) : false;
58}
59// Returns the next position after the right-most slash
60constexpr const char* after_right_slash(const char* s)
61{
62 return (*s == '/') ? (s + 1) : after_right_slash(s - 1);
63}
64// Gets the tail of string
65constexpr const char* tail(const char* s)
66{
67 return *s ? tail(s + 1) : s;
68}
70
76constexpr const char* pathToFilename(const char* path)
77{
78 return (path && path[0]) ? (containss_slash(path) ? after_right_slash(tail(path)) : path) : "";
79}
80
82void logPrintf(const char* format, ...);
84void dump(const void* addr, const size_t len, const bool align = true);
85
86using elapsed_time_t = std::chrono::milliseconds;
87// using elapsed_time_t = std::chrono::microseconds;
88
90elapsed_time_t elapsedTime();
91
93#ifndef M5_UTILITY_LOG_FORMAT
94#define M5_UTILITY_LOG_FORMAT(letter, format) \
95 "[%6lld][" #letter "][%s:%u] %s(): " format "\n", (int64_t)m5::utility::log::elapsedTime().count(), \
96 m5::utility::log::pathToFilename(__FILE__), __LINE__, __func__
97#endif
99
104#define M5_LIB_LOGE(format, ...) \
105 do { \
106 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Error) { \
107 m5::utility::log::logPrintf(M5_UTILITY_LOG_FORMAT(E, format), ##__VA_ARGS__); \
108 } \
109 } while (0)
114#define M5_LIB_LOGW(format, ...) \
115 do { \
116 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Warn) { \
117 m5::utility::log::logPrintf(M5_UTILITY_LOG_FORMAT(W, format), ##__VA_ARGS__); \
118 } \
119 } while (0)
124#define M5_LIB_LOGI(format, ...) \
125 do { \
126 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Info) { \
127 m5::utility::log::logPrintf(M5_UTILITY_LOG_FORMAT(I, format), ##__VA_ARGS__); \
128 } \
129 } while (0)
134#define M5_LIB_LOGD(format, ...) \
135 do { \
136 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Debug) { \
137 m5::utility::log::logPrintf(M5_UTILITY_LOG_FORMAT(D, format), ##__VA_ARGS__); \
138 } \
139 } while (0)
144#define M5_LIB_LOGV(format, ...) \
145 do { \
146 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Verbose) { \
147 m5::utility::log::logPrintf(M5_UTILITY_LOG_FORMAT(V, format), ##__VA_ARGS__); \
148 } \
149 } while (0)
150
155#define M5_DUMPE(addr, len) \
156 do { \
157 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Error) { \
158 m5::utility::log::dump((addr), (len)); \
159 } \
160 } while (0)
165#define M5_DUMPW(addr, len) \
166 do { \
167 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Warn) { \
168 m5::utility::log::dump((addr), (len)); \
169 } \
170 } while (0)
175#define M5_DUMPI(addr, len) \
176 do { \
177 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Info) { \
178 m5::utility::log::dump((addr), (len)); \
179 } \
180 } while (0)
185#define M5_DUMPD(addr, len) \
186 do { \
187 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Debug) { \
188 m5::utility::log::dump((addr), (len)); \
189 } \
190 } while (0)
195#define M5_DUMPV(addr, len) \
196 do { \
197 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Verbose) { \
198 m5::utility::log::dump((addr), (len)); \
199 } \
200 } while (0)
201
202} // namespace log
203} // namespace utility
204} // namespace m5
205
206#endif
LogLevel
Definition library_log.hpp:25
constexpr log_level_t logOutputLevel
Base value of log level to be output.
Definition library_log.hpp:50
constexpr const char * pathToFilename(const char *path)
Gets the filename from full pathname.
Definition library_log.hpp:76
Top level namespace of M5.
Definition bit_segment.hpp:17
For utilities.