1
0
Fork 0

common: logging: backend: Wrap IOFile in a unique_ptr

Allows us to forward declare FileUtil::IOFile.
This commit is contained in:
Morph 2023-06-24 02:00:26 +03:00 committed by GPUCode
parent 5c86147ef4
commit b559c078bc
2 changed files with 29 additions and 6 deletions

View File

@ -16,6 +16,7 @@
#define _SH_DENYWR 0 #define _SH_DENYWR 0
#endif #endif
#include "common/assert.h" #include "common/assert.h"
#include "common/file_util.h"
#include "common/logging/backend.h" #include "common/logging/backend.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/logging/text_formatter.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()}; std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};
}; };
ConsoleBackend::~ConsoleBackend() = default;
void ConsoleBackend::Write(const Entry& entry) { void ConsoleBackend::Write(const Entry& entry) {
PrintMessage(entry); PrintMessage(entry);
} }
ColorConsoleBackend::~ColorConsoleBackend() = default;
void ColorConsoleBackend::Write(const Entry& entry) { void ColorConsoleBackend::Write(const Entry& entry) {
PrintColoredMessage(entry); PrintColoredMessage(entry);
} }
LogcatBackend::~LogcatBackend() = default;
void LogcatBackend::Write(const Entry& entry) { void LogcatBackend::Write(const Entry& entry) {
PrintMessageToLogcat(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. // _SH_DENYWR allows read only access to the file for other programs.
// It is #defined to 0 on other platforms // It is #defined to 0 on other platforms
file = FileUtil::IOFile(filename, "w", _SH_DENYWR); file = std::make_unique<FileUtil::IOFile>(filename, "w", _SH_DENYWR);
} }
FileBackend::~FileBackend() = default;
void FileBackend::Write(const Entry& entry) { void FileBackend::Write(const Entry& entry) {
// prevent logs from going over the maximum size (in case its spamming and the user doesn't // prevent logs from going over the maximum size (in case its spamming and the user doesn't
// know) // know)
constexpr std::size_t MAX_BYTES_WRITTEN = 50 * 1024L * 1024L; 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; 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) { if (entry.log_level >= Level::Error) {
file.Flush(); file->Flush();
} }
} }

View File

@ -8,10 +8,13 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include "common/file_util.h"
#include "common/logging/filter.h" #include "common/logging/filter.h"
#include "common/logging/log.h" #include "common/logging/log.h"
namespace FileUtil {
class IOFile;
}
namespace Common::Log { namespace Common::Log {
/** /**
@ -36,6 +39,7 @@ struct Entry {
class Backend { class Backend {
public: public:
virtual ~Backend() = default; virtual ~Backend() = default;
virtual void SetFilter(const Filter& new_filter) { virtual void SetFilter(const Filter& new_filter) {
filter = new_filter; filter = new_filter;
} }
@ -51,6 +55,8 @@ private:
*/ */
class ConsoleBackend : public Backend { class ConsoleBackend : public Backend {
public: public:
~ConsoleBackend() override;
static const char* Name() { static const char* Name() {
return "console"; return "console";
} }
@ -65,6 +71,8 @@ public:
*/ */
class ColorConsoleBackend : public Backend { class ColorConsoleBackend : public Backend {
public: public:
~ColorConsoleBackend() override;
static const char* Name() { static const char* Name() {
return "color_console"; return "color_console";
} }
@ -80,6 +88,8 @@ public:
*/ */
class LogcatBackend : public Backend { class LogcatBackend : public Backend {
public: public:
~LogcatBackend() override;
static const char* Name() { static const char* Name() {
return "logcat"; return "logcat";
} }
@ -95,6 +105,8 @@ public:
*/ */
class FileBackend : public Backend { class FileBackend : public Backend {
public: public:
~FileBackend() override;
explicit FileBackend(const std::string& filename); explicit FileBackend(const std::string& filename);
static const char* Name() { static const char* Name() {
@ -108,7 +120,7 @@ public:
void Write(const Entry& entry) override; void Write(const Entry& entry) override;
private: private:
FileUtil::IOFile file; std::unique_ptr<FileUtil::IOFile> file;
std::size_t bytes_written = 0; std::size_t bytes_written = 0;
}; };
@ -117,6 +129,8 @@ private:
*/ */
class DebuggerBackend : public Backend { class DebuggerBackend : public Backend {
public: public:
~DebuggerBackend() override;
static const char* Name() { static const char* Name() {
return "debugger"; return "debugger";
} }