Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
beats-dh committed Oct 13, 2024
1 parent 06c6cd5 commit 2a1e52c
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 88 deletions.
1 change: 0 additions & 1 deletion src/account/account_repository_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "account/account_repository_db.hpp"

#include "database/database.hpp"
#include "lib/logging/logger.hpp"
#include "utils/definitions.hpp"
#include "utils/tools.hpp"
#include "enums/account_type.hpp"
Expand Down
1 change: 0 additions & 1 deletion src/database/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#pragma once

#include "declarations.hpp"
#include "lib/logging/logger.hpp"

#ifndef USE_PRECOMPILED_HEADERS
#include <mysql/mysql.h>
Expand Down
1 change: 0 additions & 1 deletion src/kv/kv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <list>
#endif

#include "lib/logging/logger.hpp"
#include "kv/value_wrapper.hpp"

class KV : public std::enable_shared_from_this<KV> {
Expand Down
1 change: 0 additions & 1 deletion src/kv/kv_sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "kv/kv_sql.hpp"

#include "database/database.hpp"
#include "lib/logging/logger.hpp"
#include "kv/value_wrapper_proto.hpp"
#include "utils/tools.hpp"

Expand Down
1 change: 1 addition & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
di/soft_singleton.cpp
logging/logger.cpp
logging/log_with_spd_log.cpp
thread/thread_pool.cpp
)

Expand Down
4 changes: 2 additions & 2 deletions src/lib/di/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "account/account_repository_db.hpp"
#include "lib/di/injector.hpp"
#include "lib/logging/logger.hpp"
#include "lib/logging/log_with_spd_log.hpp"
#include "kv/kv_sql.hpp"

