4 #include "sopt/config.h"
30 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;
63 static void setLevel(
const std::string& name,
int level);
72 Log(
const std::string& name);
75 Log(
const std::string& name,
int level);
80 static std::string getColorCode(
int level);
87 static Log&
getLog(
const std::string& name);
119 return (level >= _level);
134 void log(
int level,
const std::string& message);
137 std::string formatMessage(
int level,
const std::string& message);
148 std::ostream&
operator<<(Log& log,
int level);
161 template <
typename Arg>
163 char* delim = strstr(pos,
"{}");
165 ss << std::string(pos, delim-pos) << std::forward<Arg>(arg);
169 SOPT_THROW(
"Insufficient placeholders for number of arguments!");
174 template <
typename... Args>
176 std::string mys = txt;
177 std::stringstream rtn;
178 char* pos = (
char*)txt;
179 ((void)
applyFormat(rtn, pos, std::forward<Args>(args)), ...);
180 rtn << std::string(pos);
192 #define SOPT_MSG_LVL(lvl, ...) \
194 if (sopt::logging::getLog().isActive(lvl)) { \
195 sopt::logging::getLog() << lvl << sopt::logging::mkFormattedString(__VA_ARGS__) << '\n'; \
200 #define SOPT_LOG_(TYPE, ...) \
201 SOPT_MSG_LVL(sopt::logging::Log::TYPE, __VA_ARGS__)
209 #define SOPT_NOTICE(...) SOPT_LOG_(critical, __VA_ARGS__)
211 #define SOPT_ERROR(...) SOPT_LOG_(error, __VA_ARGS__)
213 #define SOPT_WARN(...) SOPT_LOG_(warn, __VA_ARGS__)
215 #define SOPT_INFO(...) SOPT_LOG_(info, __VA_ARGS__)
217 #define SOPT_DEBUG(...) SOPT_LOG_(debug, __VA_ARGS__)
220 #define SOPT_TRACE(...) SOPT_LOG_(trace, __VA_ARGS__)
223 #define SOPT_HIGH_LOG(...) SOPT_LOG_(critical, __VA_ARGS__)
225 #define SOPT_MEDIUM_LOG(...) SOPT_LOG_(info, __VA_ARGS__)
227 #define SOPT_LOW_LOG(...) SOPT_LOG_(debug, __VA_ARGS__)
Logging system for controlled & formatted writing to stdout.
std::map< std::string, int > LevelMap
Typedef for a collection of named log levels.
static Log & getLog(const std::string &name)
static Level getLevelFromName(const std::string &level)
Get a log level enum from a string.
static std::string getLevelName(int level)
Get the std::string representation of a log level.
static const int end_color
Special "level-like" code to end coloring.
friend std::ostream & operator<<(Log &log, int level)
The streaming operator can use Log's internals.
bool isActive(int level) const
Will this log level produce output on this logger at the moment?
std::map< std::string, Log > LogMap
Typedef for a collection of named logs.
int getLevel() const
Get the priority level of this logger.
std::map< int, std::string > ColorCodes
Typedef for a collection of shell color codes, accessed by log level.
Level
Log priority levels.
static void setLevel(const std::string &name, int level)
Set the log levels.
std::string getName() const
Get the name of this logger.
Log & setLevel(int level)
Set the priority level of this logger.
Log & setName(const std::string &name)
Set the name of this logger.
static void setLevels(const LevelMap &logLevels)
void set_level(const std::string &level)
Method to set the logging level of the default Log object.
Log & getLog()
Access method to default Log object.
std::string mkFormattedString(const char *txt, Args &&... args)
Helper method to construct formatted string.
ostream & operator<<(Log &log, int level)
Streaming output to a logger must have a Log::Level/int as its first argument.
void applyFormat(std::stringstream &ss, char *&pos, Arg &&arg)