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 #include <type_traits>
27 #include "../common.h"
28 #include "../logger.h"
29  
30  
31 // Line logger class - aggregates operator<< calls to fast ostream
32 // and logs upon destruction
33  
34 namespace spdlog
35 {
36 namespace details
37 {
38 class line_logger
39 {
40 public:
41     line_logger(logger* callback_logger, level::level_enum msg_level, bool enabled):
42         _callback_logger(callback_logger),
43         _log_msg(msg_level),
44         _enabled(enabled)
45     {}
46  
47     // No copy intended. Only move
48     line_logger(const line_logger& other) = delete;
49     line_logger& operator=(const line_logger&) = delete;
50     line_logger& operator=(line_logger&&) = delete;
51  
52  
53     line_logger(line_logger&& other) :
54         _callback_logger(other._callback_logger),
55         _log_msg(std::move(other._log_msg)),
56         _enabled(other._enabled)
57     {
58         other.disable();
59     }
60  
61     //Log the log message using the callback logger
62     ~line_logger()
63     {
64         if (_enabled)
65         {
66             _log_msg.logger_name = _callback_logger->name();
67             _log_msg.time = os::now();
68             _callback_logger->_log_msg(_log_msg);
69         }
70     }
71  
72     //
73     // Support for format string with variadic args
74     //
75  
76  
77     void write(const char* what)
78     {
79         if (_enabled)
80             _log_msg.raw << what;
81     }
82  
83     template <typename... Args>
84     void write(const char* fmt, const Args&... args)
85     {
86         if (!_enabled)
87             return;
88         try
89         {
90             _log_msg.raw.write(fmt, args...);
91         }
92         catch (const fmt::FormatError& e)
93         {
94             throw spdlog_ex(fmt::format("formatting error while processing format string '{}': {}", fmt, e.what()));
95         }
96     }
97  
98  
99     //
100     // Support for operator<<
101     //
102     line_logger& operator<<(const char* what)
103     {
104         if (_enabled)
105             _log_msg.raw << what;
106         return *this;
107     }
108  
109     line_logger& operator<<(const std::string& what)
110     {
111         if (_enabled)
112             _log_msg.raw << what;
113         return *this;
114     }
115  
116     line_logger& operator<<(int what)
117     {
118         if (_enabled)
119             _log_msg.raw << what;
120         return *this;
121     }
122  
123     line_logger& operator<<(unsigned int what)
124     {
125         if (_enabled)
126             _log_msg.raw << what;
127         return *this;
128     }
129  
130  
131     line_logger& operator<<(long what)
132     {
133         if (_enabled)
134             _log_msg.raw << what;
135         return *this;
136     }
137  
138     line_logger& operator<<(unsigned long what)
139     {
140         if (_enabled)
141             _log_msg.raw << what;
142         return *this;
143     }
144  
145     line_logger& operator<<(long long what)
146     {
147         if (_enabled)
148             _log_msg.raw << what;
149         return *this;
150     }
151  
152     line_logger& operator<<(unsigned long long what)
153     {
154         if (_enabled)
155             _log_msg.raw << what;
156         return *this;
157     }
158  
159     line_logger& operator<<(double what)
160     {
161         if (_enabled)
162             _log_msg.raw << what;
163         return *this;
164     }
165  
166     line_logger& operator<<(long double what)
167     {
168         if (_enabled)
169             _log_msg.raw << what;
170         return *this;
171     }
172  
173     line_logger& operator<<(float what)
174     {
175         if (_enabled)
176             _log_msg.raw << what;
177         return *this;
178     }
179  
180     line_logger& operator<<(char what)
181     {
182         if (_enabled)
183             _log_msg.raw << what;
184         return *this;
185     }
186  
187     //Support user types which implements operator<<
188     template<typename T>
189     line_logger& operator<<(const T& what)
190     {
191         if (_enabled)
192             _log_msg.raw.write("{}", what);
193         return *this;
194     }
195  
196  
197     void disable()
198     {
199         _enabled = false;
200     }
201  
202  
203 private:
204     logger* _callback_logger;
205     log_msg _log_msg;
206     bool _enabled;
207 };
208 } //Namespace details
209 } // Namespace spdlog