Skip to content

Commit

Permalink
#5231: Extract logging interface to ilogwriter.h
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Apr 27, 2020
1 parent 7ad8d69 commit 5f4e64b
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 102 deletions.
60 changes: 60 additions & 0 deletions include/ilogwriter.h
@@ -0,0 +1,60 @@
#pragma once

#include <string>

namespace applog
{

enum ELogLevel
{
SYS_VERBOSE = 0,
SYS_STANDARD,
SYS_WARNING,
SYS_ERROR,
SYS_NUM_LOGLEVELS,
};

/**
* greebo: A LogDevice is a class which is able to take log output.
*
* Examples of LogDevices are the Console and the DarkRadiant logfile.
* Note: Use the LogWriter::attach() method to register a class for logging.
*/
class ILogDevice
{
public:
virtual ~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;
};

/**
* Central logging hub, dispatching any incoming log messages
* to all attached ILogDevices.
*/
class ILogWriter
{
public:
virtual ~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;

/**
* greebo: Use these methods to attach/detach a log device from the
* writer class. After attaching a device, all log output
* will be written to it.
*/
virtual void attach(ILogDevice* device) = 0;
virtual void detach(ILogDevice* device) = 0;
};

}
1 change: 0 additions & 1 deletion radiant/log/Console.cpp
Expand Up @@ -6,7 +6,6 @@
#include "wxutil/ConsoleView.h"
#include <wx/sizer.h>

#include "LogLevels.h"
#include "LogWriter.h"
#include "StringLogDevice.h"

Expand Down
11 changes: 3 additions & 8 deletions radiant/log/Console.h
@@ -1,14 +1,12 @@
#pragma once

#include "icommandsystem.h"
#include "ilogwriter.h"

#include <wx/panel.h>

#include "wxutil/ConsoleView.h"
#include "ui/common/CommandEntry.h"
#include "LogDevice.h"

namespace gtkutil { class ConsoleView; }

namespace ui
{
Expand All @@ -25,7 +23,7 @@ typedef std::shared_ptr<Console> ConsolePtr;
*/
class Console :
public wxPanel,
public applog::LogDevice
public applog::ILogDevice
{
private:
wxutil::ConsoleView* _view;
Expand Down Expand Up @@ -57,10 +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);

private:
void shutdown();
void writeLog(const std::string& outputStr, applog::ELogLevel level) override;
};

} // namespace ui
30 changes: 0 additions & 30 deletions radiant/log/LogDevice.h

This file was deleted.

9 changes: 5 additions & 4 deletions radiant/log/LogFile.h
Expand Up @@ -2,16 +2,17 @@

#include <fstream>
#include <memory>
#include "LogDevice.h"
#include "ilogwriter.h"

