CMakeLists: add Logger from the old 1.4 branch.

This commit is contained in:
Guillaume Jacquemin 2022-11-21 10:15:10 +01:00
parent e21e7a1aba
commit bb066d3134
6 changed files with 308 additions and 0 deletions

View File

@ -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

23
src/Logger/EntryType.h Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
enum class EntryType {
Info,
Warning,
Error,
};

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

@ -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 <https://www.gnu.org/licenses/>.
#ifdef SAVETOOL_DEBUG_BUILD
#include <iostream>
#endif
#include <mutex>
#include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Utility/Debug.h>
#include <Magnum/Types.h>
#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();
}

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

@ -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 <https://www.gnu.org/licenses/>.
#include <ctime>
#include <mutex>
#include <fstream>
#include <Corrade/Containers/String.h>
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Utility/Format.h>
#include <Magnum/Types.h>
#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__))

View File

@ -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 <https://www.gnu.org/licenses/>.
#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;
}

View File

@ -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 <https://www.gnu.org/licenses/>.
#include <sstream>
#include "Logger.h"
#include "EntryType.h"
class MagnumLogBuffer : public std::stringbuf {
public:
explicit MagnumLogBuffer(EntryType type);
~MagnumLogBuffer();
private:
int sync() override;
EntryType _type;
};