3 // A very simple class that does logging to a file.
4 // Currently not thread safe and uses CRT file I/O that will melt down
5 // on Windows if too many files are open.
7 // Copyright 2004 by Keith Vertanen
10 #ifndef __FileLogger_h__
11 #define __FileLogger_h__
18 #include "DasherTypes.h"
19 // Probably better to enable in project settings since FileLogger.h is included from several physical locations.
20 // #define DEBUG_ONLY_LOGGING // Enabled debug logging that has been ifdef'd to prevent performance problems in release build
22 // Macro that lets us wrap log statements that we want compiled out of the code in non-debug builds
23 #ifdef DEBUG_ONLY_LOGGING
24 #define DEBUG_ONLY( s ) s
26 #define DEBUG_ONLY( s )
29 // Macros that can be used to call a globally declared logging object. These
30 // would need to be modified if the global variable is named differently. By
31 // using these macros you are protected from using the logger if it hasn't
32 // yet been created (it should be intialized to NULL). Also has versions that
33 // automatically indicate the log level without sending a parameter.
35 // Note: to use these you must use double open and close parentheses, this
36 // is due to the variable parameter list that logging can take to do printf
37 // style output. GCC supports variadic macros, but Visual Studio doesn't yet.
40 // General purpose version, a log level must be sent in prior to any variable
41 // length parameter list. Usage:
42 // LOG(("my favorite number is %d!", logDEBUG, 42))
44 if (g_pLogger != NULL)\
47 // Debug only logging macro. Usage:
48 // LOG(("my favorite number is %d!", 42))
49 #define LOG_DEBUG( s )\
50 if (g_pLogger != NULL)\
51 g_pLogger->LogDebug s ;
53 // Normal error message macro. Usage:
54 // LOG(("errors number %d!", 42))
55 #define LOG_ERROR( s )\
56 if (g_pLogger != NULL)\
57 g_pLogger->LogNormal s ;
59 // Normal error message macro. Usage:
60 // LOG(("plane %d crashed into plane %d!", 42, 24))
61 #define LOG_CRITICAL( s )\
62 if (g_pLogger != NULL)\
63 g_pLogger->LogCritical s ;
66 // Types required by our high resolution WIN32 timing routines
67 #include "WinCommon.h"
68 typedef std::map
<std::string
, __int64
> MAP_STRING_INT64
;
80 // Bit mask options that are used when we construct object
81 enum eFileLoggerOptions
83 logFunctionEntryExit
= 1,
87 logFunctionTiming
= 16,
95 CFileLogger(const std::string
& strFilenamePath
, eLogLevel level
, int optionsMask
);
99 void Log(const char* szText
, eLogLevel iLogLevel
= logNORMAL
, ...); // Logs a string to our file if it meets or exceeds our logging level
100 void LogDebug(const char* szText
, ...); // Logs debug level messages
101 void LogNormal(const char* szText
, ...); // Logs normal level messages
102 void LogCritical(const char* szText
, ...); // Logs critical level messages
104 // Versions that exists so we can pass in STL strings
105 void Log(const std::string strText
, eLogLevel iLogLevel
= logNORMAL
, ...); // Logs a string to our file if it meets or exceeds our logging level
107 void SetFilename(const std::string
& strFilename
);
108 void SetLogLevel(const eLogLevel newLevel
);
109 void SetFunctionLogging(bool functionLogging
);
111 void LogFunctionEntry(const std::string
& strFunctionName
); // Used by FunctionLogger to log entry to a function
112 void LogFunctionExit(const std::string
& strFunctionName
); // Used by FunctionLogger to log exit from a function
114 void LogFunctionTicks(const std::string
& strFunctionName
, __int64 iTicks
); // Used by FunctionLogger to log how long was spent in a function
116 bool GetFunctionTiming();
118 static std::string
GetFullFilenamePath(std::string strFilename
);
121 std::string m_strFilenamePath
; // Filename and path of our output file
122 eLogLevel m_iLogLevel
; // What level of logging this object should write
123 bool m_bFunctionLogging
; // Whether we will log function entry/exit
124 bool m_bTimeStamp
; // Whether we log the time
125 bool m_bDateStamp
; // Whether we log the date
126 bool m_bFunctionTiming
; // Whether our FunctionLogger objects should do performance timing
127 bool m_bDeleteOldFile
; // Should we delete a previous instance of the log file
128 bool m_bOutputScreen
; // Should we output to stdout as well as the file
129 int m_iFunctionIndentLevel
; // How many nested calls to FunctionLogger we have
131 std::string
GetIndentedString(const std::string
& strText
);
132 std::string
GetTimeDateStamp();
135 MAP_STRING_INT64 m_mapFunctionTicks
; // Keeps track of how many ticks spent in each of our functions (who create a CFunctionLogger object)
140 // Helper class, you can create CFunctionLogger objects at
141 // the top of a function and it will log its entry and exit.
142 class CFunctionLogger
145 CFunctionLogger(const std::string
& strFunctionName
, CFileLogger
* pLogger
);
149 std::string m_strFunctionName
; // Name of the function this object is logging
150 CFileLogger
* m_pLogger
; // Pointer to the logging object to use
153 LARGE_INTEGER m_iStartTicks
; // Tick count at start of timing