format
This commit is contained in:
		
							parent
							
								
									80f19d7136
								
							
						
					
					
						commit
						0d7a1d1ef9
					
				| @ -25,8 +25,8 @@ int main(int argc, char *argv[]) | ||||
|         thread_count = std::atoi(argv[1]); | ||||
| 
 | ||||
|     int howmany = 1000000; | ||||
| 	spdlog::init_thread_pool (howmany, 1); | ||||
|      | ||||
|     spdlog::init_thread_pool(howmany, 1); | ||||
| 
 | ||||
|     auto logger = spdlog::create_async_logger<spdlog::sinks::simple_file_sink_mt>("file_logger", "logs/spdlog-bench-async.log", false); | ||||
|     logger->set_pattern("[%Y-%m-%d %T.%F]: %L %t %v"); | ||||
| 
 | ||||
|  | ||||
| @ -10,8 +10,8 @@ | ||||
| #include <thread> | ||||
| #include <vector> | ||||
| 
 | ||||
| #include "spdlog/spdlog.h" | ||||
| #include "spdlog/sinks/simple_file_sink.h" | ||||
| #include "spdlog/spdlog.h" | ||||
| 
 | ||||
| using namespace std; | ||||
| 
 | ||||
|  | ||||
| @ -6,8 +6,8 @@ | ||||
| #include <chrono> | ||||
| #include <iostream> | ||||
| 
 | ||||
| #include "spdlog/spdlog.h" | ||||
| #include "spdlog/sinks/simple_file_sink.h" | ||||
| #include "spdlog/spdlog.h" | ||||
| 
 | ||||
| int main(int, char *[]) | ||||
| { | ||||
|  | ||||
| @ -82,7 +82,7 @@ int main(int, char *[]) | ||||
| 
 | ||||
|         // Asynchronous logging is very fast..
 | ||||
|         // Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous..
 | ||||
|         //async_example();
 | ||||
|         // async_example();
 | ||||
| 
 | ||||
|         // Log user-defined types example
 | ||||
|         user_defined_example(); | ||||
| @ -107,23 +107,22 @@ int main(int, char *[]) | ||||
| #include "spdlog/async.h" | ||||
| void async_example() | ||||
| { | ||||
|     //auto async_file = spd::basic_logger_mt<spdlog::create_async>("async_file_logger", "logs/async_log.txt");
 | ||||
| 	 | ||||
| 	for (int j = 0; j < 1; j++) | ||||
| 	{ | ||||
| 		spdlog::init_thread_pool(1024, 10); | ||||
| 		auto async_file = spd::stderr_color_mt<spdlog::create_async>("console"); | ||||
| 		for (int i = 0; i < 1024; ++i) | ||||
| 		{ | ||||
| 			async_file->info("{} Async message #{}", j, i); | ||||
| 		} | ||||
| 		spdlog::drop_all(); | ||||
| 	} | ||||
| 	//std::this_thread::sleep_for(std::chrono::seconds(1));
 | ||||
| 		 | ||||
|     // auto async_file = spd::basic_logger_mt<spdlog::create_async>("async_file_logger", "logs/async_log.txt");
 | ||||
| 
 | ||||
|     for (int j = 0; j < 1; j++) | ||||
|     { | ||||
|         spdlog::init_thread_pool(1024, 10); | ||||
|         auto async_file = spd::stderr_color_mt<spdlog::create_async>("console"); | ||||
|         for (int i = 0; i < 1024; ++i) | ||||
|         { | ||||
|             async_file->info("{} Async message #{}", j, i); | ||||
|         } | ||||
|         spdlog::drop_all(); | ||||
|     } | ||||
|     // std::this_thread::sleep_for(std::chrono::seconds(1));
 | ||||
| 
 | ||||
|     // you can also modify thread pool settings *before* creating the logger:
 | ||||
|     // spdlog::init_thread_pool(32768, 4); // queue with max 32k items 4 backing threads.    
 | ||||
|     // spdlog::init_thread_pool(32768, 4); // queue with max 32k items 4 backing threads.
 | ||||
| } | ||||
| 
 | ||||
| // syslog example (linux/osx/freebsd)
 | ||||
|  | ||||
| @ -179,7 +179,13 @@ using filename_t = std::wstring; | ||||
| using filename_t = std::string; | ||||
| #endif | ||||
| 
 | ||||
| 
 | ||||
| #define SPDLOG_CATCH_AND_HANDLE catch (const std::exception &ex) {_err_handler(ex.what());}\ | ||||
| 								catch (...) {_err_handler("Unknown exeption in logger");} | ||||
| #define SPDLOG_CATCH_AND_HANDLE                                                                                                            \ | ||||
|     catch (const std::exception &ex)                                                                                                       \ | ||||
|     {                                                                                                                                      \ | ||||
|         _err_handler(ex.what());                                                                                                           \ | ||||
|     }                                                                                                                                      \ | ||||
|     catch (...)                                                                                                                            \ | ||||
|     {                                                                                                                                      \ | ||||
|         _err_handler("Unknown exeption in logger");                                                                                        \ | ||||
|     } | ||||
| } // namespace spdlog
 | ||||
