AYON Cpp Api  0.1.0
Loading...
Searching...
No Matches
AyonLogger.h
Go to the documentation of this file.
1#ifndef AYONLOGGER_H
2#define AYONLOGGER_H
3#include <cstdint>
4#include <cstdlib>
5#include <filesystem>
6#include <memory>
7#include <optional>
8#include <set>
9#include <sstream>
10#include <string>
11#include "spdlog/common.h"
12#include "spdlog/sinks/basic_file_sink.h"
13#include "spdlog/sinks/stdout_color_sinks.h"
14#include "spdlog/async.h"
15#include <spdlog/spdlog.h>
24 public:
25 static AyonLogger &
26 getInstance(const std::string &filepath) {
27 static AyonLogger instance(filepath);
28 return instance;
29 }
30
31 std::set<std::string>::iterator
32 key(const std::string &key) {
33 return EnabledLoggingKeys.find(key);
34 }
35
36 bool
37 regesterLoggingKey(const std::string &KeyName) {
38 std::pair<std::set<std::string>::iterator, bool> insertion = EnabledLoggingKeys.insert(KeyName);
39 if (insertion.second) {
40 return true;
41 }
42 return false;
43 }
44
45 bool
46 unregisterLoggingKey(const std::string &KeyName) {
47 std::set<std::string>::iterator it = EnabledLoggingKeys.find(KeyName);
48 if (it != EnabledLoggingKeys.end()) {
49 EnabledLoggingKeys.erase(it);
50 return true;
51 }
52 return false;
53 }
54
55 bool
56 isKeyActive(const std::set<std::string>::iterator &logginIterator) {
57 if (logginIterator != EnabledLoggingKeys.end()) {
58 return true;
59 }
60 return false;
61 };
62
63 template<typename... Args>
64 void
65 error(const std::set<std::string>::iterator &logginIterator, const std::string &format, const Args &... args) {
66 if (logginIterator != EnabledLoggingKeys.end()) {
67 log("error", format, args...);
68 }
69 }
70
71 template<typename... Args>
72 void
73 error(const std::string &format, const Args &... args) {
74 log("error", format, args...);
75 }
76
77 template<typename... Args>
78 void
79 info(const std::set<std::string>::iterator &logginIterator, const std::string &format, const Args &... args) {
80 if (logginIterator != EnabledLoggingKeys.end()) {
81 log("info", format, args...);
82 }
83 }
84
85 template<typename... Args>
86 void
87 info(const std::string &format, const Args &... args) {
88 log("info", format, args...);
89 }
90
91 template<typename... Args>
92 void
93 warn(const std::set<std::string>::iterator &logginIterator, const std::string &format, const Args &... args) {
94 if (logginIterator != EnabledLoggingKeys.end()) {
95 log("warn", format, args...);
96 }
97 }
98
99 template<typename... Args>
100 void
101 warn(const std::string &format, const Args &... args) {
102 log("warn", format, args...);
103 }
104
105 template<typename... Args>
106 void
107 critical(const std::set<std::string>::iterator &logginIterator,
108 const std::string &format,
109 const Args &... args) {
110 if (logginIterator != EnabledLoggingKeys.end()) {
111 log("critical", format, args...);
112 }
113 }
114
115 template<typename... Args>
116 void
117 critical(const std::string &format, const Args &... args) {
118 log("critical", format, args...);
119 }
120 void
121 LogLevlInfo(const bool &alsoSetFileLogger = false) {
122 if (alsoSetFileLogger) {
123 file_logger_->set_level(spdlog::level::info);
124 }
125 console_logger_->set_level(spdlog::level::info);
126 }
127 void
128 LogLevlError(const bool &alsoSetFileLogger = false) {
129 if (alsoSetFileLogger) {
130 file_logger_->set_level(spdlog::level::err);
131 }
132 console_logger_->set_level(spdlog::level::err);
133 }
134 void
135 LogLevlWarn(const bool &alsoSetFileLogger = false) {
136 if (alsoSetFileLogger) {
137 file_logger_->set_level(spdlog::level::warn);
138 }
139 console_logger_->set_level(spdlog::level::warn);
140 }
141 void
142 LogLevlCritical(const bool &alsoSetFileLogger = false) {
143 if (alsoSetFileLogger) {
144 file_logger_->set_level(spdlog::level::critical);
145 }
146 console_logger_->set_level(spdlog::level::critical);
147 }
148 void
149 LogLevlOff(const bool &alsoSetFileLogger = false) {
150 if (alsoSetFileLogger) {
151 file_logger_->set_level(spdlog::level::off);
152 }
153 console_logger_->set_level(spdlog::level::off);
154 }
155
156 private:
157 AyonLogger(const std::string &filepath) {
158 // Initialize console logger
159 console_logger_ = spdlog::stdout_color_mt("console");
160 console_logger_->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
161
162 char* envVarFileLoggingPath = std::getenv("AYONLOGGERFILEPOS");
163
164 if (envVarFileLoggingPath != nullptr) {
165 fileLoggerFilePath = std::string(
166 std::filesystem::absolute(std::string(envVarFileLoggingPath) + "/logFile.json").string());
168 }
169
170 char* envVarFileLogging = std::getenv("AYONLOGGERFILELOGGING");
171
172 if (envVarFileLogging != nullptr) {
173 switch (envVarFileLogging[1]) {
174 case 'F':
175 enableFileLogging = false;
176 break;
177 default:
178 enableFileLogging = true;
179 file_logger_ = spdlog::basic_logger_mt<spdlog::async_factory>(
180 "fileLogger", fileLoggerFilePathOverwrite ? fileLoggerFilePath.c_str() : filepath.c_str());
181 file_logger_->set_pattern(
182 "{\"timestamp\":\"%Y-%m-%d %H:%M:%S.%e\",\"level\":\"%l\",\"Thread "
183 "Id\":\"%t\",\"Process Id\":\"%P\",\"message\":\"%v\"}");
184 break;
185 }
186 }
187
188 char* envVarLogLvl = std::getenv("AYONLOGGERLOGLVL");
189
190 if (envVarLogLvl != nullptr) {
191 switch (envVarLogLvl[0]) {
192 case 'I':
193 if (enableFileLogging) {
194 file_logger_->set_level(spdlog::level::info);
195 }
196 console_logger_->set_level(spdlog::level::info);
197 break;
198 case 'E':
199 if (enableFileLogging) {
200 file_logger_->set_level(spdlog::level::err);
201 }
202 console_logger_->set_level(spdlog::level::err);
203 break;
204 case 'W':
205 if (enableFileLogging) {
206 file_logger_->set_level(spdlog::level::warn);
207 }
208 console_logger_->set_level(spdlog::level::warn);
209 break;
210 case 'C':
211 if (enableFileLogging) {
212 file_logger_->set_level(spdlog::level::critical);
213 }
214 console_logger_->set_level(spdlog::level::critical);
215 break;
216 case 'O':
217 if (enableFileLogging) {
218 file_logger_->set_level(spdlog::level::off);
219 }
220 console_logger_->set_level(spdlog::level::off);
221 break;
222 default:
223 break;
224 }
225 }
226
227 const char* envVarLoggingKeys = std::getenv("AYON_LOGGIN_LOGGIN_KEYS");
228 if (envVarLoggingKeys != nullptr) {
229 std::string envVarString(envVarLoggingKeys);
230 std::string token;
231 std::istringstream tokenStream(envVarString);
232 while (std::getline(tokenStream, token, '/')) {
233 EnabledLoggingKeys.insert(token);
234 }
235 }
236 }
237
238 template<typename... Args>
239 void
240 log(const std::string &level, const std::string &massage, const Args &... args) {
241 // std::string formattedMassage = fmt::vformat(massage, args...);
242 std::string formatted_message = fmt::vformat(massage, fmt::make_format_args(args...));
243
244 if (enableFileLogging) {
245 file_logger_->log(spdlog::level::from_str(level), formatted_message);
246 }
247 console_logger_->log(spdlog::level::from_str(level), formatted_message);
248 }
249 std::shared_ptr<spdlog::logger> console_logger_;
250 std::shared_ptr<spdlog::logger> file_logger_;
254 std::set<std::string> EnabledLoggingKeys;
255};
256#endif
Simple Logger Class that wraps around spdlog in order to expose easy logging functions AyonLogger::...
Definition: AyonLogger.h:23
std::shared_ptr< spdlog::logger > console_logger_
Definition: AyonLogger.h:249
void error(const std::string &format, const Args &... args)
Definition: AyonLogger.h:73
void LogLevlOff(const bool &alsoSetFileLogger=false)
Definition: AyonLogger.h:149
bool fileLoggerFilePathOverwrite
Definition: AyonLogger.h:252
void LogLevlInfo(const bool &alsoSetFileLogger=false)
Definition: AyonLogger.h:121
bool enableFileLogging
Definition: AyonLogger.h:251
std::set< std::string >::iterator key(const std::string &key)
Definition: AyonLogger.h:32
bool isKeyActive(const std::set< std::string >::iterator &logginIterator)
Definition: AyonLogger.h:56
void LogLevlCritical(const bool &alsoSetFileLogger=false)
Definition: AyonLogger.h:142
std::set< std::string > EnabledLoggingKeys
Definition: AyonLogger.h:254
void LogLevlWarn(const bool &alsoSetFileLogger=false)
Definition: AyonLogger.h:135
void error(const std::set< std::string >::iterator &logginIterator, const std::string &format, const Args &... args)
Definition: AyonLogger.h:65
void warn(const std::set< std::string >::iterator &logginIterator, const std::string &format, const Args &... args)
Definition: AyonLogger.h:93
AyonLogger(const std::string &filepath)
Definition: AyonLogger.h:157
std::shared_ptr< spdlog::logger > file_logger_
Definition: AyonLogger.h:250
void info(const std::string &format, const Args &... args)
Definition: AyonLogger.h:87
void critical(const std::string &format, const Args &... args)
Definition: AyonLogger.h:117
bool unregisterLoggingKey(const std::string &KeyName)
Definition: AyonLogger.h:46
void critical(const std::set< std::string >::iterator &logginIterator, const std::string &format, const Args &... args)
Definition: AyonLogger.h:107
bool regesterLoggingKey(const std::string &KeyName)
Definition: AyonLogger.h:37
std::string fileLoggerFilePath
Definition: AyonLogger.h:253
void LogLevlError(const bool &alsoSetFileLogger=false)
Definition: AyonLogger.h:128
void info(const std::set< std::string >::iterator &logginIterator, const std::string &format, const Args &... args)
Definition: AyonLogger.h:79
void warn(const std::string &format, const Args &... args)
Definition: AyonLogger.h:101
void log(const std::string &level, const std::string &massage, const Args &... args)
Definition: AyonLogger.h:240
static AyonLogger & getInstance(const std::string &filepath)
Definition: AyonLogger.h:26