Program Listing for File logs.hpp
↰ Return to documentation for file (include/yasmin/logs.hpp
)
// Copyright (C) 2024 Miguel Ángel González Santamarta
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef YASMIN__LOGS_HPP
#define YASMIN__LOGS_HPP
#include <cstdarg>
#include <cstring>
#include <string>
namespace yasmin {
enum LogLevel {
ERROR = 0,
WARN,
INFO,
DEBUG
};
extern LogLevel log_level;
void set_log_level(LogLevel level);
const char *log_level_to_name(LogLevel level);
typedef void (*LogFunction)(LogLevel level, const char *file,
const char *function, int line, const char *text);
extern LogFunction log_message;
template <yasmin::LogLevel LEVEL>
void log_helper(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);
// Calculate the required buffer size
int size = vsnprintf(nullptr, 0, text, args) + 1;
va_end(args);
std::string buffer(size, '\0');
va_start(args, text);
vsnprintf(&buffer[0], buffer.size(), text, args);
va_end(args);
yasmin::log_message(LEVEL, file, function, line, buffer.c_str());
}
inline const char *extract_filename(const char *path) {
const char *filename = std::strrchr(path, '/');
if (!filename) {
filename = std::strrchr(path, '\\'); // handle Windows-style paths
}
return filename ? filename + 1 : path;
}
// Macros for logging with automatic file and function information
#define YASMIN_LOG_ERROR(text, ...) \
if (yasmin::log_level >= yasmin::ERROR) \
yasmin::log_helper<yasmin::ERROR>(extract_filename(__FILE__), __FUNCTION__, \
__LINE__, text, ##__VA_ARGS__)
#define YASMIN_LOG_WARN(text, ...) \
if (yasmin::log_level >= yasmin::WARN) \
yasmin::log_helper<yasmin::WARN>(extract_filename(__FILE__), __FUNCTION__, \
__LINE__, text, ##__VA_ARGS__)
#define YASMIN_LOG_INFO(text, ...) \
if (yasmin::log_level >= yasmin::INFO) \
yasmin::log_helper<yasmin::INFO>(extract_filename(__FILE__), __FUNCTION__, \
__LINE__, text, ##__VA_ARGS__)
#define YASMIN_LOG_DEBUG(text, ...) \
if (yasmin::log_level >= yasmin::DEBUG) \
yasmin::log_helper<yasmin::DEBUG>(extract_filename(__FILE__), __FUNCTION__, \
__LINE__, text, ##__VA_ARGS__)
void set_loggers(LogFunction log_message);
void set_default_loggers();
} // namespace yasmin
#endif // YASMIN__LOGS_HPP