1 // ---------------------------------------------------------------------------------------------------------------------------------
4 // | | ___ __ _ __ _ ___ _ __ ___ _ __ _ __
5 // | |/ _ \ / _` |/ _` |/ _ \ '__| / __| '_ \| '_ \
6 // | | (_) | (_| | (_| | __/ | _ | (__| |_) | |_) |
7 // |_|\___/ \__, |\__, |\___|_| (_) \___| .__/| .__/
11 // Generic informational logging class
13 // ---------------------------------------------------------------------------------------------------------------------------------
15 // Restrictions & freedoms pertaining to usage and redistribution of this software:
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.
24 // For more information, visit HTTP://www.FluidStudios.com
26 // ---------------------------------------------------------------------------------------------------------------------------------
27 // Originally created on 07/06/2000 by Paul Nettle
29 // Copyright 2000, Fluid Studios, Inc., all rights reserved.
30 // ---------------------------------------------------------------------------------------------------------------------------------
32 //#include "stdafx.h" // If this line gives you an error, comment it out
41 #include <sys/types.h>
48 // ---------------------------------------------------------------------------------------------------------------------------------
49 // The global logger object
50 // ---------------------------------------------------------------------------------------------------------------------------------
54 // ---------------------------------------------------------------------------------------------------------------------------------
56 void Logger::limitFileSize() const
58 if (fileSizeLimit() < 0) return;
60 _stat(logFile().c_str(), &sbuf);
61 if (sbuf.st_size > fileSizeLimit())
63 unlink(logFile().c_str());
67 // ---------------------------------------------------------------------------------------------------------------------------------
69 void Logger::start(const bool reset)
71 if (logStarted()) return;
76 time_t t = time(NULL);
77 string ts = asctime(localtime(&t));
78 ts[ts.length() - 1] = 0;
83 ofstream of(logFile().c_str(), ios::out | (reset ? ios::trunc : ios::app));
86 of << "---------------------------------------------- Log begins on " << ts << " ----------------------------------------------" << endl;
89 // ---------------------------------------------------------------------------------------------------------------------------------
93 // Automatic One time only startup
95 if (!logStarted()) return;
100 time_t t = time(NULL);
101 string ts = asctime(localtime(&t));
102 ts.erase(ts.length() - 1);
107 ofstream of(logFile().c_str(), ios::out|ios::app);
110 of << "----------------------------------------------- Log ends on " << ts << " -----------------------------------------------" << endl << endl;
113 // ---------------------------------------------------------------------------------------------------------------------------------
115 void Logger::logTex(const string &s, const LogFlags logBits)
117 // If the bits don't match the mask, then bail
119 if (!(logBits & logMask())) return;
124 ofstream of(logFile().c_str(), ios::out|ios::app);
127 // Output to the log file
129 of << headerString(logBits) << s << endl;
132 // ---------------------------------------------------------------------------------------------------------------------------------
134 void Logger::logRaw(const string &s)
139 ofstream of(logFile().c_str(), ios::out|ios::app);
147 // ---------------------------------------------------------------------------------------------------------------------------------
149 void Logger::logHex(const char *buffer, const unsigned int count, const LogFlags logBits)
151 // No input? No output
155 // If the bits don't match the mask, then bail
157 if (!(logBits & logMask())) return;
162 ofstream of(logFile().c_str(), ios::out|ios::app);
167 unsigned int logged = 0;
168 while(logged < count)
170 // One line at a time...
174 // The number of characters per line
176 unsigned int hexLength = 20;
178 // Default the buffer
181 for (i = 0; i < hexLength; i++)
186 for (i = 0; i < hexLength; i++)
191 // Fill it in with real data
193 for (i = 0; i < hexLength && logged < count; i++, logged++)
195 unsigned char byte = buffer[logged];
196 unsigned int index = i * 3;
198 // The hex characters
200 const string hexlist("0123456789ABCDEF");
201 line[index+0] = hexlist[byte >> 4];
202 line[index+1] = hexlist[byte & 0xf];
204 // The ascii characters
206 if (byte < 0x20 || byte > 0x7f) byte = '.';
207 line[(hexLength*3)+i+0] = byte;
210 // Write it to the log file
212 of << headerString(logBits) << line << endl;
216 // ---------------------------------------------------------------------------------------------------------------------------------
218 void Logger::indent(const string &s, const LogFlags logBits)
220 // If the bits don't match the mask, then bail
222 if (!(logBits & logMask())) return;
227 ofstream of(logFile().c_str(), ios::out|ios::app);
232 if (lineCharsFlag()) of << headerString(logBits) << "\xDA\xC4\xC4" << s << endl;
233 else of << headerString(logBits) << "+- " << s << endl;
237 _indentCount += _indentChars;
240 // ---------------------------------------------------------------------------------------------------------------------------------
242 void Logger::undent(const string &s, const LogFlags logBits)
244 // If the bits don't match the mask, then bail
246 if (!(logBits & logMask())) return;
248 // Undo the indentation
250 _indentCount -= _indentChars;
251 if (_indentCount < 0) _indentCount = 0;
256 ofstream of(logFile().c_str(), ios::out|ios::app);
261 if (lineCharsFlag()) of << headerString(logBits) << "\xC0\xC4\xC4" << s << endl;
262 else of << headerString(logBits) << "+- " << s << endl;
265 // ---------------------------------------------------------------------------------------------------------------------------------
267 const string &Logger::headerString(const LogFlags logBits) const
269 static string headerString;
270 headerString.erase();
272 // Get the string that represents the bits
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;
288 // File string (strip out the path)
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;
296 // Time string (specially formatted to save room)
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;
303 // Spaces for indentation
305 memset(temp, ' ', sizeof(temp));
306 temp[_indentCount] = '\0';
308 // Add the indentation markers
311 while(count < _indentCount)
313 if (lineCharsFlag()) temp[count] = '\xB3';
314 else temp[count] = '|';
315 count += _indentChars;
317 headerString += temp;
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*/){}
334 // ---------------------------------------------------------------------------------------------------------------------------------
335 // logger.cpp - End of file
336 // ---------------------------------------------------------------------------------------------------------------------------------