mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-28 08:20:19 +08:00
Added tweakme.h - enable users to enable/disable features at compile time
This commit is contained in:
parent
8da33db62f
commit
b715378ff5
@ -36,18 +36,7 @@
|
|||||||
#define SPDLOG_NOEXCEPT throw()
|
#define SPDLOG_NOEXCEPT throw()
|
||||||
#endif
|
#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
|
namespace spdlog
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -62,9 +62,16 @@ public:
|
|||||||
{
|
{
|
||||||
if (_enabled)
|
if (_enabled)
|
||||||
{
|
{
|
||||||
|
#ifndef SPDLOG_NO_NAME
|
||||||
_log_msg.logger_name = _callback_logger->name();
|
_log_msg.logger_name = _callback_logger->name();
|
||||||
|
#endif
|
||||||
|
#ifndef SPDLOG_NO_DATETIME
|
||||||
_log_msg.time = os::now();
|
_log_msg.time = os::now();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef SPDLOG_NO_THREAD_ID
|
||||||
_log_msg.thread_id = os::thread_id();
|
_log_msg.thread_id = os::thread_id();
|
||||||
|
#endif
|
||||||
_callback_logger->_log_msg(_log_msg);
|
_callback_logger->_log_msg(_log_msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ namespace os
|
|||||||
inline spdlog::log_clock::time_point now()
|
inline spdlog::log_clock::time_point now()
|
||||||
{
|
{
|
||||||
|
|
||||||
#ifdef SPDLOG_CLOCK_COARSE
|
#if defined __linux__ && defined SPDLOG_CLOCK_COARSE
|
||||||
timespec ts;
|
timespec ts;
|
||||||
::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
|
::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
|
||||||
return std::chrono::time_point<log_clock, typename log_clock::duration>(
|
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)
|
//It exists because the std::this_thread::get_id() is much slower(espcially under VS 2013)
|
||||||
inline size_t thread_id()
|
inline size_t thread_id()
|
||||||
{
|
{
|
||||||
|
|
||||||
#ifdef SPDLOG_NO_THREAD_ID
|
|
||||||
return 0;
|
|
||||||
#else
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
return ::GetCurrentThreadId();
|
return static_cast<size_t>(::GetCurrentThreadId());
|
||||||
#elif __linux__
|
#elif __linux__
|
||||||
return syscall(SYS_gettid);
|
return static_cast<size_t>(syscall(SYS_gettid));
|
||||||
#else
|
#else //Default to standard C++11 (OSX and other Unix)
|
||||||
return pthread_self();
|
return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id()));
|
||||||
#endif
|
#endif
|
||||||
#endif //SPDLOG_NO_THREAD_ID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} //os
|
} //os
|
||||||
|
@ -405,6 +405,7 @@ class full_formatter :public flag_formatter
|
|||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
|
#ifndef SPDLOG_NO_DATETIME
|
||||||
auto duration = msg.time.time_since_epoch();
|
auto duration = msg.time.time_since_epoch();
|
||||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
|
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),
|
level::to_str(msg.level),
|
||||||
msg.raw.str());*/
|
msg.raw.str());*/
|
||||||
|
|
||||||
|
|
||||||
// Faster (albeit uglier) way to format the line (5.6 million lines/sec under 10 threads)
|
// 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) << '-'
|
msg.formatted << '[' << static_cast<unsigned int>(tm_time.tm_year + 1900) << '-'
|
||||||
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_mon + 1), 2, '0') << '-'
|
<< 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>(tm_time.tm_sec), 2, '0') << '.'
|
||||||
<< fmt::pad(static_cast<unsigned int>(millis), 3, '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());
|
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "tweakme.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
|
||||||
@ -121,16 +122,17 @@ void drop(const std::string &name);
|
|||||||
void drop_all();
|
void drop_all();
|
||||||
|
|
||||||
|
|
||||||
//
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// Macros to be display source file & line
|
// Macros to be display source file & line
|
||||||
// Trace & Debug can be switched on/off at compile time for zero cost debug statements.
|
// 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:
|
// Example:
|
||||||
// #define SPDLOG_DEBUG_ON
|
// spdlog::set_level(spdlog::level::debug);
|
||||||
// include "spdlog/spdlog.h"
|
|
||||||
// SPDLOG_DEBUG(my_logger, "Some debug message {} {}", 1, 3.2);
|
// SPDLOG_DEBUG(my_logger, "Some debug message {} {}", 1, 3.2);
|
||||||
//
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#ifdef SPDLOG_TRACE_ON
|
#ifdef SPDLOG_TRACE_ON
|
||||||
#define SPDLOG_TRACE(logger, ...) logger->trace(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")";
|
#define SPDLOG_TRACE(logger, ...) logger->trace(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")";
|
||||||
#else
|
#else
|
||||||
@ -143,13 +145,6 @@ void drop_all();
|
|||||||
#define SPDLOG_DEBUG(logger, ...)
|
#define SPDLOG_DEBUG(logger, ...)
|
||||||
#endif
|
#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
66
include/spdlog/tweakme.h
Normal 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
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
Loading…
Reference in New Issue
Block a user