llama_ros: llama.cpp for ROS 2
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 LLAMA_UTILS__LOGS_HPP
17#define LLAMA_UTILS__LOGS_HPP
18
19#include <cstdarg>
20#include <cstdio>
21#include <cstring>
22
23namespace llama_utils {
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
69
77extern LogLevel log_level;
78
87inline const char *extract_filename(const char *path) {
88 const char *filename = std::strrchr(path, '/');
89 if (!filename) {
90 filename = std::strrchr(path, '\\'); // handle Windows-style paths
91 }
92 return filename ? filename + 1 : path;
93}
94
95#define LLAMA_LOG_ERROR(text, ...) \
96 if (llama_utils::log_level >= llama_utils::ERROR) \
97 llama_utils::log_error(llama_utils::extract_filename(__FILE__), \
98 __FUNCTION__, __LINE__, text, ##__VA_ARGS__)
99
100#define LLAMA_LOG_WARN(text, ...) \
101 if (llama_utils::log_level >= llama_utils::WARN) \
102 llama_utils::log_warn(llama_utils::extract_filename(__FILE__), __FUNCTION__, \
103 __LINE__, text, ##__VA_ARGS__)
104
105#define LLAMA_LOG_INFO(text, ...) \
106 if (llama_utils::log_level >= llama_utils::INFO) \
107 llama_utils::log_info(llama_utils::extract_filename(__FILE__), __FUNCTION__, \
108 __LINE__, text, ##__VA_ARGS__)
109
110#define LLAMA_LOG_DEBUG(text, ...) \
111 if (llama_utils::log_level >= llama_utils::DEBUG) \
112 llama_utils::log_debug(llama_utils::extract_filename(__FILE__), \
113 __FUNCTION__, __LINE__, text, ##__VA_ARGS__)
114
124
125} // namespace llama_utils
126
127#endif // llama_utils__LOGS_HPP
Definition llama_params.hpp:37
LogLevel
Enum representing different log levels for controlling log verbosity.
Definition logs.hpp:56
@ ERROR
Log level for error messages. Only critical errors should be logged.
Definition logs.hpp:58
@ WARN
Definition logs.hpp:61
@ INFO
Definition logs.hpp:64
@ DEBUG
Definition logs.hpp:67
LogLevel log_level
The current log level for the application.
Definition logs.cpp:119
void(* LogFunction)(const char *file, const char *function, int line, const char *text,...)
Type definition for a logging function.
Definition logs.hpp:39
void set_log_level(LogLevel log_level)
Sets the log level for the logs.
Definition logs.cpp:121
LogFunction log_warn
Pointer to the warning logging function.
Definition logs.cpp:114
const char * extract_filename(const char *path)
Extracts the filename from a given file path.
Definition logs.hpp:87
LogFunction log_info
Pointer to the info logging function.
Definition logs.cpp:115
LogFunction log_error
Pointer to the error logging function.
Definition logs.cpp:113
LogFunction log_debug
Pointer to the debug logging function.
Definition logs.cpp:116