Kale
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
19#include <fstream>
20#include <string>
21#include <string.h>
22#include <mutex>
23
24#ifdef KALE_DEBUG
25
26#ifdef KALE_WINDOWS
27#define TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES
28#endif
29
30#include <termcolor/termcolor.hpp>
31#include <iostream>
32
33#endif
34
35namespace Kale {
36
40 class Logger {
41 private:
42
43 // Colors for console in Debug Mode
44 #ifdef KALE_DEBUG
45
49 constexpr static auto rc = termcolor::reset;
50
54 constexpr static auto tc = termcolor::color<87>;
55
59 constexpr static auto lc = termcolor::color<249>;
60
64 constexpr static auto ic = termcolor::color<116>;
65
69 constexpr static auto wc = termcolor::color<220>;
70
74 constexpr static auto ec = termcolor::color<198>;
75
79 constexpr static auto fc = termcolor::color<85>;
80
84 constexpr static auto lic = termcolor::color<86>;
85
89 constexpr static auto dc = termcolor::color<241>;
90
91 #endif
92
96 std::ofstream logFile;
97
101 std::mutex mutex;
102
107 std::string getTimePrefix();
108
109 protected:
110
115 void load(const std::string& applicationName);
116
117 friend class Application;
118
119 public:
120
124 Logger();
125
129 Logger(const Logger& other) = delete;
130
134 Logger(Logger&& other) = delete;
135
139 void operator=(const Logger& other) = delete;
140
144 void operator=(Logger&& other) = delete;
145
150 template <typename T> void log(T msg) {
151 std::string time = getTimePrefix();
152
153 std::lock_guard<std::mutex> guard(mutex);
154 #ifdef KALE_DEBUG
155 std::cout << dc << "[" << tc << time << dc << "] " << // [HH:MM AM]
156 lc << msg << "\n" << rc; // Msg
157 #endif
158
159 logFile << "[" << time << "] " << msg << std::endl;
160 }
161
166 template <typename T> void info(T msg) {
167 std::string time = getTimePrefix();
168
169 std::lock_guard<std::mutex> guard(mutex);
170 #ifdef KALE_DEBUG
171 std::cout << dc << "[" << tc << time << dc << "] [" << // [HH:MM AM]
172 ic << "INFO" << dc << "] " << ic << msg << "\n" << rc; // [Info] Msg
173 #endif
174
175 logFile << "[" << time << "] " << "[INFO] " << msg << std::endl;
176 }
177
182 template <typename T> void warn(T msg) {
183 std::string time = getTimePrefix();
184
185 std::lock_guard<std::mutex> guard(mutex);
186 #ifdef KALE_DEBUG
187 std::cout << dc << "[" << tc << time << dc << "] [" << // [HH:MM AM]
188 wc << "WARNING" << dc << "] " << wc << msg << "\n" << rc; // [Warning] Msg
189 #endif
190
191 logFile << "[" << time << "] " << "[WARNING] " << msg << std::endl;
192 }
193
198 template <typename T> void error(T msg) {
199 std::string time = getTimePrefix();
200
201 std::lock_guard<std::mutex> guard(mutex);
202 #ifdef KALE_DEBUG
203 std::cout << dc << "[" << tc << time << dc << "] [" << // [HH:MM AM]
204 ec << "ERROR" << dc << "] " << ec << msg << "\n" << rc; // [Error] Msg
205 #endif
206
207 logFile << "[" << time << "] " << "[ERROR] " << msg << std::endl;
208 }
209
216 template <typename T> void log(unsigned int line, const char* file, T msg) {
217 std::string time = getTimePrefix();
218
219 std::lock_guard<std::mutex> guard(mutex);
220 #ifdef KALE_DEBUG
221 std::cout << dc << "[" << tc << time << dc << "] [" << // [HH:MM AM]
222 fc << file << dc << ":" << lic << line << dc << "] " << // [File:Line]
223 lc << msg << "\n" << rc; // Msg
224 #endif
225
226 logFile << "[" << time << "] " << "[" << file << ":" << line << "] " << msg << std::endl;
227 }
228
235 template <typename T> void info(unsigned int line, const char* file, T msg) {
236 std::string time = getTimePrefix();
237
238 std::lock_guard<std::mutex> guard(mutex);
239 #ifdef KALE_DEBUG
240 std::cout << dc << "[" << tc << time << dc << "] [" << // [HH:MM AM]
241 fc << file << dc << ":" << lic << line << dc << "] [" << // [File:Line]
242 ic << "INFO" << dc << "] " << // [Info]
243 ic << msg << "\n" << rc; // Msg
244 #endif
245
246 logFile << "[" << time << "] " << "[" << file << ":" << line << "] " << "[INFO] " << msg << std::endl;
247 }
248
255 template <typename T> void warn(unsigned int line, const char* file, T msg) {
256 std::string time = getTimePrefix();
257
258 std::lock_guard<std::mutex> guard(mutex);
259 #ifdef KALE_DEBUG
260 std::cout << dc << "[" << tc << time << dc << "] [" << // [HH:MM AM]
261 fc << file << dc << ":" << lic << line << dc << "] [" << // [File:Line]
262 wc << "WARNING" << dc << "] " << // [Warning]
263 wc << msg << "\n" << rc; // Msg
264 #endif
265
266 logFile << "[" << time << "] " << "[" << file << ":" << line << "] " << "[WARNING] " << msg << std::endl;
267 }
268
275 template <typename T> void error(unsigned int line, const char* file, T msg) {
276 std::string time = getTimePrefix();
277
278 std::lock_guard<std::mutex> guard(mutex);
279 #ifdef KALE_DEBUG
280 std::cout << dc << "[" << tc << time << dc << "] [" << // [HH:MM AM]
281 fc << file << dc << ":" << lic << line << dc << "] [" << // [File:Line]
282 ec << "ERROR" << dc << "] " << // [Error]
283 ec << msg << "\n" << rc; // Msg
284 #endif
285
286 logFile << "[" << time << "] " << "[" << file << ":" << line << "] " << "[ERROR] " << msg << std::endl;
287 }
288
295 template <typename T> void assertCheck(unsigned int line, const char* file, T assertion) {
296 if (assertion) return;
297 error(line, file, "Assertion Failed");
298 }
299
307 template <typename T, typename STR> void assertCheck(unsigned int line, const char* file, T assertion, STR msg) {
308 using namespace std::string_literals;
309 if (assertion) return;
310 error(line, file, "Assertion Failed - "s + msg);
311 }
312 };
313
318}
319
323#ifdef KALE_WINDOWS
324#define __FILENAME__ strrchr("\\" __FILE__, '\\') + 1
325#else
326#define __FILENAME__ strrchr("/" __FILE__, '/') + 1
327#endif
328
332#define klPrint(x) Kale::console.log(__LINE__, __FILENAME__, x)
333
337#define klInfo(x) Kale::console.info(__LINE__, __FILENAME__, x)
338
342#define klWarn(x) Kale::console.warn(__LINE__, __FILENAME__, x)
343
347#define klError(x) Kale::console.error(__LINE__, __FILENAME__, x)
348
352#define klAssert(x) Kale::console.assertCheck(__LINE__, __FILENAME__, x)
353
357#define klAssertMsg(x, y) Kale::console.assertCheck(__LINE__, __FILENAME__, x, y)
void log(unsigned int line, const char *file, T msg)
Definition Logger.hpp:216
std::ofstream logFile
Definition Logger.hpp:96
void warn(T msg)
Definition Logger.hpp:182
std::string getTimePrefix()
Definition Logger.cpp:90
Logger(const Logger &other)=delete
void error(unsigned int line, const char *file, T msg)
Definition Logger.hpp:275
void log(T msg)
Definition Logger.hpp:150
void info(unsigned int line, const char *file, T msg)
Definition Logger.hpp:235
void load(const std::string &applicationName)
Definition Logger.cpp:52
void error(T msg)
Definition Logger.hpp:198
Logger(Logger &&other)=delete
void info(T msg)
Definition Logger.hpp:166
void assertCheck(unsigned int line, const char *file, T assertion)
Definition Logger.hpp:295
void warn(unsigned int line, const char *file, T msg)
Definition Logger.hpp:255
void operator=(Logger &&other)=delete
void assertCheck(unsigned int line, const char *file, T assertion, STR msg)
Definition Logger.hpp:307
void operator=(const Logger &other)=delete
std::mutex mutex
Definition Logger.hpp:101
Logger console
Definition Logger.hpp:317