tagging release
[dasher.git] / trunk / Src / DasherCore / FileLogger.h
blobc06a2ed4864bf77abea9df1404eb4c1271c9aaf7
1 // FileLogger
2 //
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.
6 //
7 // Copyright 2004 by Keith Vertanen
8 //
10 #ifndef __FileLogger_h__
11 #define __FileLogger_h__
13 #include <stdio.h>
14 #include <string>
15 #include <stdarg.h>
16 #include <time.h>
17 #include <map>
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
25 #else
26 #define DEBUG_ONLY( s )
27 #endif
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))
43 #define LOG( s )\
44 if (g_pLogger != NULL)\
45 g_pLogger->Log s ;
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 ;
65 #ifdef WIN32
66 // Types required by our high resolution WIN32 timing routines
68 typedef struct _BinInt32
70 __int32 i32[2];
71 } BigInt32;
73 typedef struct _BigInt64
75 __int64 i64;
76 } BigInt64;
78 typedef union _bigInt
80 BigInt32 int32val;
81 BigInt64 int64val;
82 } BigInt;
84 typedef std::map<std::string, __int64> MAP_STRING_INT64;
86 #endif
89 enum eLogLevel
91 logDEBUG = 0,
92 logNORMAL = 1,
93 logCRITICAL = 2
96 // Bit mask options that are used when we construct object
97 enum eFileLoggerOptions
99 logFunctionEntryExit = 1,
100 logTimeStamp = 2,
101 logDateStamp = 4,
102 logDeleteOldFile = 8,
103 logFunctionTiming = 16,
104 logOutputScreen = 32
106 /// \ingroup Logging
107 /// @{
108 class CFileLogger
110 public:
111 CFileLogger(const std::string& strFilenamePath, eLogLevel level, int optionsMask);
112 ~CFileLogger();
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
129 #ifdef WIN32
130 void LogFunctionTicks(const std::string& strFunctionName, __int64 iTicks); // Used by FunctionLogger to log how long was spent in a function
131 #endif
132 bool GetFunctionTiming();
134 static std::string GetFullFilenamePath(std::string strFilename);
136 private:
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();
150 #ifdef WIN32
151 MAP_STRING_INT64 m_mapFunctionTicks; // Keeps track of how many ticks spent in each of our functions (who create a CFunctionLogger object)
152 #endif
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
160 public:
161 CFunctionLogger(const std::string& strFunctionName, CFileLogger* pLogger);
162 ~CFunctionLogger();
164 private:
165 std::string m_strFunctionName; // Name of the function this object is logging
166 CFileLogger* m_pLogger; // Pointer to the logging object to use
168 #ifdef WIN32
169 BigInt m_iStartTicks; // Tick count at start of timing
170 #endif
173 /// @}
174 #endif