-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
46 lines (40 loc) · 1.13 KB
/
log.cpp
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
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "log.h"
static int level = LOG_INFO;
static int LOG_BUFFER_SIZE = 2048;
static const char* loglevels[] =
{
"emerge!", "alert!", "critical!", "error!", "warn!", "notice:", "info:", "debug:"
};
void set_loglevel( int log_level )
{
level = log_level;
}
void log( int log_level, const char* file_name, int line_num, const char* format, ... )
{
if ( log_level > level )
{
return;
}
time_t tmp = time( NULL );
struct tm* cur_time = localtime( &tmp );
if ( ! cur_time )
{
return;
}
char arg_buffer[ LOG_BUFFER_SIZE ];
memset( arg_buffer, '\0', LOG_BUFFER_SIZE );
strftime( arg_buffer, LOG_BUFFER_SIZE - 1, "[ %x %X ] ", cur_time );
printf( "%s", arg_buffer );
printf( "%s:%04d ", file_name, line_num );
printf( "%s ", loglevels[ log_level - LOG_EMERG ] );
va_list arg_list;
va_start( arg_list, format );
memset( arg_buffer, '\0', LOG_BUFFER_SIZE );
vsnprintf( arg_buffer, LOG_BUFFER_SIZE - 1, format, arg_list );
printf( "%s\n", arg_buffer );
fflush( stdout );
va_end( arg_list );
}