Cleanup Logger
This commit is contained in:
@@ -1,8 +1,25 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "Logger.hpp"
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <spdlog/sinks/rotating_file_sink.h>
|
||||
#include <spdlog/sinks/null_sink.h>
|
||||
#ifndef NO_CONSOLE_LOG
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#endif
|
||||
#ifdef _MSC_VER
|
||||
#include <spdlog/sinks/msvc_sink.h>
|
||||
#endif
|
||||
|
||||
namespace openVulkanoCpp
|
||||
{
|
||||
std::vector<spdlog::sink_ptr> Logger::sinks;
|
||||
std::vector<spdlog::sink_ptr> Logger::SINKS;
|
||||
std::vector<std::weak_ptr<spdlog::logger>> Logger::LOGGERS;
|
||||
std::shared_ptr<spdlog::logger> Logger::WINDOW = nullptr;
|
||||
std::shared_ptr<spdlog::logger> Logger::MANAGER = nullptr;
|
||||
std::shared_ptr<spdlog::logger> Logger::RENDER = nullptr;
|
||||
@@ -11,4 +28,87 @@ namespace openVulkanoCpp
|
||||
std::shared_ptr<spdlog::logger> Logger::DATA = nullptr;
|
||||
std::shared_ptr<spdlog::logger> Logger::SCENE = nullptr;
|
||||
std::shared_ptr<spdlog::logger> Logger::INPUT = nullptr;
|
||||
|
||||
void Logger::SetupLogger(const std::string& logFolder, const std::string& logFile)
|
||||
{
|
||||
static bool initialized = false;
|
||||
if (initialized) return;
|
||||
try
|
||||
{
|
||||
try
|
||||
{ //TODO allow log files in folders
|
||||
SINKS.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logFile, 1024 * 1024 * 512, 3, true));
|
||||
}
|
||||
catch (const spdlog::spdlog_ex& e)
|
||||
{
|
||||
std::cerr << "Failed to create file log sink: " << e.what() << std::endl;
|
||||
}
|
||||
#ifndef NO_CONSOLE_LOG
|
||||
SINKS.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
|
||||
#endif
|
||||
#ifdef _MSC_VER // If it was build with msvc in debug we can use the msvc sink
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::msvc_sink_mt>());
|
||||
#endif
|
||||
// Make sure that there is always a sink for the loggers
|
||||
if (SINKS.empty()) SINKS.push_back(std::make_shared<spdlog::sinks::null_sink_mt>());
|
||||
|
||||
MANAGER = CreateLogger("manager");
|
||||
WINDOW = CreateLogger("window");
|
||||
RENDER = CreateLogger("render");
|
||||
PHYSIC = CreateLogger("physic");
|
||||
AUDIO = CreateLogger("audio");
|
||||
DATA = CreateLogger("data");
|
||||
SCENE = CreateLogger("scene");
|
||||
INPUT = CreateLogger("input");
|
||||
|
||||
spdlog::flush_every(std::chrono::seconds(5));
|
||||
|
||||
MANAGER->info("Logger initialized");
|
||||
initialized = true;
|
||||
}
|
||||
catch (const spdlog::spdlog_ex& e)
|
||||
{
|
||||
std::cerr << "Log initialization failed: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<spdlog::logger> Logger::CreateLogger(const std::string& name, const bool reg)
|
||||
{
|
||||
auto logger = std::make_shared<spdlog::logger>(name, SINKS.begin(), SINKS.end());
|
||||
if (reg) spdlog::register_logger(logger);
|
||||
#ifdef LOG_DATE
|
||||
logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [T%t] [%^%l%$] [%n]: %v");
|
||||
#else
|
||||
logger->set_pattern("[%H:%M:%S.%e] [T%t] [%^%l%$] [%n]: %v");
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
logger->set_level(spdlog::level::debug);
|
||||
#endif
|
||||
|
||||
LOGGERS.push_back(logger);
|
||||
|
||||
return logger;
|
||||
}
|
||||
|
||||
std::shared_ptr<spdlog::logger> Logger::GetLogger(const std::string& name)
|
||||
{
|
||||
return spdlog::get(name);
|
||||
}
|
||||
|
||||
void Logger::RegisterSink(const spdlog::sink_ptr& newSink)
|
||||
{
|
||||
auto iter = LOGGERS.begin();
|
||||
while(iter != LOGGERS.end())
|
||||
{
|
||||
if (const auto logger = (*iter).lock())
|
||||
{
|
||||
logger->sinks().push_back(newSink);
|
||||
}
|
||||
else
|
||||
{
|
||||
iter = LOGGERS.erase(iter);
|
||||
}
|
||||
}
|
||||
SINKS.push_back(newSink);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,23 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define SPDLOG_DEBUG_ON
|
||||
#define SPDLOG_TRACE_ON
|
||||
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
#include "spdlog/sinks/null_sink.h"
|
||||
#ifndef NO_CONSOLE_LOG
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#endif
|
||||
#ifdef _MSC_VER
|
||||
#include "spdlog/sinks/msvc_sink.h"
|
||||
#endif
|
||||
|
||||
namespace openVulkanoCpp
|
||||
{
|
||||
class Logger
|
||||
{ //TODO add custom sink for in game/engine console
|
||||
static std::vector<spdlog::sink_ptr> sinks;
|
||||
static std::vector<spdlog::sink_ptr> SINKS;
|
||||
static std::vector<std::weak_ptr<spdlog::logger>> LOGGERS;
|
||||
|
||||
public:
|
||||
static std::shared_ptr<spdlog::logger> WINDOW;
|
||||
static std::shared_ptr<spdlog::logger> MANAGER;
|
||||
@@ -30,68 +28,18 @@ namespace openVulkanoCpp
|
||||
static std::shared_ptr<spdlog::logger> SCENE;
|
||||
static std::shared_ptr<spdlog::logger> INPUT;
|
||||
|
||||
static void SetupLogger(std::string logFolder = "logs", std::string logFile = "openVulkano.log")
|
||||
{
|
||||
static bool initialized = false;
|
||||
if (initialized) return;
|
||||
try
|
||||
{
|
||||
try
|
||||
{ //TODO allow log files in folders
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logFile, 1024 * 1024 * 512, 3, true));
|
||||
}
|
||||
catch (const spdlog::spdlog_ex& e)
|
||||
{
|
||||
std::cerr << "Log create file log sink: " << e.what() << std::endl;
|
||||
}
|
||||
#ifndef NO_CONSOLE_LOG
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
|
||||
#endif
|
||||
#ifdef _MSC_VER // If it was build with msvc in debug we can use the msvc sink
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::msvc_sink_mt>());
|
||||
#endif
|
||||
// Make sure that there is always a sink for the loggers
|
||||
if (sinks.empty()) sinks.push_back(std::make_shared<spdlog::sinks::null_sink_mt>());
|
||||
|
||||
MANAGER = CreateLogger("manager");
|
||||
WINDOW = CreateLogger("window");
|
||||
RENDER = CreateLogger("render");
|
||||
PHYSIC = CreateLogger("physic");
|
||||
AUDIO = CreateLogger("audio");
|
||||
DATA = CreateLogger("data");
|
||||
SCENE = CreateLogger("scene");
|
||||
INPUT = CreateLogger("input");
|
||||
|
||||
spdlog::flush_every(std::chrono::seconds(5));
|
||||
|
||||
MANAGER->info("Logger initialized");
|
||||
initialized = true;
|
||||
}
|
||||
catch (const spdlog::spdlog_ex& e)
|
||||
{
|
||||
std::cerr << "Log initialization failed: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
static void SetupLogger(const std::string& logFolder = "logs", const std::string& logFile = "openVulkano.log");
|
||||
|
||||
/**
|
||||
* \brief Creates a new custom logger that writes to the main log file.
|
||||
* \param name The name of the logger
|
||||
* \param reg If set to true the logger can be accessed again with spdlog::get(name)
|
||||
* \param reg If set to true the logger can be accessed again with Logger::GetLogger(name)
|
||||
* \return The created logger
|
||||
*/
|
||||
static std::shared_ptr<spdlog::logger> CreateLogger(const std::string& name, const bool reg = true)
|
||||
{
|
||||
auto logger = std::make_shared<spdlog::logger>(name, sinks.begin(), sinks.end());
|
||||
if (reg) spdlog::register_logger(logger);
|
||||
#ifdef LOG_DATE
|
||||
logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [T%t] [%^%l%$] [%n]: %v");
|
||||
#else
|
||||
logger->set_pattern("[%H:%M:%S.%e] [T%t] [%^%l%$] [%n]: %v");
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
logger->set_level(spdlog::level::debug);
|
||||
#endif
|
||||
return logger;
|
||||
}
|
||||
static std::shared_ptr<spdlog::logger> CreateLogger(const std::string& name, bool reg = true);
|
||||
|
||||
static std::shared_ptr<spdlog::logger> GetLogger(const std::string& name);
|
||||
|
||||
static void RegisterSink(const spdlog::sink_ptr& newSink);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user