|  | ||||
| @ -80,7 +80,7 @@ inline void spdlog::async_logger::_backend_log(details::log_msg &incoming_log_ms | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 	SPDLOG_CATCH_AND_HANDLE | ||||
|     SPDLOG_CATCH_AND_HANDLE | ||||
| 
 | ||||
|     if (_should_flush(incoming_log_msg)) | ||||
|     { | ||||
| @ -97,5 +97,5 @@ inline void spdlog::async_logger::_backend_flush() | ||||
|             sink->flush(); | ||||
|         } | ||||
|     } | ||||
| 	SPDLOG_CATCH_AND_HANDLE | ||||
|     SPDLOG_CATCH_AND_HANDLE | ||||
| } | ||||
|  | ||||
| @ -66,7 +66,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Ar | ||||
| #endif | ||||
|         _sink_it(log_msg); | ||||
|     } | ||||
| 	SPDLOG_CATCH_AND_HANDLE | ||||
|     SPDLOG_CATCH_AND_HANDLE | ||||
| } | ||||
| 
 | ||||
| template<typename... Args> | ||||
| @ -82,7 +82,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg) | ||||
|         log_msg.raw << msg; | ||||
|         _sink_it(log_msg); | ||||
|     } | ||||
| 	SPDLOG_CATCH_AND_HANDLE | ||||
|     SPDLOG_CATCH_AND_HANDLE | ||||
| } | ||||
| 
 | ||||
| template<typename T> | ||||
| @ -98,7 +98,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg) | ||||
|         log_msg.raw << msg; | ||||
|         _sink_it(log_msg); | ||||
|     } | ||||
| 	SPDLOG_CATCH_AND_HANDLE | ||||
|     SPDLOG_CATCH_AND_HANDLE | ||||
| } | ||||
| 
 | ||||
| template<typename Arg1, typename... Args> | ||||
| @ -257,11 +257,11 @@ inline spdlog::log_err_handler spdlog::logger::error_handler() | ||||
| 
 | ||||
| inline void spdlog::logger::flush() | ||||
| { | ||||
| 	try  | ||||
| 	{ | ||||
| 		_flush(); | ||||
| 	} | ||||
| 	SPDLOG_CATCH_AND_HANDLE | ||||
|     try | ||||
|     { | ||||
|         _flush(); | ||||
|     } | ||||
|     SPDLOG_CATCH_AND_HANDLE | ||||
| } | ||||
| 
 | ||||
| inline void spdlog::logger::flush_on(level::level_enum log_level) | ||||
|  | ||||
| @ -10,197 +10,195 @@ | ||||
| #include <vector> | ||||
| 
 | ||||
