PDI 1.11.0

the PDI data interface

logger.h
1/*******************************************************************************
2 * Copyright (C) 2026 Commissariat a l'energie atomique et aux energies alternatives (CEA)
3 * Copyright (C) 2018-2022 Institute of Bioorganic Chemistry Polish Academy of Science (PSNC)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of CEA nor the names of its contributors may be used to
14 * endorse or promote products derived from this software without specific
15 * prior written permission.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 ******************************************************************************/
25
26#ifndef PDI_LOGGER_H_
27#define PDI_LOGGER_H_
28
29#include <string>
30#include <utility>
31#include <vector>
32
33#include <spdlog/logger.h>
34#include <spdlog/spdlog.h>
35
36#include <pdi/pdi_fwd.h>
37#include <pdi/paraconf_wrapper.h>
38
39namespace PDI {
40
42class PDI_EXPORT Logger
43{
45 std::shared_ptr<spdlog::logger> m_logger;
46
48 std::vector<std::reference_wrapper<Logger>> m_default_pattern_observers;
49
51 std::vector<std::string> m_pattern_blocks;
52
54 std::string m_pattern = "[%T][%n] *** %^%l%$: %v";
55
57 bool m_pattern_from_config = false;
58
60 void build_pattern();
61
63 Logger* m_parent_logger = nullptr; //change in c++17 to std::optional
64
65public:
67 Logger() = default;
68
74 Logger(const std::string& logger_name, PC_tree_t config, spdlog::level::level_enum level = spdlog::level::info);
75
87 Logger(Logger& parent_logger, const std::string& logger_name, PC_tree_t config);
88
94 void setup(const std::string& logger_name, PC_tree_t config, spdlog::level::level_enum level = spdlog::level::info);
95
101 void setup(Logger& parent_logger, const std::string& logger_name, PC_tree_t config);
102
107 void pattern(const std::string& pattern);
108
116 void global_pattern(const std::string& pattern);
117
123 void default_pattern(const std::string& pattern);
124
129 void add_pattern_block(const std::string& block);
130
137 void add_pattern_global_block(const std::string& block);
138
142 const std::string& pattern() const;
143
147 void level(spdlog::level::level_enum log_level);
148
152 spdlog::level::level_enum level() const;
153
160 void evaluate_pattern(Context& ctx) const;
161
170
175 template <typename... Args>
176 void trace(fmt::format_string<Args...> fmt, Args&&... args)
177 {
178 m_logger->trace(fmt, std::forward<Args>(args)...);
179 }
180
185 template <typename... Args>
186 void debug(fmt::format_string<Args...> fmt, Args&&... args)
187 {
188 m_logger->debug(fmt, std::forward<Args>(args)...);
189 }
190
195 template <typename... Args>
196 void info(fmt::format_string<Args...> fmt, Args&&... args)
197 {
198 m_logger->info(fmt, std::forward<Args>(args)...);
199 }
200
205 template <typename... Args>
206 void warn(fmt::format_string<Args...> fmt, Args&&... args)
207 {
208 m_logger->warn(fmt, std::forward<Args>(args)...);
209 }
210
215 template <typename... Args>
216 void error(fmt::format_string<Args...> fmt, Args&&... args)
217 {
218 m_logger->error(fmt, std::forward<Args>(args)...);
219 }
220
224 std::shared_ptr<spdlog::logger> real_logger();
225};
226
227} // namespace PDI
228
229#endif // PDI_LOGGER_H_
Definition context.h:44
void error(fmt::format_string< Args... > fmt, Args &&... args)
Writes error level message.
Definition logger.h:216
const std::string & pattern() const
Returns pattern of the logger.
void trace(fmt::format_string< Args... > fmt, Args &&... args)
Writes trace level message.
Definition logger.h:176
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 default_pattern(const std::string &pattern)
Changes default pattern of the logger (won't be updated if current pattern is from config).
void debug(fmt::format_string< Args... > fmt, Args &&... args)
Writes debug level message.
Definition logger.h:186
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 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 add_pattern_block(const std::string &block)
Add new element to default pattern.
void warn(fmt::format_string< Args... > fmt, Args &&... args)
Writes warning level message.
Definition logger.h:206
void info(fmt::format_string< Args... > fmt, Args &&... args)
Writes info level message.
Definition logger.h:196
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