From b559c078bc80859f9f5d54c668415535010076b8 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Sat, 24 Jun 2023 02:00:26 +0300 Subject: [PATCH] common: logging: backend: Wrap IOFile in a unique_ptr Allows us to forward declare FileUtil::IOFile. --- src/common/logging/backend.cpp | 17 +++++++++++++---- src/common/logging/backend.h | 18 ++++++++++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index cdfa0964a..1fcd482f7 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -16,6 +16,7 @@ #define _SH_DENYWR 0 #endif #include "common/assert.h" +#include "common/file_util.h" #include "common/logging/backend.h" #include "common/logging/log.h" #include "common/logging/text_formatter.h" @@ -133,14 +134,20 @@ private: std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()}; }; +ConsoleBackend::~ConsoleBackend() = default; + void ConsoleBackend::Write(const Entry& entry) { PrintMessage(entry); } +ColorConsoleBackend::~ColorConsoleBackend() = default; + void ColorConsoleBackend::Write(const Entry& entry) { PrintColoredMessage(entry); } +LogcatBackend::~LogcatBackend() = default; + void LogcatBackend::Write(const Entry& entry) { PrintMessageToLogcat(entry); } @@ -157,19 +164,21 @@ FileBackend::FileBackend(const std::string& filename) { // _SH_DENYWR allows read only access to the file for other programs. // It is #defined to 0 on other platforms - file = FileUtil::IOFile(filename, "w", _SH_DENYWR); + file = std::make_unique(filename, "w", _SH_DENYWR); } +FileBackend::~FileBackend() = default; + void FileBackend::Write(const Entry& entry) { // prevent logs from going over the maximum size (in case its spamming and the user doesn't // know) constexpr std::size_t MAX_BYTES_WRITTEN = 50 * 1024L * 1024L; - if (!file.IsOpen() || bytes_written > MAX_BYTES_WRITTEN) { + if (!file->IsOpen() || bytes_written > MAX_BYTES_WRITTEN) { return; } - bytes_written += file.WriteString(FormatLogMessage(entry).append(1, '\n')); + bytes_written += file->WriteString(FormatLogMessage(entry).append(1, '\n')); if (entry.log_level >= Level::Error) { - file.Flush(); + file->Flush(); } } diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h index b9e5386a0..47b084d34 100644 --- a/src/common/logging/backend.h +++ b/src/common/logging/backend.h @@ -8,10 +8,13 @@ #include #include #include -#include "common/file_util.h" #include "common/logging/filter.h" #include "common/logging/log.h" +namespace FileUtil { +class IOFile; +} + namespace Common::Log { /** @@ -36,6 +39,7 @@ struct Entry { class Backend { public: virtual ~Backend() = default; + virtual void SetFilter(const Filter& new_filter) { filter = new_filter; } @@ -51,6 +55,8 @@ private: */ class ConsoleBackend : public Backend { public: + ~ConsoleBackend() override; + static const char* Name() { return "console"; } @@ -65,6 +71,8 @@ public: */ class ColorConsoleBackend : public Backend { public: + ~ColorConsoleBackend() override; + static const char* Name() { return "color_console"; } @@ -80,6 +88,8 @@ public: */ class LogcatBackend : public Backend { public: + ~LogcatBackend() override; + static const char* Name() { return "logcat"; } @@ -95,6 +105,8 @@ public: */ class FileBackend : public Backend { public: + ~FileBackend() override; + explicit FileBackend(const std::string& filename); static const char* Name() { @@ -108,7 +120,7 @@ public: void Write(const Entry& entry) override; private: - FileUtil::IOFile file; + std::unique_ptr file; std::size_t bytes_written = 0; }; @@ -117,6 +129,8 @@ private: */ class DebuggerBackend : public Backend { public: + ~DebuggerBackend() override; + static const char* Name() { return "debugger"; }