| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../common_types.h"
 | 
					
						
							|  |  |  | #include "../logger.h"
 | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  | #include "stack_oss.h"
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-31 01:06:46 +03:00
										 |  |  | // line_logger class.
 | 
					
						
							|  |  |  | // aggregates single log line (on the stack if possibe) and calls the logger upon destruction
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace c11log | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | //class logger;
 | 
					
						
							|  |  |  | namespace details | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class line_logger | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     line_logger(logger* callback_logger, level::level_enum msg_level, bool enabled): | 
					
						
							|  |  |  |         _callback_logger(callback_logger), | 
					
						
							| 
									
										
										
										
											2014-03-29 00:46:45 +03:00
										 |  |  |         _log_msg(msg_level), | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |         _oss(), | 
					
						
							| 
									
										
										
										
											2014-03-31 01:06:46 +03:00
										 |  |  |         _enabled(enabled), | 
					
						
							| 
									
										
										
										
											2014-03-31 02:31:26 +03:00
										 |  |  |         _empty(true) | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         if(enabled) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2014-03-29 01:38:05 +03:00
										 |  |  |             _log_msg.msg_time = log_clock::now(); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |             callback_logger->_formatter->format_header(callback_logger->_logger_name, | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |                     _log_msg.msg_level, | 
					
						
							| 
									
										
										
										
											2014-03-28 16:16:36 +03:00
										 |  |  |                     _log_msg.msg_time, | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |                     _oss); | 
					
						
							| 
									
										
										
										
											2014-03-28 16:16:36 +03:00
										 |  |  |             _log_msg.msg_header_size = _oss.size(); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // No copy intended. Only move
 | 
					
						
							|  |  |  |     line_logger(const line_logger& other) = delete; | 
					
						
							|  |  |  |     line_logger& operator=(const line_logger&) = delete; | 
					
						
							|  |  |  |     line_logger& operator=(line_logger&&) = delete; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     line_logger(line_logger&& other) : | 
					
						
							| 
									
										
										
										
											2014-03-22 16:37:48 +02:00
										 |  |  |         _callback_logger(other._callback_logger), | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |         _log_msg(other._log_msg), | 
					
						
							|  |  |  |         // The move ctor should only be called on start of logging line,
 | 
					
						
							|  |  |  |         // where no logging happened yet for this line so no need to copy the oss from the other
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |         _oss(), | 
					
						
							| 
									
										
										
										
											2014-03-22 16:37:48 +02:00
										 |  |  |         _enabled(other._enabled) {} | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ~line_logger() | 
					
						
							| 
									
										
										
										
											2014-03-31 01:12:49 +03:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-03-31 02:31:26 +03:00
										 |  |  |         //only if enabled and not empty
 | 
					
						
							| 
									
										
										
										
											2014-03-31 01:06:46 +03:00
										 |  |  |         if (!_empty) | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |         { | 
					
						
							|  |  |  |             _oss << os::eol(); | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |             _log_msg.msg_buf = _oss.buf(); | 
					
						
							|  |  |  |             _callback_logger->_log_it(_log_msg); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-03-29 01:38:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-31 02:31:26 +03:00
										 |  |  |     template<typename T> | 
					
						
							| 
									
										
										
										
											2014-03-31 02:09:13 +03:00
										 |  |  |     void write(const T& what) | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (_enabled) | 
					
						
							| 
									
										
										
										
											2014-03-31 02:31:26 +03:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |             _oss << what; | 
					
						
							| 
									
										
										
										
											2014-03-31 02:31:26 +03:00
										 |  |  |             _empty = false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-03-31 02:09:13 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename T> | 
					
						
							|  |  |  |     line_logger& operator<<(const T& what) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         write(what); | 
					
						
							| 
									
										
										
										
											2014-03-29 00:27:13 +03:00
										 |  |  |         return *this; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-31 02:09:13 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | private: | 
					
						
							|  |  |  |     logger* _callback_logger; | 
					
						
							| 
									
										
										
										
											2014-03-28 19:03:24 +03:00
										 |  |  |     log_msg _log_msg; | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |     details::stack_oss _oss; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     bool _enabled; | 
					
						
							| 
									
										
										
										
											2014-03-31 02:31:26 +03:00
										 |  |  |     bool _empty; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | } //Namespace details
 | 
					
						
							|  |  |  | } // Namespace c11log
 |