diff --git a/detail/log.h b/detail/log.h index aff0264..297473e 100644 --- a/detail/log.h +++ b/detail/log.h @@ -6,6 +6,7 @@ // ASIO_NET_LOG_FOR_MCU 更适用于MCU环境 // ASIO_NET_LOG_NOT_EXIT_ON_FATAL FATAL默认退出程序 添加此宏将不退出 // ASIO_NET_LOG_DISABLE_ALL 关闭所有日志 +// L_O_G_DISABLE_ALL(关闭所有日志 包含所有库) // // c++11环境默认打开以下内容 // ASIO_NET_LOG_ENABLE_THREAD_SAFE 线程安全 @@ -22,7 +23,7 @@ // // 在库中使用时 // 1. 修改此文件中的`ASIO_NET_LOG`以包含库名前缀(全部替换即可) -// 2. 取消这行注释: #define ASIO_NET_LOG_IN_LIB +// 2. 取消这行注释(以屏蔽DEBUG日志): #define ASIO_NET_LOG_IN_LIB // 库中可配置项 // ASIO_NET_LOG_SHOW_DEBUG 开启ASIO_NET_LOGD的输出 // @@ -39,7 +40,7 @@ // 在库中使用时需取消注释 #define ASIO_NET_LOG_IN_LIB -#ifdef ASIO_NET_LOG_DISABLE_ALL +#if defined(ASIO_NET_LOG_DISABLE_ALL) || defined(L_O_G_DISABLE_ALL) #define ASIO_NET_LOG(fmt, ...) ((void)0) #define ASIO_NET_LOGT(tag, fmt, ...) ((void)0) @@ -91,7 +92,11 @@ #endif #endif +#ifdef __FILE_NAME__ +#define ASIO_NET_LOG_BASE_FILENAME (__FILE_NAME__) +#else #define ASIO_NET_LOG_BASE_FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) +#endif #define ASIO_NET_LOG_WITH_COLOR @@ -140,19 +145,24 @@ #endif #ifdef ASIO_NET_LOG_ENABLE_THREAD_SAFE +#ifndef L_O_G_NS_MUTEX +#define L_O_G_NS_MUTEX L_O_G_NS_MUTEX #include -struct ASIO_NET_LOG_Mutex { +// 1. struct instead of namespace, ensure single instance +struct L_O_G_NS_MUTEX { static std::mutex& mutex() { -// 1. never delete, avoid destroy before user log -// 2. static memory, avoid memory fragmentation -static char memory[sizeof(std::mutex)]; -static std::mutex& mutex = *(new (memory) std::mutex()); -return mutex; + // 2. never delete, avoid destroy before user log + // 3. static memory, avoid memory fragmentation + static char memory[sizeof(std::mutex)]; + static std::mutex& mutex = *(new (memory) std::mutex()); + return mutex; } }; -#define ASIO_NET_LOG_PRINTF_IMPL(...) \ -std::lock_guard lock(ASIO_NET_LOG_Mutex::mutex()); \ -ASIO_NET_LOG_PRINTF(__VA_ARGS__) +#endif +#define ASIO_NET_LOG_PRINTF_IMPL(...) { \ + std::lock_guard lock(L_O_G_NS_MUTEX::mutex()); \ + ASIO_NET_LOG_PRINTF(__VA_ARGS__); \ +} #else #define ASIO_NET_LOG_PRINTF_IMPL(...) ASIO_NET_LOG_PRINTF(__VA_ARGS__) #endif @@ -162,18 +172,30 @@ extern int ASIO_NET_LOG_PRINTF_IMPL(const char *fmt, ...); #endif #ifdef ASIO_NET_LOG_ENABLE_THREAD_ID -#include -#include -#include -namespace ASIO_NET_LOG { -inline std::string get_thread_id() { -std::stringstream ss; -ss << std::this_thread::get_id(); -return ss.str(); +#ifndef L_O_G_NS_GET_TID +#define L_O_G_NS_GET_TID L_O_G_NS_GET_TID +#include +#ifdef __linux__ +#include +#include +struct L_O_G_NS_GET_TID { +static inline uint32_t get_tid() { + return syscall(SYS_gettid); } +}; +#else /* for mac, bsd.. */ +#include +struct L_O_G_NS_GET_TID { +static inline uint32_t get_tid() { + uint64_t x; + pthread_threadid_np(nullptr, &x); + return (uint32_t)x; } -#define ASIO_NET_LOG_THREAD_LABEL "%s " -#define ASIO_NET_LOG_THREAD_VALUE ,ASIO_NET_LOG::get_thread_id().c_str() +}; +#endif +#endif +#define ASIO_NET_LOG_THREAD_LABEL "%u " +#define ASIO_NET_LOG_THREAD_VALUE ,L_O_G_NS_GET_TID::get_tid() #else #define ASIO_NET_LOG_THREAD_LABEL #define ASIO_NET_LOG_THREAD_VALUE @@ -182,19 +204,22 @@ return ss.str(); #ifdef ASIO_NET_LOG_ENABLE_DATE_TIME #include #include -#include -namespace ASIO_NET_LOG { -inline std::string get_time() { -auto now = std::chrono::system_clock::now(); -std::time_t time = std::chrono::system_clock::to_time_t(now); -auto ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; -std::stringstream ss; -ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S") << '.' << std::setw(3) << std::setfill('0') << ms.count(); -return ss.str(); -} +#include // std::put_time +#ifndef L_O_G_NS_GET_TIME +#define L_O_G_NS_GET_TIME L_O_G_NS_GET_TIME +struct L_O_G_NS_GET_TIME { +static inline std::string get_time() { + auto now = std::chrono::system_clock::now(); + std::time_t time = std::chrono::system_clock::to_time_t(now); + auto ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; + std::stringstream ss; + ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S") << '.' << std::setw(3) << std::setfill('0') << ms.count(); + return ss.str(); } +}; +#endif #define ASIO_NET_LOG_TIME_LABEL "%s " -#define ASIO_NET_LOG_TIME_VALUE ,ASIO_NET_LOG::get_time().c_str() +#define ASIO_NET_LOG_TIME_VALUE ,L_O_G_NS_GET_TIME::get_time().c_str() #else #define ASIO_NET_LOG_TIME_LABEL #define ASIO_NET_LOG_TIME_VALUE diff --git a/rpc/rpc_core b/rpc/rpc_core index cb612e8..cfec24d 160000 --- a/rpc/rpc_core +++ b/rpc/rpc_core @@ -1 +1 @@ -Subproject commit cb612e871802b30dd815cfeed082e20969aa0901 +Subproject commit cfec24d5b2c39d852b0873e0cc81d856ca8d7930 diff --git a/test/log.h b/test/log.h index f5f01a4..31a3b00 100644 --- a/test/log.h +++ b/test/log.h @@ -5,6 +5,8 @@ // LOG_LINE_END_CRLF 默认是\n结尾 添加此宏将以\r\n结尾 // LOG_FOR_MCU 更适用于MCU环境 // LOG_NOT_EXIT_ON_FATAL FATAL默认退出程序 添加此宏将不退出 +// LOG_DISABLE_ALL 关闭所有日志 +// L_O_G_DISABLE_ALL(关闭所有日志 包含所有库) // // c++11环境默认打开以下内容 // LOG_ENABLE_THREAD_SAFE 线程安全 @@ -21,7 +23,7 @@ // // 在库中使用时 // 1. 修改此文件中的`LOG`以包含库名前缀(全部替换即可) -// 2. 取消这行注释: #define LOG_IN_LIB +// 2. 取消这行注释(以屏蔽DEBUG日志): #define LOG_IN_LIB // 库中可配置项 // LOG_SHOW_DEBUG 开启LOGD的输出 // @@ -38,6 +40,19 @@ // 在库中使用时需取消注释 //#define LOG_IN_LIB +#if defined(LOG_DISABLE_ALL) || defined(L_O_G_DISABLE_ALL) + +#define LOG(fmt, ...) ((void)0) +#define LOGT(tag, fmt, ...) ((void)0) +#define LOGI(fmt, ...) ((void)0) +#define LOGW(fmt, ...) ((void)0) +#define LOGE(fmt, ...) ((void)0) +#define LOGF(fmt, ...) ((void)0) +#define LOGD(fmt, ...) ((void)0) +#define LOGV(fmt, ...) ((void)0) + +#else + #ifdef __cplusplus #include #include @@ -77,7 +92,11 @@ #endif #endif +#ifdef __FILE_NAME__ +#define LOG_BASE_FILENAME (__FILE_NAME__) +#else #define LOG_BASE_FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) +#endif #define LOG_WITH_COLOR @@ -126,16 +145,24 @@ #endif #ifdef LOG_ENABLE_THREAD_SAFE +#ifndef L_O_G_NS_MUTEX +#define L_O_G_NS_MUTEX L_O_G_NS_MUTEX #include -struct LOG_Mutex { +// 1. struct instead of namespace, ensure single instance +struct L_O_G_NS_MUTEX { static std::mutex& mutex() { -static std::mutex mutex; -return mutex; + // 2. never delete, avoid destroy before user log + // 3. static memory, avoid memory fragmentation + static char memory[sizeof(std::mutex)]; + static std::mutex& mutex = *(new (memory) std::mutex()); + return mutex; } }; -#define LOG_PRINTF_IMPL(...) \ -std::lock_guard lock(LOG_Mutex::mutex()); \ -LOG_PRINTF(__VA_ARGS__) +#endif +#define LOG_PRINTF_IMPL(...) { \ + std::lock_guard lock(L_O_G_NS_MUTEX::mutex()); \ + LOG_PRINTF(__VA_ARGS__); \ +} #else #define LOG_PRINTF_IMPL(...) LOG_PRINTF(__VA_ARGS__) #endif @@ -145,18 +172,30 @@ extern int LOG_PRINTF_IMPL(const char *fmt, ...); #endif #ifdef LOG_ENABLE_THREAD_ID -#include -#include -#include -namespace LOG { -inline std::string get_thread_id() { -std::stringstream ss; -ss << std::this_thread::get_id(); -return ss.str(); +#ifndef L_O_G_NS_GET_TID +#define L_O_G_NS_GET_TID L_O_G_NS_GET_TID +#include +#ifdef __linux__ +#include +#include +struct L_O_G_NS_GET_TID { +static inline uint32_t get_tid() { + return syscall(SYS_gettid); } +}; +#else /* for mac, bsd.. */ +#include +struct L_O_G_NS_GET_TID { +static inline uint32_t get_tid() { + uint64_t x; + pthread_threadid_np(nullptr, &x); + return (uint32_t)x; } -#define LOG_THREAD_LABEL "%s " -#define LOG_THREAD_VALUE ,LOG::get_thread_id().c_str() +}; +#endif +#endif +#define LOG_THREAD_LABEL "%u " +#define LOG_THREAD_VALUE ,L_O_G_NS_GET_TID::get_tid() #else #define LOG_THREAD_LABEL #define LOG_THREAD_VALUE @@ -165,19 +204,22 @@ return ss.str(); #ifdef LOG_ENABLE_DATE_TIME #include #include -#include -namespace LOG { -inline std::string get_time() { -auto now = std::chrono::system_clock::now(); -std::time_t time = std::chrono::system_clock::to_time_t(now); -auto ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; -std::stringstream ss; -ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S") << '.' << std::setw(3) << std::setfill('0') << ms.count(); -return ss.str(); -} +#include // std::put_time +#ifndef L_O_G_NS_GET_TIME +#define L_O_G_NS_GET_TIME L_O_G_NS_GET_TIME +struct L_O_G_NS_GET_TIME { +static inline std::string get_time() { + auto now = std::chrono::system_clock::now(); + std::time_t time = std::chrono::system_clock::to_time_t(now); + auto ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; + std::stringstream ss; + ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S") << '.' << std::setw(3) << std::setfill('0') << ms.count(); + return ss.str(); } +}; +#endif #define LOG_TIME_LABEL "%s " -#define LOG_TIME_VALUE ,LOG::get_time().c_str() +#define LOG_TIME_VALUE ,L_O_G_NS_GET_TIME::get_time().c_str() #else #define LOG_TIME_LABEL #define LOG_TIME_VALUE @@ -205,3 +247,5 @@ return ss.str(); #else #define LOGV(fmt, ...) ((void)0) #endif + +#endif