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 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_LOGGING_H
16#define LIEF_LOGGING_H
17
18#include "LIEF/visibility.h"
19#include <string_view>
20
21#include <cstdint>
22#include <memory>
23#include <string>
24#include <vector>
25
26namespace spdlog {
27class logger;
28}
29
30
31namespace LIEF::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, std::string_view msg);
69
70LIEF_API void log(Level level, std::string_view fmt,
71 const std::vector<std::string>& args);
72
73template<typename... Args>
74void log(Level level, std::string_view 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(std::string_view msg) {
90 log(Level::Debug, msg);
91}
92
93inline void debug(std::string_view fmt, const std::vector<std::string>& args) {
94 log(Level::Debug, fmt, args);
95}
96
97template<typename... Args>
98void debug(std::string_view 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(std::string_view msg) {
108 log(Level::Info, msg);
109}
110
111inline void info(std::string_view fmt, const std::vector<std::string>& args) {
112 log(Level::Info, fmt, args);
113}
114
115template<typename... Args>
116void info(std::string_view 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(std::string_view msg) {
126 log(Level::Warn, msg);
127}
128
129inline void warn(std::string_view fmt, const std::vector<std::string>& args) {
130 log(Level::Warn, fmt, args);
131}
132
133template<typename... Args>
134void warn(std::string_view 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(std::string_view msg) {
144 log(Level::Err, msg);
145}
146
147inline void err(std::string_view fmt, const std::vector<std::string>& args) {
148 log(Level::Err, fmt, args);
149}
150
151template<typename... Args>
152void err(std::string_view 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(std::string_view msg) {
162 log(Level::Critical, msg);
163}
164
165inline void critical(std::string_view fmt, const std::vector<std::string>& args) {
166 log(Level::Critical, fmt, args);
167}
168
169template<typename... Args>
170void critical(std::string_view fmt, const Args&... args) {
171 std::vector<std::string> vec_args;
172 vec_args.insert(vec_args.end(),
173 {static_cast<decltype(vec_args)::value_type>(args)...});
174 return critical(fmt, vec_args);
175}
176
177// -----------------------------------------------------------------------------
178namespace named {
180LIEF_API Level get_level(const char* name);
181
183LIEF_API void disable(const char* name);
184
186LIEF_API void enable(const char* name);
187
189LIEF_API void set_level(const char* name, Level level);
190
192LIEF_API void set_path(const char* name, const std::string& path);
193
195LIEF_API void log(const char* name, Level level, std::string_view msg);
196
198LIEF_API void set_logger(const char* name, std::shared_ptr<spdlog::logger> logger);
199
200LIEF_API spdlog::logger& get_sink(const char* name);
201
203LIEF_API void reset(const char* name);
204
206inline void enable_debug(const char* name) {
207 set_level(name, Level::Debug);
208}
209
210inline void debug(const char* name, std::string_view msg) {
211 log(name, Level::Debug, msg);
212}
213
214inline void info(const char* name, std::string_view msg) {
215 log(name, Level::Info, msg);
216}
217
218inline void warn(const char* name, std::string_view msg) {
219 log(name, Level::Warn, msg);
220}
221
222inline void err(const char* name, std::string_view msg) {
223 log(name, Level::Err, msg);
224}
225
226inline void critical(const char* name, std::string_view msg) {
227 log(name, Level::Critical, msg);
228}
229}
230
231
232class Scoped {
233 public:
234 Scoped(const Scoped&) = delete;
235 Scoped& operator=(const Scoped&) = delete;
236
237 Scoped(Scoped&&) = delete;
238 Scoped& operator=(Scoped&&) = delete;
239
240 explicit Scoped(Level level) :
241 level_(get_level()) {
242 set_level(level);
243 }
244
245 explicit Scoped(Level level, std::string name) :
246 level_(get_level()),
247 name_(std::move(name)) {
248 set_level(level);
249 }
250
251 const Scoped& set_level(Level lvl) const {
252 if (name_.empty()) {
254 } else {
255 logging::named::set_level(name_.c_str(), lvl);
256 }
257 return *this;
258 }
259
260 void reset() {
261 set_level(level_);
262 }
263
265 reset();
266 }
267
268 private:
269 Level level_ = Level::Info;
270 std::string name_;
271};
272
273
274}
275
276
277#endif
Scoped(const Scoped &)=delete
Scoped & operator=(Scoped &&)=delete
Scoped(Scoped &&)=delete
Scoped(Level level)
Definition logging.hpp:240
~Scoped()
Definition logging.hpp:264
void reset()
Definition logging.hpp:260
Scoped(Level level, std::string name)
Definition logging.hpp:245
const Scoped & set_level(Level lvl) const
Definition logging.hpp:251
Scoped & operator=(const Scoped &)=delete
Definition logging.hpp:178
void info(const char *name, std::string_view msg)
Definition logging.hpp:214
void err(const char *name, std::string_view msg)
Definition logging.hpp:222
void warn(const char *name, std::string_view msg)
Definition logging.hpp:218
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:210
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:226
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
void info(std::string_view msg)
Definition logging.hpp:107
void critical(std::string_view msg)
Definition logging.hpp:161
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:39
@ Critical
Definition logging.hpp:47
@ Info
Definition logging.hpp:44
@ Warn
Definition logging.hpp:45
@ Debug
Definition logging.hpp:43
@ Err
Definition logging.hpp:46
@ Off
Definition logging.hpp:40
@ Trace
Definition logging.hpp:42
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:125
void debug(std::string_view msg)
Definition logging.hpp:89
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:143
void enable_debug()
Definition logging.hpp:85
Definition logging.hpp:26
#define LIEF_API
Definition visibility.h:44