Etterna 0.75.1
Loading...
Searching...
No Matches
ILogger.hpp
1#ifndef CORE_SERVICES_ILOGGER_HPP
2#define CORE_SERVICES_ILOGGER_HPP
3
4#include <string>
5#include <fmt/format.h>
6#include <utility>
7
8namespace Core {
19class ILogger {
20
21public:
23 virtual ~ILogger() = default;
24
29 enum class Severity {TRACE, DEBUG, INFO, WARN, ERR, FATAL};
30
31 // Logging Specific
32 template<typename... Args>
33 void trace(fmt::format_string<Args...> log, Args&&... args)
34 {
35 this->log(Severity::TRACE,
36 safe_format(log, std::forward<Args>(args)...));
37 }
38 template<typename... Args>
39 void debug(fmt::format_string<Args...> log, Args&&... args)
40 {
41 this->log(Severity::DEBUG,
42 safe_format(log, std::forward<Args>(args)...));
43 }
44 template<typename... Args>
45 void info(fmt::format_string<Args...> log, Args&&... args)
46 {
47 this->log(Severity::INFO,
48 safe_format(log, std::forward<Args>(args)...));
49 }
50 template<typename... Args>
51 void warn(fmt::format_string<Args...> log, Args&&... args)
52 {
53 this->log(Severity::WARN,
54 safe_format(log, std::forward<Args>(args)...));
55 }
56 template<typename... Args>
57 void error(fmt::format_string<Args...> log, Args&&... args)
58 {
59 this->log(Severity::ERR, safe_format(log, std::forward<Args>(args)...));
60 }
61 template<typename... Args>
62 void fatal(fmt::format_string<Args...> log, Args&&... args)
63 {
64 this->log(Severity::FATAL,
65 safe_format(log, std::forward<Args>(args)...));
66 }
67
68 virtual void setLogLevel(ILogger::Severity logLevel) = 0;
69 virtual std::string getLogFile() = 0;
70
71protected:
80 virtual void log(ILogger::Severity logLevel, const std::string_view message) = 0;
81
82private:
83 template<typename... Args>
84 inline std::string safe_format(fmt::format_string<Args...> log, Args&&... args)
85 {
86 try {
87 return fmt::format(log, std::forward<Args>(args)...);
88 } catch (fmt::format_error& e) {
89 std::string msg("There was an error formatting the next log "
90 "message - Report to developers: ");
91 msg.append(e.what());
92 this->log(Severity::ERR, msg);
93 return std::string(log.get().data());
94 }
95 }
96};
97}
98
99#endif // CORE_SERVICES_ILOGGER_HPP
Definition ILogger.hpp:19
virtual ~ILogger()=default
virtual void log(ILogger::Severity logLevel, const std::string_view message)=0
Severity
Definition ILogger.hpp:29