Skip to content

Commit

Permalink
feat: update log
Browse files Browse the repository at this point in the history
  • Loading branch information
shuai132 committed Sep 14, 2023
1 parent af81f8e commit 9f8c93b
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 61 deletions.
96 changes: 64 additions & 32 deletions detail/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 线程安全
Expand All @@ -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的输出
//
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 <mutex>
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<std::mutex> lock(ASIO_NET_LOG_Mutex::mutex()); \
ASIO_NET_LOG_PRINTF(__VA_ARGS__)
#endif
#define ASIO_NET_LOG_PRINTF_IMPL(...) { \
std::lock_guard<std::mutex> 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
Expand All @@ -162,18 +172,37 @@ extern int ASIO_NET_LOG_PRINTF_IMPL(const char *fmt, ...);
#endif

#ifdef ASIO_NET_LOG_ENABLE_THREAD_ID
#include <thread>
#include <sstream>
#include <string>
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 <cstdint>
#ifdef _WIN32
#include <processthreadsapi.h>
struct L_O_G_NS_GET_TID {
static inline uint32_t get_tid() {
return GetCurrentThreadId();
}
};
#elif defined(__linux__)
#include <sys/syscall.h>
#include <unistd.h>
struct L_O_G_NS_GET_TID {
static inline uint32_t get_tid() {
return syscall(SYS_gettid);
}
};
#else /* for mac, bsd.. */
#include <pthread.h>
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
Expand All @@ -182,19 +211,22 @@ return ss.str();
#ifdef ASIO_NET_LOG_ENABLE_DATE_TIME
#include <chrono>
#include <sstream>
#include <iomanip>
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<std::chrono::milliseconds>(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 <iomanip> // 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<std::chrono::milliseconds>(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
Expand Down
2 changes: 1 addition & 1 deletion rpc/rpc_core
Submodule rpc_core updated 2 files
+1 −1 CMakeLists.txt
+64 −32 src/detail/log.h
107 changes: 79 additions & 28 deletions test/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 线程安全
Expand All @@ -21,7 +23,7 @@
//
// 在库中使用时
// 1. 修改此文件中的`LOG`以包含库名前缀(全部替换即可)
// 2. 取消这行注释: #define LOG_IN_LIB
// 2. 取消这行注释(以屏蔽DEBUG日志): #define LOG_IN_LIB
// 库中可配置项
// LOG_SHOW_DEBUG 开启LOGD的输出
//
Expand All @@ -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 <cstring>
#include <cstdlib>
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 <mutex>
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<std::mutex> lock(LOG_Mutex::mutex()); \
LOG_PRINTF(__VA_ARGS__)
#endif
#define LOG_PRINTF_IMPL(...) { \
std::lock_guard<std::mutex> lock(L_O_G_NS_MUTEX::mutex()); \
LOG_PRINTF(__VA_ARGS__); \
}
#else
#define LOG_PRINTF_IMPL(...) LOG_PRINTF(__VA_ARGS__)
#endif
Expand All @@ -145,18 +172,37 @@ extern int LOG_PRINTF_IMPL(const char *fmt, ...);
#endif

#ifdef LOG_ENABLE_THREAD_ID
#include <thread>
#include <sstream>
#include <string>
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 <cstdint>
#ifdef _WIN32
#include <processthreadsapi.h>
struct L_O_G_NS_GET_TID {
static inline uint32_t get_tid() {
return GetCurrentThreadId();
}
};
#elif defined(__linux__)
#include <sys/syscall.h>
#include <unistd.h>
struct L_O_G_NS_GET_TID {
static inline uint32_t get_tid() {
return syscall(SYS_gettid);
}
};
#else /* for mac, bsd.. */
#include <pthread.h>
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
Expand All @@ -165,19 +211,22 @@ return ss.str();
#ifdef LOG_ENABLE_DATE_TIME
#include <chrono>
#include <sstream>
#include <iomanip>
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<std::chrono::milliseconds>(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 <iomanip> // 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<std::chrono::milliseconds>(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
Expand Down Expand Up @@ -205,3 +254,5 @@ return ss.str();
#else
#define LOGV(fmt, ...) ((void)0)
#endif

#endif

0 comments on commit 9f8c93b

Please sign in to comment.