diff --git a/include/ilogwriter.h b/include/ilogwriter.h new file mode 100644 index 0000000000..bdb3ebee52 --- /dev/null +++ b/include/ilogwriter.h @@ -0,0 +1,60 @@ +#pragma once + +#include + +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; +}; + +} diff --git a/radiant/log/Console.cpp b/radiant/log/Console.cpp index 8be3480b30..72193fca66 100644 --- a/radiant/log/Console.cpp +++ b/radiant/log/Console.cpp @@ -6,7 +6,6 @@ #include "wxutil/ConsoleView.h" #include -#include "LogLevels.h" #include "LogWriter.h" #include "StringLogDevice.h" diff --git a/radiant/log/Console.h b/radiant/log/Console.h index d19a2b7728..b3930c8ff7 100644 --- a/radiant/log/Console.h +++ b/radiant/log/Console.h @@ -1,14 +1,12 @@ #pragma once #include "icommandsystem.h" +#include "ilogwriter.h" #include #include "wxutil/ConsoleView.h" #include "ui/common/CommandEntry.h" -#include "LogDevice.h" - -namespace gtkutil { class ConsoleView; } namespace ui { @@ -25,7 +23,7 @@ typedef std::shared_ptr ConsolePtr; */ class Console : public wxPanel, - public applog::LogDevice + public applog::ILogDevice { private: wxutil::ConsoleView* _view; @@ -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 diff --git a/radiant/log/LogDevice.h b/radiant/log/LogDevice.h deleted file mode 100644 index 4dea025815..0000000000 --- a/radiant/log/LogDevice.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef _LOG_DEVICE_H_ -#define _LOG_DEVICE_H_ - -#include -#include "LogLevels.h" - -namespace applog { - -/** - * 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 LogDevice { -public: - /** - * Destructor - */ - virtual ~LogDevice() {} - /** - * greebo: This method gets called by the Writer with - * a logging string as argument. - */ - virtual void writeLog(const std::string& outputStr, ELogLevel level) = 0; -}; - -} // namespace applog - -#endif /* _LOG_DEVICE_H_ */ diff --git a/radiant/log/LogFile.h b/radiant/log/LogFile.h index 071aef0a96..92bc699493 100644 --- a/radiant/log/LogFile.h +++ b/radiant/log/LogFile.h @@ -2,16 +2,17 @@ #include #include -#include "LogDevice.h" +#include "ilogwriter.h" -namespace applog { +namespace applog +{ // Shared_ptr forward declaration class LogFile; typedef std::shared_ptr LogFilePtr; class LogFile : - public LogDevice + public ILogDevice { // The log file name including path std::string _logFilename; @@ -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); diff --git a/radiant/log/LogLevels.h b/radiant/log/LogLevels.h deleted file mode 100644 index c9a4cd529d..0000000000 --- a/radiant/log/LogLevels.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _LOGLEVELS_H_ -#define _LOGLEVELS_H_ - -namespace applog { - -enum ELogLevel -{ - SYS_VERBOSE = 0, - SYS_STANDARD, - SYS_WARNING, - SYS_ERROR, - SYS_NUM_LOGLEVELS, -}; - -} // namespace applog - -#endif /* _LOGLEVELS_H_ */ diff --git a/radiant/log/LogStreamBuf.h b/radiant/log/LogStreamBuf.h index a7aa893e17..cd80e048ff 100644 --- a/radiant/log/LogStreamBuf.h +++ b/radiant/log/LogStreamBuf.h @@ -1,10 +1,10 @@ -#ifndef _LOG_STREAM_BUF_H_ -#define _LOG_STREAM_BUF_H_ +#pragma once +#include "ilogwriter.h" #include -#include "LogLevels.h" -namespace applog { +namespace applog +{ /** * greebo: The LogStreamBuf adapts the std::streambuf to use to the @@ -41,5 +41,3 @@ class LogStreamBuf : }; } // namespace applog - -#endif /* _LOG_STREAM_BUF_H_ */ diff --git a/radiant/log/LogWriter.cpp b/radiant/log/LogWriter.cpp index 51a138a19a..6d4e6597a0 100644 --- a/radiant/log/LogWriter.cpp +++ b/radiant/log/LogWriter.cpp @@ -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); } diff --git a/radiant/log/LogWriter.h b/radiant/log/LogWriter.h index 42382596d8..bf742658bd 100644 --- a/radiant/log/LogWriter.h +++ b/radiant/log/LogWriter.h @@ -1,15 +1,17 @@ #pragma once #include -#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 LogDevices; + typedef std::set LogDevices; LogDevices _devices; public: @@ -17,15 +19,15 @@ 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); + 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(); diff --git a/radiant/log/StringLogDevice.cpp b/radiant/log/StringLogDevice.cpp index 58f422b3bf..5e77742566 100644 --- a/radiant/log/StringLogDevice.cpp +++ b/radiant/log/StringLogDevice.cpp @@ -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 diff --git a/radiant/log/StringLogDevice.h b/radiant/log/StringLogDevice.h index 2feaaa299b..fe8f7e5c18 100644 --- a/radiant/log/StringLogDevice.h +++ b/radiant/log/StringLogDevice.h @@ -1,25 +1,26 @@ -#ifndef _STRING_LOG_DEVICE_H_ -#define _STRING_LOG_DEVICE_H_ +#pragma once #include -#include "LogDevice.h" #include +#include "ilogwriter.h" -namespace applog { - -class StringLogDevice; -typedef std::shared_ptr 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 Ptr; + StringLogDevice(); ~StringLogDevice(); @@ -27,7 +28,7 @@ class 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); @@ -35,9 +36,7 @@ class StringLogDevice : // Destroys the static instance static void destroy(); - static StringLogDevicePtr& InstancePtr(); + static Ptr& InstancePtr(); }; } // namespace applog - -#endif /* _STRING_LOG_DEVICE_H_ */ diff --git a/tools/msvc/DarkRadiant.vcxproj b/tools/msvc/DarkRadiant.vcxproj index 168c40c92c..9d8275496c 100644 --- a/tools/msvc/DarkRadiant.vcxproj +++ b/tools/msvc/DarkRadiant.vcxproj @@ -1560,9 +1560,7 @@ - - diff --git a/tools/msvc/DarkRadiant.vcxproj.filters b/tools/msvc/DarkRadiant.vcxproj.filters index 7cc8b7641b..a6f90a280c 100644 --- a/tools/msvc/DarkRadiant.vcxproj.filters +++ b/tools/msvc/DarkRadiant.vcxproj.filters @@ -2259,15 +2259,9 @@ src\log - - src\log - src\log - - src\log - src\log diff --git a/tools/msvc/include.vcxproj b/tools/msvc/include.vcxproj index 47494a2e56..3fc3cf9b77 100644 --- a/tools/msvc/include.vcxproj +++ b/tools/msvc/include.vcxproj @@ -132,6 +132,7 @@ +