xref: /aosp_15_r20/external/gmmlib/Source/GmmLib/Utility/GmmLog/spdlog/logger.h (revision 35ffd701415c9e32e53136d61a677a8d0a8fc4a5)
1 //
2 // Copyright(c) 2015 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 // Thread safe logger
9 // Has name, log level, vector of std::shared sink pointers and formatter
10 // Upon each log write the logger:
11 // 1. Checks if its log level is enough to log the message
12 // 2. Format the message using the formatter function
13 // 3. Pass the formatted message to its sinks to performa the actual logging
14 
15 #include <spdlog/sinks/base_sink.h>
16 #include <spdlog/common.h>
17 
18 #include <vector>
19 #include <memory>
20 #include <string>
21 
22 namespace spdlog
23 {
24 
25 class logger
26 {
27 public:
28     logger(const std::string& logger_name, sink_ptr single_sink);
29     logger(const std::string& name, sinks_init_list);
30     template<class It>
31     logger(const std::string& name, const It& begin, const It& end);
32 
33     virtual ~logger();
34     logger(const logger&) = delete;
35     logger& operator=(const logger&) = delete;
36 
37 
38     template <typename... Args> void log(level::level_enum lvl, const char* fmt, const Args&... args);
39     template <typename... Args> void log(level::level_enum lvl, const char* msg);
40     template <typename... Args> void trace(const char* fmt, const Args&... args);
41     template <typename... Args> void debug(const char* fmt, const Args&... args);
42     template <typename... Args> void info(const char* fmt, const Args&... args);
43     template <typename... Args> void warn(const char* fmt, const Args&... args);
44     template <typename... Args> void error(const char* fmt, const Args&... args);
45     template <typename... Args> void critical(const char* fmt, const Args&... args);
46 
47     template <typename T> void log(level::level_enum lvl, const T&);
48     template <typename T> void trace(const T&);
49     template <typename T> void debug(const T&);
50     template <typename T> void info(const T&);
51     template <typename T> void warn(const T&);
52     template <typename T> void error(const T&);
53     template <typename T> void critical(const T&);
54 
55     bool should_log(level::level_enum) const;
56     void set_level(level::level_enum);
57     level::level_enum level() const;
58     const std::string& name() const;
59     void set_pattern(const std::string&);
60     void set_formatter(formatter_ptr);
61 
62     // error handler
63     void set_error_handler(log_err_handler);
64     log_err_handler error_handler();
65 
66     // automatically call flush() if message level >= log_level
67     void flush_on(level::level_enum log_level);
68 
69     virtual void flush();
70 
71     const std::vector<sink_ptr>& sinks() const;
72 
73 protected:
74     virtual void _sink_it(details::log_msg&);
75     virtual void _set_pattern(const std::string&);
76     virtual void _set_formatter(formatter_ptr);
77 
78     // default error handler: print the error to stderr with the max rate of 1 message/minute
79     virtual void _default_err_handler(const std::string &msg);
80 
81     // return true if the given message level should trigger a flush
82     bool _should_flush_on(const details::log_msg&);
83 
84     const std::string _name;
85     std::vector<sink_ptr> _sinks;
86     formatter_ptr _formatter;
87     spdlog::level_t _level;
88     spdlog::level_t _flush_level;
89     log_err_handler _err_handler;
90     std::atomic<time_t> _last_err_time;
91 };
92 }
93 
94 #include <spdlog/details/logger_impl.h>
95