PDI 1.13.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 * Copyright (C) 2026 Julien Bigot <julien@julien-bigot.fr>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of CEA nor the names of its contributors may be used to
15 * endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 ******************************************************************************/
26
27#ifndef PDI_LOGGER_H_
28#define PDI_LOGGER_H_
29
30#include <functional>
31#include <memory>
32#include <string>
33#include <vector>
34
35#include <spdlog/logger.h>
36
37#include <pdi/pdi_fwd.h>
38#include <pdi/paraconf_wrapper.h>
39
40namespace PDI {
41
43class PDI_EXPORT Logger
44{
46 std::shared_ptr<spdlog::logger> m_logger;
47
49 std::vector<std::reference_wrapper<Logger>> m_default_pattern_observers;
50
52 std::vector<std::string> m_pattern_blocks;
53
55 std::string m_pattern = "[%T][%n] *** %^%l%$: %v";
56
58 bool m_pattern_from_config = false;
59
61 void build_pattern();
62
64 Logger* m_parent_logger = nullptr;
65
66public:
69
75 Logger(const std::string& logger_name, PC_tree_t config, spdlog::level::level_enum level = spdlog::level::info);
76
88 Logger(Logger& parent_logger, const std::string& logger_name, PC_tree_t config);
89
95 void setup(const std::string& logger_name, PC_tree_t config, spdlog::level::level_enum level = spdlog::level::info);
96
102 void setup(Logger& parent_logger, const std::string& logger_name, PC_tree_t config);
103
108 void pattern(const std::string& pattern);
109
117 void global_pattern(const std::string& pattern);
118
125 void global_level(spdlog::level::level_enum log_level);
126
132 void default_pattern(const std::string& pattern);
133
138 void add_pattern_block(const std::string& block);
139
146 void add_pattern_global_block(const std::string& block);
147
151 const std::string& pattern() const;
152
156 void level(spdlog::level::level_enum log_level);
157
161 spdlog::level::level_enum level() const;
162
169 void evaluate_pattern(Context& ctx) const;
170
179
184 template <typename... Args>
185 void trace(fmt::format_string<Args...> fmt, Args&&... args)
186 {
187 m_logger->trace(fmt, std::forward<Args>(args)...);
188 }
189
194 template <typename... Args>
195 void debug(fmt::format_string<Args...> fmt, Args&&... args)
196 {
197 m_logger->debug(fmt, std::forward<Args>(args)...);
198 }
199
204 template <typename... Args>
205 void info(fmt::format_string<Args...> fmt, Args&&... args)
206 {
207 m_logger->info(fmt, std::forward<Args>(args)...);
208 }
209
214 template <typename... Args>
215 void warn(fmt::format_string<Args...> fmt, Args&&... args)
216 {
217 m_logger->warn(fmt, std::forward<Args>(args)...);
218 }
219
224 template <typename... Args>
225 void error(fmt::format_string<Args...> fmt, Args&&... args)
226 {
227 m_logger->error(fmt, std::forward<Args>(args)...);
228 }
229
233 std::shared_ptr<spdlog::logger> real_logger();
234};
235
236} // namespace PDI
237
238#endif // PDI_LOGGER_H_
Definition context.h:43
void error(fmt::format_string< Args... > fmt, Args &&... args)
Writes error level message.
Definition logger.h:225
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:185
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:195
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:215
void info(fmt::format_string< Args... > fmt, Args &&... args)
Writes info level message.
Definition logger.h:205
spdlog::level::level_enum level() const
Returns level of the logger.
void global_level(spdlog::level::level_enum log_level)
Changes level of the global 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.
Logger()
Creates new empty 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