diff options
Diffstat (limited to '')
-rw-r--r-- | src/logger.cpp | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/logger.cpp b/src/logger.cpp new file mode 100644 index 0000000..848dfb1 --- /dev/null +++ b/src/logger.cpp | |||
@@ -0,0 +1,123 @@ | |||
1 | #include "bu/logger.h" | ||
2 | #include <stdarg.h> | ||
3 | #include <time.h> | ||
4 | #include <stdio.h> | ||
5 | |||
6 | Bu::Logger::Logger() | ||
7 | { | ||
8 | } | ||
9 | |||
10 | Bu::Logger::~Logger() | ||
11 | { | ||
12 | } | ||
13 | |||
14 | void Bu::Logger::log( int nLevel, const char *sFile, const char *sFunction, int nLine, const char *sFormat, ...) | ||
15 | { | ||
16 | if( (nLevel&nLevelMask) == 0 ) | ||
17 | return; | ||
18 | |||
19 | va_list ap; | ||
20 | va_start( ap, sFormat ); | ||
21 | char *text; | ||
22 | vasprintf( &text, sFormat, ap ); | ||
23 | va_end(ap); | ||
24 | |||
25 | time_t t = time(NULL); | ||
26 | |||
27 | char *line = NULL; | ||
28 | struct tm *pTime; | ||
29 | pTime = localtime( &t ); | ||
30 | asprintf( | ||
31 | &line, | ||
32 | sLogFormat.getStr(), | ||
33 | pTime->tm_year+1900, | ||
34 | pTime->tm_mon+1, | ||
35 | pTime->tm_mday, | ||
36 | pTime->tm_hour, | ||
37 | pTime->tm_min, | ||
38 | pTime->tm_sec, | ||
39 | nLevel, | ||
40 | sFile, | ||
41 | nLine, | ||
42 | text, | ||
43 | sFunction | ||
44 | ); | ||
45 | write( fileno(stdout), line, strlen(line) ); | ||
46 | free( text ); | ||
47 | free( line ); | ||
48 | } | ||
49 | |||
50 | void Bu::Logger::setFormat( const Bu::FString &str ) | ||
51 | { | ||
52 | sLogFormat = ""; | ||
53 | |||
54 | static char fmts[][4]={ | ||
55 | {'y', 'd', '0', '1'}, | ||
56 | {'m', 'd', '0', '2'}, | ||
57 | {'d', 'd', '0', '3'}, | ||
58 | {'h', 'd', '0', '4'}, | ||
59 | {'M', 'd', '0', '5'}, | ||
60 | {'s', 'd', '0', '6'}, | ||
61 | {'L', 'd', '0', '7'}, | ||
62 | {'f', 's', '0', '8'}, | ||
63 | {'l', 'd', '0', '9'}, | ||
64 | {'t', 's', '1', '0'}, | ||
65 | {'F', 's', '1', '1'}, | ||
66 | {'\0', '\0', '\0', '\0'}, | ||
67 | }; | ||
68 | |||
69 | for( const char *s = str.getStr(); *s; s++ ) | ||
70 | { | ||
71 | if( *s == '%' ) | ||
72 | { | ||
73 | sLogFormat += '%'; | ||
74 | s++; | ||
75 | for( int l = 0;; l++ ) | ||
76 | { | ||
77 | if( fmts[l][0] == '\0' ) | ||
78 | { | ||
79 | sLogFormat += *s; | ||
80 | break; | ||
81 | } | ||
82 | else if( *s == fmts[l][0] ) | ||
83 | { | ||
84 | sLogFormat += fmts[l][2]; | ||
85 | sLogFormat += fmts[l][3]; | ||
86 | sLogFormat += '$'; | ||
87 | sLogFormat += fmts[l][1]; | ||
88 | break; | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | else | ||
93 | { | ||
94 | sLogFormat += *s; | ||
95 | } | ||
96 | } | ||
97 | sLogFormat += '\n'; | ||
98 | |||
99 | write( fileno(stdout), sLogFormat.getStr(), sLogFormat.getSize() ); | ||
100 | } | ||
101 | |||
102 | void Bu::Logger::setMask( int n ) | ||
103 | { | ||
104 | nLevelMask = n; | ||
105 | } | ||
106 | |||
107 | void Bu::Logger::setLevel( int n ) | ||
108 | { | ||
109 | int j; | ||
110 | for( j = 31; j > 0; j-- ) | ||
111 | { | ||
112 | if( (n&(1<<j)) ) | ||
113 | { | ||
114 | for(; j >= 0; j-- ) | ||
115 | { | ||
116 | n |= (1<<j); | ||
117 | } | ||
118 | nLevelMask = n; | ||
119 | return; | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | |||