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 <cstdio>
21#include <cstring>
22
23namespace yasmin {
24
39typedef void (*LogFunction)(const char *file, const char *function, int line,
40 const char *text, ...);
41
42// Declare function pointers for logging at different severity levels
44extern LogFunction log_warn;
45extern LogFunction log_info;
47
56inline const char *extract_filename(const char *path) {
57 const char *filename = std::strrchr(path, '/');
58 if (!filename) {
59 filename = std::strrchr(path, '\\'); // handle Windows-style paths
60 }
61 return filename ? filename + 1 : path;
62}
63
64// Macros for logging with automatic file and function information
65#define YASMIN_LOG_ERROR(text, ...) \
66 yasmin::log_error(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \
67 ##__VA_ARGS__)
68#define YASMIN_LOG_WARN(text, ...) \
69 yasmin::log_warn(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \
70 ##__VA_ARGS__)
71#define YASMIN_LOG_INFO(text, ...) \
72 yasmin::log_info(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \
73 ##__VA_ARGS__)
74#define YASMIN_LOG_DEBUG(text, ...) \
75 yasmin::log_debug(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \
76 ##__VA_ARGS__)
77
90void set_loggers(LogFunction error, LogFunction warn, LogFunction info,
91 LogFunction debug);
92
100
101} // namespace yasmin
102
103#endif // YASMIN__LOGS_HPP
Definition blackboard.hpp:29
LogFunction log_warn
Pointer to the warning logging function.
Definition logs.cpp:114
LogFunction log_info
Pointer to the info logging function.
Definition logs.cpp:115
void(* LogFunction)(const char *file, const char *function, int line, const char *text,...)
Type definition for a logging function.
Definition logs.hpp:39
LogFunction log_debug
Pointer to the debug logging function.
Definition logs.cpp:116
const char * extract_filename(const char *path)
Extracts the filename from a given file path.
Definition logs.hpp:56
void set_default_loggers()
Sets the default logging functions for all log levels.
Definition logs.cpp:126
LogFunction log_error
Pointer to the error logging function.
Definition logs.cpp:113
void set_loggers(LogFunction error, LogFunction warn, LogFunction info, LogFunction debug)
Sets custom logging functions for different log levels.
Definition logs.cpp:118