Brenta Engine 1.0
Loading...
Searching...
No Matches
engine_logger.hpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#pragma once
28
29#include "utils_types.hpp"
30
31#include <fstream>
32#include <iostream>
33#include <string>
34
35#define DEBUG(...) \
36 Brenta::Utils::Logger::Log(Brenta::Types::LogLevel::DEBUG, __VA_ARGS__)
37#define INFO(...) \
38 Brenta::Utils::Logger::Log(Brenta::Types::LogLevel::INFO, __VA_ARGS__)
39#define WARNING(...) \
40 Brenta::Utils::Logger::Log(Brenta::Types::LogLevel::WARNING, __VA_ARGS__)
41#define ERROR(...) \
42 Brenta::Utils::Logger::Log(Brenta::Types::LogLevel::ERROR, __VA_ARGS__)
43
44namespace Brenta
45{
46
47namespace Utils
48{
49
56class Logger
57{
58 public:
67 static Brenta::Types::LogLevel level;
74 static std::ofstream log_file;
75
82 static void Init();
92 static void SetLogLevel(Brenta::Types::LogLevel level);
101 static void SetLogFile(const std::string &file);
108 static void Close();
109
119 template <typename T, typename... Args>
120 static void LogToStdout(T message, Args... args)
121 {
122 std::cout << message;
123 LogToStdout(args...);
124 }
125
136 template <typename T, typename... Args>
137 static void LogToFile(T message, Args... args)
138 {
139 log_file << message;
140 LogToFile(args...);
141 }
152 template <typename... Args>
153 static void Log(Brenta::Types::LogLevel level, Args... args)
154 {
155 if (Logger::level > level)
156 return;
157 LogToStdout(level, ": ", args...);
158 if (Logger::log_file.is_open())
159 LogToFile(level, ": ", args...);
160 }
161
162 private:
163 /* Base cases */
164 template <typename T> static void LogToStdout(T message)
165 {
166 std::cout << message << std::endl;
167 }
168 template <typename T> static void LogToFile(T message)
169 {
170 log_file << message << std::endl;
171 }
172};
173
174} // namespace Utils
175
176} // namespace Brenta
static std::ofstream log_file
Log file.
static void SetLogFile(const std::string &file)
Set the log file.
static void Log(Brenta::Types::LogLevel level, Args... args)
Log a message.
static void Init()
Initialize the logger.
static void LogToFile(T message, Args... args)
Log a message to the log file.
static void Close()
Close the logger.
static void LogToStdout(T message, Args... args)
Log a message to stdout.
static void SetLogLevel(Brenta::Types::LogLevel level)
Set the log level.
static Brenta::Types::LogLevel level
Log level.