C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
logs.hpp
Go to the documentation of this file.
1// Copyright (C) 2024 Miguel Ángel González Santamarta
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#ifndef YASMIN__LOGS_HPP
17#define YASMIN__LOGS_HPP
18
19#include <cstdarg>
20#include <cstring>
21#include <string>
22
23namespace yasmin {
24
46
54extern LogLevel log_level;
55
66void default_log_message(LogLevel level, const char *file, const char *function,
67 int line, const char *text);
68
77void set_log_level(LogLevel new_log_level);
78
86const char *log_level_to_name(LogLevel level);
87
103typedef void (*LogFunction)(LogLevel level, const char *file,
104 const char *function, int line, const char *text);
105
107
123template <yasmin::LogLevel LEVEL>
124void log_helper(const char *file, const char *function, int line,
125 const char *text, ...) {
126 va_list args;
127 va_start(args, text);
128
129 // Calculate the required buffer size
130 int size = vsnprintf(nullptr, 0, text, args) + 1;
131 va_end(args);
132
133 std::string buffer(size, '\0');
134 va_start(args, text);
135 vsnprintf(&buffer[0], buffer.size(), text, args);
136
137 va_end(args);
138
139 yasmin::log_message(LEVEL, file, function, line, buffer.c_str());
140}
141
150inline const char *extract_filename(const char *path) {
151 const char *filename = std::strrchr(path, '/');
152 if (!filename) {
153 filename = std::strrchr(path, '\\'); // handle Windows-style paths
154 }
155 return filename ? filename + 1 : path;
156}
157
158// Macros for logging with automatic file and function information
159#define YASMIN_LOG_ERROR(text, ...) \
160 if (yasmin::log_level >= yasmin::ERROR) \
161 yasmin::log_helper<yasmin::ERROR>(::yasmin::extract_filename(__FILE__), \
162 __FUNCTION__, __LINE__, text, \
163 ##__VA_ARGS__)
164#define YASMIN_LOG_WARN(text, ...) \
165 if (yasmin::log_level >= yasmin::WARN) \
166 yasmin::log_helper<yasmin::WARN>(::yasmin::extract_filename(__FILE__), \
167 __FUNCTION__, __LINE__, text, \
168 ##__VA_ARGS__)
169#define YASMIN_LOG_INFO(text, ...) \
170 if (yasmin::log_level >= yasmin::INFO) \
171 yasmin::log_helper<yasmin::INFO>(::yasmin::extract_filename(__FILE__), \
172 __FUNCTION__, __LINE__, text, \
173 ##__VA_ARGS__)
174#define YASMIN_LOG_DEBUG(text, ...) \
175 if (yasmin::log_level >= yasmin::DEBUG) \
176 yasmin::log_helper<yasmin::DEBUG>(::yasmin::extract_filename(__FILE__), \
177 __FUNCTION__, __LINE__, text, \
178 ##__VA_ARGS__)
179
193
201
202} // namespace yasmin
203
204#endif // YASMIN__LOGS_HPP
Definition blackboard.hpp:29
void default_log_message(LogLevel level, const char *file, const char *function, int line, const char *text)
Default logging function.
Definition logs.cpp:22
void set_loggers(LogFunction log_message)
Sets custom logging functions for different log levels.
Definition logs.cpp:58
void(* LogFunction)(LogLevel level, const char *file, const char *function, int line, const char *text)
Type definition for a logging function.
Definition logs.hpp:103
void log_helper(const char *file, const char *function, int line, const char *text,...)
Variadic template function to log messages at different levels.
Definition logs.hpp:124
LogLevel log_level
The current log level for the application.
Definition logs.cpp:29
void set_log_level(LogLevel new_log_level)
Sets the log level for the logs.
Definition logs.cpp:33
LogFunction log_message
Pointer to the logging function.
Definition logs.cpp:31
const char * log_level_to_name(LogLevel level)
Parse LogLevel to string.
Definition logs.cpp:35
const char * extract_filename(const char *path)
Extracts the filename from a given file path.
Definition logs.hpp:150
void set_default_loggers()
Sets the default logging function for all log levels.
Definition logs.cpp:60
LogLevel
Enum representing different log levels for controlling log verbosity.
Definition logs.hpp:33
@ INFO
Definition logs.hpp:41
@ WARN
Definition logs.hpp:38
@ DEBUG
Definition logs.hpp:44
@ ERROR
Log level for error messages. Only critical errors should be logged.
Definition logs.hpp:35