LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_LOGGING_H
17#define LIEF_LOGGING_H
18
19#include "LIEF/visibility.h"
20
21#include <string>
22#include <memory>
23#include <vector>
24#include <cstdint>
25
26namespace spdlog {
27class logger;
28}
29
30namespace LIEF {
31namespace logging {
32
39enum class LEVEL : uint32_t {
40 OFF = 0,
41
48};
49
52
53LIEF_API const char* to_string(LEVEL e);
54
57
60
63
65LIEF_API void set_path(const std::string& path);
66
68LIEF_API void log(LEVEL level, const std::string& msg);
69
70LIEF_API void log(LEVEL level, const std::string& fmt,
71 const std::vector<std::string>& args);
72
73template<typename... Args>
74void log(LEVEL level, const std::string& fmt, const Args&... args) {
75 std::vector<std::string> vec_args;
76 vec_args.insert(vec_args.end(),
77 {static_cast<decltype(vec_args)::value_type>(args)...});
78 return log(level, fmt, vec_args);
79}
80
81LIEF_API void set_logger(std::shared_ptr<spdlog::logger> logger);
82
84
85inline void enable_debug() {
87}
88
89inline void debug(const std::string& msg) {
90 log(LEVEL::DEBUG, msg);
91}
92
93inline void debug(const std::string& fmt, const std::vector<std::string>& args) {
94 log(LEVEL::DEBUG, fmt, args);
95}
96
97template<typename... Args>
98void debug(const std::string& fmt, const Args&... args) {
99 std::vector<std::string> vec_args;
100 vec_args.insert(vec_args.end(),
101 {static_cast<decltype(vec_args)::value_type>(args)...});
102 return debug(fmt, vec_args);
103}
104
105// -----------------------------------------------------------------------------
106
107inline void info(const std::string& msg) {
108 log(LEVEL::INFO, msg);
109}
110
111inline void info(const std::string& fmt, const std::vector<std::string>& args) {
112 log(LEVEL::INFO, fmt, args);
113}
114
115template<typename... Args>
116void info(const std::string& fmt, const Args&... args) {
117 std::vector<std::string> vec_args;
118 vec_args.insert(vec_args.end(),
119 {static_cast<decltype(vec_args)::value_type>(args)...});
120 return info(fmt, vec_args);
121}
122
123// -----------------------------------------------------------------------------
124
125inline void warn(const std::string& msg) {
126 log(LEVEL::WARN, msg);
127}
128
129inline void warn(const std::string& fmt, const std::vector<std::string>& args) {
130 log(LEVEL::WARN, fmt, args);
131}
132
133template<typename... Args>
134void warn(const std::string& fmt, const Args&... args) {
135 std::vector<std::string> vec_args;
136 vec_args.insert(vec_args.end(),
137 {static_cast<decltype(vec_args)::value_type>(args)...});
138 return warn(fmt, vec_args);
139}
140
141// -----------------------------------------------------------------------------
142
143inline void err(const std::string& msg) {
144 log(LEVEL::ERR, msg);
145}
146
147inline void err(const std::string& fmt, const std::vector<std::string>& args) {
148 log(LEVEL::ERR, fmt, args);
149}
150
151template<typename... Args>
152void err(const std::string& fmt, const Args&... args) {
153 std::vector<std::string> vec_args;
154 vec_args.insert(vec_args.end(),
155 {static_cast<decltype(vec_args)::value_type>(args)...});
156 return err(fmt, vec_args);
157}
158
159// -----------------------------------------------------------------------------
160
161inline void critical(const std::string& msg) {
162 log(LEVEL::CRITICAL, msg);
163}
164
165inline void critical(const std::string& fmt,
166 const std::vector<std::string>& args) {
167 log(LEVEL::CRITICAL, fmt, args);
168}
169
170template<typename... Args>
171void critical(const std::string& fmt, const Args&... args) {
172 std::vector<std::string> vec_args;
173 vec_args.insert(vec_args.end(),
174 {static_cast<decltype(vec_args)::value_type>(args)...});
175 return critical(fmt, vec_args);
176}
177
178// -----------------------------------------------------------------------------
179namespace named {
181LIEF_API LEVEL get_level(const char* name);
182
184LIEF_API void disable(const char* name);
185
187LIEF_API void enable(const char* name);
188
190LIEF_API void set_level(const char* name, LEVEL level);
191
193LIEF_API void set_path(const char* name, const std::string& path);
194
196LIEF_API void log(const char* name, LEVEL level, const std::string& msg);
197
199LIEF_API void set_logger(const char* name, std::shared_ptr<spdlog::logger> logger);
200
201LIEF_API spdlog::logger& get_sink(const char* name);
202
204LIEF_API void reset(const char* name);
205
207inline void enable_debug(const char* name) {
208 set_level(name, LEVEL::DEBUG);
209}
210
211inline void debug(const char* name, const std::string& msg) {
212 log(name, LEVEL::DEBUG, msg);
213}
214
215inline void info(const char* name, const std::string& msg) {
216 log(name, LEVEL::INFO, msg);
217}
218
219inline void warn(const char* name, const std::string& msg) {
220 log(name, LEVEL::WARN, msg);
221}
222
223inline void err(const char* name, const std::string& msg) {
224 log(name, LEVEL::ERR, msg);
225}
226
227inline void critical(const char* name, const std::string& msg) {
228 log(name, LEVEL::CRITICAL, msg);
229}
230}
231
232
233class Scoped {
234 public:
235 Scoped(const Scoped&) = delete;
236 Scoped& operator=(const Scoped&) = delete;
237
238 Scoped(Scoped&&) = delete;
239 Scoped& operator=(Scoped&&) = delete;
240
241 explicit Scoped(LEVEL level) :
242 level_(get_level()) {
243 set_level(level);
244 }
245
246 explicit Scoped(LEVEL level, std::string name) :
247 level_(get_level()),
248 name_(std::move(name)) {
249 set_level(level);
250 }
251
252 const Scoped& set_level(LEVEL lvl) const {
253 if (name_.empty()) {
255 } else {
256 logging::named::set_level(name_.c_str(), lvl);
257 }
258 return *this;
259 }
260
261 void reset() {
262 set_level(level_);
263 }
264
266 reset();
267 }
268
269 private:
270 LEVEL level_ = LEVEL::INFO;
271 std::string name_;
272};
273
274
275}
276}
277
278#endif
Scoped(const Scoped &)=delete
Scoped & operator=(Scoped &&)=delete
Scoped(Scoped &&)=delete
Scoped(LEVEL level)
Definition logging.hpp:241
~Scoped()
Definition logging.hpp:265
void reset()
Definition logging.hpp:261
const Scoped & set_level(LEVEL lvl) const
Definition logging.hpp:252
Scoped & operator=(const Scoped &)=delete
Scoped(LEVEL level, std::string name)
Definition logging.hpp:246
Definition logging.hpp:179
void warn(const char *name, const std::string &msg)
Definition logging.hpp:219
void set_level(const char *name, LEVEL level)
Set the log level for the logger with the given name.
void debug(const char *name, const std::string &msg)
Definition logging.hpp:211
void set_path(const char *name, const std::string &path)
Change the logger with the given name to a file-based logging and set its path.
void err(const char *name, const std::string &msg)
Definition logging.hpp:223
void critical(const char *name, const std::string &msg)
Definition logging.hpp:227
spdlog::logger & get_sink(const char *name)
void info(const char *name, const std::string &msg)
Definition logging.hpp:215
void log(const char *name, LEVEL level, const std::string &msg)
Log a message with the logger whose name is provided in the first parameter.
void set_logger(const char *name, std::shared_ptr< spdlog::logger > logger)
Set a spdlog sink for the logger with the given name.
Definition logging.hpp:31
LEVEL get_level()
Current log level.
LEVEL
Hierarchical logging level
Definition logging.hpp:39
@ TRACE
Definition logging.hpp:42
@ WARN
Definition logging.hpp:45
@ INFO
Definition logging.hpp:44
@ OFF
Definition logging.hpp:40
@ CRITICAL
Definition logging.hpp:47
@ ERR
Definition logging.hpp:46
@ DEBUG
Definition logging.hpp:43
void warn(const std::string &msg)
Definition logging.hpp:125
void set_path(const std::string &path)
Change the logger to a file-based logging and set its path.
void critical(const std::string &msg)
Definition logging.hpp:161
void err(const std::string &msg)
Definition logging.hpp:143
void enable()
Globally enable the logging module.
void set_logger(std::shared_ptr< spdlog::logger > logger)
void disable()
Globally disable the logging module.
void info(const std::string &msg)
Definition logging.hpp:107
void log(LEVEL level, const std::string &msg)
Log a message with the LIEF's logger.
void set_level(LEVEL level)
Change the logging level (hierarchical).
const char * to_string(LEVEL e)
void debug(const std::string &msg)
Definition logging.hpp:89
void enable_debug()
Definition logging.hpp:85
LIEF namespace.
Definition Abstract/Binary.hpp:40
Definition logging.hpp:26
#define LIEF_API
Definition visibility.h:45