Skip to content

Commit 1668466

Browse files
committed
add simple log
simple log, no multithread protected
1 parent f10459c commit 1668466

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

MBase/MBase.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
</Link>
9393
</ItemDefinitionGroup>
9494
<ItemGroup>
95+
<ClCompile Include="base_struct\M_log.c" />
9596
<ClCompile Include="base_struct\M_poolinf.c" />
9697
<ClCompile Include="base_struct\M_radix_mata.c" />
9798
<ClCompile Include="base_struct\M_radix_tree.c" />
@@ -111,6 +112,7 @@
111112
<ClCompile Include="error\M_error.c" />
112113
</ItemGroup>
113114
<ItemGroup>
115+
<ClInclude Include="..\M_include\MBase_header\M_log.h" />
114116
<ClInclude Include="..\M_include\MBase_header\M_poolinf.h" />
115117
<ClInclude Include="..\M_include\MBase_header\M_radix_mata.h" />
116118
<ClInclude Include="..\M_include\MBase_header\M_radix_tree.h" />

MBase/MBase.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
<ClCompile Include="base_struct\M_radix_mata.c">
7676
<Filter>base_struct</Filter>
7777
</ClCompile>
78+
<ClCompile Include="base_struct\M_log.c">
79+
<Filter>base_struct</Filter>
80+
</ClCompile>
7881
</ItemGroup>
7982
<ItemGroup>
8083
<ClInclude Include="MBase_priv.h">
@@ -140,6 +143,9 @@
140143
<ClInclude Include="..\M_include\MBase_header\M_radix_mata.h">
141144
<Filter>base_struct</Filter>
142145
</ClInclude>
146+
<ClInclude Include="..\M_include\MBase_header\M_log.h">
147+
<Filter>base_struct</Filter>
148+
</ClInclude>
143149
</ItemGroup>
144150
<ItemGroup>
145151
<None Include="ReadMe.txt" />

MBase/base_struct/M_log.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
M_log.h : a simple logger
3+
4+
5+
2014/02/20
6+
*/
7+
8+
#include "../MBase_priv.h"
9+
#include "MBase.h"
10+
#include <assert.h>
11+
#include <stdarg.h>
12+
13+
static INLINE M_sint32 init_log_f(M_log* self, const M_sint8* log_file, M_sint32 log_level)
14+
{
15+
//strcpy(self->log_file, log_file);
16+
if( !(self->fp = fopen(log_file, "a+")) )
17+
return -1;
18+
self->log_level = log_level;
19+
20+
return 0;
21+
22+
}
23+
static INLINE void set_log_level_f(M_log* self, M_sint32 log_level)
24+
{
25+
self->log_level = log_level;
26+
}
27+
28+
static char* str_log_level[LOG_DETAIL+1] = {"LOG_FATAL", "LOG_ERROR", "LOG_WARNING", "LOG_INFO", "LOG_DETAIL"};
29+
30+
static INLINE M_sint32 dump_log_f(M_log* self, M_sint32 log_level, const M_sint8* fmt, ...)
31+
{
32+
M_sint32 written_bytes = 0;
33+
if(self->log_level <= log_level)
34+
{
35+
va_list arg_ptr;
36+
va_start(arg_ptr,fmt); //以固定参数的地址为起点确定变参的内存起始地址。
37+
written_bytes = vfprintf(self->fp, fmt, arg_ptr);
38+
va_end(arg_ptr);
39+
}
40+
return written_bytes;
41+
}
42+
static INLINE void close_log_f(M_log* self)
43+
{
44+
fclose(self->fp);
45+
}
46+
47+
INLINE void M_log_construct(M_log* obj)
48+
{
49+
obj->init = (M_sint32(*)(M_self*, const M_sint8*, M_sint32))init_log_f;
50+
obj->set_log_level = (void(*)(M_self*, M_sint32))set_log_level_f;
51+
obj->dump_log = (M_sint32(*)(M_self*, M_sint32, const M_sint8*, ...))dump_log_f;
52+
obj->close_log = (void(*)(M_self*))close_log_f;
53+
54+
//obj->log_file[0] = 0;
55+
obj->fp = NULL;
56+
obj->log_level = LOG_INFO;
57+
}

M_include/MBase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@
4848
#include "MBase_header/M_lqueue.h"
4949
#include "MBase_header/M_hash.h"
5050
#include "MBase_header/M_tls.h"
51+
#include "MBase_header/M_log.h"
5152

5253
#include "MBase_header/M_error.h"

M_include/MBase_header/M_log.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
M_log.h : a simple logger, support multi thread logging
3+
4+
5+
2014/02/20
6+
*/
7+
#ifndef __M_LOG_H__
8+
#define __M_LOG_H__
9+
10+
#include <stdio.h>
11+
#include "M_types.h"
12+
//#include "M_atomic.h"
13+
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
#define LOG_FATAL 0
20+
#define LOG_ERROR 1
21+
#define LOG_WARNING 2
22+
#define LOG_INFO 3
23+
#define LOG_DETAIL 4
24+
25+
typedef struct st_log
26+
{
27+
M_sint32 (*init)(M_self* self, const M_sint8* log_file, M_sint32 log_level);
28+
void (*set_log_level)(M_self* self, M_sint32 log_level);
29+
M_sint32 (*dump_log)(M_self* self, M_sint32 log_level, const M_sint8* fmt, ...);
30+
void (*close_log)(M_self* self);
31+
32+
//M_sint8 log_file[PATHLEN];
33+
FILE* fp;
34+
M_sint8 log_level;
35+
//M_atomic lock;
36+
} M_log;
37+
38+
MBASE_API INLINE void M_log_construct(M_log* obj);
39+
40+
41+
#ifdef __cplusplus
42+
}
43+
#endif
44+
45+
#endif //__M_LOG_H__

M_include/M_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ static INLINE void M_free(void* mem, void* pool)
172172
#define GROUND_4(x) ((x) - ((x) & 0x3))
173173
#define CEILING_4(x) GROUND_4((x)+3)
174174

175+
#define PATHLEN 256
176+
175177
#ifdef __cplusplus
176178
}
177179
#endif

0 commit comments

Comments
 (0)