#include "debug.h" #include #include using namespace std; #ifdef _DEBUG FILE *g_fDebug = 0; time_t g_lastTime = time(0); unsigned int iIndent = 0; pthread_mutex_t m_oneAtATime = PTHREAD_MUTEX_INITIALIZER; bool DebugPrint(const char *sMessage, const int iType) { if (!sMessage) return false; bool bFinishLine = true; bool bShowIndent = true; bool bShowMessage = true; char *sPrefix = 0, *sPostfix = 0; unsigned int iNewIndent=iIndent; switch (iType) { case DEBUG_ERROR: { //breakpoint here to highlight all error states in debug sPrefix = "ERROR ************** "; sPostfix = " ***************"; break; } case DEBUG_LINE_QUALIFIED: { sPrefix = "{"; sPostfix = "}"; break; } case DEBUG_FUNCTIONSTART: { sPrefix = "["; sPostfix = "]"; } case DEBUG_BLOCKSTART: { iNewIndent = iIndent+1; break; } case DEBUG_CHECK: { sPostfix = "..."; //bFinishLine=false; iNewIndent = iIndent+1; break; } case DEBUG_BLOCKEND: case DEBUG_FUNCTIONEND: { bFinishLine = false; bShowIndent = false; bShowMessage = false; if (iIndent > 0) iNewIndent = --iIndent; break; } case DEBUG_RESULT: { //bShowIndent=false; sPrefix = "("; sPostfix = ")"; if (iIndent > 0) iNewIndent = --iIndent; break; } } //indent char sIndent[1024]; if (iIndent > 1023) iIndent = 1023; memset(sIndent, '\t', iIndent); sIndent[iIndent] = '\0'; pthread_mutex_lock(&m_oneAtATime); time_t now = time(0); unsigned int seconds = now - g_lastTime; g_lastTime = now; cout << "(" << seconds << ") -> " << sMessage << "\n"; fflush(stdout); //file output if (g_fDebug) { if (bShowIndent) fputs(sIndent, g_fDebug); //indent if (sPrefix) fputs(sPrefix, g_fDebug); //pre if (bShowMessage) fputs(sMessage, g_fDebug); //message if (sPostfix) fputs(sPostfix, g_fDebug); //post if (bFinishLine) fputs("\n", g_fDebug); //EOL fflush(g_fDebug); } iIndent = iNewIndent; pthread_mutex_unlock(&m_oneAtATime); return true; } bool DebugInit() { if (!g_fDebug) _FOPEN(g_fDebug, "tips.log", "w" ); //creates the log file in the source code directory return (g_fDebug != NULL); } bool DebugFinalise() { if (g_fDebug) {fclose(g_fDebug); g_fDebug = 0;} return true; } #endif