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