PDI 1.6.0

the PDI data interface

logger.h
1/*******************************************************************************
2 * Copyright (C) 2018-2022 Institute of Bioorganic Chemistry Polish Academy of Science (PSNC)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of CEA nor the names of its contributors may be used to
13 * endorse or promote products derived from this software without specific
14 * prior written permission.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 ******************************************************************************/
24
25#ifndef PDI_LOGGER_H_
26#define PDI_LOGGER_H_
27
28#include <utility>
29#include <string>
30#include <vector>
31
32#include <spdlog/logger.h>
33#include <spdlog/spdlog.h>
34
35#include <pdi/pdi_fwd.h>
36#include <pdi/paraconf_wrapper.h>
37
38namespace PDI {
39
41class PDI_EXPORT Logger
42{
44 std::shared_ptr<spdlog::logger> m_logger;
45
47 std::vector<std::reference_wrapper<Logger>> m_default_pattern_observers;
48
50 std::vector<std::string> m_pattern_blocks;
51
53 std::string m_pattern = "[%T][%n] *** %^%l%$: %v";
54
56 bool m_pattern_from_config = false;
57
59 void build_pattern();
60
62 Logger* m_parent_logger = nullptr; //change in c++17 to std::optional
63
64public:
66 Logger() = default;
67
73 Logger(const std::string& logger_name, PC_tree_t config, spdlog::level::level_enum level = spdlog::level::info);
74
86 Logger(Logger& parent_logger, const std::string& logger_name, PC_tree_t config);
87
93 void setup(const std::string& logger_name, PC_tree_t config, spdlog::level::level_enum level = spdlog::level::info);
94
100 void setup(Logger& parent_logger, const std::string& logger_name, PC_tree_t config);
101
106 void pattern(const std::string& pattern);
107
115 void global_pattern(const std::string& pattern);
116
122 void default_pattern(const std::string& pattern);
123
128 void add_pattern_block(const std::string& block);
129
136 void add_pattern_global_block(const std::string& block);
137
141 const std::string& pattern() const;
142
146 void level(spdlog::level::level_enum log_level);
147
151 spdlog::level::level_enum level() const;
152
159 void evaluate_pattern(Context& ctx) const;
160
169
174 template<typename... Args>
175 void trace(const char* fmt, Args&& ... args)
176 {
177 m_logger->trace(fmt, std::forward<Args>(args)...);
178 }
179
184 template<typename... Args>
185 void debug(const char* fmt, Args&& ... args)
186 {
187 m_logger->debug(fmt, std::forward<Args>(args)...);
188 }
189
194 template<typename... Args>
195 void info(const char* fmt, Args&& ... args)
196 {
197 m_logger->info(fmt, std::forward<Args>(args)...);
198 }
199
204 template<typename... Args>
205 void warn(const char* fmt, Args&& ... args)
206 {
207 m_logger->warn(fmt, std::forward<Args>(args)...);
208 }
209
214 template<typename... Args>
215 void error(const char* fmt, Args&& ... args)
216 {
217 m_logger->error(fmt, std::forward<Args>(args)...);
218 }
219
223 std::shared_ptr<spdlog::logger> real_logger();
224
225};
226
227} // namespace PDI
228
229#endif // PDI_LOGGER_H_
Definition: context.h:45
Wrapper for spdlog::logger with additional pattern getter method.
Definition: logger.h:42
const std::string & pattern() const
Returns pattern of the logger.
Logger()=default
Creates new empty logger.
void setup(Logger &parent_logger, const std::string &logger_name, PC_tree_t config)
Sets up the logger with parent logger.
void level(spdlog::level::level_enum log_level)
Sets logger level.
void error(const char *fmt, Args &&... args)
Writes error level message.
Definition: logger.h:215
void default_pattern(const std::string &pattern)
Changes default pattern of the logger (won't be updated if current pattern is from config)
void debug(const char *fmt, Args &&... args)
Writes debug level message.
Definition: logger.h:185
void global_pattern(const std::string &pattern)
Changes pattern of the global logger.
Logger(Logger &parent_logger, const std::string &logger_name, PC_tree_t config)
Creates new logger with parent logger.
void warn(const char *fmt, Args &&... args)
Writes warning level message.
Definition: logger.h:205
void evaluate_global_pattern(Context &ctx) const
Evaluate global pattern.
Logger(const std::string &logger_name, PC_tree_t config, spdlog::level::level_enum level=spdlog::level::info)
Creates new logger.
void add_pattern_global_block(const std::string &block)
Add new element to default pattern of the global logger.
void trace(const char *fmt, Args &&... args)
Writes trace level message.
Definition: logger.h:175
void add_pattern_block(const std::string &block)
Add new element to default pattern.
void info(const char *fmt, Args &&... args)
Writes info level message.
Definition: logger.h:195
spdlog::level::level_enum level() const
Returns level of the logger.
void setup(const std::string &logger_name, PC_tree_t config, spdlog::level::level_enum level=spdlog::level::info)
Sets up the logger.
void pattern(const std::string &pattern)
Changes pattern of the logger.
void evaluate_pattern(Context &ctx) const
Evaluate pattern.
std::shared_ptr< spdlog::logger > real_logger()
Returns real spdlog logger.
Definition: array_datatype.h:38