LIEF: Library to Instrument Executable Formats Version 2.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#include <string_view>
21
22#include <cstdint>
23#include <memory>
24#include <string>
25#include <vector>
26
27namespace spdlog {
28class logger;
29}
30
31
32namespace LIEF::logging {
33
40enum class Level : uint32_t {
41 Off = 0,
42
49};
50
53
54LIEF_API const char* to_string(Level e);
55
58
61
64
66LIEF_API void set_path(const std::string& path);
67
69LIEF_API void log(Level level, std::string_view msg);
70
71LIEF_API void log(Level level, std::string_view fmt,
72 const std::vector<std::string>& args);
73
74template<typename... Args>
75void log(Level level, std::string_view fmt, const Args&... args) {
76 std::vector<std::string> vec_args;
77 vec_args.insert(vec_args.end(),
78 {static_cast<decltype(vec_args)::value_type>(args)...});
79 return log(level, fmt, vec_args);
80}
81
82LIEF_API void set_logger(std::shared_ptr<spdlog::logger> logger);
83
85
86inline void enable_debug() {
88}
89
90inline void debug(std::string_view msg) {
91 log(Level::Debug, msg);
92}
93
94inline void debug(std::string_view fmt, const std::vector<std::string>& args) {
95 log(Level::Debug, fmt, args);
96}
97
98template<typename... Args>
99void debug(std::string_view fmt, const Args&... args) {
100 std::vector<std::string> vec_args;
101 vec_args.insert(vec_args.end(),
102 {static_cast<decltype(vec_args)::value_type>(args)...});
103 return debug(fmt, vec_args);
104}
105
106// -----------------------------------------------------------------------------
107
108inline void info(std::string_view msg) {
109 log(Level::Info, msg);
110}
111
112inline void info(std::string_view fmt, const std::vector<std::string>& args) {
113 log(Level::Info, fmt, args);
114}
115
116template<typename... Args>
117void info(std::string_view fmt, const Args&... args) {
118 std::vector<std::string> vec_args;
119 vec_args.insert(vec_args.end(),
120 {static_cast<decltype(vec_args)::value_type>(args)...});
121 return info(fmt, vec_args);
122}
123
124// -----------------------------------------------------------------------------
125
126inline void warn(std::string_view msg) {
127 log(Level::Warn, msg);
128}
129
130inline void warn(std::string_view fmt, const std::vector<std::string>& args) {
131 log(Level::Warn, fmt, args);
132}
133
134template<typename... Args>
135void warn(std::string_view fmt, const Args&... args) {
136 std::vector<std::string> vec_args;
137 vec_args.insert(vec_args.end(),
138 {static_cast<decltype(vec_args)::value_type>(args)...});
139 return warn(fmt, vec_args);
140}
141
142// -----------------------------------------------------------------------------
143
144inline void err(std::string_view msg) {
145 log(Level::Err, msg);
146}
147
148inline void err(std::string_view fmt, const std::vector<std::string>& args) {
149 log(Level::Err, fmt, args);
150}
151
152template<typename... Args>
153void err(std::string_view fmt, const Args&... args) {
154 std::vector<std::string> vec_args;
155 vec_args.insert(vec_args.end(),
156 {static_cast<decltype(vec_args)::value_type>(args)...});
157 return err(fmt, vec_args);
158}
159
160// -----------------------------------------------------------------------------
161
162inline void critical(std::string_view msg) {
163 log(Level::Critical, msg);
164}
165
166inline void critical(std::string_view fmt, const std::vector<std::string>& args) {
167 log(Level::Critical, fmt, args);
168}
169
170template<typename... Args>
171void critical(std::string_view 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, std::string_view 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, std::string_view msg) {
212 log(name, Level::Debug, msg);
213}
214
215inline void info(const char* name, std::string_view msg) {
216 log(name, Level::Info, msg);
217}
218
219inline void warn(const char* name, std::string_view msg) {
220 log(name, Level::Warn, msg);
221}
222
223inline void err(const char* name, std::string_view msg) {
224 log(name, Level::Err, msg);
225}
226
227inline void critical(const char* name, std::string_view 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
Scoped(Level level, std::string name)
Definition logging.hpp:246
const Scoped & set_level(Level lvl) const
Definition logging.hpp:252
Scoped & operator=(const Scoped &)=delete
Definition logging.hpp:179
void info(const char *name, std::string_view msg)
Definition logging.hpp:215
void err(const char *name, std::string_view msg)
Definition logging.hpp:223
void warn(const char *name, std::string_view 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 log(const char *name, Level level, std::string_view msg)
Log a message with the logger whose name is provided in the first parameter.
void debug(const char *name, std::string_view 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.
spdlog::logger & get_sink(const char *name)
void critical(const char *name, std::string_view msg)
Definition logging.hpp:227
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:32
void info(std::string_view msg)
Definition logging.hpp:108
void critical(std::string_view msg)
Definition logging.hpp:162
void set_path(const std::string &path)
Change the logger to a file-based logging and set its path.
Level get_level()
Current log level.
Level
Hierarchical logging level
Definition logging.hpp:40
@ Critical
Definition logging.hpp:48
@ Info
Definition logging.hpp:45
@ Warn
Definition logging.hpp:46
@ Debug
Definition logging.hpp:44
@ Err
Definition logging.hpp:47
@ Off
Definition logging.hpp:41
@ Trace
Definition logging.hpp:43
void log(Level level, std::string_view msg)
Log a message with the LIEF's logger.
void enable()
Globally enable the logging module.
void warn(std::string_view msg)
Definition logging.hpp:126
void debug(std::string_view msg)
Definition logging.hpp:90
void set_logger(std::shared_ptr< spdlog::logger > logger)
void disable()
Globally disable the logging module.
const char * to_string(Level e)
void set_level(Level level)
Change the logging level (hierarchical).
void err(std::string_view msg)
Definition logging.hpp:144
void enable_debug()
Definition logging.hpp:86
Definition logging.hpp:27
#define LIEF_API
Definition visibility.h:45