Added tweakme.h - enable users to enable/disable features at compile time

This commit is contained in:
gabime 2015-04-09 18:05:16 +03:00
parent 8da33db62f
commit b715378ff5
6 changed files with 97 additions and 34 deletions

View File

@ -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
{

View File

@ -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);
}
}

View File

@ -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<log_clock, typename log_clock::duration>(
@ -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<size_t>(::GetCurrentThreadId());
#elif __linux__
return syscall(SYS_gettid);
#else
return pthread_self();
return static_cast<size_t>(syscall(SYS_gettid));
#else //Default to standard C++11 (OSX and other Unix)
return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id()));
#endif
#endif //SPDLOG_NO_THREAD_ID
}
} //os

View File

@ -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<std::chrono::milliseconds>(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<unsigned int>(tm_time.tm_year + 1900) << '-'
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_mon + 1), 2, '0') << '-'
@ -430,7 +432,16 @@ class full_formatter :public flag_formatter
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_sec), 2, '0') << '.'
<< fmt::pad(static_cast<unsigned int>(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());
}
};

View File

@ -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__ <<")";
}

66
include/spdlog/tweakme.h Normal file
View File

@ -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
///////////////////////////////////////////////////////////////////////////////