Add Logger: a new thread-safe logging system.

Closes #25.
This commit is contained in:
Guillaume Jacquemin 2022-04-24 14:25:47 +02:00
parent bb74a5c713
commit dd6d2491fd
5 changed files with 241 additions and 0 deletions

View File

@ -160,6 +160,10 @@ add_executable(MassBuilderSaveTool WIN32
Maps/WeaponTypes.hpp
ToastQueue/ToastQueue.h
ToastQueue/ToastQueue.cpp
Logger/Logger.h
Logger/Logger.cpp
Logger/MagnumLogBuffer.h
Logger/MagnumLogBuffer.cpp
Utilities/Crc32.h
FontAwesome/IconsFontAwesome5.h
FontAwesome/IconsFontAwesome5Brands.h

111
src/Logger/Logger.cpp Normal file
View File

@ -0,0 +1,111 @@
// MassBuilderSaveTool
// Copyright (C) 2021-2022 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifdef SAVETOOL_DEBUG_BUILD
#include <iostream>
#else
#include <fstream>
#endif
#include <mutex>
#include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Utility/Debug.h>
#include "Logger.h"
using Containers::Array;
using Utility::Debug;
using Utility::Warning;
using Utility::Error;
namespace MassBuilderSaveTool { namespace Logger {
#ifndef SAVETOOL_DEBUG_BUILD
static std::ofstream log_file{"SaveToolLog.txt", std::ios::trunc};
#endif
static std::mutex _logMutex;
static Array<LogEntry> _entries;
inline Debug&
operator<<(Debug& out, const LogEntry& entry) {
using namespace Containers::Literals;
static char formatted_time[22] = {'\0'};
std::strftime(&formatted_time[0], sizeof(formatted_time)/sizeof(formatted_time[0]), "[%Y-%m-%d %H:%M:%S]", &entry.timestamp);
out << formatted_time;
#ifdef SAVETOOL_DEBUG_BUILD
#define COLOUR(col) << Debug::color(Debug::Color::col)
#else
#define COLOUR(col)
#endif
switch(entry.type) {
case EntryType::Info:
out << "[INFO]"_s COLOUR(Default);
break;
case EntryType::Success:
out << "[SUCCESS]"_s COLOUR(Green);
break;
case EntryType::Warning:
out << "[WARNING]"_s COLOUR(Yellow);
break;
case EntryType::Error:
out << "[ERROR]"_s COLOUR(Red);
break;
}
#undef DEBUG_COLOUR
out << entry.message << Debug::resetColor;
return out;
}
void
initialise() {
arrayReserve(_entries, 100);
}
void
addEntry(EntryType type, StringView message) {
_logMutex.lock();
auto time = std::time(nullptr);
LogEntry entry{
*std::localtime(&time), type,
(message.back() == '\n') ? String{message.exceptSuffix(1)} : String{message}
};
Debug{
#ifndef SAVETOOL_DEBUG_BUILD
&log_file
#else
&std::cout
#endif
} << entry;
arrayAppend(_entries, std::move(entry));
_logMutex.unlock();
}
} // Logger
} // MassBuilderSaveTool

54
src/Logger/Logger.h Normal file
View File

@ -0,0 +1,54 @@
#pragma once
// MassBuilderSaveTool
// Copyright (C) 2021-2022 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <ctime>
#include <Corrade/Containers/String.h>
using namespace Corrade;
using Containers::String;
using Containers::StringView;
namespace MassBuilderSaveTool { namespace Logger {
enum class EntryType {
Info,
Success,
Warning,
Error,
};
struct LogEntry {
std::tm timestamp;
EntryType type;
String message;
};
void initialise();
void addEntry(EntryType type, StringView message);
} // Logger
} // MassBuilderSaveTool
#define LOG_INFO(message) MassBuilderSaveTool::Logger::addEntry(MassBuilderSaveTool::Logger::EntryType::Info, message)
#define LOG_SUCCESS(message) MassBuilderSaveTool::Logger::addEntry(MassBuilderSaveTool::Logger::EntryType::Success, message)
#define LOG_WARNING(message) MassBuilderSaveTool::Logger::addEntry(MassBuilderSaveTool::Logger::EntryType::Warning, message)
#define LOG_ERROR(message) MassBuilderSaveTool::Logger::addEntry(MassBuilderSaveTool::Logger::EntryType::Error, message)

View File

@ -0,0 +1,34 @@
// MassBuilderSaveTool
// Copyright (C) 2021-2022 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "MagnumLogBuffer.h"
namespace MassBuilderSaveTool { namespace Logger {
MagnumLogBuffer::MagnumLogBuffer(EntryType type): std::stringbuf(std::ios_base::out), _type{type} {}
MagnumLogBuffer::~MagnumLogBuffer() = default;
int
MagnumLogBuffer::sync() {
addEntry(_type, str().c_str());
str({});
return 0;
}
} // Logger
} // MassBuilderSaveTool

View File

@ -0,0 +1,38 @@
#pragma once
// MassBuilderSaveTool
// Copyright (C) 2021-2022 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <sstream>
#include "Logger.h"
namespace MassBuilderSaveTool { namespace Logger {
class MagnumLogBuffer : public std::stringbuf {
public:
explicit MagnumLogBuffer(EntryType type);
~MagnumLogBuffer();
private:
int sync() override;
EntryType _type;
};
} // Logger
} // MassBuilderSaveTool