Skip to content

Commit

Permalink
Don't put the log levels in the global namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jhanssen committed Jun 12, 2015
1 parent f809c0d commit 40b89b0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
32 changes: 16 additions & 16 deletions rct/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static Flags<LogFileFlag> sFlags;
static StopWatch sStart;
static Set<std::shared_ptr<LogOutput> > 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<LogOutput::LogFlag> flags)
Expand All @@ -29,7 +29,7 @@ class FileOutput : public LogOutput
{
public:
FileOutput(FILE *f)
: LogOutput(INT_MAX), file(f)
: LogOutput(LogLevel::Max), file(f)
{
}
~FileOutput()
Expand All @@ -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<LogOutput::LogFlag> flags, const char *msg, int len) override
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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<LogOutput::LogFlag> flags)
void logDirect(LogLevel level, const char *msg, int len, Flags<LogOutput::LogFlag> flags)
{
Set<std::shared_ptr<LogOutput> > logs;
{
Expand Down Expand Up @@ -159,7 +159,7 @@ void log(const std::function<void(const std::shared_ptr<LogOutput> &)> &func)
}
}

void log(int level, const char *format, ...)
void log(LogLevel level, const char *format, ...)
{
va_list v;
va_start(v, format);
Expand All @@ -171,35 +171,35 @@ void debug(const char *format, ...)
{
va_list v;
va_start(v, format);
log(Debug, format, v);
log(LogLevel::Debug, format, v);
va_end(v);
}

void verboseDebug(const char *format, ...)
{
va_list v;
va_start(v, format);
log(VerboseDebug, format, v);
log(LogLevel::VerboseDebug, format, v);
va_end(v);
}

void warning(const char *format, ...)
{
va_list v;
va_start(v, format);
log(Warning, format, v);
log(LogLevel::Warning, format, v);
va_end(v);
}

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<std::mutex> lock(sOutputsMutex);
if (sOutputs.isEmpty())
Expand All @@ -211,12 +211,12 @@ bool testLog(int level)
return false;
}

int logLevel()
LogLevel logLevel()
{
return sLevel;
}

bool initLogging(const char* ident, Flags<LogMode> mode, int level, const Path &file, Flags<LogFileFlag> flags)
bool initLogging(const char* ident, Flags<LogMode> mode, LogLevel level, const Path &file, Flags<LogFileFlag> flags)
{
sStart.start();
sFlags = flags;
Expand Down Expand Up @@ -263,7 +263,7 @@ Log::Log(String *out)
mData.reset(new Data(out));
}

Log::Log(int level, Flags<LogOutput::LogFlag> flags)
Log::Log(LogLevel level, Flags<LogOutput::LogFlag> flags)
{
if (testLog(level))
mData.reset(new Data(level, flags));
Expand All @@ -280,7 +280,7 @@ Log &Log::operator=(const Log &other)
return *this;
}

LogOutput::LogOutput(int logLevel)
LogOutput::LogOutput(LogLevel logLevel)
: mLogLevel(logLevel)
{
}
Expand Down
47 changes: 25 additions & 22 deletions rct/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,35 @@
#include <rct/Flags.h>
#include <assert.h>
#include <cxxabi.h>
#include <climits>
#include <sstream>
#include <memory>

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<LogOutput>
{
public:
LogOutput(int logLevel);
LogOutput(LogLevel logLevel);
virtual ~LogOutput();

void add();
void remove();

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,
Expand All @@ -53,34 +56,34 @@ class LogOutput : public std::enable_shared_from_this<LogOutput>
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<LogOutput::LogFlag> flags = LogOutput::DefaultFlags);
inline void logDirect(int level, const String &out, Flags<LogOutput::LogFlag> flags = LogOutput::DefaultFlags)
void logDirect(LogLevel level, const char *str, int length, Flags<LogOutput::LogFlag> flags = LogOutput::DefaultFlags);
inline void logDirect(LogLevel level, const String &out, Flags<LogOutput::LogFlag> flags = LogOutput::DefaultFlags)
{
return logDirect(level, out.constData(), out.size(), flags);
}
void log(const std::function<void(const std::shared_ptr<LogOutput> &)> &func);

bool testLog(int level);
bool testLog(LogLevel level);
enum LogMode {
LogStderr = 0x1,
LogSyslog = 0x2
Expand All @@ -95,17 +98,17 @@ RCT_FLAGS_OPERATORS(LogFileFlag);

bool initLogging(const char* ident,
Flags<LogMode> mode = LogStderr,
int logLevel = Error,
LogLevel logLevel = LogLevel::Error,
const Path &logFile = Path(),
Flags<LogFileFlag> = Flags<LogFileFlag>());
void cleanupLogging();
int logLevel();
LogLevel logLevel();
void restartTime();
class Log
{
public:
Log(String *out);
Log(int level = 0, Flags<LogOutput::LogFlag> flags = LogOutput::DefaultFlags);
Log(LogLevel level = LogLevel::Error, Flags<LogOutput::LogFlag> flags = LogOutput::DefaultFlags);
Log(const Log &other);
Log &operator=(const Log &other);
#if defined(OS_Darwin)
Expand Down Expand Up @@ -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<LogOutput::LogFlag> f)
Data(LogLevel lvl, Flags<LogOutput::LogFlag> f)
: outPtr(0), level(lvl), spacing(true), disableSpacingOverride(0), flags(f)
{
}
Expand All @@ -221,7 +224,7 @@ class Log
}

String *outPtr;
const int level;
const LogLevel level;
String out;
bool spacing;
int disableSpacingOverride;
Expand Down Expand Up @@ -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

0 comments on commit 40b89b0

Please sign in to comment.