Rev Author Line No. Line
23 ovan 1 /*************************************************************************/
2 /* spdlog - an extremely fast and easy to use c++11 logging library.     */
3 /* Copyright (c) 2014 Gabi Melman.                                       */
4 /*                                                                       */
5 /* Permission is hereby granted, free of charge, to any person obtaining */
6 /* a copy of this software and associated documentation files (the       */
7 /* "Software"), to deal in the Software without restriction, including   */
8 /* without limitation the rights to use, copy, modify, merge, publish,   */
9 /* distribute, sublicense, and/or sell copies of the Software, and to    */
10 /* permit persons to whom the Software is furnished to do so, subject to */
11 /* the following conditions:                                             */
12 /*                                                                       */
13 /* The above copyright notice and this permission notice shall be        */
14 /* included in all copies or substantial portions of the Software.       */
15 /*                                                                       */
16 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
17 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
18 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
19 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
20 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
21 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
22 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
23 /*************************************************************************/
24  
25 #pragma once
26  
27 // Thread safe logger
28 // Has name, log level, vector of std::shared sink pointers and formatter
29 // Upon each log write the logger:
30 // 1. Checks if its log level is enough to log the message
31 // 2. Format the message using the formatter function
32 // 3. Pass the formatted message to its sinks to performa the actual logging
33  
34 #include<vector>
35 #include<memory>
36 #include "sinks/base_sink.h"
37 #include "common.h"
38  
39 namespace spdlog
40 {
41  
42 namespace details
43 {
44 class line_logger;
45 }
46  
47 class logger
48 {
49 public:
50     logger(const std::string& logger_name, sink_ptr single_sink);
51     logger(const std::string& name, sinks_init_list);
52     template<class It>
53     logger(const std::string& name, const It& begin, const It& end);
54  
55     virtual ~logger();
56     logger(const logger&) = delete;
57     logger& operator=(const logger&) = delete;
58  
59     void set_level(level::level_enum);
60     level::level_enum level() const;
61  
62     const std::string& name() const;
63     bool should_log(level::level_enum) const;
64  
65     // logger.info(cppformat_string, arg1, arg2, arg3, ...) call style
66     template <typename... Args> details::line_logger trace(const char* fmt, const Args&... args);
67     template <typename... Args> details::line_logger debug(const char* fmt, const Args&... args);
68     template <typename... Args> details::line_logger info(const char* fmt, const Args&... args);
69     template <typename... Args> details::line_logger notice(const char* fmt, const Args&... args);
70     template <typename... Args> details::line_logger warn(const char* fmt, const Args&... args);
71     template <typename... Args> details::line_logger error(const char* fmt, const Args&... args);
72     template <typename... Args> details::line_logger critical(const char* fmt, const Args&... args);
73     template <typename... Args> details::line_logger alert(const char* fmt, const Args&... args);
74     template <typename... Args> details::line_logger emerg(const char* fmt, const Args&... args);
75  
76  
77     // logger.info(msg) << ".." call style
78     template <typename T> details::line_logger trace(const T&);
79     template <typename T> details::line_logger debug(const T&);
80     template <typename T> details::line_logger info(const T&);
81     template <typename T> details::line_logger notice(const T&);
82     template <typename T> details::line_logger warn(const T&);
83     template <typename T> details::line_logger error(const T&);
84     template <typename T> details::line_logger critical(const T&);
85     template <typename T> details::line_logger alert(const T&);
86     template <typename T> details::line_logger emerg(const T&);
87  
88  
89     // logger.info() << ".." call  style
90     details::line_logger trace();
91     details::line_logger debug();
92     details::line_logger info();
93     details::line_logger notice();
94     details::line_logger warn();
95     details::line_logger error();
96     details::line_logger critical();
97     details::line_logger alert();
98     details::line_logger emerg();
99  
100  
101  
102     // Create log message with the given level, no matter what is the actual logger's level
103     template <typename... Args>
104     details::line_logger force_log(level::level_enum lvl, const char* fmt, const Args&... args);
105  
106     // Set the format of the log messages from this logger
107     void set_pattern(const std::string&);
108     void set_formatter(formatter_ptr);
109  
110  
111 protected:
112     virtual void _log_msg(details::log_msg&);
113     virtual void _set_pattern(const std::string&);
114     virtual void _set_formatter(formatter_ptr);
115     details::line_logger _log_if_enabled(level::level_enum lvl);
116     template <typename... Args>
117     details::line_logger _log_if_enabled(level::level_enum lvl, const char* fmt, const Args&... args);
118     template<typename T>
119     inline details::line_logger _log_if_enabled(level::level_enum lvl, const T& msg);
120  
121  
122     friend details::line_logger;
123     std::string _name;
124     std::vector<sink_ptr> _sinks;
125     formatter_ptr _formatter;
126     std::atomic_int _level;
127  
128 };
129 }
130  
131 #include "./details/logger_impl.h"