| namespace spdlog { | ||||
| 	namespace details { | ||||
| namespace details { | ||||
| 
 | ||||
| 		using async_logger_ptr = std::shared_ptr<spdlog::async_logger>; | ||||
| using async_logger_ptr = std::shared_ptr<spdlog::async_logger>; | ||||
| 
 | ||||
| 		enum class async_msg_type | ||||
| 		{ | ||||
| 			log, | ||||
| 			flush, | ||||
| 			terminate | ||||
| 		}; | ||||
| enum class async_msg_type | ||||
| { | ||||
|     log, | ||||
|     flush, | ||||
|     terminate | ||||
| }; | ||||
| 
 | ||||
| 		// Async msg to move to/from the queue
 | ||||
| 		// Movable only. should never be copied
 | ||||
| 		struct async_msg | ||||
| 		{ | ||||
| 			async_msg_type msg_type; | ||||
| 			level::level_enum level; | ||||
| 			log_clock::time_point time; | ||||
| 			size_t thread_id; | ||||
| 			std::string txt; | ||||
| // Async msg to move to/from the queue
 | ||||
| // Movable only. should never be copied
 | ||||
| struct async_msg | ||||
| { | ||||
|     async_msg_type msg_type; | ||||
|     level::level_enum level; | ||||
|     log_clock::time_point time; | ||||
|     size_t thread_id; | ||||
|     std::string txt; | ||||
| 
 | ||||
| 			size_t msg_id; | ||||
| 			async_logger_ptr worker_ptr; | ||||
|     size_t msg_id; | ||||
|     async_logger_ptr worker_ptr; | ||||
| 
 | ||||
| 			async_msg() = default; | ||||
| 			~async_msg() = default; | ||||
|     async_msg() = default; | ||||
|     ~async_msg() = default; | ||||
| 
 | ||||
| 			// should only be moved in or out of the queue..
 | ||||
| 			async_msg(const async_msg &) = delete; | ||||
| 			async_msg(async_msg &&other) = default; | ||||
| 			async_msg &operator=(async_msg &&other) = default; | ||||
|     // should only be moved in or out of the queue..
 | ||||
|     async_msg(const async_msg &) = delete; | ||||
|     async_msg(async_msg &&other) = default; | ||||
|     async_msg &operator=(async_msg &&other) = default; | ||||
| 
 | ||||
| 			// construct from log_msg with given type
 | ||||
| 			async_msg(async_logger_ptr &&worker, async_msg_type the_type, details::log_msg &&m) | ||||
| 				: msg_type(the_type) | ||||
| 				, level(m.level) | ||||
| 				, time(m.time) | ||||
| 				, thread_id(m.thread_id) | ||||
| 				, txt(m.raw.data(), m.raw.size()) | ||||
| 				, msg_id(m.msg_id) | ||||
| 				, worker_ptr(std::forward<async_logger_ptr>(worker)) | ||||
| 			{ | ||||
| 			} | ||||
|     // construct from log_msg with given type
 | ||||
|     async_msg(async_logger_ptr &&worker, async_msg_type the_type, details::log_msg &&m) | ||||
|         : msg_type(the_type) | ||||
|         , level(m.level) | ||||
|         , time(m.time) | ||||
|         , thread_id(m.thread_id) | ||||
|         , txt(m.raw.data(), m.raw.size()) | ||||
|         , msg_id(m.msg_id) | ||||
|         , worker_ptr(std::forward<async_logger_ptr>(worker)) | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
| 			async_msg(async_logger_ptr &&worker, async_msg_type the_type) | ||||
| 				: async_msg(std::forward<async_logger_ptr>(worker), the_type, details::log_msg()) | ||||
| 			{ | ||||
| 			} | ||||
|     async_msg(async_logger_ptr &&worker, async_msg_type the_type) | ||||
|         : async_msg(std::forward<async_logger_ptr>(worker), the_type, details::log_msg()) | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
| 			async_msg(async_msg_type the_type) | ||||
| 				: async_msg(nullptr, the_type, details::log_msg()) | ||||
| 			{ | ||||
| 			} | ||||
|     async_msg(async_msg_type the_type) | ||||
|         : async_msg(nullptr, the_type, details::log_msg()) | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
| 			// copy into log_msg
 | ||||
| 			void to_log_msg(log_msg &&msg) | ||||
| 			{ | ||||
| 				msg.logger_name = &worker_ptr->name(); | ||||
| 				msg.level = level; | ||||
| 				msg.time = time; | ||||
| 				msg.thread_id = thread_id; | ||||
| 				msg.raw.clear(); | ||||
| 				msg.raw << txt; | ||||
| 				msg.formatted.clear(); | ||||
| 				msg.msg_id = msg_id; | ||||
| 				msg.color_range_start = 0; | ||||
| 				msg.color_range_end = 0; | ||||
| 			} | ||||
| 		}; | ||||
|     // copy into log_msg
 | ||||
|     void to_log_msg(log_msg &&msg) | ||||
|     { | ||||
|         msg.logger_name = &worker_ptr->name(); | ||||
|         msg.level = level; | ||||
|         msg.time = time; | ||||
|         msg.thread_id = thread_id; | ||||
|         msg.raw.clear(); | ||||
|         msg.raw << txt; | ||||
|         msg.formatted.clear(); | ||||
|         msg.msg_id = msg_id; | ||||
|         msg.color_range_start = 0; | ||||
|         msg.color_range_end = 0; | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| 		class thread_pool | ||||
| 		{ | ||||
| 		public: | ||||
| 			using item_type = async_msg; | ||||
| 			using q_type = details::mpmc_blocking_queue<item_type>; | ||||
| 			using clock_type = std::chrono::steady_clock; | ||||
| class thread_pool | ||||
| { | ||||
| public: | ||||
|     using item_type = async_msg; | ||||
|     using q_type = details::mpmc_blocking_queue<item_type>; | ||||
|     using clock_type = std::chrono::steady_clock; | ||||
| 
 | ||||
| 			thread_pool(size_t q_size_bytes, size_t threads_n) | ||||
| 				: msg_counter_(0) | ||||
| 				, _q(q_size_bytes) | ||||
| 			{ | ||||
| 				// std::cout << "thread_pool()  q_size_bytes: " << q_size_bytes << "\tthreads_n: " << threads_n << std::endl;
 | ||||
| 				if (threads_n == 0 || threads_n > 1000) | ||||
| 				{ | ||||
| 					throw spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid range is 1-1000)"); | ||||
| 				} | ||||
| 				for (size_t i = 0; i < threads_n; i++) | ||||
| 				{ | ||||
| 					_threads.emplace_back(std::bind(&thread_pool::worker_loop, this)); | ||||
| 				} | ||||
| 			} | ||||
|     thread_pool(size_t q_size_bytes, size_t threads_n) | ||||
|         : msg_counter_(0) | ||||
|         , _q(q_size_bytes) | ||||
|     { | ||||
|         // std::cout << "thread_pool()  q_size_bytes: " << q_size_bytes << "\tthreads_n: " << threads_n << std::endl;
 | ||||
|         if (threads_n == 0 || threads_n > 1000) | ||||
|         { | ||||
|             throw spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid range is 1-1000)"); | ||||
|         } | ||||
|         for (size_t i = 0; i < threads_n; i++) | ||||
|         { | ||||
|             _threads.emplace_back(std::bind(&thread_pool::worker_loop, this)); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 			// message all threads to terminate gracefully join them
 | ||||
| 			~thread_pool() | ||||
| 			{ | ||||
| 				try | ||||
| 				{ | ||||
| 					for (size_t i = 0; i < _threads.size(); i++) | ||||
| 					{ | ||||
| 						post_async_msg(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry); | ||||
| 					} | ||||
|     // message all threads to terminate gracefully join them
 | ||||
|     ~thread_pool() | ||||
|     { | ||||
|         try | ||||
|         { | ||||
|             for (size_t i = 0; i < _threads.size(); i++) | ||||
|             { | ||||
|                 post_async_msg(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry); | ||||
|             } | ||||
| 
 | ||||
| 					for (auto &t : _threads) | ||||
| 					{ | ||||
| 						t.join(); | ||||
| 					} | ||||
| 					// std::cout << "~thread_pool()  msg_counter_: " << msg_counter_ << std::endl;
 | ||||
| 				} | ||||
| 				catch (...) | ||||
| 				{ | ||||
| 				} | ||||
| 			} | ||||
|             for (auto &t : _threads) | ||||
|             { | ||||
|                 t.join(); | ||||
|             } | ||||
|             // std::cout << "~thread_pool()  msg_counter_: " << msg_counter_ << std::endl;
 | ||||
|         } | ||||
|         catch (...) | ||||
|         { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 			void post_log(async_logger_ptr &&worker_ptr, details::log_msg &&msg, async_overflow_policy overflow_policy) | ||||
| 			{ | ||||
| 				async_msg async_m(std::forward<async_logger_ptr>(worker_ptr), async_msg_type::log, std::forward<log_msg>(msg)); | ||||
| 				post_async_msg(std::move(async_m), overflow_policy); | ||||
| 			} | ||||
|     void post_log(async_logger_ptr &&worker_ptr, details::log_msg &&msg, async_overflow_policy overflow_policy) | ||||
|     { | ||||
|         async_msg async_m(std::forward<async_logger_ptr>(worker_ptr), async_msg_type::log, std::forward<log_msg>(msg)); | ||||
|         post_async_msg(std::move(async_m), overflow_policy); | ||||
|     } | ||||
| 
 | ||||
| 			void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy) | ||||
| 			{ | ||||
| 				post_async_msg(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy); | ||||
| 			} | ||||
|     void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy) | ||||
|     { | ||||
|         post_async_msg(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy); | ||||
|     } | ||||
| 
 | ||||
| 			size_t msg_counter() | ||||
| 			{ | ||||
| 				return msg_counter_.load(std::memory_order_relaxed); | ||||
| 			} | ||||
|     size_t msg_counter() | ||||
|     { | ||||
|         return msg_counter_.load(std::memory_order_relaxed); | ||||
|     } | ||||
| 
 | ||||
| 		private: | ||||
| 			std::atomic<size_t> msg_counter_; // total # of messages processed in this pool
 | ||||
| 			q_type _q; | ||||
| private: | ||||
|     std::atomic<size_t> msg_counter_; // total # of messages processed in this pool
 | ||||
|     q_type _q; | ||||
| 
 | ||||
| 			std::vector<std::thread> _threads; | ||||
|     std::vector<std::thread> _threads; | ||||
| 
 | ||||
| 			void post_async_msg(async_msg &&new_msg, async_overflow_policy overflow_policy) | ||||
| 			{ | ||||
| 				if (overflow_policy == async_overflow_policy::block_retry) | ||||
| 				{ | ||||
| 					_q.enqueue(std::move(new_msg)); | ||||
| 				} | ||||
| 				else | ||||
| 				{ | ||||
| 					_q.enqueue_nowait(std::move(new_msg)); | ||||
| 				}				 | ||||
| 			} | ||||
|     void post_async_msg(async_msg &&new_msg, async_overflow_policy overflow_policy) | ||||
|     { | ||||
|         if (overflow_policy == async_overflow_policy::block_retry) | ||||
|         { | ||||
|             _q.enqueue(std::move(new_msg)); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             _q.enqueue_nowait(std::move(new_msg)); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 			void worker_loop() | ||||
| 			{ | ||||
| 				while (process_next_msg()) | ||||
| 				{ | ||||
| 				}; | ||||
| 			} | ||||
|     void worker_loop() | ||||
|     { | ||||
|         while (process_next_msg()) {}; | ||||
|     } | ||||
| 
 | ||||
| 			// process next message in the queue
 | ||||
| 			// return true if this thread should still be active (while no terminate msg was received)
 | ||||
| 			bool process_next_msg() | ||||
| 			{ | ||||
| 				async_msg incoming_async_msg; | ||||
| 				bool dequeued = _q.dequeue_for(incoming_async_msg, std::chrono::seconds(10)); | ||||
| 				if (!dequeued) | ||||
| 				{ | ||||
| 					return true; | ||||
| 				} | ||||
|     // process next message in the queue
 | ||||
|     // return true if this thread should still be active (while no terminate msg was received)
 | ||||
|     bool process_next_msg() | ||||
|     { | ||||
|         async_msg incoming_async_msg; | ||||
|         bool dequeued = _q.dequeue_for(incoming_async_msg, std::chrono::seconds(10)); | ||||
|         if (!dequeued) | ||||
|         { | ||||
|             return true; | ||||
|         } | ||||
| 
 | ||||
| 				switch (incoming_async_msg.msg_type) | ||||
| 				{ | ||||
| 					case async_msg_type::flush: | ||||
| 					{ | ||||
| 						incoming_async_msg.worker_ptr->_backend_flush(); | ||||
| 						return true; | ||||
| 					} | ||||
|         switch (incoming_async_msg.msg_type) | ||||
|         { | ||||
|         case async_msg_type::flush: | ||||
|         { | ||||
|             incoming_async_msg.worker_ptr->_backend_flush(); | ||||
|             return true; | ||||
|         } | ||||
| 
 | ||||
| 					case async_msg_type::terminate: | ||||
| 					{ | ||||
| 						return false; | ||||
| 					} | ||||
|         case async_msg_type::terminate: | ||||
|         { | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
| 					default: | ||||
| 					{ | ||||
| 						log_msg msg; | ||||
| 						incoming_async_msg.to_log_msg(std::move(msg)); | ||||
| 						incoming_async_msg.worker_ptr->_backend_log(msg); | ||||
| 						msg_counter_.fetch_add(1, std::memory_order_relaxed); | ||||
| 						return true; | ||||
| 					} | ||||
| 				} | ||||
| 				assert(false); | ||||
| 				return true; // should not be reached
 | ||||
| 			} | ||||
| 		}; | ||||
|         default: | ||||
|         { | ||||
|             log_msg msg; | ||||
|             incoming_async_msg.to_log_msg(std::move(msg)); | ||||
|             incoming_async_msg.worker_ptr->_backend_log(msg); | ||||
|             msg_counter_.fetch_add(1, std::memory_order_relaxed); | ||||
|             return true; | ||||
|         } | ||||
|         } | ||||
|         assert(false); | ||||
|         return true; // should not be reached
 | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| 	} // namespace details
 | ||||
| } // namespace details
 | ||||
| } // namespace spdlog
 | ||||
|  | ||||
| @ -13,14 +13,14 @@ class failing_sink : public spdlog::sinks::sink | ||||
|     } | ||||
| 
 | ||||
|     void flush() override | ||||
| 	{ | ||||
| 		throw std::runtime_error("some error happened during flush"); | ||||
| 	} | ||||
|     { | ||||
|         throw std::runtime_error("some error happened during flush"); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| TEST_CASE("default_error_handler", "[errors]]") | ||||
| { | ||||
| 	spdlog::drop_all(); | ||||
|     spdlog::drop_all(); | ||||
|     prepare_logdir(); | ||||
|     std::string filename = "logs/simple_log.txt"; | ||||
| 
 | ||||
| @ -44,7 +44,7 @@ struct custom_ex | ||||
| }; | ||||
| TEST_CASE("custom_error_handler", "[errors]]") | ||||
| { | ||||
| 	spdlog::drop_all(); | ||||
|     spdlog::drop_all(); | ||||
|     prepare_logdir(); | ||||
|     std::string filename = "logs/simple_log.txt"; | ||||
|     auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true); | ||||
| @ -62,7 +62,7 @@ TEST_CASE("custom_error_handler", "[errors]]") | ||||
| 
 | ||||
| TEST_CASE("default_error_handler2", "[errors]]") | ||||
| { | ||||
| 	spdlog::drop_all(); | ||||
|     spdlog::drop_all(); | ||||
|     auto logger = spdlog::create<failing_sink>("failed_logger"); | ||||
|     logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); | ||||
|     REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex); | ||||
| @ -70,15 +70,15 @@ TEST_CASE("default_error_handler2", "[errors]]") | ||||
| 
 | ||||
| TEST_CASE("flush_error_handler", "[errors]]") | ||||
| { | ||||
| 	spdlog::drop_all(); | ||||
| 	auto logger = spdlog::create<failing_sink>("failed_logger"); | ||||
| 	logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); | ||||
| 	REQUIRE_THROWS_AS(logger->flush(), custom_ex); | ||||
|     spdlog::drop_all(); | ||||
|     auto logger = spdlog::create<failing_sink>("failed_logger"); | ||||
|     logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); | ||||
|     REQUIRE_THROWS_AS(logger->flush(), custom_ex); | ||||
| } | ||||
| 
 | ||||
| TEST_CASE("async_error_handler", "[errors]]") | ||||
| { | ||||
| 	spdlog::drop_all(); | ||||
|     spdlog::drop_all(); | ||||
|     prepare_logdir(); | ||||
|     std::string err_msg("log failed with some msg"); | ||||
| 
 | ||||
| @ -109,7 +109,7 @@ TEST_CASE("async_error_handler", "[errors]]") | ||||
| // Make sure async error handler is executed
 | ||||
| TEST_CASE("async_error_handler2", "[errors]]") | ||||
| { | ||||
| 	spdlog::drop_all(); | ||||
|     spdlog::drop_all(); | ||||
|     prepare_logdir(); | ||||
|     std::string err_msg("This is async handler error message"); | ||||
|     { | ||||
|  | ||||
| @ -50,7 +50,7 @@ TEST_CASE("flush_on", "[flush_on]]") | ||||
| TEST_CASE("rotating_file_logger1", "[rotating_logger]]") | ||||
| { | ||||
|     prepare_logdir(); | ||||
| 	size_t max_size = 1024 * 10; | ||||
|     size_t max_size = 1024 * 10; | ||||
|     std::string basename = "logs/rotating_log"; | ||||
|     auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0); | ||||
| 
 | ||||
| @ -71,7 +71,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]") | ||||
| TEST_CASE("rotating_file_logger2", "[rotating_logger]]") | ||||
| { | ||||
|     prepare_logdir(); | ||||
| 	size_t max_size = 1024 * 10; | ||||
|     size_t max_size = 1024 * 10; | ||||
|     std::string basename = "logs/rotating_log"; | ||||
|     auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 1); | ||||
|     for (int i = 0; i < 10; ++i) | ||||
|  | ||||
| @ -1,9 +1,9 @@ | ||||
| #include "includes.h" | ||||
| #include "test_sink.h" | ||||
| #include "spdlog/async.h" | ||||
| #include "spdlog/sinks/simple_file_sink.h" | ||||
| #include "test_sink.h" | ||||
| 
 | ||||
| //std::unique_ptr<spdlog::async_logger> create_logger(size_t tp_queue_size, size_t tp_threads)
 | ||||
| // std::unique_ptr<spdlog::async_logger> create_logger(size_t tp_queue_size, size_t tp_threads)
 | ||||
| //{
 | ||||
| //	auto tp = std::make_shared<details::thread_pool>(8192, 1);
 | ||||
| //	auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry);
 | ||||
| @ -11,124 +11,122 @@ | ||||
| 
 | ||||
| TEST_CASE("basic async test ", "[async]") | ||||
| { | ||||
| 	using namespace spdlog; | ||||
| 	auto test_sink = std::make_shared<sinks::test_sink_mt>(); | ||||
| 	size_t queue_size = 128; | ||||
| 	size_t messages = 256; | ||||
| 	{ | ||||
| 		auto tp = std::make_shared<details::thread_pool>(queue_size, 1); | ||||
| 		auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry); | ||||
| 		for (size_t i = 0; i < messages; i++) | ||||
| 		{ | ||||
| 			logger->info("Hello message #{}", i); | ||||
| 		} | ||||
| 		logger->flush(); | ||||
| 	} | ||||
| 	REQUIRE(test_sink->msg_counter() == messages); | ||||
| 	REQUIRE(test_sink->flushed_msg_counter() == messages); | ||||
|     using namespace spdlog; | ||||
|     auto test_sink = std::make_shared<sinks::test_sink_mt>(); | ||||
|     size_t queue_size = 128; | ||||
|     size_t messages = 256; | ||||
|     { | ||||
|         auto tp = std::make_shared<details::thread_pool>(queue_size, 1); | ||||
|         auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry); | ||||
|         for (size_t i = 0; i < messages; i++) | ||||
|         { | ||||
|             logger->info("Hello message #{}", i); | ||||
|         } | ||||
|         logger->flush(); | ||||
|     } | ||||
|     REQUIRE(test_sink->msg_counter() == messages); | ||||
|     REQUIRE(test_sink->flushed_msg_counter() == messages); | ||||
| } | ||||
| 
 | ||||
| TEST_CASE("discard policy ", "[async]") | ||||
| { | ||||
| 	using namespace spdlog; | ||||
| 	auto test_sink = std::make_shared<sinks::test_sink_mt>(); | ||||
| 	size_t queue_size = 2; | ||||
| 	size_t messages = 1024; | ||||
| 	{ | ||||
| 		auto tp = std::make_shared<details::thread_pool>(queue_size, 1); | ||||
| 		auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::discard_log_msg); | ||||
| 		for (size_t i = 0; i < messages; i++) | ||||
| 		{ | ||||
| 			logger->info("Hello message #{}", i); | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	REQUIRE(test_sink->msg_counter() < messages); | ||||
| 	REQUIRE(test_sink->flushed_msg_counter() < messages); | ||||
|     using namespace spdlog; | ||||
|     auto test_sink = std::make_shared<sinks::test_sink_mt>(); | ||||
|     size_t queue_size = 2; | ||||
|     size_t messages = 1024; | ||||
|     { | ||||
|         auto tp = std::make_shared<details::thread_pool>(queue_size, 1); | ||||
|         auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::discard_log_msg); | ||||
|         for (size_t i = 0; i < messages; i++) | ||||
|         { | ||||
|             logger->info("Hello message #{}", i); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     REQUIRE(test_sink->msg_counter() < messages); | ||||
|     REQUIRE(test_sink->flushed_msg_counter() < messages); | ||||
| } | ||||
| 
 | ||||
| TEST_CASE("flush", "[async]") | ||||
| { | ||||
| 	using namespace spdlog; | ||||
| 	auto test_sink = std::make_shared<sinks::test_sink_mt>(); | ||||
| 	size_t queue_size = 256; | ||||
| 	size_t messages = 256; | ||||
| 	{ | ||||
| 		auto tp = std::make_shared<details::thread_pool>(queue_size, 1); | ||||
| 		auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry); | ||||
| 		for (size_t i = 0; i < messages; i++) | ||||
| 		{ | ||||
| 			logger->info("Hello message #{}", i); | ||||
| 		} | ||||
| 		 | ||||
| 		logger->flush(); | ||||
| 	} | ||||
| 	std::this_thread::sleep_for(std::chrono::milliseconds(250)); | ||||
| 	REQUIRE(test_sink->msg_counter() == messages); | ||||
| 	REQUIRE(test_sink->flushed_msg_counter() == messages);	 | ||||
|     using namespace spdlog; | ||||
|     auto test_sink = std::make_shared<sinks::test_sink_mt>(); | ||||
|     size_t queue_size = 256; | ||||
|     size_t messages = 256; | ||||
|     { | ||||
|         auto tp = std::make_shared<details::thread_pool>(queue_size, 1); | ||||
|         auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry); | ||||
|         for (size_t i = 0; i < messages; i++) | ||||
|         { | ||||
|             logger->info("Hello message #{}", i); | ||||
|         } | ||||
| 
 | ||||
|         logger->flush(); | ||||
|     } | ||||
|     std::this_thread::sleep_for(std::chrono::milliseconds(250)); | ||||
|     REQUIRE(test_sink->msg_counter() == messages); | ||||
|     REQUIRE(test_sink->flushed_msg_counter() == messages); | ||||
| } | ||||
| 
 | ||||
| TEST_CASE("multi threads", "[async]") | ||||
| { | ||||
| 	using namespace spdlog; | ||||
| 	auto test_sink = std::make_shared<sinks::test_sink_mt>(); | ||||
| 	size_t queue_size = 128; | ||||
| 	size_t messages = 256; | ||||
| 	size_t n_threads = 10; | ||||
| 	{ | ||||
| 		auto tp = std::make_shared<details::thread_pool>(queue_size, 1); | ||||
| 		auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry); | ||||
|     using namespace spdlog; | ||||
|     auto test_sink = std::make_shared<sinks::test_sink_mt>(); | ||||
|     size_t queue_size = 128; | ||||
|     size_t messages = 256; | ||||
|     size_t n_threads = 10; | ||||
|     { | ||||
|         auto tp = std::make_shared<details::thread_pool>(queue_size, 1); | ||||
|         auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry); | ||||
| 
 | ||||
| 		std::vector<std::thread> threads; | ||||
| 		for (size_t i = 0; i < n_threads; i++) | ||||
| 		{ | ||||
| 			threads.emplace_back([logger, messages] { | ||||
| 				for (size_t j = 0; j < messages; j++) | ||||
| 				{ | ||||
| 					logger->info("Hello message #{}", j); | ||||
| 				} | ||||
| 				 | ||||
| 			}); | ||||
| 		} | ||||
| 		 | ||||
| 		for (auto &t : threads) | ||||
| 		{ | ||||
| 			t.join(); | ||||
| 		} | ||||
| 		logger->flush(); | ||||
|         std::vector<std::thread> threads; | ||||
|         for (size_t i = 0; i < n_threads; i++) | ||||
|         { | ||||
|             threads.emplace_back([logger, messages] { | ||||
|                 for (size_t j = 0; j < messages; j++) | ||||
|                 { | ||||
|                     logger->info("Hello message #{}", j); | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
| 
 | ||||
| 	} | ||||
| 		 | ||||
| 	REQUIRE(test_sink->msg_counter() == messages * n_threads); | ||||
| 	REQUIRE(test_sink->flushed_msg_counter() == messages * n_threads); | ||||
|         for (auto &t : threads) | ||||
|         { | ||||
|             t.join(); | ||||
|         } | ||||
|         logger->flush(); | ||||
|     } | ||||
| 
 | ||||
|     REQUIRE(test_sink->msg_counter() == messages * n_threads); | ||||
|     REQUIRE(test_sink->flushed_msg_counter() == messages * n_threads); | ||||
| } | ||||
| 
 | ||||
| TEST_CASE("to_file", "[async]") | ||||
| { | ||||
| 	prepare_logdir(); | ||||
| 	size_t queue_size = 512; | ||||
| 	size_t messages = 512; | ||||
| 	size_t n_threads = 4;	 | ||||
| 	spdlog::init_thread_pool(queue_size, n_threads); | ||||
| 	auto logger= spdlog::basic_logger_mt<spdlog::create_async>("as", "logs/async_test.log", true); | ||||
| 	 | ||||
| 	std::vector<std::thread> threads; | ||||
| 	for (size_t i = 0; i < n_threads; i++) | ||||
| 	{ | ||||
| 		threads.emplace_back([logger, messages] { | ||||
| 			for (size_t j = 0; j < messages; j++) | ||||
| 			{ | ||||
| 				logger->info("Hello message #{}", j); | ||||
| 			} | ||||
| 		}); | ||||
| 	} | ||||
|     prepare_logdir(); | ||||
|     size_t queue_size = 512; | ||||
|     size_t messages = 512; | ||||
|     size_t n_threads = 4; | ||||
|     spdlog::init_thread_pool(queue_size, n_threads); | ||||
|     auto logger = spdlog::basic_logger_mt<spdlog::create_async>("as", "logs/async_test.log", true); | ||||
| 
 | ||||
| 	for (auto &t : threads) | ||||
| 	{ | ||||
| 		t.join(); | ||||
| 	}	 | ||||
| 	logger.reset(); | ||||
| 	spdlog::drop("as"); | ||||
| 	std::this_thread::sleep_for(std::chrono::seconds(1)); | ||||
| 	REQUIRE(count_lines("logs/async_test.log") == messages * n_threads); | ||||
|     std::vector<std::thread> threads; | ||||
|     for (size_t i = 0; i < n_threads; i++) | ||||
|     { | ||||
|         threads.emplace_back([logger, messages] { | ||||
|             for (size_t j = 0; j < messages; j++) | ||||
|             { | ||||
|                 logger->info("Hello message #{}", j); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     for (auto &t : threads) | ||||
|     { | ||||
|         t.join(); | ||||
|     } | ||||
|     logger.reset(); | ||||
|     spdlog::drop("as"); | ||||
|     std::this_thread::sleep_for(std::chrono::seconds(1)); | ||||
|     REQUIRE(count_lines("logs/async_test.log") == messages * n_threads); | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user