Skip to content

Commit

Permalink
#5231: Convert ELogLevel enumeration to an enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Apr 28, 2020
1 parent 08024cd commit f0909e1
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 63 deletions.
22 changes: 14 additions & 8 deletions include/ilogwriter.h
Expand Up @@ -5,13 +5,19 @@
namespace applog
{

enum ELogLevel
enum class LogLevel
{
SYS_VERBOSE = 0,
SYS_STANDARD,
SYS_WARNING,
SYS_ERROR,
SYS_NUM_LOGLEVELS,
Verbose = 0,
Standard,
Warning,
Error,
};

constexpr LogLevel AllLogLevels[] = {
LogLevel::Verbose,
LogLevel::Standard,
LogLevel::Warning,
LogLevel::Error
};

/**
Expand All @@ -29,7 +35,7 @@ class ILogDevice
* greebo: This method gets called by the ILogWriter with
* a log string as argument.
*/
virtual void writeLog(const std::string& outputStr, ELogLevel level) = 0;
virtual void writeLog(const std::string& outputStr, LogLevel level) = 0;
};

/**
Expand All @@ -46,7 +52,7 @@ class ILogWriter
* greebo: Writes the given buffer p with the given length to the
* various output devices (i.e. Console and Log file).
*/
virtual void write(const char* p, std::size_t length, ELogLevel level) = 0;
virtual void write(const char* p, std::size_t length, LogLevel level) = 0;

/**
* greebo: Use these methods to attach/detach a log device from the
Expand Down
34 changes: 16 additions & 18 deletions radiant/log/Console.cpp
Expand Up @@ -37,15 +37,13 @@ Console::Console(wxWindow* parent) :
{
applog::StringLogDevice& logger = *applog::StringLogDevice::InstancePtr();

for (int level = applog::SYS_VERBOSE;
level < applog::SYS_NUM_LOGLEVELS;
level++)
for (auto level : applog::AllLogLevels)
{
std::string bufferedText = logger.getString(static_cast<applog::ELogLevel>(level));
std::string bufferedText = logger.getString(static_cast<applog::LogLevel>(level));

if (bufferedText.empty()) continue;

writeLog(bufferedText + "\n", static_cast<applog::ELogLevel>(level));
writeLog(bufferedText + "\n", static_cast<applog::LogLevel>(level));
}
}

Expand All @@ -71,22 +69,22 @@ void Console::toggle(const cmd::ArgumentList& args)
GlobalGroupDialog().togglePage("console");
}

void Console::writeLog(const std::string& outputStr, applog::ELogLevel level)
void Console::writeLog(const std::string& outputStr, applog::LogLevel level)
{
switch (level)
{
case applog::SYS_VERBOSE:
case applog::SYS_STANDARD:
_view->appendText(outputStr, wxutil::ConsoleView::ModeStandard);
break;
case applog::SYS_WARNING:
_view->appendText(outputStr, wxutil::ConsoleView::ModeWarning);
break;
case applog::SYS_ERROR:
_view->appendText(outputStr, wxutil::ConsoleView::ModeError);
break;
default:
_view->appendText(outputStr, wxutil::ConsoleView::ModeStandard);
case applog::LogLevel::Verbose:
case applog::LogLevel::Standard:
_view->appendText(outputStr, wxutil::ConsoleView::ModeStandard);
break;
case applog::LogLevel::Warning:
_view->appendText(outputStr, wxutil::ConsoleView::ModeWarning);
break;
case applog::LogLevel::Error:
_view->appendText(outputStr, wxutil::ConsoleView::ModeError);
break;
default:
_view->appendText(outputStr, wxutil::ConsoleView::ModeStandard);
};
}

Expand Down
2 changes: 1 addition & 1 deletion radiant/log/Console.h
Expand Up @@ -55,7 +55,7 @@ class Console :
* The log level indicates which tag is used for colouring the output.
* (Note: this gets called by the LogWriter automatically).
*/
void writeLog(const std::string& outputStr, applog::ELogLevel level) override;
void writeLog(const std::string& outputStr, applog::LogLevel level) override;
};

} // namespace ui
2 changes: 1 addition & 1 deletion radiant/log/LogFile.cpp
Expand Up @@ -57,7 +57,7 @@ const std::string& LogFile::getFullPath() const
return _logFilePath;
}

void LogFile::writeLog(const std::string& outputStr, ELogLevel level)
void LogFile::writeLog(const std::string& outputStr, LogLevel level)
{
_buffer.append(outputStr);

Expand Down
2 changes: 1 addition & 1 deletion radiant/log/LogFile.h
Expand Up @@ -36,7 +36,7 @@ class LogFile :
* Use this to write a string to the logfile. This usually gets
* called by the LogWriter class, but it can be called independently.
*/
void writeLog(const std::string& outputStr, ELogLevel level) override;
void writeLog(const std::string& outputStr, LogLevel level) override;
};

} // namespace applog
17 changes: 10 additions & 7 deletions radiant/log/LogStream.cpp
Expand Up @@ -9,7 +9,7 @@ namespace applog

std::mutex LogStream::_streamLock;

LogStream::LogStream(ELogLevel logLevel) :
LogStream::LogStream(LogLevel logLevel) :
std::ostream(new LogStreamBuf(logLevel))
{}

Expand All @@ -23,18 +23,21 @@ LogStream::~LogStream()
}
}

std::ostream& getGlobalOutputStream() {
static applog::LogStream _stream(SYS_STANDARD);
std::ostream& getGlobalOutputStream()
{
static LogStream _stream(LogLevel::Standard);
return _stream;
}

std::ostream& getGlobalErrorStream() {
static applog::LogStream _stream(SYS_ERROR);
std::ostream& getGlobalErrorStream()
{
static LogStream _stream(LogLevel::Error);
return _stream;
}

std::ostream& getGlobalWarningStream() {
static applog::LogStream _stream(SYS_WARNING);
std::ostream& getGlobalWarningStream()
{
static LogStream _stream(LogLevel::Warning);
return _stream;
}

Expand Down
2 changes: 1 addition & 1 deletion radiant/log/LogStream.h
Expand Up @@ -25,7 +25,7 @@ class LogStream :
private:
static std::mutex _streamLock;
public:
LogStream(ELogLevel logLevel);
LogStream(LogLevel logLevel);

virtual ~LogStream();

Expand Down
2 changes: 1 addition & 1 deletion radiant/log/LogStreamBuf.cpp
Expand Up @@ -6,7 +6,7 @@

namespace applog {

LogStreamBuf::LogStreamBuf(ELogLevel level, int bufferSize) :
LogStreamBuf::LogStreamBuf(LogLevel level, int bufferSize) :
_reserve(nullptr),
_level(level)
{
Expand Down
4 changes: 2 additions & 2 deletions radiant/log/LogStreamBuf.h
Expand Up @@ -17,14 +17,14 @@ class LogStreamBuf :
char* _reserve;

// The associated level, is passed to the LogWriter
ELogLevel _level;
LogLevel _level;

public:
/**
* greebo: Pass the level and the optional buffersize to the constructor.
* Level can be something like SYS_ERROR, SYS_STANDARD, etc.
*/
LogStreamBuf(ELogLevel level, int bufferSize = 0);
LogStreamBuf(LogLevel level, int bufferSize = 0);

// Cleans up the buffer
virtual ~LogStreamBuf();
Expand Down
2 changes: 1 addition & 1 deletion radiant/log/LogWriter.cpp
Expand Up @@ -2,7 +2,7 @@

namespace applog {

void LogWriter::write(const char* p, std::size_t length, ELogLevel level)
void LogWriter::write(const char* p, std::size_t length, LogLevel level)
{
// Convert the buffer to a string
std::string output(p, length);
Expand Down
2 changes: 1 addition & 1 deletion radiant/log/LogWriter.h
Expand Up @@ -19,7 +19,7 @@ class LogWriter :
* greebo: Writes the given buffer p with the given length to the
* various output devices (i.e. Console and Log file).
*/
void write(const char* p, std::size_t length, ELogLevel level) override;
void write(const char* p, std::size_t length, LogLevel level) override;

/**
* greebo: Use these methods to attach/detach a log device from the
Expand Down
40 changes: 21 additions & 19 deletions radiant/log/StringLogDevice.cpp
Expand Up @@ -11,31 +11,33 @@ StringLogDevice::~StringLogDevice() {
LogWriter::Instance().detach(this);
}

void StringLogDevice::writeLog(const std::string& outputStr, ELogLevel level) {
switch (level) {
case SYS_ERROR:
_errorStream << outputStr;
break;
case SYS_WARNING:
_warningStream << outputStr;
break;
default:
_logStream << outputStr;
void StringLogDevice::writeLog(const std::string& outputStr, LogLevel level)
{
switch (level)
{
case LogLevel::Error:
_errorStream << outputStr;
break;
case LogLevel::Warning:
_warningStream << outputStr;
break;
default:
_logStream << outputStr;
};
}

std::string StringLogDevice::getString(ELogLevel level)
std::string StringLogDevice::getString(LogLevel level)
{
switch (level)
{
case SYS_ERROR:
return _errorStream.str();
case SYS_WARNING:
return _warningStream.str();
case SYS_STANDARD:
return _logStream.str();
default:
return "";
case LogLevel::Error:
return _errorStream.str();
case LogLevel::Warning:
return _warningStream.str();
case LogLevel::Standard:
return _logStream.str();
default:
return "";
};
}

Expand Down
4 changes: 2 additions & 2 deletions radiant/log/StringLogDevice.h
Expand Up @@ -28,10 +28,10 @@ class StringLogDevice :
* greebo: This method gets called by the Writer with
* a logging string as argument.
*/
void writeLog(const std::string& outputStr, ELogLevel level) override;
void writeLog(const std::string& outputStr, LogLevel level) override;

// Returns the temporary buffer for the given level
std::string getString(ELogLevel level);
std::string getString(LogLevel level);

// Destroys the static instance
static void destroy();
Expand Down

0 comments on commit f0909e1

Please sign in to comment.