MassBuilderSaveTool/src/Logger/Logger.h

78 lines
2.4 KiB
C++

#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>
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Utility/Format.h>
using namespace Corrade;
using Containers::ArrayView;
using Containers::String;
using Containers::StringView;
namespace Logger {
enum class EntryType {
Info,
Success,
Warning,
Error,
};
struct LogEntry {
String timestamp;
EntryType type;
String message;
};
void initialise();
void indent();
void unindent();
void addEntry(EntryType type, StringView message);
auto entries() -> ArrayView<const LogEntry>;
void lockMutex();
void unlockMutex();
bool tryLockMutex();
} // Logger
#define LOG_INFO(message) Logger::lockMutex(); \
Logger::addEntry(Logger::EntryType::Info, message); \
Logger::unlockMutex()
#define LOG_SUCCESS(message) Logger::lockMutex(); \
Logger::addEntry(Logger::EntryType::Success, message); \
Logger::unlockMutex()
#define LOG_WARNING(message) Logger::lockMutex(); \
Logger::addEntry(Logger::EntryType::Warning, message); \
Logger::unlockMutex()
#define LOG_ERROR(message) Logger::lockMutex(); \
Logger::addEntry(Logger::EntryType::Error, message); \
Logger::unlockMutex()
#define LOG_INFO_FORMAT(message, ...) LOG_INFO(Utility::format(message, __VA_ARGS__))
#define LOG_SUCCESS_FORMAT(message, ...) LOG_SUCCESS(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__))