Skip to content

Commit

Permalink
feat: Add example of logger usage via DI inject
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrossi committed Aug 12, 2023
1 parent dd33007 commit 7648476
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
66 changes: 66 additions & 0 deletions src/lib/logging/Logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2022 OpenTibiaBR <opentibiabr@outlook.com>
* 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/
*/
#ifndef CANARY_ILOGGER_HPP
#define CANARY_ILOGGER_HPP

#include <fmt/core.h>
#include <string>

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

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

template <typename... Args>
void trace(const std::string &format, Args &&... args) {
_trace(_format(format, args...));
}

template <typename... Args>
void debug(const std::string &format, Args &&... args) {
_debug(_format(format, args...));
}

template <typename... Args>
void info(const std::string &format, Args &&... args) {
_info(_format(format, args...));
}

template <typename... Args>
void warn(auto &format, Args &&... args) {
_warn(_format(format, args...));
}

template <typename... Args>
void error(const std::string &format, Args &&... args) {
_error(_format(format, args...));
}

template <typename... Args>
void critical(const std::string &format, Args &&... args) {
_critical(_format(format, args...));
}

private:
template <typename... Args>
std::string _format(const std::string &format, Args &&... args) const {
return fmt::format(fmt::runtime(format), args...);
}

virtual void _trace(const std::string &format) = 0;
virtual void _debug(const std::string &format) = 0;
virtual void _info(const std::string &format) = 0;
virtual void _warn(const std::string &format) = 0;
virtual void _error(const std::string &format) = 0;
virtual void _critical(const std::string &format) = 0;
};

#endif // CANARY_ILOGGER_HPP
2 changes: 1 addition & 1 deletion src/otserv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void mainLoader(int, char*[], ServiceManager* services) {
platform = "unknown";
#endif

SPDLOG_INFO("Compiled with {}, on {} {}, for platform {}\n", getCompiler(), __DATE__, __TIME__, platform);
inject<Logger>().info("Compiled with {}, on {} {}, for platform {}\n", getCompiler(), __DATE__, __TIME__, platform);

#if defined(LUAJIT_VERSION)
SPDLOG_INFO("Linked with {} for Lua support", LUAJIT_VERSION);
Expand Down

0 comments on commit 7648476

Please sign in to comment.