C語言--使用技巧(二)日誌分水平等級列印
阿新 • • 發佈:2019-02-19
debug.h
#ifndef _DEBUG_H_
#define _DEBUG_H_
#include "stdio.h"
// 定義log列印水平
#ifndef _LOGLEVEL_
#define _LOGLEVEL_ 3
#endif
#define LOGERROR(x) if(_LOGLEVEL_>0) { printf("[WiFiDEV] "); printf(x); }
#define LOGERROR1(x,y) if(_LOGLEVEL_>2) { printf("[WiFiDEV] "); printf(x); printf(" "); printf(y); }
#define LOGWARN(x) if(_LOGLEVEL_>1) { printf("[WiFiDEV] "); printf(x); }
#define LOGWARN1(x,y) if(_LOGLEVEL_>2) { printf("[WiFiDEV] "); printf(x); printf(" "); printf(y); }
#define LOGINFO(x) if(_LOGLEVEL_>2) { printf("[WiFiDEV] "); printf(x); }
#define LOGINFO1(x,y) if(_LOGLEVEL_>2) { printf("[WiFiDEV] "); printf(x); printf(" "); printf(y); }
#define LOGDEBUG(x) if(_LOGLEVEL_>3) { printf(x); }
#define LOGDEBUG0(x) if(_LOGLEVEL_>3) { printf(x); }
#define LOGDEBUG1(x,y) if(_LOGLEVEL_>3) { printf(x); print(" "); printf(y); }
#define LOGDEBUG2(x,y,z) if(_LOGLEVEL_>3) { printf(x); print(" "); printf(y); printf(" "); printf(z); }
#endif
改進:
debug.c
#ifndef _DEBUG_H_
#define _DEBUG_H_
#include "stdio.h"
/************************************精簡版通用日誌框架************************************
*
* @author 葛勞-霍特戈(liefyuan) <[email protected]>
*
* 定義顯示的日誌級別
*
* eg. LOGE("StackOverFlow");
*****************************************************************************************/
#ifndef _LOGLEVEL_
#define _LOGLEVEL_ 4 // 定義日誌顯示等級:1,2,3,4
#endif
#define LOGE(x) if(_LOGLEVEL_>0) { printf("[LOGERROR] "); printf(x); printf("\r"); }
#define LOGW(x) if(_LOGLEVEL_>1) { printf("[LOGWARN] "); printf(x); printf("\r"); }
#define LOGI(x) if(_LOGLEVEL_>2) { printf("[LOGINFO] "); printf(x); printf("\r"); }
#define LOGD(x) if(_LOGLEVEL_>3) { printf("[LOGDEBUG] "); printf(x); printf("\r"); }
#endif
還需要增加如下功能:
int a = 100;
LOGD(">>>a = %d\r", a)
完成時間待定!