00001 #ifndef CS_LOG_H
00002 #define CS_LOG_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #define LOG_GLOBAL_NAME "CSLIB"
00014
00015 #include <stdio.h>
00016 #include <vector>
00017 #include <string>
00018 #include <map>
00019
00020 #ifdef SDL_TIMING
00021 #include <SDL.h>
00022 #endif
00023
00024 class CSLog;
00025 class CSLogCallback;
00026
00027 typedef std::map<std::string, CSLog *> LogMap;
00028 typedef std::vector<CSLogCallback *> CSLogCallbacks;
00029
00030
00031 const int MAX_METHOD_DEPTH = 80;
00032
00033 const int LOG_TYPE_HIGHER_LEVEL = 0;
00034 const int LOG_TYPE_UNIQUE_LEVEL = 1;
00035
00036 const int LOG_LEVEL_DEBUG = 0;
00037 const int LOG_LEVEL_INFO = 1;
00038 const int LOG_LEVEL_WARNING = 2;
00039 const int LOG_LEVEL_ERROR = 3;
00040 const int LOG_LEVEL_NONE = 4;
00041
00042 #ifdef LOG_ENABLE
00043
00044 #ifdef SDL_TIMING
00045 #define NLOG_EXACT_TIMING(logname,b) \
00046 { \
00047 CSLog *localLog = CSLog::getInstance(logname);\
00048 if (localLog) localLog->setExactTiming((b)); \
00049 }
00050 #endif
00051
00052 #define NLOG_TRACK_METHOD(logname,methodname) \
00053 { \
00054 CSLog *localLog = CSLog::getInstance(logname);\
00055 if (localLog)localLog->setOnlyTrackMethod((methodname)); \
00056 }
00057
00058 #define NLOG_TRACK_CLASS(logname,classname) \
00059 { \
00060 CSLog *localLog = CSLog::getInstance(logname);\
00061 if (localLog) localLog->setOnlyTrackClass((classname)); \
00062 }
00063
00064 #define NLOG_SHORT_MESSAGE(logname,b) \
00065 { \
00066 CSLog *localLog = CSLog::getInstance(logname);\
00067 if (localLog) localLog->setShortMessage((b)); \
00068 }
00069
00070 #define NLOG_FUNCTION_TRACKING(logname,b) \
00071 { \
00072 CSLog *localLog = CSLog::getInstance(logname);\
00073 if (localLog) localLog->setFunctionTracking((b)); \
00074 }
00075
00076 #define NLOG_AMESSAGE(logname,text) \
00077 { \
00078 CSLog *localLog = CSLog::getInstance(logname);\
00079 if (localLog) localLog->print(-1, (text)); \
00080 }
00081
00082 #define NLOG_APRINT(logname,text,value) \
00083 { \
00084 CSLog *localLog = CSLog::getInstance(logname);\
00085 if (localLog) localLog->print(-1, (text), (value)); \
00086 }
00087
00088 #define NLOG_ENTER(logname) \
00089 { \
00090 CSLog *localLog = CSLog::getInstance(logname); \
00091 if (localLog) localLog->startFunction(functionName, CLASS); \
00092 }
00093
00094 #define NLOG_EXIT(logname) \
00095 { \
00096 CSLog *localLog = CSLog::getInstance(logname); \
00097 if (localLog) localLog->endFunction(); \
00098 }
00099
00100 #define NLOG_MESSAGE(logname,level,text) \
00101 { \
00102 CSLog *localLog = CSLog::getInstance(logname);\
00103 if (localLog) localLog->print((level), (text)); \
00104 }
00105
00106 #define NLOG_PRINT(logname,level,text,value) \
00107 { \
00108 CSLog *localLog = CSLog::getInstance(logname);\
00109 if (localLog) localLog->print((level), (text), (value)); \
00110 }
00111
00112 #define NLOG_SET_LEVEL(logname,level) \
00113 { \
00114 CSLog *localLog = CSLog::getInstance(logname);\
00115 if (localLog) localLog->setDebugLevel((level)); \
00116 }
00117
00118 #define NLOG_SET_TYPE(logname,type) \
00119 { \
00120 CSLog *localLog = CSLog::getInstance(logname);\
00121 if (localLog) localLog->setDebugType((level)); \
00122 }
00123
00124 #else
00125 #ifdef SDL_TIMING
00126 #define NLOG_EXACT_TIMING(logname,b)
00127 #endif
00128 #define NLOG_TRACK_METHOD(logname,methodname)
00129 #define NLOG_TRACK_CLASS(logname,classname)
00130 #define NLOG_FUNCTION_TRACKING(logname,b)
00131 #define NLOG_SHORT_MESSAGE(logname,b)
00132 #define NLOG_AMESSAGE(logname,text)
00133 #define NLOG_APRINT(logname,text,value)
00134 #define NLOG_ENTER(logname)
00135 #define NLOG_EXIT(logname)
00136 #define NLOG_MESSAGE(logname,level,text)
00137 #define NLOG_PRINT(logname,level,text,value)
00138 #define NLOG_SET_LEVEL(logname,level)
00139 #define NLOG_SET_TYPE(logname,type)
00140 #endif
00141
00142 #define NLOG_DEBUG_MESSAGE(logname,text) NLOG_MESSAGE((logname),(LOG_LEVEL_DEBUG),(text))
00143 #define NLOG_DEBUG_PRINT(logname,text,value) NLOG_PRINT((logname),(LOG_LEVEL_DEBUG),(text),(value))
00144 #define NLOG_INFO_MESSAGE(logname,text) NLOG_MESSAGE((logname),(LOG_LEVEL_INFO),(text))
00145 #define NLOG_INFO_PRINT(logname),text,value) NLOG_PRINT((logname),(LOG_LEVEL_INFO),(text),(value))
00146 #define NLOG_WARN_MESSAGE(logname,text) NLOG_MESSAGE((logname),(LOG_LEVEL_WARNING),(text))
00147 #define NLOG_WARN_PRINT(logname,text,value) NLOG_PRINT((logname),(LOG_LEVEL_WARNING),(text),(value))
00148 #define NLOG_ERROR_MESSAGE(logname,text) NLOG_MESSAGE((logname),(LOG_LEVEL_ERROR),(text))
00149 #define NLOG_ERROR_PRINT(logname),text,value) NLOG_PRINT((logname),(LOG_LEVEL_ERROR),(text),(value))
00150
00151 #ifdef SDL_TIMING
00152 #define LOG_EXACT_TIMING(b) NLOG_EXACT_TIMING(LOG_GLOBAL_NAME,b)
00153 #else
00154 #define LOG_EXACT_TIMING(b)
00155 #endif
00156 #define LOG_ENTER NLOG_ENTER(LOG_GLOBAL_NAME)
00157 #define LOG_EXIT NLOG_EXIT(LOG_GLOBAL_NAME)
00158 #define LOG_MESSAGE(level,text) NLOG_MESSAGE((LOG_GLOBAL_NAME),(level),(text))
00159 #define LOG_PRINT(level,text,value) NLOG_PRINT((LOG_GLOBAL_NAME),(level),(text),(value))
00160 #define LOG_AMESSAGE(text) NLOG_AMESSAGE((LOG_GLOBAL_NAME),(text))
00161 #define LOG_APRINT(text,value) NLOG_APRINT((LOG_GLOBAL_NAME),(text),(value))
00162 #define LOG_DEBUG_MESSAGE(text) NLOG_MESSAGE((LOG_GLOBAL_NAME),(LOG_LEVEL_DEBUG),(text))
00163 #define LOG_DEBUG_PRINT(text,value) NLOG_PRINT((LOG_GLOBAL_NAME),(LOG_LEVEL_DEBUG),(text),(value))
00164 #define LOG_INFO_MESSAGE(text) NLOG_MESSAGE((LOG_GLOBAL_NAME),(LOG_LEVEL_INFO),(text))
00165 #define LOG_INFO_PRINT(text,value) NLOG_PRINT((LOG_GLOBAL_NAME),(LOG_LEVEL_INFO),(text),(value))
00166 #define LOG_WARN_MESSAGE(text) NLOG_MESSAGE((LOG_GLOBAL_NAME),(LOG_LEVEL_WARNING),(text))
00167 #define LOG_WARN_PRINT(text,value) NLOG_PRINT((LOG_GLOBAL_NAME),(LOG_LEVEL_WARNING),(text),(value))
00168 #define LOG_ERROR_MESSAGE(text) NLOG_MESSAGE((LOG_GLOBAL_NAME),(LOG_LEVEL_ERROR),(text))
00169 #define LOG_ERROR_PRINT(text,value) NLOG_PRINT((LOG_GLOBAL_NAME),(LOG_LEVEL_ERROR),(text),(value))
00170 #define LOG_SET_LEVEL(level) NLOG_SET_LEVEL((LOG_GLOBAL_NAME),level)
00171 #define LOG_SET_TYPE(type) NLOG_SET_TYPE((LOG_GLOBAL_NAME),type)
00172 #define LOG_TRACK_METHOD(methodname) NLOG_TRACK_METHOD((LOG_GLOBAL_NAME),methodname)
00173 #define LOG_TRACK_CLASS(classname) NLOG_TRACK_CLASS((LOG_GLOBAL_NAME),classname)
00174 #define LOG_FUNCTION_TRACKING(b) NLOG_FUNCTION_TRACKING((LOG_GLOBAL_NAME),b)
00175 #define LOG_SHORT_MESSAGE(b) NLOG_SHORT_MESSAGE((LOG_GLOBAL_NAME),b)
00176
00177 class CSLogCallback
00178 {
00179 public:
00180 virtual void printLog(const std::string &text) = 0;
00181 };
00182
00183 class CSLog
00184 {
00185 private:
00186 static LogMap mMap;
00187 static std::string mDebugLevelString[];
00188
00189 CSLogCallbacks mCallbacks;
00190 const std::string mIdentifier;
00191 std::string mLogFilename;
00192 std::string mCurrentMethod[MAX_METHOD_DEPTH];
00193 std::string mCurrentClass[MAX_METHOD_DEPTH];
00194 #ifdef SDL_TIMING
00195 long mCurrentTime[MAX_METHOD_DEPTH];
00196 bool mExactTiming;
00197 #endif
00198 bool mFileState;
00199 bool mFunctionTracking;
00200 bool mShortMessage;
00201 int mLogLevel;
00202 int mDebugType;
00203 int mFunctionDepth;
00204 FILE *mDebugFile;
00205 std::string mTrackClassName;
00206 std::string mTrackMethodName;
00207
00208 CSLog(const std::string &identifier);
00209 bool openFile();
00210 bool closeFile();
00211 bool printInternal(int level, const std::string &text);
00212 virtual ~CSLog();
00213 protected:
00214 public:
00215 static CSLog *getInstance(const std::string &identifier);
00216 static void deleteInstance(const std::string &identifier);
00217 void print(int level, const std::string &text);
00218 void print(int level, const std::string &text, int value);
00219 void print(int level, const std::string &text, long value);
00220 void print(int level, const std::string &text, float value);
00221 void startFunction(const std::string &methodName, const std::string &className);
00222 void endFunction();
00223 void setDebugType(int type);
00224 int getDebugType();
00225 void setDebugLevel(int level);
00226 int getDebugLevel();
00227 void addCallback(CSLogCallback *callback);
00228 void removeCallback(CSLogCallback *callback);
00229 void setFunctionTracking(bool b) {mFunctionTracking = b;}
00230 void setShortMessage(bool b) {mShortMessage = b;}
00231
00232
00233
00234
00235 void setOnlyTrackClass(const std::string &className) {mTrackClassName = className;}
00236 void setOnlyTrackMethod(const std::string &methodName) {mTrackMethodName = methodName;}
00237 #ifdef SDL_TIMING
00238 void setExactTiming(bool b) {mExactTiming = b;}
00239 #endif
00240 };
00241 #endif // CS_LOG_H
00242