diff --git a/rct/Log.cpp b/rct/Log.cpp index d9e26d3..07fdf44 100644 --- a/rct/Log.cpp +++ b/rct/Log.cpp @@ -12,7 +12,7 @@ static Flags sFlags; static StopWatch sStart; static Set > sOutputs; static std::mutex sOutputsMutex; -static int sLevel = 0; +static LogLevel sLevel = LogLevel::Error; static const bool sTimedLogs = getenv("RCT_LOG_TIME"); static inline void writeLog(FILE *f, const char *msg, int len, Flags flags) @@ -29,7 +29,7 @@ class FileOutput : public LogOutput { public: FileOutput(FILE *f) - : LogOutput(INT_MAX), file(f) + : LogOutput(LogLevel::Max), file(f) { } ~FileOutput() @@ -49,7 +49,7 @@ class FileOutput : public LogOutput class StderrOutput : public LogOutput { public: - StderrOutput(int lvl) + StderrOutput(LogLevel lvl) : LogOutput(lvl) {} virtual void log(Flags flags, const char *msg, int len) override @@ -61,7 +61,7 @@ class StderrOutput : public LogOutput class SyslogOutput : public LogOutput { public: - SyslogOutput(const char* ident, int lvl) + SyslogOutput(const char* ident, LogLevel lvl) : LogOutput(lvl) { ::openlog(ident, LOG_CONS | LOG_NOWAIT | LOG_PID, LOG_USER); @@ -102,7 +102,7 @@ static inline String prettyTimeSinceStarted() } #endif -static void log(int level, const char *format, va_list v) +static void log(LogLevel level, const char *format, va_list v) { if (!testLog(level)) return; @@ -127,7 +127,7 @@ static void log(int level, const char *format, va_list v) va_end(v2); } -void logDirect(int level, const char *msg, int len, Flags flags) +void logDirect(LogLevel level, const char *msg, int len, Flags flags) { Set > logs; { @@ -159,7 +159,7 @@ void log(const std::function &)> &func) } } -void log(int level, const char *format, ...) +void log(LogLevel level, const char *format, ...) { va_list v; va_start(v, format); @@ -171,7 +171,7 @@ void debug(const char *format, ...) { va_list v; va_start(v, format); - log(Debug, format, v); + log(LogLevel::Debug, format, v); va_end(v); } @@ -179,7 +179,7 @@ void verboseDebug(const char *format, ...) { va_list v; va_start(v, format); - log(VerboseDebug, format, v); + log(LogLevel::VerboseDebug, format, v); va_end(v); } @@ -187,7 +187,7 @@ void warning(const char *format, ...) { va_list v; va_start(v, format); - log(Warning, format, v); + log(LogLevel::Warning, format, v); va_end(v); } @@ -195,11 +195,11 @@ void error(const char *format, ...) { va_list v; va_start(v, format); - log(Error, format, v); + log(LogLevel::Error, format, v); va_end(v); } -bool testLog(int level) +bool testLog(LogLevel level) { std::lock_guard lock(sOutputsMutex); if (sOutputs.isEmpty()) @@ -211,12 +211,12 @@ bool testLog(int level) return false; } -int logLevel() +LogLevel logLevel() { return sLevel; } -bool initLogging(const char* ident, Flags mode, int level, const Path &file, Flags flags) +bool initLogging(const char* ident, Flags mode, LogLevel level, const Path &file, Flags flags) { sStart.start(); sFlags = flags; @@ -263,7 +263,7 @@ Log::Log(String *out) mData.reset(new Data(out)); } -Log::Log(int level, Flags flags) +Log::Log(LogLevel level, Flags flags) { if (testLog(level)) mData.reset(new Data(level, flags)); @@ -280,7 +280,7 @@ Log &Log::operator=(const Log &other) return *this; } -LogOutput::LogOutput(int logLevel) +LogOutput::LogOutput(LogLevel logLevel) : mLogLevel(logLevel) { } diff --git a/rct/Log.h b/rct/Log.h index 106ceaf..4b34f57 100644 --- a/rct/Log.h +++ b/rct/Log.h @@ -10,22 +10,25 @@ #include #include #include +#include #include #include class Path; -enum LogLevel { +enum class LogLevel { + None = -1, Error = 0, Warning = 1, Debug = 2, - VerboseDebug = 3 + VerboseDebug = 3, + Max = INT_MAX }; class LogOutput : public std::enable_shared_from_this { public: - LogOutput(int logLevel); + LogOutput(LogLevel logLevel); virtual ~LogOutput(); void add(); @@ -33,9 +36,9 @@ class LogOutput : public std::enable_shared_from_this virtual unsigned int flags() const { return 0; } - virtual bool testLog(int level) const + virtual bool testLog(LogLevel level) const { - return level >= 0 && level <= mLogLevel; + return level >= LogLevel::Error && level <= mLogLevel; } enum LogFlag { None = 0x0, @@ -53,34 +56,34 @@ class LogOutput : public std::enable_shared_from_this va_end(args); } - int logLevel() const { return mLogLevel; } + LogLevel logLevel() const { return mLogLevel; } private: - int mLogLevel; + LogLevel mLogLevel; }; RCT_FLAGS_OPERATORS(LogOutput::LogFlag); #ifdef __GNUC__ -void log(int level, const char *format, ...) __attribute__ ((format (printf, 2, 3))); +void log(LogLevel level, const char *format, ...) __attribute__ ((format (printf, 2, 3))); void debug(const char *format, ...) __attribute__ ((format (printf, 1, 2))); void verboseDebug(const char *format, ...) __attribute__ ((format (printf, 1, 2))); void warning(const char *format, ...) __attribute__ ((format (printf, 1, 2))); void error(const char *format, ...) __attribute__ ((format (printf, 1, 2))); #else -void log(int level, const char *format, ...); +void log(LogLevel level, const char *format, ...); void debug(const char *format, ...); void verboseDebug(const char *format, ...); void warning(const char *format, ...); void error(const char *format, ...); #endif -void logDirect(int level, const char *str, int length, Flags flags = LogOutput::DefaultFlags); -inline void logDirect(int level, const String &out, Flags flags = LogOutput::DefaultFlags) +void logDirect(LogLevel level, const char *str, int length, Flags flags = LogOutput::DefaultFlags); +inline void logDirect(LogLevel level, const String &out, Flags flags = LogOutput::DefaultFlags) { return logDirect(level, out.constData(), out.size(), flags); } void log(const std::function &)> &func); -bool testLog(int level); +bool testLog(LogLevel level); enum LogMode { LogStderr = 0x1, LogSyslog = 0x2 @@ -95,17 +98,17 @@ RCT_FLAGS_OPERATORS(LogFileFlag); bool initLogging(const char* ident, Flags mode = LogStderr, - int logLevel = Error, + LogLevel logLevel = LogLevel::Error, const Path &logFile = Path(), Flags = Flags()); void cleanupLogging(); -int logLevel(); +LogLevel logLevel(); void restartTime(); class Log { public: Log(String *out); - Log(int level = 0, Flags flags = LogOutput::DefaultFlags); + Log(LogLevel level = LogLevel::Error, Flags flags = LogOutput::DefaultFlags); Log(const Log &other); Log &operator=(const Log &other); #if defined(OS_Darwin) @@ -207,9 +210,9 @@ class Log { public: Data(String *string) - : outPtr(string), level(-1), spacing(true), disableSpacingOverride(0) + : outPtr(string), level(LogLevel::None), spacing(true), disableSpacingOverride(0) {} - Data(int lvl, Flags f) + Data(LogLevel lvl, Flags f) : outPtr(0), level(lvl), spacing(true), disableSpacingOverride(0), flags(f) { } @@ -221,7 +224,7 @@ class Log } String *outPtr; - const int level; + const LogLevel level; String out; bool spacing; int disableSpacingOverride; @@ -388,22 +391,22 @@ String &operator<<(String &str, const T &t) static inline Log error() { - return Log(Error); + return Log(LogLevel::Error); } static inline Log warning() { - return Log(Warning); + return Log(LogLevel::Warning); } static inline Log debug() { - return Log(Debug); + return Log(LogLevel::Debug); } static inline Log verboseDebug() { - return Log(VerboseDebug); + return Log(LogLevel::VerboseDebug); } #endif