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 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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// -----------------------------------------------------------------------------
171
172
173class Scoped {
174 public:
175 Scoped(const Scoped&) = delete;
176 Scoped& operator=(const Scoped&) = delete;
177
178 Scoped(Scoped&&) = delete;
179 Scoped& operator=(Scoped&&) = delete;
180
181 explicit Scoped(LEVEL level) :
182 level_(get_level())
183 {
184 set_level(level);
185 }
186
187 const Scoped& set_level(LEVEL lvl) const {
189 return *this;
190 }
191
193 set_level(level_);
194 }
195
196 private:
197 LEVEL level_ = LEVEL::INFO;
198};
199
200
201}
202}
203
204#endif
Definition logging.hpp:173
Scoped(const Scoped &)=delete
Scoped & operator=(Scoped &&)=delete
Scoped(Scoped &&)=delete
Scoped(LEVEL level)
Definition logging.hpp:181
~Scoped()
Definition logging.hpp:192
const Scoped & set_level(LEVEL lvl) const
Definition logging.hpp:187
Scoped & operator=(const Scoped &)=delete
Definition logging.hpp:31
LEVEL get_level()
Current log level.
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