1 #ifndef PURIFY_LOGGING_H
2 #define PURIFY_LOGGING_H
4 #include "purify/config.h"
32 using LogMap = std::map<std::string, Log>;
42 thread_local
static LogMap existingLogs;
45 thread_local
static LevelMap defaultLevels;
48 static bool showTimestamp;
51 static bool showLogLevel;
54 static bool showLoggerName;
57 static bool useShellColors;
61 static void setLevel(
const std::string& name,
int level);
69 Log(
const std::string& name);
72 Log(
const std::string& name,
int level);
77 static std::string getColorCode(
int level);
82 static Log&
getLog(
const std::string& name);
109 bool isActive(
int level)
const {
return (level >= _level); }
120 void log(
int level,
const std::string& message);
123 std::string formatMessage(
int level,
const std::string& message);
131 std::ostream&
operator<<(Log& log,
int level);
141 template <
typename Arg>
143 char* delim = strstr(pos,
"{}");
145 ss << std::string(pos, delim - pos) << std::forward<Arg>(arg);
148 throw std::runtime_error(
"Insufficient placeholders for number of arguments!");
153 template <
typename...
Args>
155 std::string mys = txt;
156 std::stringstream rtn;
157 char* pos = (
char*)txt;
158 ((void)
applyFormat(rtn, pos, std::forward<Args>(args)), ...);
159 rtn << std::string(pos);
173 #define PURIFY_MSG_LVL(lvl, ...) \
175 if (purify::logging::getLog().isActive(lvl)) { \
176 purify::logging::getLog() << lvl << purify::logging::mkFormattedString(__VA_ARGS__) << '\n'; \
181 #define PURIFY_LOG_(TYPE, ...) PURIFY_MSG_LVL(purify::logging::Log::TYPE, __VA_ARGS__)
188 #define PURIFY_CRITICAL(...) PURIFY_LOG_(critical, __VA_ARGS__)
190 #define PURIFY_ERROR(...) PURIFY_LOG_(error, __VA_ARGS__)
192 #define PURIFY_WARN(...) PURIFY_LOG_(warn, __VA_ARGS__)
195 #define PURIFY_INFO(...) PURIFY_LOG_(info, __VA_ARGS__)
197 #define PURIFY_DEBUG(...) PURIFY_LOG_(debug, __VA_ARGS__)
200 #define PURIFY_TRACE(...) PURIFY_LOG_(trace, __VA_ARGS__)
203 #define PURIFY_HIGH_LOG(...) PURIFY_LOG_(critical, __VA_ARGS__)
205 #define PURIFY_MEDIUM_LOG(...) PURIFY_LOG_(info, __VA_ARGS__)
207 #define PURIFY_LOW_LOG(...) PURIFY_LOG_(debug, __VA_ARGS__)
Args({128, 10000, 4, 10}) -> UseManualTime() ->MinTime(10.0) ->MinWarmUpTime(5.0) ->Repetitions(3) ->Unit(benchmark::kMillisecond)
Logging system for controlled & formatted writing to stdout.
static std::string getLevelName(int level)
Get the std::string representation of a log level.
std::map< std::string, Log > LogMap
Typedef for a collection of named logs.
friend std::ostream & operator<<(Log &log, int level)
The streaming operator can use Log's internals.
Level
Log priority levels.
Log & setName(const std::string &name)
Set the name of this logger.
static const int end_color
Special "level-like" code to end coloring.
Log & setLevel(int level)
Set the priority level of this logger.
static Level getLevelFromName(const std::string &level)
Get a log level enum from a string.
static Log & getLog(const std::string &name)
bool isActive(int level) const
Will this log level produce output on this logger at the moment?
int getLevel() const
Get the priority level of this logger.
static void setLevels(const LevelMap &logLevels)
std::map< std::string, int > LevelMap
Typedef for a collection of named log levels.
std::string getName() const
Get the name of this logger.
std::map< int, std::string > ColorCodes
Typedef for a collection of shell color codes, accessed by log level.
static void setLevel(const std::string &name, int level)
Set the log levels.
Log & getLog()
Access method to default Log object.
void applyFormat(std::stringstream &ss, char *&pos, Arg &&arg)
void set_level(const std::string &level)
Method to set the logging level of the default Log object.
ostream & operator<<(Log &log, int level)
Streaming output to a logger must have a Log::Level/int as its first argument.
std::string mkFormattedString(const char *txt, Args &&... args)
Helper method to construct formatted string.