namespace di = boost::di;
Expand All @@ -21,7 +21,7 @@ class DI final {
const inline static auto defaultContainer = di::make_injector(
di::bind<AccountRepository>().to<AccountRepositoryDB>().in(di::singleton),
di::bind<KVStore>().to<KVSQL>().in(di::singleton),
di::bind<Logger>().to<Logger>().in(di::singleton)
di::bind<Logger>().to<LogWithSpdLog>().in(di::singleton)
);

public:
Expand Down
1 change: 0 additions & 1 deletion src/lib/di/soft_singleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#pragma once

#include <iostream>
#include "lib/logging/logger.hpp"

class SoftSingleton {
public:
Expand Down
61 changes: 61 additions & 0 deletions src/lib/logging/log_with_spd_log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <[email protected]>
* Repository: https://github.com/opentibiabr/canary
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.com/
*/

#include <spdlog/spdlog.h>
#include "lib/di/container.hpp"

LogWithSpdLog::LogWithSpdLog() {
setLevel("info");
spdlog::set_pattern("[%Y-%d-%m %H:%M:%S.%e] [%^%l%$] %v ");

#ifdef DEBUG_LOG
spdlog::set_pattern("[%Y-%d-%m %H:%M:%S.%e] [thread %t] [%^%l%$] %v ");
#endif
}

Logger &LogWithSpdLog::getInstance() {
return inject<Logger>();
}

void LogWithSpdLog::setLevel(const std::string &name) const {
debug("Setting log level to: {}.", name);
const auto level = spdlog::level::from_str(name);
spdlog::set_level(level);
}

std::string LogWithSpdLog::getLevel() const {
const auto level = spdlog::level::to_string_view(spdlog::get_level());
return std::string { level.begin(), level.end() };
}

void LogWithSpdLog::info(const std::string &msg) const {
SPDLOG_INFO(msg);
}

void LogWithSpdLog::warn(const std::string &msg) const {
SPDLOG_WARN(msg);
}

void LogWithSpdLog::error(const std::string &msg) const {
SPDLOG_WARN(msg);
}

void LogWithSpdLog::critical(const std::string &msg) const {
SPDLOG_WARN(msg);
}

#if defined(DEBUG_LOG)
void LogWithSpdLog::debug(const std::string &msg) const {
SPDLOG_DEBUG(msg);
}

void LogWithSpdLog::trace(const std::string &msg) const {
SPDLOG_TRACE(msg);
}
#endif
53 changes: 53 additions & 0 deletions src/lib/logging/log_with_spd_log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <[email protected]>
* Repository: https://github.com/opentibiabr/canary
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.com/
*/
#pragma once

#include "logger.hpp"

class LogWithSpdLog final : public Logger {
public:
LogWithSpdLog();
~LogWithSpdLog() override = default;

static Logger &getInstance();

void setLevel(const std::string &name) const override;
std::string getLevel() const override;

void info(const std::string &msg) const override;
void warn(const std::string &msg) const override;
void error(const std::string &msg) const override;
void critical(const std::string &msg) const override;

#if defined(DEBUG_LOG)
void debug(const std::string &msg) const override;
void trace(const std::string &msg) const override;

template <typename... Args>
void debug(const fmt::format_string<Args...> &fmt, Args &&... args) const {
debug(fmt::format(fmt, std::forward<Args>(args)...));
}

template <typename... Args>
void trace(const fmt::format_string<Args...> &fmt, Args &&... args) const {
trace(fmt::format(fmt, std::forward<Args>(args)...));
}
#else
void debug(const std::string &) const override { }
void trace(const std::string &) const override { }

template <typename... Args>
void debug(const fmt::format_string<Args...> &, Args &&...) const { }

template <typename... Args>
void trace(const fmt::format_string<Args...> &, Args &&...) const { }
#endif
};

constexpr auto g_logger = LogWithSpdLog::getInstance;
50 changes: 0 additions & 50 deletions src/lib/logging/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,8 @@
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>

#include "pch.hpp"
#include "lib/di/container.hpp"

Logger::Logger() {
spdlog::set_pattern("[%Y-%d-%m %H:%M:%S.%e] [%^%l%$] %v ");

#ifdef DEBUG_LOG
spdlog::set_level(spdlog::level::trace);
spdlog::set_pattern("[%Y-%d-%m %H:%M:%S.%e] [thread %t] [%^%l%$] %v ");
#endif

const std::tm local_tm = get_local_time();

std::ostringstream oss;
oss << std::put_time(&local_tm, "%Y-%m-%d_%H-%M-%S");
std::string filename = "log/server_log_" + oss.str() + ".txt";

try {
// Limpar logs antigos
cleanOldLogs("log", 2);

auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);

const auto combined_logger = std::make_shared<spdlog::logger>(
"",
spdlog::sinks_init_list { console_sink, file_sink }
);

combined_logger->set_level(spdlog::get_level());

combined_logger->set_pattern("[%Y-%d-%m %H:%M:%S.%e] [%^%l%$] %v ");

#ifdef DEBUG_LOG
combined_logger->set_pattern("[%Y-%d-%m %H:%M:%S.%e] [thread %t] [%^%l%$] %v ");
#endif

combined_logger->flush_on(spdlog::level::info);

set_default_logger(combined_logger);

info("Logger initialized and configured for console and file output.");
} catch (const spdlog::spdlog_ex &ex) {
std::cerr << "Log initialization failed: " << ex.what() << std::endl;
}
}

Logger &Logger::getInstance() {
return inject<Logger>();
}

void Logger::setLevel(const std::string &name) const {
debug("Setting log level to: {}.", name);
const auto level = spdlog::level::from_str(name);
Expand Down
30 changes: 15 additions & 15 deletions src/lib/logging/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ namespace spdlog {

class Logger {
public:
Logger();
~Logger() = default;
Logger() = default;
virtual ~Logger() = default;

static Logger &getInstance();
// Ensures that we don't accidentally copy it
Logger(const Logger &) = delete;
virtual Logger &operator=(const Logger &) = delete;

void setLevel(const std::string &name) const;
std::string getLevel() const;
virtual void setLevel(const std::string &name) const = 0;
virtual std::string getLevel() const = 0;

void logProfile(const std::string &name, double duration_ms) const;
void cleanOldLogs(const std::string &logDirectory, int days) const;

void info(const std::string &msg) const;
void warn(const std::string &msg) const;
void error(const std::string &msg) const;
void critical(const std::string &msg) const;
virtual void info(const std::string &msg) const;
virtual void warn(const std::string &msg) const;
virtual void error(const std::string &msg) const;
virtual void critical(const std::string &msg) const;

template <typename Func>
auto profile(const std::string &name, Func func) -> decltype(func()) {
Expand All @@ -44,26 +46,26 @@ class Logger {
}

#if defined(DEBUG_LOG)
void debug(const std::string &msg) const;
virtual void debug(const std::string &msg) const;

template <typename... Args>
void debug(const fmt::format_string<Args...> &fmt, Args &&... args) const {
debug(fmt::format(fmt, std::forward<Args>(args)...));
}

void trace(const std::string &msg) const;
virtual void trace(const std::string &msg) const;

template <typename... Args>
void trace(const fmt::format_string<Args...> &fmt, Args &&... args) const {
trace(fmt::format(fmt, std::forward<Args>(args)...));
}
#else
void debug(const std::string &) const { }
virtual void debug(const std::string &) const { }

template <typename... Args>
void debug(const fmt::format_string<Args...> &, Args &&...) const { }

void trace(const std::string &) const { }
virtual void trace(const std::string &) const { }

template <typename... Args>
void trace(const fmt::format_string<Args...> &, Args &&...) const { }
Expand Down Expand Up @@ -106,5 +108,3 @@ class Logger {
return local_tm;
}
};

constexpr auto g_logger = Logger::getInstance;
1 change: 0 additions & 1 deletion src/lib/thread/thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
#pragma once

#include "lib/logging/logger.hpp"
#include "BS_thread_pool.hpp"

class ThreadPool : public BS::thread_pool {
Expand Down
1 change: 0 additions & 1 deletion src/lua/scripts/luascript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#pragma once

#include "lib/logging/logger.hpp"
#include "lua/functions/lua_functions_loader.hpp"
#include "lua/scripts/script_environment.hpp"

Expand Down
24 changes: 14 additions & 10 deletions src/pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@
#include <fmt/ranges.h>

// FMT Custom Formatter for Enums
template <typename E>
struct fmt::formatter<E, std::enable_if_t<std::is_enum_v<E>, char>> : fmt::formatter<std::underlying_type_t<E>> {
template <typename FormatContext>
auto format(E e, FormatContext &ctx) const {
return fmt::formatter<std::underlying_type_t<E>>::format(
static_cast<std::underlying_type_t<E>>(e), ctx
);
}
};
namespace fmt {
template <typename E>
struct formatter<E, std::enable_if_t<std::is_enum_v<E>, char>> : formatter<std::underlying_type_t<E>> {
// Define o formato do enum convertendo para seu tipo subjacente
template <typename FormatContext>
auto format(E e, FormatContext &ctx) const -> decltype(ctx.out()) {
return formatter<std::underlying_type_t<E>>::format(
static_cast<std::underlying_type_t<E>>(e), ctx
);
}
};
}


// GMP
#include <gmp.h>
Expand Down Expand Up @@ -167,7 +171,7 @@ struct fmt::formatter<E, std::enable_if_t<std::is_enum_v<E>, char>> : fmt::forma
#include "lib/messaging/message.hpp"
#include "lib/messaging/command.hpp"
#include "lib/messaging/event.hpp"
#include "lib/logging/logger.hpp"
#include <lib/logging/log_with_spd_log.hpp>

#include <eventpp/utilities/scopedremover.h>
#include <eventpp/eventdispatcher.h>
Expand Down
1 change: 0 additions & 1 deletion src/server/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#pragma once

#include "lib/logging/logger.hpp"
#include "lib/metrics/metrics.hpp"
#include "server/network/connection/connection.hpp"
#include "server/signals.hpp"
Expand Down
Loading

0 comments on commit 2a1e52c

Please sign in to comment.