lintel 0.2.0
Loading...
Searching...
No Matches
LoggerService.h
Go to the documentation of this file.
1#ifndef LOGGING_LOGGER_H
2#define LOGGING_LOGGER_H
3
14#include <cstdint>
15#include <format>
16#include <memory>
17#include <mutex>
18#include <string_view>
19
20namespace lintel {
21class Configuration;
22}// namespace lintel
24
25namespace spdlog {
27class logger;
28}// namespace spdlog
29
30namespace lintel {
35enum class LogLevel : std::uint8_t { trace = 0, debug = 1, info = 2, warn = 3, error = 4, critical = 5, off = 6 };
36
38#ifndef LINTEL_FUNCTION
39#if defined(_MSC_VER)
40#define LINTEL_FUNCTION __FUNCSIG__
41#elif defined(__GNUC__) || defined(__clang__)
42#define LINTEL_FUNCTION __PRETTY_FUNCTION__
43#else
44#define LINTEL_FUNCTION __func__
45#endif
46#endif
47
50{
51private:
52 // variables
53 std::shared_ptr<ProcessName> m_processName;
54 std::shared_ptr<Configuration> m_configuration;
55 std::shared_ptr<spdlog::logger> m_logger;
56
58 std::shared_ptr<spdlog::logger> init();
59
64 void logImpl(const char *file,
65 int line,
66 const char *function,
68 std::string_view message,
69 std::format_args args);
70
71 static std::unique_ptr<LoggerService> m_instance;
72 static std::mutex m_instanceMutex;
73
74public:
77
81 LoggerService(std::shared_ptr<ProcessName> processName, std::shared_ptr<Configuration> configuration);
82
85
90 static LoggerService &getOrCreate(const std::shared_ptr<ProcessName> &processInfo,
91 const std::shared_ptr<Configuration> &configuration);
92
95 static LoggerService &get();
96
100 static void initSingleton(const std::shared_ptr<ProcessName> &processInfo,
101 const std::shared_ptr<Configuration> &configuration);
102
104 static void initSingleton2();
105
116 template<typename... Args>
117 void log(const char *file, int line, const char *function, LogLevel lvl, std::string_view message, Args &&...args)
118 {
119 logImpl(file, line, function, lvl, message, std::make_format_args(args...));
120 }
121};
122
123#define DECLARE_LOGGER(processName, configuration) lintel::LoggerService::getOrCreate(processName, configuration)
124#define LOG_INFO(message, ...) \
125 lintel::LoggerService::get().log(__FILE__, __LINE__, LINTEL_FUNCTION, lintel::LogLevel::info, message, ##__VA_ARGS__)
126#define LOG_DEBUG(message, ...) \
127 lintel::LoggerService::get().log(__FILE__, __LINE__, LINTEL_FUNCTION, lintel::LogLevel::debug, message, ##__VA_ARGS__)
128#define LOG_TRACE(message, ...) \
129 lintel::LoggerService::get().log(__FILE__, __LINE__, LINTEL_FUNCTION, lintel::LogLevel::trace, message, ##__VA_ARGS__)
130#define LOG_ERROR(message, ...) \
131 lintel::LoggerService::get().log(__FILE__, __LINE__, LINTEL_FUNCTION, lintel::LogLevel::error, message, ##__VA_ARGS__)
132#define LOG_FATAL(message, ...) \
133 lintel::LoggerService::get().log( \
134 __FILE__, __LINE__, LINTEL_FUNCTION, lintel::LogLevel::critical, message, ##__VA_ARGS__)
135#define LOG_WARN(message, ...) \
136 lintel::LoggerService::get().log(__FILE__, __LINE__, LINTEL_FUNCTION, lintel::LogLevel::warn, message, ##__VA_ARGS__)
137}// namespace lintel
138#endif// LOGGING_LOGGER_H
Definition LoggerService.h:50
LoggerService(std::shared_ptr< ProcessName > processName, std::shared_ptr< Configuration > configuration)
static LoggerService & get()
void log(const char *file, int line, const char *function, LogLevel lvl, std::string_view message, Args &&...args)
Definition LoggerService.h:117
static void initSingleton(const std::shared_ptr< ProcessName > &processInfo, const std::shared_ptr< Configuration > &configuration)
static void initSingleton2()
static LoggerService & getOrCreate(const std::shared_ptr< ProcessName > &processInfo, const std::shared_ptr< Configuration > &configuration)
Definition BaseFeature.h:6
LogLevel
Definition LoggerService.h:35
constexpr std::string_view type_name()
Definition TypeName.h:9
Definition LoggerService.h:25