Add logging using spdlog

This commit is contained in:
karl 2021-03-20 16:55:43 +01:00
parent a626a4bca0
commit fb29ca07d5
7 changed files with 71 additions and 4 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "cpp/vendor/spdlog"]
path = cpp/vendor/spdlog
url = https://github.com/gabime/spdlog

View File

@ -1,5 +1,9 @@
#!python
def add_third_party_includes(env):
env.Append(CPPPATH=['cpp/', 'cpp/vendor/spdlog/include'])
# Create the environment and create a Compilation Database for use in VSCodium
env = Environment(tools=['default', 'compilation_db'])
env.CompilationDatabase()
@ -7,6 +11,8 @@ env.CompilationDatabase()
env.Append(CCFLAGS=["-Wall", "-Wextra", "-Werror", "-pedantic"])
env.Append(CPPPATH=['cpp/', 'include/'])
add_third_party_includes(env)
# Build the library
gedeng = env.SharedLibrary('lib/gedeng', Glob('cpp/*.cpp'))
env.Install('test/bin/lib/', gedeng)
@ -25,6 +31,8 @@ testEnv.Append(LINKFLAGS=[
'-Wl,--disable-new-dtags,-rpath,\'$$ORIGIN/lib/\''
])
add_third_party_includes(testEnv)
# Build the test programs
catch_cpp = "test/catch_amalgamated.cpp"
testEnv.Program('test/bin/vector-test.out', [catch_cpp, 'test/vector/vector-test.cpp'])

View File

@ -1,10 +1,21 @@
#include "Gedeng/Logger.h"
#include <iostream>
#include "spdlog/common.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
namespace Gedeng {
void Logger::debug(const String &text) {
std::cout << "Debug: " << text << std::endl;
std::shared_ptr<spdlog::logger> Logger::core_logger;
std::shared_ptr<spdlog::logger> Logger::client_logger;
void Logger::init() {
spdlog::set_pattern("%^[%T] %n: %v%$");
core_logger = spdlog::stdout_color_mt("GEDENG");
core_logger->set_level(spdlog::level::trace);
client_logger = spdlog::stdout_color_mt("APP");
client_logger->set_level(spdlog::level::trace);
}
} // namespace Gedeng

1
cpp/vendor/spdlog vendored Submodule

@ -0,0 +1 @@
Subproject commit 44e1f9f6829cd8acbfea282d390560452e889aff

View File

@ -7,6 +7,10 @@ extern Gedeng::Application *Gedeng::create_application();
// If the user program defines GEDENG_MAIN, supply this main function
#ifdef GEDENG_MAIN
int main() {
// Initialize logging
Gedeng::Logger::init();
GG_CORE_WARN("Logger initialized");
Gedeng::Application *app = Gedeng::create_application();
app->run();

View File

@ -1,11 +1,49 @@
#pragma once
#include "Gedeng/String.h"
#include "spdlog/spdlog.h"
#include <memory>
namespace Gedeng {
class Logger {
public:
static void init();
inline static std::shared_ptr<spdlog::logger> &get_core_logger() {
return core_logger;
}
inline static std::shared_ptr<spdlog::logger> &get_client_logger() {
return client_logger;
}
static void debug(const String &text);
private:
static std::shared_ptr<spdlog::logger> core_logger;
static std::shared_ptr<spdlog::logger> client_logger;
};
} // namespace Gedeng
} // namespace Gedeng
// Core log macros
#define GG_CORE_FATAL(...) \
::Gedeng::Logger::get_core_logger()->fatal(__VA_ARGS__)
#define GG_CORE_ERROR(...) \
::Gedeng::Logger::get_core_logger()->error(__VA_ARGS__)
#define GG_CORE_WARN(...) ::Gedeng::Logger::get_core_logger()->warn(__VA_ARGS__)
#define GG_CORE_INFO(...) ::Gedeng::Logger::get_core_logger()->info(__VA_ARGS__)
#define GG_CORE_TRACE(...) \
::Gedeng::Logger::get_core_logger()->trace(__VA_ARGS__)
// Client log macros
#define GG_CLIENT_FATAL(...) \
::Gedeng::Logger::get_client_logger()->fatal(__VA_ARGS__)
#define GG_CLIENT_ERROR(...) \
::Gedeng::Logger::get_client_logger()->error(__VA_ARGS__)
#define GG_CLIENT_WARN(...) \
::Gedeng::Logger::get_client_logger()->warn(__VA_ARGS__)
#define GG_CLIENT_INFO(...) \
::Gedeng::Logger::get_client_logger()->info(__VA_ARGS__)
#define GG_CLIENT_TRACE(...) \
::Gedeng::Logger::get_client_logger()->trace(__VA_ARGS__)

View File

@ -1,3 +1,4 @@
#include "Gedeng/Logger.h"
#define GEDENG_MAIN
#include <Gedeng.h>
@ -9,5 +10,6 @@ class TestApp : public Gedeng::Application {
};
Gedeng::Application *Gedeng::create_application() {
GG_CLIENT_INFO("Creating Application");
return new TestApp();
}