| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <chrono>
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:52:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | #include <iomanip>
 | 
					
						
							|  |  |  | #include <thread>
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							| 
									
										
										
										
											2014-05-08 02:23:07 +03:00
										 |  |  | #include <sstream>
 | 
					
						
							| 
									
										
										
										
											2014-05-09 15:27:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | #include "common_types.h"
 | 
					
						
							|  |  |  | #include "details/os.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-09 15:27:06 +03:00
										 |  |  | #include "details/log_msg.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  | #include "details/fast_oss.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-08 02:23:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace c11log | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | namespace formatters | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class formatter | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-05-09 18:00:10 +03:00
										 |  |  |     virtual void format(details::log_msg& msg) = 0; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class default_formatter: public formatter | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     // Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body
 | 
					
						
							| 
									
										
										
										
											2014-05-09 18:00:10 +03:00
										 |  |  |     void format(details::log_msg& msg) override | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  |         details::fast_oss oss; | 
					
						
							| 
									
										
										
										
											2014-05-09 15:27:06 +03:00
										 |  |  |         _format_time(msg.time, oss); | 
					
						
							| 
									
										
										
										
											2014-05-13 01:41:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 18:00:10 +03:00
										 |  |  |         if(!msg.logger_name.empty()) | 
					
						
							|  |  |  |             oss << " [" <<  msg.logger_name << ':' << c11log::level::to_str(msg.level) << "] "; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2014-05-09 15:27:06 +03:00
										 |  |  |             oss << " [" << c11log::level::to_str(msg.level) << "] "; | 
					
						
							| 
									
										
										
										
											2014-05-13 01:41:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 15:27:06 +03:00
										 |  |  |         oss << msg.raw << details::os::eol(); | 
					
						
							|  |  |  |         msg.formatted = oss.str(); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2014-05-09 15:27:06 +03:00
										 |  |  |     void _format_time(const log_clock::time_point& tp, std::ostream& output); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } //namespace formatter
 | 
					
						
							|  |  |  | } //namespace c11log
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Format datetime like this: [2014-03-14 17:15:22]
 | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  | inline void c11log::formatters::default_formatter::_format_time(const log_clock::time_point& tp, std::ostream &output) | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     using namespace c11log::details::os; | 
					
						
							|  |  |  |     using namespace std::chrono; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 15:27:06 +03:00
										 |  |  | #ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword    
 | 
					
						
							|  |  |  |     __declspec(thread) static char s_cache_timestr[128]; | 
					
						
							|  |  |  |     __declspec(thread) static int s_cache_timesize = 0; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     __declspec(thread) static std::time_t s_cache_time_t = 0; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2014-05-09 15:27:06 +03:00
										 |  |  |     thread_local static char s_cache_timestr[128]; | 
					
						
							|  |  |  |     thread_local static int s_cache_timesize = 0; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     thread_local static std::time_t s_cache_time_t = 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //Cache every second
 | 
					
						
							|  |  |  |     std::time_t tp_time_t = log_clock::to_time_t(tp); | 
					
						
							|  |  |  |     if(tp_time_t != s_cache_time_t) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto tm_now = details::os::localtime(tp_time_t); | 
					
						
							| 
									
										
										
										
											2014-05-08 02:23:07 +03:00
										 |  |  |         std::ostringstream time_oss; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |         time_oss.fill('0'); | 
					
						
							|  |  |  |         time_oss << '[' << tm_now.tm_year + 1900 << '-'; | 
					
						
							|  |  |  |         time_oss.width(2); | 
					
						
							|  |  |  |         time_oss << tm_now.tm_mon + 1 << '-'; | 
					
						
							|  |  |  |         time_oss.width(2); | 
					
						
							|  |  |  |         time_oss << tm_now.tm_mday << ' '; | 
					
						
							|  |  |  |         time_oss.width(2); | 
					
						
							|  |  |  |         time_oss << tm_now.tm_hour << ':'; | 
					
						
							|  |  |  |         time_oss.width(2); | 
					
						
							|  |  |  |         time_oss << tm_now.tm_min << ':'; | 
					
						
							|  |  |  |         time_oss.width(2); | 
					
						
							|  |  |  |         time_oss << tm_now.tm_sec << ']'; | 
					
						
							|  |  |  |         //Cache the resulted string and its size
 | 
					
						
							|  |  |  |         s_cache_time_t = tp_time_t; | 
					
						
							| 
									
										
										
										
											2014-05-09 15:27:06 +03:00
										 |  |  |         const std::string s = time_oss.str(); | 
					
						
							|  |  |  |         s_cache_timesize = s.size(); | 
					
						
							|  |  |  |         std::memcpy(s_cache_timestr, s.c_str(), s_cache_timesize); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-05-09 15:27:06 +03:00
										 |  |  |     output.write(s_cache_timestr, s_cache_timesize); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | } |