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
#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<FileUtil::IOFile>(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();
}
}

View File

@ -8,10 +8,13 @@
#include <memory>
#include <string>
#include <string_view>
#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<FileUtil::IOFile> 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";
}