namespace applog {
namespace applog
{

// Shared_ptr forward declaration
class LogFile;
typedef std::shared_ptr<LogFile> LogFilePtr;

class LogFile :
public LogDevice
public ILogDevice
{
// The log file name including path
std::string _logFilename;
Expand All @@ -36,7 +37,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);
void writeLog(const std::string& outputStr, ELogLevel level) override;

// Creates the singleton logfile with the given filename
static void create(const std::string& filename);
Expand Down
17 changes: 0 additions & 17 deletions radiant/log/LogLevels.h

This file was deleted.

10 changes: 4 additions & 6 deletions radiant/log/LogStreamBuf.h
@@ -1,10 +1,10 @@
#ifndef _LOG_STREAM_BUF_H_
#define _LOG_STREAM_BUF_H_
#pragma once

#include "ilogwriter.h"
#include <streambuf>
#include "LogLevels.h"

namespace applog {
namespace applog
{

/**
* greebo: The LogStreamBuf adapts the std::streambuf to use to the
Expand Down Expand Up @@ -41,5 +41,3 @@ class LogStreamBuf :
};

} // namespace applog

#endif /* _LOG_STREAM_BUF_H_ */
6 changes: 3 additions & 3 deletions radiant/log/LogWriter.cpp
Expand Up @@ -8,18 +8,18 @@ void LogWriter::write(const char* p, std::size_t length, ELogLevel level)
std::string output(p, length);

// Visit all the logfiles and write the string
for (LogDevice* device : _devices)
for (auto device : _devices)
{
device->writeLog(output, level);
}
}

void LogWriter::attach(LogDevice* device)
void LogWriter::attach(ILogDevice* device)
{
_devices.insert(device);
}

void LogWriter::detach(LogDevice* device)
void LogWriter::detach(ILogDevice* device)
{
_devices.erase(device);
}
Expand Down
18 changes: 10 additions & 8 deletions radiant/log/LogWriter.h
@@ -1,31 +1,33 @@
#pragma once

#include <set>
#include "LogLevels.h"
#include "LogDevice.h"
#include "ilogwriter.h"

namespace applog {
namespace applog
{

class LogWriter
class LogWriter :
public ILogWriter
{
private:
// The set of unique log devices
typedef std::set<LogDevice*> LogDevices;
typedef std::set<ILogDevice*> LogDevices;
LogDevices _devices;

public:
/**
* 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);
void write(const char* p, std::size_t length, ELogLevel level) override;

/**
* greebo: Use these methods to attach/detach a log device from the
* writer class. After attaching a device, all log output
* will be written to it.
*/
void attach(LogDevice* device);
void detach(LogDevice* device);
void attach(ILogDevice* device) override;
void detach(ILogDevice* device) override;

// Contains the static singleton instance of this writer
static LogWriter& Instance();
Expand Down
12 changes: 7 additions & 5 deletions radiant/log/StringLogDevice.cpp
Expand Up @@ -39,13 +39,15 @@ std::string StringLogDevice::getString(ELogLevel level)
};
}

void StringLogDevice::destroy() {
InstancePtr() = StringLogDevicePtr();
void StringLogDevice::destroy()
{
InstancePtr().reset();
}

StringLogDevicePtr& StringLogDevice::InstancePtr() {
static StringLogDevicePtr _instance;
return _instance;
StringLogDevice::Ptr& StringLogDevice::InstancePtr()
{
static Ptr _instancePtr;
return _instancePtr;
}

} // namespace applog
23 changes: 11 additions & 12 deletions radiant/log/StringLogDevice.h
@@ -1,43 +1,42 @@
#ifndef _STRING_LOG_DEVICE_H_
#define _STRING_LOG_DEVICE_H_
#pragma once

#include <sstream>
#include "LogDevice.h"
#include <memory>
#include "ilogwriter.h"

namespace applog {

class StringLogDevice;
typedef std::shared_ptr<StringLogDevice> StringLogDevicePtr;
namespace applog
{

/**
* greebo: A StringLogDevice is a class which logs into a local string buffer.
*/
class StringLogDevice :
public LogDevice
public ILogDevice
{
private:
std::ostringstream _errorStream;
std::ostringstream _warningStream;
std::ostringstream _logStream;

public:
typedef std::shared_ptr<StringLogDevice> Ptr;

StringLogDevice();
~StringLogDevice();

/**
* greebo: This method gets called by the Writer with
* a logging string as argument.
*/
void writeLog(const std::string& outputStr, ELogLevel level);
void writeLog(const std::string& outputStr, ELogLevel level) override;

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

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

static StringLogDevicePtr& InstancePtr();
static Ptr& InstancePtr();
};

} // namespace applog

#endif /* _STRING_LOG_DEVICE_H_ */
2 changes: 0 additions & 2 deletions tools/msvc/DarkRadiant.vcxproj
Expand Up @@ -1560,9 +1560,7 @@
<ClInclude Include="..\..\radiant\layers\SetLayerSelectedWalker.h" />
<ClInclude Include="..\..\radiant\log\Console.h" />
<ClInclude Include="..\..\radiant\log\COutRedirector.h" />
<ClInclude Include="..\..\radiant\log\LogDevice.h" />
<ClInclude Include="..\..\radiant\log\LogFile.h" />
<ClInclude Include="..\..\radiant\log\LogLevels.h" />
<ClInclude Include="..\..\radiant\log\LogStream.h" />
<ClInclude Include="..\..\radiant\log\LogStreamBuf.h" />
<ClInclude Include="..\..\radiant\log\LogWriter.h" />
Expand Down
6 changes: 0 additions & 6 deletions tools/msvc/DarkRadiant.vcxproj.filters
Expand Up @@ -2259,15 +2259,9 @@
<ClInclude Include="..\..\radiant\log\COutRedirector.h">
<Filter>src\log</Filter>
</ClInclude>
<ClInclude Include="..\..\radiant\log\LogDevice.h">
<Filter>src\log</Filter>
</ClInclude>
<ClInclude Include="..\..\radiant\log\LogFile.h">
<Filter>src\log</Filter>
</ClInclude>
<ClInclude Include="..\..\radiant\log\LogLevels.h">
<Filter>src\log</Filter>
</ClInclude>
<ClInclude Include="..\..\radiant\log\LogStream.h">
<Filter>src\log</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/include.vcxproj
Expand Up @@ -132,6 +132,7 @@
<ClInclude Include="..\..\include\ikeyvaluestore.h" />
<ClInclude Include="..\..\include\ilayer.h" />
<ClInclude Include="..\..\include\ilightnode.h" />
<ClInclude Include="..\..\include\ilogwriter.h" />
<ClInclude Include="..\..\include\imainframe.h" />
<ClInclude Include="..\..\include\imainframelayout.h" />
<ClInclude Include="..\..\include\imap.h" />
Expand Down

0 comments on commit 5f4e64b

Please sign in to comment.