-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_log.c
131 lines (102 loc) · 2.16 KB
/
tg_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "tg_log.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#define BUFFER_SIZE 1024
// native print message function
// just wrap printf
static void __log(const char * tag, const int level,char * msg)
{
char * czLevel=getenv("LOG_LEVEL");
int _l=czLevel==NULL?LOG_LEVEL_WARNING:atoi(czLevel);
time_t ticks;
ticks=time(NULL);
char * szPrintLogLevel;
switch(level)
{
case LOG_LEVEL_ERROR:
szPrintLogLevel=" [ERROR] ";
break;
case LOG_LEVEL_WARNING:
szPrintLogLevel=" [WARNING] ";
break;
case LOG_LEVEL_INFO:
szPrintLogLevel=" [INFO] ";
break;
case LOG_LEVEL_DEBUG:
szPrintLogLevel=" [DEBUG] ";
break;
default:
szPrintLogLevel=" [INFO] ";
}
if(_l>=level)
{
char buf_head[100];
snprintf(buf_head,sizeof(buf_head),"%.24s[%s]%s==>",ctime(&ticks),tag,szPrintLogLevel);
printf("%s",buf_head);
printf("%s\n",msg);
}
}
void logN(const char * tag,const int level,const char * fmt,...)
{
char buf[BUFFER_SIZE];
va_list ap;
va_start(ap,fmt);
vsnprintf(buf,BUFFER_SIZE,fmt,ap);
va_end(ap);
__log(tag,level,buf);
}
// print message of debug level
void logD(const char * tag,const char * fmt,...)
{
char buf[BUFFER_SIZE];
va_list ap;
va_start(ap,fmt);
vsnprintf(buf,BUFFER_SIZE,fmt,ap);
va_end(ap);
__log(tag,LOG_LEVEL_DEBUG,buf);
}
// print message of warning level
void logW(const char * tag,const char * fmt,...)
{
char buf[BUFFER_SIZE];
va_list ap;
va_start(ap,fmt);
vsnprintf(buf,BUFFER_SIZE,fmt,ap);
va_end(ap);
__log(tag,LOG_LEVEL_WARNING,buf);
}
// print message of INFO level
void logI(const char * tag,const char * fmt,...)
{
char buf[BUFFER_SIZE];
va_list ap;
va_start(ap,fmt);
vsnprintf(buf,BUFFER_SIZE,fmt,ap);
va_end(ap);
__log(tag,LOG_LEVEL_INFO,buf);
}
// print message of error level
void logE(const char * tag,const char * fmt,...)
{
char buf[BUFFER_SIZE];
va_list ap;
va_start(ap,fmt);
vsnprintf(buf,BUFFER_SIZE,fmt,ap);
va_end(ap);
__log(tag,LOG_LEVEL_ERROR,buf);
}
void logM(const char * pData, const int size)
{
int i=0;
for(i=0;i<size;i++)
{
printf("%02x",(unsigned char)pData[i]);
if(i%4==0)
{
printf(" ");
}
}
printf("\n");
}