diff options
Diffstat (limited to 'src/stable/logger.cpp')
-rw-r--r-- | src/stable/logger.cpp | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/src/stable/logger.cpp b/src/stable/logger.cpp new file mode 100644 index 0000000..8e46390 --- /dev/null +++ b/src/stable/logger.cpp | |||
@@ -0,0 +1,209 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/logger.h" | ||
9 | #include <stdarg.h> | ||
10 | #include <time.h> | ||
11 | #include <stdio.h> | ||
12 | #include <stdlib.h> | ||
13 | #include <unistd.h> | ||
14 | |||
15 | Bu::Logger::Logger() | ||
16 | { | ||
17 | setFormat("%t"); | ||
18 | } | ||
19 | |||
20 | Bu::Logger::~Logger() | ||
21 | { | ||
22 | } | ||
23 | |||
24 | void Bu::Logger::log( uint32_t nLevel, const char *sFile, const char *sFunction, int nLine, const char *sFormat, ...) | ||
25 | { | ||
26 | #ifndef WIN32 | ||
27 | if( (nLevel&nLevelMask) == 0 ) | ||
28 | return; | ||
29 | |||
30 | va_list ap; | ||
31 | va_start( ap, sFormat ); | ||
32 | char *text; | ||
33 | if( vasprintf( &text, sFormat, ap ) < 0 ) | ||
34 | { | ||
35 | printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WTF?\n"); | ||
36 | return; | ||
37 | } | ||
38 | va_end(ap); | ||
39 | |||
40 | time_t t = time(NULL); | ||
41 | |||
42 | char *line = NULL; | ||
43 | struct tm *pTime; | ||
44 | pTime = localtime( &t ); | ||
45 | if ( asprintf( | ||
46 | &line, | ||
47 | sLogFormat.getStr(), | ||
48 | pTime->tm_year+1900, | ||
49 | pTime->tm_mon+1, | ||
50 | pTime->tm_mday, | ||
51 | pTime->tm_hour, | ||
52 | pTime->tm_min, | ||
53 | pTime->tm_sec, | ||
54 | nLevel, | ||
55 | sFile, | ||
56 | nLine, | ||
57 | text, | ||
58 | sFunction | ||
59 | ) < 0 ) | ||
60 | { | ||
61 | //printf("LOGGER: ERROR ALLOCATING STRING: %s\n", strerror( errno ) ); | ||
62 | return; | ||
63 | } | ||
64 | write( fileno(stdout), line, strlen(line) ); | ||
65 | free( text ); | ||
66 | free( line ); | ||
67 | #else | ||
68 | #warning Bu::Logger::log IS A STUB for WIN32!!!! | ||
69 | #endif | ||
70 | } | ||
71 | |||
72 | void Bu::Logger::setFormat( const Bu::String &str ) | ||
73 | { | ||
74 | sLogFormat = ""; | ||
75 | |||
76 | static char fmts[][4]={ | ||
77 | {'y', 'd', '0', '1'}, | ||
78 | {'m', 'd', '0', '2'}, | ||
79 | {'d', 'd', '0', '3'}, | ||
80 | {'h', 'd', '0', '4'}, | ||
81 | {'M', 'd', '0', '5'}, | ||
82 | {'s', 'd', '0', '6'}, | ||
83 | {'L', 'd', '0', '7'}, | ||
84 | {'f', 's', '0', '8'}, | ||
85 | {'l', 'd', '0', '9'}, | ||
86 | {'t', 's', '1', '0'}, | ||
87 | {'F', 's', '1', '1'}, | ||
88 | {'\0', '\0', '\0', '\0'}, | ||
89 | }; | ||
90 | |||
91 | for( const char *s = str.getStr(); *s; s++ ) | ||
92 | { | ||
93 | if( *s == '%' ) | ||
94 | { | ||
95 | sLogFormat += '%'; | ||
96 | Bu::String sBuf; | ||
97 | for(;;) | ||
98 | { | ||
99 | s++; | ||
100 | int l; | ||
101 | for( l = 0;; l++ ) | ||
102 | { | ||
103 | if( fmts[l][0] == '\0' ) | ||
104 | { | ||
105 | sBuf += *s; | ||
106 | break; | ||
107 | } | ||
108 | else if( *s == fmts[l][0] ) | ||
109 | { | ||
110 | sLogFormat += fmts[l][2]; | ||
111 | sLogFormat += fmts[l][3]; | ||
112 | sLogFormat += '$'; | ||
113 | sLogFormat += sBuf; | ||
114 | sLogFormat += fmts[l][1]; | ||
115 | break; | ||
116 | } | ||
117 | } | ||
118 | if( fmts[l][0] != '\0' ) | ||
119 | break; | ||
120 | } | ||
121 | } | ||
122 | else | ||
123 | { | ||
124 | sLogFormat += *s; | ||
125 | } | ||
126 | } | ||
127 | sLogFormat += '\n'; | ||
128 | |||
129 | //write( fileno(stdout), sLogFormat.getStr(), sLogFormat.getSize() ); | ||
130 | } | ||
131 | |||
132 | void Bu::Logger::setMask( uint32_t n ) | ||
133 | { | ||
134 | nLevelMask = n; | ||
135 | } | ||
136 | |||
137 | uint32_t Bu::Logger::getMask() | ||
138 | { | ||
139 | return nLevelMask; | ||
140 | } | ||
141 | |||
142 | void Bu::Logger::setLevel( uint32_t n ) | ||
143 | { | ||
144 | int j; | ||
145 | for( j = 31; j > 0; j-- ) | ||
146 | { | ||
147 | if( (n&(1<<j)) ) | ||
148 | { | ||
149 | for(; j >= 0; j-- ) | ||
150 | { | ||
151 | n |= (1<<j); | ||
152 | } | ||
153 | nLevelMask = n; | ||
154 | return; | ||
155 | } | ||
156 | } | ||
157 | } | ||
158 | |||
159 | void Bu::Logger::hexDump( uint32_t nLevel, const char *sFile, | ||
160 | const char *sFunction, int nLine, const void *pDataV, long nDataLen, | ||
161 | const char *lpName ) | ||
162 | { | ||
163 | if( (nLevel&nLevelMask) == 0 ) | ||
164 | return; | ||
165 | |||
166 | log( nLevel, sFile, sFunction, nLine, "Displaying %ld bytes of %s.", nDataLen, lpName ); | ||
167 | const unsigned char *pData = (const unsigned char *)pDataV; | ||
168 | int j = 0; | ||
169 | Bu::String sBorder; | ||
170 | for( int l = 0; l < 8*3+2*8+2+5; l++ ) sBorder += ((l!=11&&l!=37)?("-"):("+")); | ||
171 | log( nLevel, sFile, sFunction, nLine, sBorder.getStr() ); | ||
172 | Bu::String sLine; | ||
173 | for(;;) | ||
174 | { | ||
175 | { | ||
176 | char buf[16]; | ||
177 | sprintf( buf, "%010d | ", j ); | ||
178 | sLine += buf; | ||
179 | } | ||
180 | int kmax = 8; | ||
181 | if( nDataLen-j < 8 ) kmax = nDataLen-j; | ||
182 | for(int k = 0; k < 8; k++ ) | ||
183 | { | ||
184 | if( k < kmax ) | ||
185 | { | ||
186 | char buf[4]; | ||
187 | sprintf( buf, "%02X ", (int)((unsigned char)pData[j+k]) ); | ||
188 | sLine += buf; | ||
189 | } | ||
190 | else | ||
191 | { | ||
192 | sLine += "-- "; | ||
193 | } | ||
194 | } | ||
195 | sLine += "| "; | ||
196 | for(int k = 0; k < kmax; k++ ) | ||
197 | { | ||
198 | char buf[3]; | ||
199 | sprintf( buf, "%c", (pData[j+k]>32 && pData[j+k]<=128)?(pData[j+k]):('.') ); | ||
200 | sLine += buf; | ||
201 | } | ||
202 | log( nLevel, sFile, sFunction, nLine, sLine.getStr() ); | ||
203 | sLine = ""; | ||
204 | j += kmax; | ||
205 | if( j >= nDataLen ) break; | ||
206 | } | ||
207 | log( nLevel, sFile, sFunction, nLine, sBorder.getStr() ); | ||
208 | } | ||
209 | |||