57 Logger(
const std::string& className, std::mutex* mtx);
62 std::string getClassName();
67 static void setLoggerLevel(Level level);
72 template <
typename... Args>
73 void trace(
const std::string& format, Args... args)
75 if (mLevel >= Level::logTrace)
76 log(
"TRACE", format, std::forward<Args>(args)...);
82 template <
typename... Args>
83 void debug(
const std::string& format, Args... args)
85 if (mLevel >= Level::logDebug)
86 log(
"DEBUG", format, std::forward<Args>(args)...);
92 template <
typename... Args>
93 void warn(
const std::string& format, Args... args)
95 if (mLevel >= Level::logWarn)
96 log(
"WARN", format, std::forward<Args>(args)...);
102 template <
typename... Args>
103 void info(
const std::string& format, Args... args)
105 if (mLevel >= Level::logInfo)
106 log(
"INFO", format, std::forward<Args>(args)...);
112 template <
typename... Args>
113 void error(
const std::string& format, Args... args)
115 if (mLevel >= Level::logError)
116 log(
"ERROR", format, std::forward<Args>(args)...);
128 const size_t maxClassNameLength = 100;
133 const std::string className;
143 static const std::string getCurrentTimestamp();
149 std::string demangle(
const char* mangled_name)
153 std::unique_ptr<char,
decltype(&std::free)> ptr(
154 __cxxabiv1::__cxa_demangle(mangled_name,
nullptr, &len, &status),
156 std::string s(ptr.get());
157 if (s.size() > maxClassNameLength)
158 s.resize(maxClassNameLength);
162 std::string demangle(
const char* name)
165 if (s.size() > maxClassNameLength)
166 s.resize(maxClassNameLength);
171 void printf(std::ostringstream& os,
const char* s)
174 if (*s ==
'%' && *(s + 1) !=
'%')
175 throw std::runtime_error(
"invalid format: missing arguments");
180 template <
typename T,
typename... Args>
181 void printf(std::ostringstream& os,
const char* s, T& value, Args... args)
184 if (*s ==
'%' && *(s + 1) !=
'%') {
186 return printf(os, ++s, args...);
190 throw std::runtime_error(
"extra arguments provided to printf");
193 template <
typename T,
typename... Args>
194 void printf(std::ostringstream& os,
const char* s, T* value, Args... args)
197 if (*s ==
'%' && *(s + 1) !=
'%') {
199 return printf(os, ++s, args...);
203 throw std::runtime_error(
"extra arguments provided to printf");
210 template <
typename... Args>
211 void log(
const std::string& label,
const std::string& format, Args... args)
213 const std::lock_guard<std::mutex> lock(*mtx);
216 std::string name = className;
218 std::printf(
"[%s] [%5s] [%-70s] ",
219 getCurrentTimestamp().c_str(),
224 std::ostringstream os;
225 printf(os, format.c_str(), args...);
226 const std::string& str = os.str();
227 std::printf(
"%s", str.c_str());
void trace(const std::string &format, Args... args)
void error(const std::string &format, Args... args)
void info(const std::string &format, Args... args)
void debug(const std::string &format, Args... args)
void warn(const std::string &format, Args... args)