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
64void set_log_level(LogLevel level);
65
73const char *log_level_to_name(LogLevel level);
74
90typedef void (*LogFunction)(LogLevel level, const char *file,
91 const char *function, int line, const char *text);
92
94
111template <yasmin::LogLevel LEVEL>
112void log_helper(const char *file, const char *function, int line,
113 const char *text, ...) {
114 va_list args;
115 va_start(args, text);
116
117 // Calculate the required buffer size
118 int size = vsnprintf(nullptr, 0, text, args) + 1;
119 va_end(args);
120
121 std::string buffer(size, '\0');
122 va_start(args, text);
123 vsnprintf(&buffer[0], buffer.size(), text, args);
124
125 va_end(args);
126
127 yasmin::log_message(LEVEL, file, function, line, buffer.c_str());
128}
129
138inline const char *extract_filename(const char *path) {
139 const char *filename = std::strrchr(path, '/');
140 if (!filename) {
141 filename = std::strrchr(path, '\\'); // handle Windows-style paths
142 }
143 return filename ? filename + 1 : path;
144}
145
146// Macros for logging with automatic file and function information
147#define YASMIN_LOG_ERROR(text, ...) \
148 if (yasmin::log_level >= yasmin::ERROR) \
149 yasmin::log_helper<yasmin::ERROR>(extract_filename(__FILE__), __FUNCTION__, \
150 __LINE__, text, ##__VA_ARGS__)
151#define YASMIN_LOG_WARN(text, ...) \
152 if (yasmin::log_level >= yasmin::WARN) \
153 yasmin::log_helper<yasmin::WARN>(extract_filename(__FILE__), __FUNCTION__, \
154 __LINE__, text, ##__VA_ARGS__)
155#define YASMIN_LOG_INFO(text, ...) \
156 if (yasmin::log_level >= yasmin::INFO) \
157 yasmin::log_helper<yasmin::INFO>(extract_filename(__FILE__), __FUNCTION__, \
158 __LINE__, text, ##__VA_ARGS__)
159#define YASMIN_LOG_DEBUG(text, ...) \
160 if (yasmin::log_level >= yasmin::DEBUG) \
161 yasmin::log_helper<yasmin::DEBUG>(extract_filename(__FILE__), __FUNCTION__, \
162 __LINE__, text, ##__VA_ARGS__)
163
177
185
186} // namespace yasmin
187
188#endif // YASMIN__LOGS_HPP
Definition blackboard.hpp:29
void set_log_level(LogLevel level)
Sets the log level for the logs.
Definition logs.cpp:45
void set_loggers(LogFunction log_message)
Sets custom logging functions for different log levels.
Definition logs.cpp:70
void(* LogFunction)(LogLevel level, const char *file, const char *function, int line, const char *text)
Type definition for a logging function.
Definition logs.hpp:90
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:112
LogLevel log_level
The current log level for the application.
Definition logs.cpp:41
LogFunction log_message
Pointer to the logging function.
Definition logs.cpp:43
const char * log_level_to_name(LogLevel level)
Parse LogLevel to string.
Definition logs.cpp:47
const char * extract_filename(const char *path)
Extracts the filename from a given file path.
Definition logs.hpp:138
void set_default_loggers()
Sets the default logging function for all log levels.
Definition logs.cpp:72
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