From bb066d3134b3a2f26e1cfc9ac19388bcd13a3a11 Mon Sep 17 00:00:00 2001 From: William JCM Date: Mon, 21 Nov 2022 10:15:10 +0100 Subject: [PATCH] CMakeLists: add Logger from the old 1.4 branch. --- src/CMakeLists.txt | 15 +++++ src/Logger/EntryType.h | 23 +++++++ src/Logger/Logger.cpp | 118 +++++++++++++++++++++++++++++++++ src/Logger/Logger.h | 88 ++++++++++++++++++++++++ src/Logger/MagnumLogBuffer.cpp | 30 +++++++++ src/Logger/MagnumLogBuffer.h | 34 ++++++++++ 6 files changed, 308 insertions(+) create mode 100644 src/Logger/EntryType.h create mode 100644 src/Logger/Logger.cpp create mode 100644 src/Logger/Logger.h create mode 100644 src/Logger/MagnumLogBuffer.cpp create mode 100644 src/Logger/MagnumLogBuffer.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fbafbc6..6487079 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,6 +28,19 @@ set_directory_properties(PROPERTIES CORRADE_USE_PEDANTIC_FLAGS ON) corrade_add_resource(Assets assets.conf) +add_library(Logger STATIC EXCLUDE_FROM_ALL + Logger/Logger.h + Logger/Logger.cpp + Logger/EntryType.h + Logger/MagnumLogBuffer.h + Logger/MagnumLogBuffer.cpp +) + +target_link_libraries(Logger PRIVATE + Corrade::Utility + Magnum::Magnum +) + add_library(UESaveFile STATIC EXCLUDE_FROM_ALL UESaveFile/Serialisers/AbstractUnrealCollectionPropertySerialiser.h UESaveFile/Serialisers/AbstractUnrealPropertySerialiser.h @@ -108,6 +121,7 @@ target_link_libraries(UESaveFile PRIVATE Corrade::Containers Corrade::Utility Magnum::Magnum + Logger ) add_executable(MassBuilderSaveTool WIN32 @@ -196,6 +210,7 @@ target_link_libraries(MassBuilderSaveTool PRIVATE Magnum::GL Magnum::Sdl2Application MagnumIntegration::ImGui + Logger UESaveFile efsw zip diff --git a/src/Logger/EntryType.h b/src/Logger/EntryType.h new file mode 100644 index 0000000..ac34d5c --- /dev/null +++ b/src/Logger/EntryType.h @@ -0,0 +1,23 @@ +#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 . + +enum class EntryType { + Info, + Warning, + Error, +}; diff --git a/src/Logger/Logger.cpp b/src/Logger/Logger.cpp new file mode 100644 index 0000000..6715886 --- /dev/null +++ b/src/Logger/Logger.cpp @@ -0,0 +1,118 @@ +// 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 . + +#ifdef SAVETOOL_DEBUG_BUILD +#include +#endif +#include + +#include +#include + +#include + +#include "Logger.h" + +using Containers::Array; +using Utility::Debug; +using Utility::Warning; +using Utility::Error; + +using namespace Magnum; + +Logger& +Logger::instance() { + static Logger logger; + return logger; +} + +void +Logger::initialise() { +#ifndef SAVETOOL_DEBUG_BUILD + _logFile.open("SaveToolLog.txt", std::ios::trunc); + _logFile << "In case you encounter a bug:\n" << + "1. Do not run the Save Tool again, as this log will be cleared.\n" << + "2. Go to either the official Sekai Project Discord guild, or the community M.A.S.S. Builder one.\n" << + "3. Mention me (William JCM#2301) to get my attention, with a description of the bug.\n" + " Please include as many details as possible, I don't want to play \"20 questions\", and neither do you.\n" << + "4. Send me this file _when I ask for it_, preferably in DMs.\n" << + std::endl; +#endif +} + +void +Logger::indent() { + _indentLevel++; +} + +void +Logger::unindent() { + if(_indentLevel > 0) { + _indentLevel--; + } +} + +void +Logger::log(EntryType type, StringView location, StringView message) { + Debug d{ + #ifndef SAVETOOL_DEBUG_BUILD + &_logFile + #else + &std::cout + #endif + }; + + #ifdef SAVETOOL_DEBUG_BUILD + #define COLOURED_TEXT(colour, text) Debug::color(Debug::Color::colour) << (text) << Debug::resetColor + #else + #define COLOURED_TEXT(colour, text) (text) + #endif + switch(type) { + case EntryType::Info: + d << COLOURED_TEXT(Default, "[ INFO]"_s); + break; + case EntryType::Warning: + d << COLOURED_TEXT(Yellow, "[WARNING]"_s); + break; + case EntryType::Error: + d << COLOURED_TEXT(Red, "[ ERROR]"_s); + break; + } + #undef COLOURED_TEXT + + d << "["_s << Debug::nospace << location << Debug::nospace << "]"; + + for(UnsignedInt i = 0; i < _indentLevel; i++) { + d << Debug::nospace << " "_s << Debug::nospace; + } + + d << ((message.back() == '\n') ? message.exceptSuffix(1) : message); +} + +void +Logger::lockMutex() { + _logMutex.lock(); +} + +void +Logger::unlockMutex() { + _logMutex.unlock(); +} + +Logger& +logger() { + return Logger::instance(); +} diff --git a/src/Logger/Logger.h b/src/Logger/Logger.h new file mode 100644 index 0000000..75d0f2d --- /dev/null +++ b/src/Logger/Logger.h @@ -0,0 +1,88 @@ +#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 . + +#include + +#include +#include + +#include +#include +#include + +#include + +#include "EntryType.h" + +using namespace Corrade; + +using Containers::ArrayView; +using Containers::String; +using Containers::StringView; + +using namespace Magnum; + +using namespace Containers::Literals; + +class Logger { + public: + Logger(const Logger&) = delete; + Logger& operator=(const Logger&) = delete; + + Logger(Logger&&) = delete; + Logger& operator=(Logger&&) = delete; + + static auto instance() -> Logger&; + + void initialise(); + + void indent(); + void unindent(); + + void log(EntryType type, StringView location, StringView message); + + void lockMutex(); + void unlockMutex(); + + private: + Logger() = default; + + #ifndef SAVETOOL_DEBUG_BUILD + std::ofstream _logFile; + #endif + + UnsignedInt _indentLevel = 0; + + std::mutex _logMutex{}; +}; + +auto logger() -> Logger&; + +#define LOG(entry_type, message) logger().lockMutex(); \ + logger().log(EntryType::entry_type, \ + Utility::format("{}:{}", StringView{__builtin_FILE()}.find("src"_s).data() + 4, __builtin_LINE()), \ + message); \ + logger().unlockMutex() + +#define LOG_INFO(message) LOG(Info, message) +#define LOG_WARNING(message) LOG(Warning, message) +#define LOG_ERROR(message) LOG(Error, message) + +#define LOG_INFO_FORMAT(message, ...) LOG_INFO(Utility::format(message, __VA_ARGS__)) +#define LOG_WARNING_FORMAT(message, ...) LOG_WARNING(Utility::format(message, __VA_ARGS__)) +#define LOG_ERROR_FORMAT(message, ...) LOG_ERROR(Utility::format(message, __VA_ARGS__)) diff --git a/src/Logger/MagnumLogBuffer.cpp b/src/Logger/MagnumLogBuffer.cpp new file mode 100644 index 0000000..e2e3df9 --- /dev/null +++ b/src/Logger/MagnumLogBuffer.cpp @@ -0,0 +1,30 @@ +// 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 . + +#include "MagnumLogBuffer.h" + +MagnumLogBuffer::MagnumLogBuffer(EntryType type): std::stringbuf(std::ios_base::out), _type{type} {} + +MagnumLogBuffer::~MagnumLogBuffer() = default; + +int +MagnumLogBuffer::sync() { + logger().lockMutex(); + logger().log(_type, "Corrade/Magnum"_s, str().c_str()); + logger().unlockMutex(); + str({}); + return 0; +} diff --git a/src/Logger/MagnumLogBuffer.h b/src/Logger/MagnumLogBuffer.h new file mode 100644 index 0000000..ea7888c --- /dev/null +++ b/src/Logger/MagnumLogBuffer.h @@ -0,0 +1,34 @@ +#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 . + +#include + +#include "Logger.h" + +#include "EntryType.h" + +class MagnumLogBuffer : public std::stringbuf { + public: + explicit MagnumLogBuffer(EntryType type); + ~MagnumLogBuffer(); + + private: + int sync() override; + + EntryType _type; +};