LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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 {
32enum class LEVEL : uint32_t {
39 OFF = 0,
40
47};
51
52LIEF_API const char* to_string(LEVEL e);
53LIEF_API void disable();
56LIEF_API void enable();
59LIEF_API void set_level(LEVEL level);
62LIEF_API void set_path(const std::string& path);
65LIEF_API void log(LEVEL level, const std::string& msg);
68
69LIEF_API void log(LEVEL level, const std::string& fmt,
70 const std::vector<std::string>& args);
71
72template <typename... Args>
73void log(LEVEL level, const std::string& fmt, const Args &... args) {
74 std::vector<std::string> vec_args;
75 vec_args.insert(vec_args.end(), { static_cast<decltype(vec_args)::value_type>(args)...});
76 return log(level, fmt, vec_args);
77}
78
79LIEF_API void set_logger(std::shared_ptr<spdlog::logger> logger);
80
82
83inline void enable_debug() {
85}
86
87inline void debug(const std::string& msg) {
88 log(LEVEL::DEBUG, msg);
89}
90
91inline void debug(const std::string& fmt, const std::vector<std::string>& args) {
92 log(LEVEL::DEBUG, fmt, args);
93}
94
95template <typename... Args>
96void debug(const std::string& fmt, const Args &... args) {
97 std::vector<std::string> vec_args;
98 vec_args.insert(vec_args.end(), { static_cast<decltype(vec_args)::value_type>(args)...});
99 return debug(fmt, vec_args);
100}
101
102// -----------------------------------------------------------------------------
103
104inline void info(const std::string& msg) {
105 log(LEVEL::INFO, msg);
106}
107
108inline void info(const std::string& fmt, const std::vector<std::string>& args) {
109 log(LEVEL::INFO, fmt, args);
110}
111
112template <typename... Args>
113void info(const std::string& fmt, const Args &... args) {
114 std::vector<std::string> vec_args;
115 vec_args.insert(vec_args.end(), { static_cast<decltype(vec_args)::value_type>(args)...});
116 return info(fmt, vec_args);
117}
118
119// -----------------------------------------------------------------------------
120
121inline void warn(const std::string& msg) {
122 log(LEVEL::WARN, msg);
123}
124
125inline void warn(const std::string& fmt, const std::vector<std::string>& args) {
126 log(LEVEL::WARN, fmt, args);
127}
128
129template <typename... Args>
130void warn(const std::string& fmt, const Args &... args) {
131 std::vector<std::string> vec_args;
132 vec_args.insert(vec_args.end(), { static_cast<decltype(vec_args)::value_type>(args)...});
133 return warn(fmt, vec_args);
134}
135
136// -----------------------------------------------------------------------------
137
138inline void err(const std::string& msg) {
139 log(LEVEL::ERR, msg);
140}
141
142inline void err(const std::string& fmt, const std::vector<std::string>& args) {
143 log(LEVEL::ERR, fmt, args);
144}
145
146template <typename... Args>
147void err(const std::string& fmt, const Args &... args) {
148 std::vector<std::string> vec_args;
149 vec_args.insert(vec_args.end(), { static_cast<decltype(vec_args)::value_type>(args)...});
150 return err(fmt, vec_args);
151}
152
153// -----------------------------------------------------------------------------
154
155inline void critical(const std::string& msg) {
156 log(LEVEL::CRITICAL, msg);
157}
158
159inline void critical(const std::string& fmt, const std::vector<std::string>& args) {
160 log(LEVEL::CRITICAL, fmt, args);
161}
162
163template <typename... Args>
164void critical(const std::string& fmt, const Args &... args) {
165 std::vector<std::string> vec_args;
166 vec_args.insert(vec_args.end(), { static_cast<decltype(vec_args)::value_type>(args)...});
167 return critical(fmt, vec_args);
168}
169
170// -----------------------------------------------------------------------------
171namespace named {LIEF_API LEVEL get_level(const char* name);
174LIEF_API void disable(const char* name);
177LIEF_API void enable(const char* name);
180LIEF_API void set_level(const char* name, LEVEL level);
183LIEF_API void set_path(const char* name, const std::string& path);
186LIEF_API void log(const char* name, LEVEL level, const std::string& msg);
189LIEF_API void set_logger(const char* name, std::shared_ptr<spdlog::logger> logger);
192LIEF_API void reset(const char* name);
195inline void enable_debug(const char* name) {
198 set_level(name, LEVEL::DEBUG);
199}
200
201inline void debug(const char* name, const std::string& msg) {
202 log(name, LEVEL::DEBUG, msg);
203}
204
205inline void info(const char* name, const std::string& msg) {
206 log(name, LEVEL::INFO, msg);
207}
208
209inline void warn(const char* name, const std::string& msg) {
210 log(name, LEVEL::WARN, msg);
211}
212
213inline void err(const char* name, const std::string& msg) {
214 log(name, LEVEL::ERR, msg);
215}
216
217inline void critical(const char* name, const std::string& msg) {
218 log(name, LEVEL::CRITICAL, msg);
219}
220}
221
222
223class Scoped {
224 public:
225 Scoped(const Scoped&) = delete;
226 Scoped& operator=(const Scoped&) = delete;
227
228 Scoped(Scoped&&) = delete;
229 Scoped& operator=(Scoped&&) = delete;
230
231 explicit Scoped(LEVEL level) :
232 level_(get_level())
233 {
234 set_level(level);
235 }
236
237 const Scoped& set_level(LEVEL lvl) const {
239 return *this;
240 }
241
243 set_level(level_);
244 }
245
246 private:
247 LEVEL level_ = LEVEL::INFO;
248};
249
250
251}
252}
253
254#endif
Definition logging.hpp:223
Scoped(const Scoped &)=delete
Scoped & operator=(Scoped &&)=delete
Scoped(Scoped &&)=delete
Scoped(LEVEL level)
Definition logging.hpp:231
~Scoped()
Definition logging.hpp:242
const Scoped & set_level(LEVEL lvl) const
Definition logging.hpp:237
Scoped & operator=(const Scoped &)=delete
Definition logging.hpp:171
void warn(const char *name, const std::string &msg)
Definition logging.hpp:209
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:201
void set_path(const char *name, const std::string &path)
Change the logger with the given as a file-base logging and set its path.
void err(const char *name, const std::string &msg)
Definition logging.hpp:213
void critical(const char *name, const std::string &msg)
Definition logging.hpp:217
void info(const char *name, const std::string &msg)
Definition logging.hpp:205
void log(const char *name, LEVEL level, const std::string &msg)
Log a message with the logger whose name in 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.
void reset()
LEVEL
Hierarchical logging level
Definition logging.hpp:38
@ TRACE
Definition logging.hpp:41
@ WARN
Definition logging.hpp:44
@ INFO
Definition logging.hpp:43
@ OFF
Definition logging.hpp:39
@ CRITICAL
Definition logging.hpp:46
@ ERR
Definition logging.hpp:45
@ DEBUG
Definition logging.hpp:42
void warn(const std::string &msg)
Definition logging.hpp:121
void set_path(const std::string &path)
Change the logger as a file-base logging and set its path.
void critical(const std::string &msg)
Definition logging.hpp:155
void err(const std::string &msg)
Definition logging.hpp:138
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:104
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:87
void enable_debug()
Definition logging.hpp:83
LIEF namespace.
Definition Abstract/Binary.hpp:36
Definition logging.hpp:26
#define LIEF_API
Definition visibility.h:41