M5Utility 0.0.12 git rev:13bfbd6
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
24enum class LogLevel : uint8_t {
25 None,
26 Error,
27 Warn,
28 Info,
29 Debug,
30 Verbose,
31};
32using log_level_t = LogLevel;
33
34#if defined(NDEBUG)
35constexpr log_level_t logOutputLevel = log_level_t::None;
36#elif defined(M5_LOG_LEVEL)
37constexpr log_level_t logOutputLevel = static_cast<log_level_t>(M5_LOG_LEVEL);
38#elif defined(CORE_DEBUG_LEVEL)
39constexpr log_level_t logOutputLevel = static_cast<log_level_t>(CORE_DEBUG_LEVEL);
40#else
49constexpr log_level_t logOutputLevel = log_level_t::None;
50#endif
51
53// Does the string contain slash?
54constexpr bool contains_slash(const char* s)
55{
56 return *s ? (*s == '/' ? true : contains_slash(s + 1)) : false;
57}
58// Returns the next position after the right-most slash
59constexpr const char* after_right_slash(const char* s)
60{
61 return (*s == '/') ? (s + 1) : after_right_slash(s - 1);
62}
63// Gets the tail of string
64constexpr const char* tail(const char* s)
65{
66 return *s ? tail(s + 1) : s;
67}
69
75constexpr const char* pathToFilename(const char* path)
76{
77 return (path && path[0]) ? (contains_slash(path) ? after_right_slash(tail(path)) : path) : "";
78}
79
81void logPrintf(const char* format, ...);
83void dump(const void* addr, const size_t len, const bool align = true);
84
85using elapsed_time_t = std::chrono::milliseconds;
86// using elapsed_time_t = std::chrono::microseconds;
87
89elapsed_time_t elapsedTime();
90
92#ifndef M5_UTILITY_LOG_FORMAT
93#define M5_UTILITY_LOG_FORMAT(letter, format) \
94 "[%6lld][" #letter "][%s:%u] %s(): " format "\n", (int64_t)m5::utility::log::elapsedTime().count(), \
95 m5::utility::log::pathToFilename(__FILE__), __LINE__, __func__
96#endif
98
103#define M5_LIB_LOGE(format, ...) \
104 do { \
105 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Error) { \
106 m5::utility::log::logPrintf(M5_UTILITY_LOG_FORMAT(E, format), ##__VA_ARGS__); \
107 } \
108 } while (0)
113#define M5_LIB_LOGW(format, ...) \
114 do { \
115 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Warn) { \
116 m5::utility::log::logPrintf(M5_UTILITY_LOG_FORMAT(W, format), ##__VA_ARGS__); \
117 } \
118 } while (0)
123#define M5_LIB_LOGI(format, ...) \
124 do { \
125 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Info) { \
126 m5::utility::log::logPrintf(M5_UTILITY_LOG_FORMAT(I, format), ##__VA_ARGS__); \
127 } \
128 } while (0)
133#define M5_LIB_LOGD(format, ...) \
134 do { \
135 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Debug) { \
136 m5::utility::log::logPrintf(M5_UTILITY_LOG_FORMAT(D, format), ##__VA_ARGS__); \
137 } \
138 } while (0)
143#define M5_LIB_LOGV(format, ...) \
144 do { \
145 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Verbose) { \
146 m5::utility::log::logPrintf(M5_UTILITY_LOG_FORMAT(V, format), ##__VA_ARGS__); \
147 } \
148 } while (0)
149
154#define M5_DUMPE(addr, len) \
155 do { \
156 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Error) { \
157 m5::utility::log::dump((addr), (len)); \
158 } \
159 } while (0)
164#define M5_DUMPW(addr, len) \
165 do { \
166 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Warn) { \
167 m5::utility::log::dump((addr), (len)); \
168 } \
169 } while (0)
174#define M5_DUMPI(addr, len) \
175 do { \
176 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Info) { \
177 m5::utility::log::dump((addr), (len)); \
178 } \
179 } while (0)
184#define M5_DUMPD(addr, len) \
185 do { \
186 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Debug) { \
187 m5::utility::log::dump((addr), (len)); \
188 } \
189 } while (0)
194#define M5_DUMPV(addr, len) \
195 do { \
196 if (m5::utility::log::logOutputLevel >= m5::utility::log::log_level_t::Verbose) { \
197 m5::utility::log::dump((addr), (len)); \
198 } \
199 } while (0)
200
201} // namespace log
202} // namespace utility
203} // namespace m5
204
205#endif
LogLevel
Log output control level.
Definition library_log.hpp:24
constexpr log_level_t logOutputLevel
Base value of log level to be output.
Definition library_log.hpp:49
constexpr const char * pathToFilename(const char *path)
Gets the filename from full pathname.
Definition library_log.hpp:75
Top level namespace of M5.
Definition base64.cpp:39
For utilities.