#ifndef __LOG_CLASS_HEADER__ #define __LOG_CLASS_HEADER__ /** * This file contains class for logging and filtering output. * This header file also includes a debug macro which * tracks any possible memory leaks within the program. * * @author Tomasz Przedzinski * @date 14 November 2009 */ #include #include #include #include #include #include using std::stringstream; using std::string; using std::streambuf; using std::ostream; using std::list; using std::cout; using std::endl; namespace Tauolapp { class Log { public: /** Shows the summary of all messages. */ static void Summary(); /** Shows the summary at the end of the program. */ static void SummaryAtExit() { atexit(Summary); } /** Adds the decay to the counter. The type is: 0 - gun, 1 - no mothers & grandmothers, 2 - no mothers, 3 - ok. */ static void AddDecay(int type); /** Four logging entries. Usage: Log::Info()<<"Logging some info: "<<8<<" > "<<7.9< *PointerList; public: #ifdef _LOG_DEBUG_MODE_ static void NewPointer(unsigned long address, unsigned long size, const char *file, unsigned long line) { if(!PointerList) { PointerList = new list(); atexit(PrintAllocatedPointers); } Pointer *info = new Pointer(); info->address = address; info->size = size; info->line = line; strncpy(info->file, file, 63); PointerList->push_front(info); } static void DeletePointer(unsigned long address) { if(!PointerList) return; for(list::iterator i = PointerList->begin(); i!=PointerList->end(); i++) { if((*i)->address == address) { PointerList->remove((*i)); break; } } } static bool PointerCompare(Pointer *one, Pointer *two) { int eq = strcmp(one->file,two->file); if(eq<0) return true; else if(eq>0) return false; return (one->line <= two->line); } static void PrintAllocatedPointers() { if(!PointerList) return; int pointers=0,buf=0; unsigned long total=0; char *lastS=" "; int lastL=0; if(PointerList->size()==0) { cout<<"----------------------------UNFREED MEMORY POINTERS----------------------------\n"; cout<<" ... NONE ...\n"; cout<<"-------------------------------------------------------------------------------\n"; return; } PointerList->sort(PointerCompare); cout<<"---------------------------UNFREED MEMORY POINTERS---------------------------\n"; for(list::iterator i = PointerList->begin(); i!=PointerList->end(); i++) { total+=(*i)->size; ++pointers; if(strcmp(lastS,(*i)->file)==0) { if(lastL==(*i)->line) { printf("%56s%10lub (%lu)\n"," ",(*i)->size,(*i)->address); continue; } } lastS=(*i)->file; lastL=(*i)->line; printf("%s%n:",(*i)->file,&buf); printf("%-*lu%10lub (%lu)\n",55-buf,(*i)->line,(*i)->size,(*i)->address); } cout<