增加 使用 Define 宏自定义 Log 系统.
Signed-off-by: rick.chan <cy187lion@sina.com>
This commit is contained in:
parent
7e46079ef7
commit
fe68ab0824
|
@ -0,0 +1,85 @@
|
|||
# 使用 Define 宏自定义 Log 系统
|
||||
|
||||
## C/C++ 语言简易版本
|
||||
|
||||
注意 "] " 前后要有空格,否则编译错误。
|
||||
|
||||
```cpp
|
||||
#include <stdio.h>
|
||||
|
||||
#define LOG_DBG 0
|
||||
#define LOG_INF 1
|
||||
#define LOG_WAR 2
|
||||
#define LOG_ERR 3
|
||||
#define LOG_NOP 4
|
||||
#define LOG_LEVEL LOG_DBG
|
||||
|
||||
#if (LOG_LEVEL<=LOG_DBG)
|
||||
#define DLog(tag, ...) printf("[DBG|"#tag "] " __VA_ARGS__)
|
||||
#else // !(LOG_LEVEL<LOG_DBG)
|
||||
#define DLog(tag, ...)
|
||||
#endif // (LOG_LEVEL<LOG_DBG)
|
||||
|
||||
#if (LOG_LEVEL<=LOG_INF)
|
||||
#define ILog(tag, ...) printf("[INF|"#tag "] " __VA_ARGS__)
|
||||
#else // !(LOG_LEVEL<LOG_INF)
|
||||
#define ILog(tag, ...)
|
||||
#endif // (LOG_LEVEL<LOG_INF)
|
||||
|
||||
#if (LOG_LEVEL<=LOG_WAR)
|
||||
#define WLog(tag, ...) printf("[WAR|"#tag "] " __VA_ARGS__)
|
||||
#else // !(LOG_LEVEL<LOG_WAR)
|
||||
#define WLog(tag, ...)
|
||||
#endif // (LOG_LEVEL<LOG_WAR)
|
||||
|
||||
#if (LOG_LEVEL<=LOG_ERR)
|
||||
#define ELog(tag, ...) printf("[ERR|"#tag "] " __VA_ARGS__)
|
||||
#else // !(LOG_LEVEL<LOG_WAR)
|
||||
#define ELog(tag, ...)
|
||||
#endif // (LOG_LEVEL<LOG_WAR)
|
||||
```
|
||||
|
||||
## C++ 带类和函数的扩展版
|
||||
|
||||
```cpp
|
||||
#include <stdio.h>
|
||||
|
||||
#define LOG_DBG 0
|
||||
#define LOG_INF 1
|
||||
#define LOG_WAR 2
|
||||
#define LOG_ERR 3
|
||||
#define LOG_NOP 4
|
||||
#define LOG_LEVEL LOG_DBG
|
||||
|
||||
#if (LOG_LEVEL<=LOG_DBG)
|
||||
#define DLog0(fmt) printf("[HDBG|%s]" fmt, __PRETTY_FUNCTION__)
|
||||
#define DLog(fmt, ...) printf("[HDBG|%s]" fmt, __PRETTY_FUNCTION__, __VA_ARGS__)
|
||||
#else // !(LOG_LEVEL<LOG_DBG)
|
||||
#define DLog0(fmt)
|
||||
#define DLog(fmt, ...)
|
||||
#endif // (LOG_LEVEL<LOG_DBG)
|
||||
|
||||
#if (LOG_LEVEL<=LOG_INF)
|
||||
#define ILog0(fmt) printf("[HINF|%s]" fmt, __PRETTY_FUNCTION__)
|
||||
#define ILog(fmt, ...) printf("[HINF|%s]" fmt, __PRETTY_FUNCTION__, __VA_ARGS__)
|
||||
#else // !(LOG_LEVEL<LOG_INF)
|
||||
#define ILog0(fmt)
|
||||
#define ILog(fmt, ...)
|
||||
#endif // (LOG_LEVEL<LOG_INF)
|
||||
|
||||
#if (LOG_LEVEL<=LOG_WAR)
|
||||
#define WLog0(fmt) printf("[HWAR|%s]" fmt, __PRETTY_FUNCTION__)
|
||||
#define WLog(fmt, ...) printf("[HWAR|%s]" fmt, __PRETTY_FUNCTION__, __VA_ARGS__)
|
||||
#else // !(LOG_LEVEL<LOG_WAR)
|
||||
#define WLog0(fmt)
|
||||
#define WLog(fmt, ...)
|
||||
#endif // (LOG_LEVEL<LOG_WAR)
|
||||
|
||||
#if (LOG_LEVEL<=LOG_ERR)
|
||||
#define ELog0(fmt) printf("[HERR|%s]" fmt, __PRETTY_FUNCTION__)
|
||||
#define ELog(fmt, ...) printf("[HERR|%s]" fmt, __PRETTY_FUNCTION__, __VA_ARGS__)
|
||||
#else // !(LOG_LEVEL<LOG_WAR)
|
||||
#define ELog0(fmt)
|
||||
#define ELog(fmt, ...)
|
||||
#endif // (LOG_LEVEL<LOG_WAR)
|
||||
```
|
Loading…
Reference in New Issue