1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
|
#include "../Common/Common.h"
#include <cstring>
#include "FileLogger.h"
// Track memory leaks on Windows to the line that new'd the memory
#ifdef _WIN32
#ifdef _DEBUG
#define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#endif
#ifdef _WIN32
#include <sys/timeb.h>
#else
#include <sys/time.h>
#endif
CFileLogger::CFileLogger(const std::string& strFilenamePath, eLogLevel iLogLevel, int iOptionsMask)
{
m_strFilenamePath = "";
m_iLogLevel = iLogLevel;
m_iFunctionIndentLevel = 0;
m_bFunctionLogging = false;
m_bTimeStamp = false;
m_bDateStamp = false;
m_bDeleteOldFile = false;
m_bFunctionTiming = false;
m_bOutputScreen = false;
// See what options are set in our bit mask options parameter
if (iOptionsMask & logFunctionEntryExit)
m_bFunctionLogging = true;
if (iOptionsMask & logTimeStamp)
m_bTimeStamp = true;
if (iOptionsMask & logDateStamp)
m_bDateStamp = true;
if (iOptionsMask & logDeleteOldFile)
m_bDeleteOldFile = true;
if (iOptionsMask & logFunctionTiming)
m_bFunctionTiming = true;
if (iOptionsMask & logOutputScreen)
m_bOutputScreen = true;
// On windows anyway if somebody can open up a file with CreateFile() in a different
// directory and cause the working directory to change. We don't want our log file
// moving around, so we'll find a absolute path when we are created and stick to
// that for the remainder of our life.
m_strFilenamePath = GetFullFilenamePath(strFilenamePath);
// See if we should get rid of any existing filename with our given name. This prevents having
// to remember to delete the file before every new debugging run.
if (m_bDeleteOldFile)
{
FILE* fp = NULL;
fp = fopen(m_strFilenamePath.c_str(), "w");
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
}
CFileLogger::~CFileLogger()
{
#ifdef _WIN32
if (m_bFunctionTiming)
{
// Dump the results of our function timing logging
MAP_STRING_INT64::iterator map_iter;
Log("%-60s%20s%10s", logNORMAL, "Function","Ticks", "Percent");
Log("%-60s%20s%10s", logNORMAL, "--------","-----", "-------");
__int64 iMaxTicks = 0;
// First pass to count the max ticks
// We assume that there was a function logger on the outer most (main) program.
// This allows the percent reflect the relative time spent inside emedded calls.
for (map_iter = m_mapFunctionTicks.begin(); map_iter != m_mapFunctionTicks.end(); map_iter++)
{
if (map_iter->second > iMaxTicks)
iMaxTicks = map_iter->second;
}
for (map_iter = m_mapFunctionTicks.begin(); map_iter != m_mapFunctionTicks.end(); map_iter++)
{
std::string name = map_iter->first;
__int64 iTicks = map_iter->second;
Log("%-60s%20I64Ld%10.2f", logNORMAL, name.c_str(), iTicks, (double) iTicks / (double) iMaxTicks * (double) 100.0);
}
}
#endif
}
// Changes the filename of this logging object
void CFileLogger::SetFilename(const std::string& strFilename)
{
m_strFilenamePath = strFilename;
// See if we should get rid of any existing filename with our given name. This prevents having
// to remember to delete the file before every new debugging run.
if (m_bDeleteOldFile)
{
FILE* fp = NULL;
fp = fopen(m_strFilenamePath.c_str(), "w");
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
}
// Logs a string to our file. eLogLevel specifies the importance of this message, we
// only write to the log file if it is at least as important as the level set in the
// constructor. Accepts printf style formatting in the first string which must be
// filled with the variable parameter list at the end.
// NOTE: Currently not thread safe!
void CFileLogger::Log(const char* szText, eLogLevel iLogLevel, ...)
{
va_list args;
if ((m_strFilenamePath.length() > 0) &&
(m_iLogLevel <= iLogLevel) &&
(szText != NULL))
{
FILE* fp = NULL;
fp = fopen(m_strFilenamePath.c_str(), "a");
std::string strTimeStamp = GetTimeDateStamp();
if (fp != NULL)
{
std::string strIndented = strTimeStamp + GetIndentedString(szText) + "\n";
va_start(args, iLogLevel);
vfprintf(fp, strIndented.c_str(), args);
va_end(args);
// Optionally we can output message to stdout
if (m_bOutputScreen)
{
va_start(args, iLogLevel);
vprintf(strIndented.c_str(), args);
va_end(args);
}
fclose(fp);
fp = NULL;
}
}
}
// Overloaded version that takes a STL::string
void CFileLogger::Log(const std::string strText, eLogLevel iLogLevel, ...)
{
va_list args;
if ((m_strFilenamePath.length() > 0) &&
(m_iLogLevel <= iLogLevel))
{
FILE* fp = NULL;
fp = fopen(m_strFilenamePath.c_str(), "a");
std::string strTimeStamp = GetTimeDateStamp();
if (fp != NULL)
{
std::string strIndented = strTimeStamp + GetIndentedString(strText) + "\n";
va_start(args, iLogLevel);
vfprintf(fp, strIndented.c_str(), args);
va_end(args);
// Optionally we can output message to stdout
if (m_bOutputScreen)
{
va_start(args, iLogLevel);
vprintf(strIndented.c_str(), args);
va_end(args);
}
fclose(fp);
fp = NULL;
}
}
}
// Version that assume log level is logDEBUG
void CFileLogger::LogDebug(const char* szText, ...)
{
// Note: it would be nice not to duplicate code, but the variable
// parameter list makes this problematic.
va_list args;
if ((m_strFilenamePath.length() > 0) &&
(m_iLogLevel == logDEBUG) &&
(szText != NULL))
{
FILE* fp = NULL;
fp = fopen(m_strFilenamePath.c_str(), "a");
std::string strTimeStamp = GetTimeDateStamp();
if (fp != NULL)
{
std::string strIndented = strTimeStamp + GetIndentedString(szText) + "\n";
va_start(args, szText);
vfprintf(fp, strIndented.c_str(), args);
va_end(args);
// Optionally we can output message to stdout
if (m_bOutputScreen)
{
va_start(args, szText);
vprintf(strIndented.c_str(), args);
va_end(args);
}
fclose(fp);
fp = NULL;
}
}
}
// Version that assume log level is logNormal
void CFileLogger::LogNormal(const char* szText, ...)
{
// Note: it would be nice not to duplicate code, but the variable
// parameter list makes this problematic.
va_list args;
if ((m_strFilenamePath.length() > 0) &&
(m_iLogLevel <= logNORMAL) &&
(szText != NULL))
{
FILE* fp = NULL;
fp = fopen(m_strFilenamePath.c_str(), "a");
std::string strTimeStamp = GetTimeDateStamp();
if (fp != NULL)
{
std::string strIndented = strTimeStamp + GetIndentedString(szText) + "\n";
va_start(args, szText);
vfprintf(fp, strIndented.c_str(), args);
va_end(args);
// Optionally we can output message to stdout
if (m_bOutputScreen)
{
va_start(args, szText);
vprintf(strIndented.c_str(), args);
va_end(args);
}
fclose(fp);
fp = NULL;
}
}
}
// Version that assume log level is logCRITICAL
void CFileLogger::LogCritical(const char* szText, ...)
{
// Note: it would be nice not to duplicate code, but the variable
// parameter list makes this problematic.
va_list args;
// Always log critical messages
if ((m_strFilenamePath.length() > 0) &&
(szText != NULL))
{
FILE* fp = NULL;
fp = fopen(m_strFilenamePath.c_str(), "a");
std::string strTimeStamp = GetTimeDateStamp();
if (fp != NULL)
{
std::string strIndented = strTimeStamp + GetIndentedString(szText) + "\n";
va_start(args, szText);
vfprintf(fp, strIndented.c_str(), args);
va_end(args);
// Optionally we can output message to stdout
if (m_bOutputScreen)
{
va_start(args, szText);
vprintf(strIndented.c_str(), args);
va_end(args);
}
fclose(fp);
fp = NULL;
}
}
}
// Logs entry into a particular function
void CFileLogger::LogFunctionEntry(const std::string& strFunctionName)
{
if (m_bFunctionLogging)
{
std::string strStart = "start: ";
strStart += strFunctionName;
Log(strStart.c_str());
m_iFunctionIndentLevel++;
}
}
// Logs exit into a particular function
void CFileLogger::LogFunctionExit(const std::string& strFunctionName)
{
if (m_bFunctionLogging)
{
m_iFunctionIndentLevel--;
std::string strEnd = "end: ";
strEnd += strFunctionName;
Log(strEnd.c_str());
}
}
#ifdef _WIN32
void CFileLogger::LogFunctionTicks(const std::string& strFunctionName, __int64 iTicks)
{
__int64 iCurrent;
iCurrent = m_mapFunctionTicks[strFunctionName];
iCurrent = iCurrent + iTicks;
m_mapFunctionTicks[strFunctionName] = iCurrent;
}
#endif
// Gets an indented version of the function name
std::string CFileLogger::GetIndentedString(const std::string& strText)
{
std::string strIndented = "";
for (int i = 0; i < m_iFunctionIndentLevel; i++)
strIndented += " ";
strIndented += strText;
return strIndented;
}
bool CFileLogger::GetFunctionTiming()
{
return m_bFunctionTiming;
}
// Utility method that converts a filename into a fully qualified
// path and filename on Windows. This can be used to make sure
// a relative filename stays pointed at the same location despite
// changes in the working directory.
std::string CFileLogger::GetFullFilenamePath(std::string strFilename)
{
#ifdef _WIN32
// Windows code
const int MAX_PATH_LENGTH = 1024;
#ifdef _UNICODE
// In Unicode, we need the parameters to GetFullPathName() in wide characters
wchar_t szPath[MAX_PATH_LENGTH];
wchar_t* pszFilePart = NULL;
wchar_t wstrFilenamePath[MAX_PATH_LENGTH];
char szResult[MAX_PATH_LENGTH];
unsigned int i = 0;
for (i = 0; i < strFilename.length(); i++)
wstrFilenamePath[i] = (wchar_t) strFilename[i];
wstrFilenamePath[i] = '\0';
::GetFullPathName(wstrFilenamePath, MAX_PATH_LENGTH, szPath, &pszFilePart);
i = 0;
while (szPath[i] != '\0')
{
szResult[i] = (char) szPath[i];
i++;
}
szResult[i] = '\0';
return szResult;
#else
// Using normal non-unicode strings
char szPath[MAX_PATH_LENGTH];
char* pszFilePart = NULL;
::GetFullPathName(strFilename.c_str(), MAX_PATH_LENGTH, szPath, &pszFilePart);
return szPath;
#endif
#else
// Non-windows code
return strFilename;
#endif
}
/////////////////////////////////////// CFunctionLogger /////////////////////////////////////////////////////////////
CFunctionLogger::CFunctionLogger(const std::string& strFunctionName, CFileLogger* pLogger)
{
m_pLogger = pLogger;
if ((m_pLogger != NULL) && (strFunctionName.length() > 0))
{
m_strFunctionName = strFunctionName;
if (!m_pLogger->GetFunctionTiming())
m_pLogger->LogFunctionEntry(m_strFunctionName);
else {
#ifdef _WIN32
QueryPerformanceCounter(&m_iStartTicks);
#endif
}
}
}
CFunctionLogger::~CFunctionLogger()
{
if ((m_pLogger != NULL) && (m_strFunctionName.length() > 0))
{
if (!m_pLogger->GetFunctionTiming())
m_pLogger->LogFunctionExit(m_strFunctionName);
else
{
#ifdef _WIN32
LARGE_INTEGER iEndTicks;
QueryPerformanceCounter(&iEndTicks);
// Add our total ticks to the tracking map object in the logger object
m_pLogger->LogFunctionTicks(m_strFunctionName,
iEndTicks.QuadPart - m_iStartTicks.QuadPart);
#endif
}
}
}
// Update what log level this object is using
void CFileLogger::SetLogLevel(const eLogLevel iNewLevel)
{
m_iLogLevel = iNewLevel;
}
// Update whether function entry/exit is logged
void CFileLogger::SetFunctionLogging(bool bFunctionLogging)
{
m_bFunctionLogging = bFunctionLogging;
}
// Gets the time and/or date stamp as specified
// by our construction options.
std::string CFileLogger::GetTimeDateStamp()
{
std::string strTimeStamp = "";
if ((m_bTimeStamp) || (m_bDateStamp))
{
#ifdef _WIN32
struct timeb sTimeBuffer;
#else
struct timeval sTimeBuffer;
struct timezone sTimezoneBuffer;
time_t t;
#endif
char* szTimeLine = NULL;
#ifdef _WIN32
ftime(&sTimeBuffer);
szTimeLine = ctime(&(sTimeBuffer.time));
#else
gettimeofday(&sTimeBuffer, &sTimezoneBuffer);
t = sTimeBuffer.tv_sec;
szTimeLine = ctime(&t);
#endif
// Format is:
// Wed Jun 22 10:22:00 2005
// 0123456789012345678901234
if ((szTimeLine != NULL) && (strlen(szTimeLine) > 23))
{
if (m_bDateStamp)
{
for (int i = 4; i < 10; i++)
strTimeStamp += szTimeLine[i];
for (int i = 19; i < 24; i++)
strTimeStamp += szTimeLine[i];
if (m_bTimeStamp)
strTimeStamp += " ";
}
if (m_bTimeStamp)
{
for (int i = 11; i < 19; i++)
strTimeStamp += szTimeLine[i];
strTimeStamp += ".";
char strMs[16];
#ifdef _WIN32
sprintf(strMs, "%d", sTimeBuffer.millitm);
#else
sprintf(strMs, "%d", static_cast<int>(sTimeBuffer.tv_usec / 1000));
#endif
if (strlen(strMs) == 1)
strTimeStamp += "00";
else if (strlen(strMs) == 2)
strTimeStamp += "0";
strTimeStamp += strMs;
}
strTimeStamp += "\t";
}
}
return strTimeStamp;
}
|