From b715378ff55eb40664d215e9264407b0310ec763 Mon Sep 17 00:00:00 2001 From: gabime Date: Thu, 9 Apr 2015 18:05:16 +0300 Subject: [PATCH] Added tweakme.h - enable users to enable/disable features at compile time --- include/spdlog/common.h | 11 ---- include/spdlog/details/line_logger.h | 7 ++ include/spdlog/details/os.h | 17 ++--- .../spdlog/details/pattern_formatter_impl.h | 13 +++- include/spdlog/spdlog.h | 17 ++--- include/spdlog/tweakme.h | 66 +++++++++++++++++++ 6 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 include/spdlog/tweakme.h diff --git a/include/spdlog/common.h b/include/spdlog/common.h index b420ef82..cde5a9eb 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -36,18 +36,7 @@ #define SPDLOG_NOEXCEPT throw() #endif -// under linux, you can use the much faster CLOCK_REALTIME_COARSE clock. -// this clock is less accurate - can be off by few millis - depending on the kernel HZ -// uncomment to use it instead of the regular (and slower) clock -//#ifdef __linux__ -//#define SPDLOG_CLOCK_COARSE -//#endif - - -// uncomment if thread id logging is needed - to gain few nanos -// #define SPDLOG_NO_THREAD_ID -// namespace spdlog { diff --git a/include/spdlog/details/line_logger.h b/include/spdlog/details/line_logger.h index d037554f..3a946cb6 100644 --- a/include/spdlog/details/line_logger.h +++ b/include/spdlog/details/line_logger.h @@ -62,9 +62,16 @@ public: { if (_enabled) { +#ifndef SPDLOG_NO_NAME _log_msg.logger_name = _callback_logger->name(); +#endif +#ifndef SPDLOG_NO_DATETIME _log_msg.time = os::now(); +#endif + +#ifndef SPDLOG_NO_THREAD_ID _log_msg.thread_id = os::thread_id(); +#endif _callback_logger->_log_msg(_log_msg); } } diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index e4ad717a..4ba58cbf 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -51,7 +51,7 @@ namespace os inline spdlog::log_clock::time_point now() { -#ifdef SPDLOG_CLOCK_COARSE +#if defined __linux__ && defined SPDLOG_CLOCK_COARSE timespec ts; ::clock_gettime(CLOCK_REALTIME_COARSE, &ts); return std::chrono::time_point( @@ -175,19 +175,14 @@ inline int utc_minutes_offset(const std::tm& tm = details::os::localtime()) //It exists because the std::this_thread::get_id() is much slower(espcially under VS 2013) inline size_t thread_id() { - -#ifdef SPDLOG_NO_THREAD_ID - return 0; -#else - #ifdef _WIN32 - return ::GetCurrentThreadId(); + return static_cast(::GetCurrentThreadId()); #elif __linux__ - return syscall(SYS_gettid); -#else - return pthread_self(); + return static_cast(syscall(SYS_gettid)); +#else //Default to standard C++11 (OSX and other Unix) + return static_cast(std::hash()(std::this_thread::get_id())); #endif -#endif //SPDLOG_NO_THREAD_ID + } } //os diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 076d76ce..62965364 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -405,6 +405,7 @@ class full_formatter :public flag_formatter { void format(details::log_msg& msg, const std::tm& tm_time) override { +#ifndef SPDLOG_NO_DATETIME auto duration = msg.time.time_since_epoch(); auto millis = std::chrono::duration_cast(duration).count() % 1000; @@ -421,6 +422,7 @@ class full_formatter :public flag_formatter level::to_str(msg.level), msg.raw.str());*/ + // Faster (albeit uglier) way to format the line (5.6 million lines/sec under 10 threads) msg.formatted << '[' << static_cast(tm_time.tm_year + 1900) << '-' << fmt::pad(static_cast(tm_time.tm_mon + 1), 2, '0') << '-' @@ -430,7 +432,16 @@ class full_formatter :public flag_formatter << fmt::pad(static_cast(tm_time.tm_sec), 2, '0') << '.' << fmt::pad(static_cast(millis), 3, '0') << "] "; - msg.formatted << '[' << msg.logger_name << "] [" << level::to_str(msg.level) << "] "; +//no datetime needed +#else + (void)tm_time; +#endif; + +#ifndef SPDLOG_NO_NAME + msg.formatted << '[' << msg.logger_name << "] "; +#endif + + msg.formatted << '[' << level::to_str(msg.level) << "] "; msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size()); } }; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 85ac8205..73ea63a9 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -28,6 +28,7 @@ #pragma once +#include "tweakme.h" #include "common.h" #include "logger.h" @@ -121,16 +122,17 @@ void drop(const std::string &name); void drop_all(); -// +/////////////////////////////////////////////////////////////////////////////// // // Macros to be display source file & line // Trace & Debug can be switched on/off at compile time for zero cost debug statements. +// Uncomment SPDLOG_DEBUG_ON/SPDLOG_TRACE_ON in teakme.h to enable. // // Example: -// #define SPDLOG_DEBUG_ON -// include "spdlog/spdlog.h" +// spdlog::set_level(spdlog::level::debug); // SPDLOG_DEBUG(my_logger, "Some debug message {} {}", 1, 3.2); -// +/////////////////////////////////////////////////////////////////////////////// + #ifdef SPDLOG_TRACE_ON #define SPDLOG_TRACE(logger, ...) logger->trace(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")"; #else @@ -143,13 +145,6 @@ void drop_all(); #define SPDLOG_DEBUG(logger, ...) #endif -#define SPDLOG_INFO(logger, ...) logger->info(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")"; -#define SPDLOG_NOTICE(logger, ...) logger->notice(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")"; -#define SPDLOG_WARN(logger, ...) logger->warn(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")"; -#define SPDLOG_ERROR(logger, ...) logger->error(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")"; -#define SPDLOG_CRITICAL(logger, ...) logger->critical(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")"; -#define SPDLOG_ALERT(logger, ...) logger->alert(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")"; -#define SPDLOG_EMERG(logger, ...) logger->emerg(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")"; } diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h new file mode 100644 index 00000000..427bbd7a --- /dev/null +++ b/include/spdlog/tweakme.h @@ -0,0 +1,66 @@ +/*************************************************************************/ +/* spdlog - an extremely fast and easy to use c++11 logging library. */ +/* Copyright (c) 2014 Gabi Melman. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + + +#pragma once + +/////////////////////////////////////////////////////////////////////////////// +// Edit this file to squeeze every last drop of performance out of spdlog. +/////////////////////////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////////////////////////////////////// +// Under Linux, the much faster CLOCK_REALTIME_COARSE clock can be used. +// This clock is less accurate - can be off by dozens millis - depending on the kernel HZ +// Uncomment to use it instead of the regular (but slower) clock. +// #define SPDLOG_CLOCK_COARSE +/////////////////////////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment if date/time logging is not needed. +// This will prevent spdlog from quering the system clock on each log call. +// #define SPDLOG_NO_DATETIME +/////////////////////////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment if thread id logging is not needed (i.e. no %t in the log pattern) +// This will prevent spdlog from quering the thread id on each log call. +// #define SPDLOG_NO_THREAD_ID +/////////////////////////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment if logger name logging is not needed. +// This will prevent spdlog from copying the logger name on each log call. +// #define SPDLOG_NO_NAME +/////////////////////////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to enable the SPDLOG_DEBUG/SPDLOG_TRACE macros +// #define SPDLOG_DEBUG_ON +// #define SPDLOG_TRACE_ON +///////////////////////////////////////////////////////////////////////////////