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 paranethesis, 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
68 typedef struct _BinInt32
73 typedef struct _BigInt64
84 typedef std::map
<std::string
, __int64
> MAP_STRING_INT64
;
96 // Bit mask options that are used when we construct object
97 enum eFileLoggerOptions
99 logFunctionEntryExit
= 1,
102 logDeleteOldFile
= 8,
103 logFunctionTiming
= 16,
111 CFileLogger(const std::string
& strFilenamePath
, eLogLevel level
, int optionsMask
);
115 void Log(const char* szText
, eLogLevel iLogLevel
= logNORMAL
, ...); // Logs a string to our file if it meets our exceeds our logging level
116 void LogDebug(const char* szText
, ...); // Logs debug level messages
117 void LogNormal(const char* szText
, ...); // Logs normal level messages
118 void LogCritical(const char* szText
, ...); // Logs critical level messages
120 // Versions that exists so we can pass in STL strings
121 void Log(const std::string strText
, eLogLevel iLogLevel
= logNORMAL
, ...); // Logs a string to our file if it meets our exceeds our logging level
123 void SetFilename(const std::string
& strFilename
);
124 void SetLogLevel(const eLogLevel newLevel
);
125 void SetFunctionLogging(bool functionLogging
);
127 void LogFunctionEntry(const std::string
& strFunctionName
); // Used by FunctionLogger to log entry to a function
128 void LogFunctionExit(const std::string
& strFunctionName
); // Used by FunctionLogger to log exit from a function
130 void LogFunctionTicks(const std::string
& strFunctionName
, __int64 iTicks
); // Used by FunctionLogger to log how long was spent in a function
132 bool GetFunctionTiming();
134 static std::string
GetFullFilenamePath(std::string strFilename
);
137 std::string m_strFilenamePath
; // Filename and path of our output file
138 eLogLevel m_iLogLevel
; // What level of logging this object should write
139 bool m_bFunctionLogging
; // Whether we will log function entry/exit
140 bool m_bTimeStamp
; // Whether we log the time
141 bool m_bDateStamp
; // Whether we log the date
142 bool m_bFunctionTiming
; // Whether our FunctionLogger objects should do performance timing
143 bool m_bDeleteOldFile
; // Should we delete a previous instance of the log file
144 bool m_bOutputScreen
; // Should we output to stdout as well as the file
145 int m_iFunctionIndentLevel
; // How many nested calls to FunctionLogger we have
147 std::string
GetIndentedString(const std::string
& strText
);
148 std::string
GetTimeDateStamp();
151 MAP_STRING_INT64 m_mapFunctionTicks
; // Keeps track of how many ticks spent in each of our functions (who create a CFunctionLogger object)
156 // Helper class, you can create CFunctionLogger objects at
157 // the top of a function and it will log its entry and exit.
158 class CFunctionLogger
161 CFunctionLogger(const std::string
& strFunctionName
, CFileLogger
* pLogger
);
165 std::string m_strFunctionName
; // Name of the function this object is logging
166 CFileLogger
* m_pLogger
; // Pointer to the logging object to use
169 BigInt m_iStartTicks
; // Tick count at start of timing