]> git.jsancho.org Git - lugaru.git/blob - Source/logger/logger.cpp
Added prebuilt Mac SDL libraries.
[lugaru.git] / Source / logger / logger.cpp
1 // ---------------------------------------------------------------------------------------------------------------------------------
2 //  _                                                  
3 // | |                                                 
4 // | | ___   __ _  __ _  ___ _ __      ___ _ __  _ __  
5 // | |/ _ \ / _` |/ _` |/ _ \ '__|    / __| '_ \| '_ \ 
6 // | | (_) | (_| | (_| |  __/ |    _ | (__| |_) | |_) |
7 // |_|\___/ \__, |\__, |\___|_|   (_) \___| .__/| .__/ 
8 //           __/ | __/ |                  | |   | |    
9 //          |___/ |___/                   |_|   |_|    
10 //
11 // Generic informational logging class
12 //
13 // ---------------------------------------------------------------------------------------------------------------------------------
14 //
15 // Restrictions & freedoms pertaining to usage and redistribution of this software:
16 //
17 //  * This software is 100% free
18 //  * If you use this software (in part or in whole) you must credit the author.
19 //  * This software may not be re-distributed (in part or in whole) in a modified
20 //    form without clear documentation on how to obtain a copy of the original work.
21 //  * You may not use this software to directly or indirectly cause harm to others.
22 //  * This software is provided as-is and without warrantee. Use at your own risk.
23 //
24 // For more information, visit HTTP://www.FluidStudios.com
25 //
26 // ---------------------------------------------------------------------------------------------------------------------------------
27 // Originally created on 07/06/2000 by Paul Nettle
28 //
29 // Copyright 2000, Fluid Studios, Inc., all rights reserved.
30 // ---------------------------------------------------------------------------------------------------------------------------------
31
32 //#include "stdafx.h" // If this line gives you an error, comment it out
33 #include <string>
34 #include <list>
35 #include <map>
36 #include <vector>
37 #include <iostream>
38 #include <fstream>
39 #include <iomanip>
40 #include <strstream>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <ctime>
44 using namespace std;
45
46 #include "logger.h"
47
48 // ---------------------------------------------------------------------------------------------------------------------------------
49 // The global logger object
50 // ---------------------------------------------------------------------------------------------------------------------------------
51
52 Logger  logger;
53
54 // ---------------------------------------------------------------------------------------------------------------------------------
55 #ifdef _DEBUG
56 void    Logger::limitFileSize() const
57 {
58         if (fileSizeLimit() < 0) return;
59         struct  _stat sbuf;
60         _stat(logFile().c_str(), &sbuf);
61         if (sbuf.st_size > fileSizeLimit())
62         {
63                 unlink(logFile().c_str());
64         }
65 }
66
67 // ---------------------------------------------------------------------------------------------------------------------------------
68
69 void    Logger::start(const bool reset)
70 {
71         if (logStarted()) return;
72         _logStarted = true;
73
74         // Get the time
75
76         time_t  t = time(NULL);
77         string  ts = asctime(localtime(&t));
78         ts[ts.length() - 1] = 0;
79
80         // Start the log
81
82         limitFileSize();
83         ofstream of(logFile().c_str(), ios::out | (reset ? ios::trunc : ios::app));
84         if (!of) return;
85
86         of << "---------------------------------------------- Log begins on " << ts << " ----------------------------------------------" << endl;
87 }
88
89 // ---------------------------------------------------------------------------------------------------------------------------------
90
91 void    Logger::stop()
92 {
93         // Automatic One time only startup
94
95         if (!logStarted()) return;
96         _logStarted = false;
97
98         // Get the time
99
100         time_t  t = time(NULL);
101         string  ts = asctime(localtime(&t));
102         ts.erase(ts.length() - 1);
103
104         // Stop the log
105
106         limitFileSize();
107         ofstream of(logFile().c_str(), ios::out|ios::app);
108         if (!of) return;
109
110         of << "----------------------------------------------- Log ends on " << ts << " -----------------------------------------------" << endl << endl;
111 }
112
113 // ---------------------------------------------------------------------------------------------------------------------------------
114
115 void    Logger::logTex(const string &s, const LogFlags logBits)
116 {
117         // If the bits don't match the mask, then bail
118
119         if (!(logBits & logMask())) return;
120
121         // Open the file
122
123         limitFileSize();
124         ofstream of(logFile().c_str(), ios::out|ios::app);
125         if (!of) return;
126
127         // Output to the log file
128
129         of << headerString(logBits) << s << endl;
130 }
131
132 // ---------------------------------------------------------------------------------------------------------------------------------
133
134 void    Logger::logRaw(const string &s)
135 {
136         // Open the file
137
138         limitFileSize();
139         ofstream of(logFile().c_str(), ios::out|ios::app);
140         if (!of) return;
141
142         // Log the output
143
144         of << s << endl;
145 }
146
147 // ---------------------------------------------------------------------------------------------------------------------------------
148
149 void    Logger::logHex(const char *buffer, const unsigned int count, const LogFlags logBits)
150 {
151         // No input? No output
152
153         if (!buffer) return;
154
155         // If the bits don't match the mask, then bail
156
157         if (!(logBits & logMask())) return;
158
159         // Open the file
160
161         limitFileSize();
162         ofstream of(logFile().c_str(), ios::out|ios::app);
163         if (!of) return;
164
165         // Log the output
166
167         unsigned int    logged = 0;
168         while(logged < count)
169         {
170                 // One line at a time...
171
172                 string          line;
173
174                 // The number of characters per line
175
176                 unsigned int    hexLength = 20;
177
178                 // Default the buffer
179
180                 unsigned int    i;
181                 for (i = 0; i < hexLength; i++)
182                 {
183                         line += "-- ";
184                 }
185
186                 for (i = 0; i < hexLength; i++)
187                 {
188                         line += ".";
189                 }
190
191                 // Fill it in with real data
192
193                 for (i = 0; i < hexLength && logged < count; i++, logged++)
194                 {
195                         unsigned char   byte = buffer[logged];
196                         unsigned int    index = i * 3;
197
198                         // The hex characters
199
200                         const string    hexlist("0123456789ABCDEF");
201                         line[index+0] = hexlist[byte >> 4];
202                         line[index+1] = hexlist[byte & 0xf];
203
204                         // The ascii characters
205
206                         if (byte < 0x20 || byte > 0x7f) byte = '.';
207                         line[(hexLength*3)+i+0] = byte;
208                 }
209
210                 // Write it to the log file
211
212                 of << headerString(logBits) << line << endl;
213         }
214 }
215
216 // ---------------------------------------------------------------------------------------------------------------------------------
217
218 void    Logger::indent(const string &s, const LogFlags logBits)
219 {
220         // If the bits don't match the mask, then bail
221
222         if (!(logBits & logMask())) return;
223
224         // Open the file
225
226         limitFileSize();
227         ofstream of(logFile().c_str(), ios::out|ios::app);
228         if (!of) return;
229
230         // Log the output
231
232         if (lineCharsFlag())    of << headerString(logBits) << "\xDA\xC4\xC4" << s << endl;
233         else                    of << headerString(logBits) << "+-  " << s << endl;
234
235         // Indent...
236
237         _indentCount += _indentChars;
238 }
239
240 // ---------------------------------------------------------------------------------------------------------------------------------
241
242 void    Logger::undent(const string &s, const LogFlags logBits)
243 {
244         // If the bits don't match the mask, then bail
245
246         if (!(logBits & logMask())) return;
247
248         // Undo the indentation
249
250         _indentCount -= _indentChars;
251         if (_indentCount < 0) _indentCount = 0;
252
253         // Open the file
254
255         limitFileSize();
256         ofstream of(logFile().c_str(), ios::out|ios::app);
257         if (!of) return;
258
259         // Log the output
260
261         if (lineCharsFlag())    of << headerString(logBits) << "\xC0\xC4\xC4" << s << endl;
262         else                    of << headerString(logBits) << "+-  " << s << endl;
263 }
264
265 // ---------------------------------------------------------------------------------------------------------------------------------
266
267 const   string  &Logger::headerString(const LogFlags logBits) const
268 {
269         static  string  headerString;
270         headerString.erase();
271
272         // Get the string that represents the bits
273
274         switch(logBits)
275         {
276                 case LOG_INDENT : headerString += "> "; break;
277                 case LOG_UNDENT : headerString += "< "; break;
278                 case LOG_ALL    : headerString += "A "; break;
279                 case LOG_CRIT   : headerString += "! "; break;
280                 case LOG_DATA   : headerString += "D "; break;
281                 case LOG_ERR    : headerString += "E "; break;
282                 case LOG_FLOW   : headerString += "F "; break;
283                 case LOG_INFO   : headerString += "I "; break;
284                 case LOG_WARN   : headerString += "W "; break;
285                 default:          headerString += "  "; break;
286         }
287
288         // File string (strip out the path)
289
290         char    temp[1024];
291         int     ix = sourceFile().rfind('\\');
292         ix = (ix == (int) string::npos ? 0: ix+1);
293         sprintf(temp, "%25s[%04d]", sourceFile().substr(ix).c_str(), sourceLine());
294         headerString += temp;
295
296         // Time string (specially formatted to save room)
297
298         time_t  t = time(NULL);
299         struct  tm *tme = localtime(&t);
300         sprintf(temp, "%02d/%02d %02d:%02d ", tme->tm_mon + 1, tme->tm_mday, tme->tm_hour, tme->tm_min);
301         headerString += temp;
302
303         // Spaces for indentation
304
305         memset(temp, ' ', sizeof(temp));
306         temp[_indentCount] = '\0';
307
308         // Add the indentation markers
309
310         int     count = 0;
311         while(count < _indentCount)
312         {
313                 if (lineCharsFlag())    temp[count] = '\xB3';
314                 else                    temp[count] = '|';
315                 count += _indentChars;
316         }
317         headerString += temp;
318
319         return headerString;
320 }
321
322 #else /*Release Build*/
323         std::string release_Null;
324         void            Logger::limitFileSize() const {}
325         const   string          &Logger::headerString(const LogFlags logBits) const{return release_Null;}
326         void            Logger::start(const bool reset){}
327         void            Logger::stop(){}
328         void            Logger::logTex(const string &s, const LogFlags logBits /*= LOG_INFO*/){}
329         void            Logger::logRaw(const string &s){}
330         void            Logger::logHex(const char *buffer, const unsigned int count, const LogFlags logBits /*= LOG_INFO*/){}
331         void            Logger::indent(const string &s, const LogFlags logBits /*= LOG_INDENT*/){}
332         void            Logger::undent(const string &s, const LogFlags logBits /*= LOG_UNDENT*/){}
333 #endif
334 // ---------------------------------------------------------------------------------------------------------------------------------
335 // logger.cpp - End of file
336 // ---------------------------------------------------------------------------------------------------------------------------------
337