// *************************************************************** // Function: GWLPLog.cpp // *************************************************************** ///////////////////////////////////////////////////////////////// //Copyright 2007 GWLP //By: Theora ///////////////////////////////////////////////////////////////// /* Logging class configuration */ //Message type headers #define LOGTH_DEBUG "" #define LOGTH_WARNING "[-] " #define LOGTH_NOTICE "[+] " #define LOGTH_ERROR "[!] " #define LOGTH_ALWAYS "" //Colors #define COLOR_1 "\\red221\\green221\\blue221;" #define COLOR_2 "\\red102\\green102\\blue102;" #define COLOR_3 "\\red180\\green180\\blue0;" #define COLOR_4 "\\red0\\green180\\blue0;" #define COLOR_5 "\\red255\\green0\\blue0;" #define COLOR_6 "\\red221\\green221\\blue221;" #define COLOR_7 "\\red0\\green0\\blue0;" #define COLOR_8 "\\red0\\green0\\blue0;" //Font options #define FONT_FAMILY "Courier New" #define FONT_SIZE 12 //Category options, step 5 #define CAT_STEP 3 //Memory allocation #define MAX_MESSAGE_BUFFER 20000 //Post colon color slot #define POST_COLON 7 ///////////////////////////////////////////////////////////////// //Don't touch below this line. ///////////////////////////////////////////////////////////////// #include "GWLPLog.h" #include #include #include "timer.h" //To add another option, add an entry here and change an x? in Output settings //----------------- #define OP_DEBUG_LEVEL 0//0 = debug level message #define OP_WARNING_LEVEL 1//1 = warning level message #define OP_NOTICE_LEVEL 2//2 = notice level message #define OP_ERROR_LEVEL 3//3 = error level message #define OP_ALWAYS_LEVEL 4//4 = always log level #define OP_C1 5//5 = use color 1 #define OP_C2 6//6 = use color 2 #define OP_C3 7//7 = use color 3 #define OP_C4 8//8 = use color 4 #define OP_C5 9//9 = use color 5 #define OP_C6 10//10 = use color 6 #define OP_C7 11//14 = use color 7 #define OP_C8 12//12 = use color 8 #define OP_HEAD_DEBUG 13//13 = use message header debug #define OP_HEAD_WARNING 14//14 = use message header warning #define OP_HEAD_NOTICE 15//15 = use message header notice #define OP_HEAD_ERROR 16//16 = use message header error #define OP_HEAD_ALWAYS 17//17 = use message header always #define OP_APP_TIME 18//18 = append timestamp to message #define OP_APP_LOCATION 19//19 = append location info to message #define OP_IGNORE_INDENT 20//20 = ignore category indentation #define OP_NEWLINE 21//21 = append a new line to the end #define OP_CCSEMICOLON 22//22 = change color on first encountered semicolon on a line #define OP_USEDEFAULT 23//23 = Use default bitset //Global variables for this function //Empty constructor =( GWLPLog::GWLPLog() { memset(mPFile, 0, 150); memset(mLocFunction, 0, 150); mLine=-1; strcpy(mFileHeader.program, "N/A"); strcpy(mFileHeader.version, "N/A"); } //************************************ // Name: SetLocation // // Sets the location stamp information. // // Parameters: // pFile - File name // pFunction - Function name // line - Line number // pName - Custom name // // Return: // //************************************ void GWLPLog::SetLocation(const char *pFile, const char* pFunction, unsigned long line, const char* pName) { strcpy(this->mPFile, pFile); strcpy(this->mLocFunction, pFunction); if (pName!=NULL) { strcpy(this->mPName, pName); } else { this->mPName[0]=0; } this->mLine=line; } //************************************ // Name: Section // // Write a section header and add it to the stack. // // Parameters: // pString - Section name // options - Options for the section format // // Return: // //************************************ void GWLPLog::Section(const char* pString, bitset<32> options) { float time=TimerGetTime(); SECTION s; strcpy(s.section, pString); s.time=time; mSectionStack.push_front(s); options.set(OP_NEWLINE, true); WriteRaw((char*)pString, options); } //************************************ // Name: SectionOk // // Pops the section stack and outputs section success. // // Parameters: // options - Format options // // Return: // //************************************ void GWLPLog::SectionOk(bitset<32> options) { char mess[100]; float timeElapsed=(float)((TimerGetTime()-mSectionStack.front().time)/1000.0); sprintf(mess, "[OK] in %f seconds.\n", timeElapsed); options.set(OP_NEWLINE, true); WriteRaw(mess, options); mSectionStack.pop_front(); } //************************************ // Name: SectionFail // // Pops the section stack and outputs section failure. // // Parameters: // options - Section format options // // Return: // //************************************ void GWLPLog::SectionFail(bitset<32> options) { char mess[100]; float timeElapsed=(float)((TimerGetTime()-mSectionStack.front().time)/1000.0); sprintf(mess, "[FAIL] in %f seconds.\n", timeElapsed); options.set(OP_NEWLINE, true); WriteRaw(mess, options); mSectionStack.pop_front(); } //************************************ // Name: Write // // Write text to the log file. Using format options. // // Parameters: // options - Format options // pString - String to output // - Document // // Return: // //************************************ void GWLPLog::Write(bitset<32> options, const char* pString, ...) { va_list args; va_start(args, pString); char string[MAX_MESSAGE_BUFFER]; vsprintf(string, pString, args); va_end(args); WriteRaw(string, options); } //************************************ // Name: SetSpecialDefaults // // Set the default formatting. // // Parameters: // catOptions - Category formatting // section - Section formatting // sectionOK - SectionOK formatting // sectionFAIL - SectionFAIL formatting // // Return: // //************************************ void GWLPLog::SetSpecialDefaults(bitset<32> catOptions, bitset<32> section, bitset<32> sectionOK, bitset<32> sectionFAIL) { this->mCategoryOptions=catOptions; this->mSectionOptions=section; this->mSectionOkOptions=sectionOK; this->mSectionFailOptions=sectionFAIL; } //************************************ // Name: SetLevelDefaults // // Sets logging level default formatting. // // Parameters: // debugOptions - Debug level formatting // warningOptions - Waring level formatting // noticeOptions - Notice level formatting // errorOptions - Error level formatting // alwaysOptions - Always level formatting // // Return: // //************************************ void GWLPLog::SetLevelDefaults(bitset<32> debugOptions, bitset<32> warningOptions, bitset<32> noticeOptions, bitset<32> errorOptions, bitset<32> alwaysOptions) { this->mDebugOptions=debugOptions; this->mWarningOptions=warningOptions; this->mNoticeOptions=noticeOptions; this->mErrorOptions=errorOptions; this->mAlwaysOptions=alwaysOptions; } //************************************ // Name: debug // // Output a message at debug level. // // Parameters: // pString - Text string // // Return: // //************************************ void GWLPLog::debug(const char* pString, ...) { va_list args; va_start(args, pString); char string[MAX_MESSAGE_BUFFER]; vsprintf(string, pString, args); va_end(args); WriteRaw(string, mDebugOptions); } //************************************ // Name: warning // // Output a message a warning level. // // Parameters: // pString - Text string // // Return: // //************************************ void GWLPLog::warning(const char* pString, ...) { va_list args; va_start(args, pString); char string[MAX_MESSAGE_BUFFER]; vsprintf(string, pString, args); va_end(args); WriteRaw(string, mWarningOptions); } //************************************ // Name: notice // // Output a message at notice level. // // Parameters: // pString - Text string // // Return: // //************************************ void GWLPLog::notice(const char* pString, ...) { va_list args; va_start(args, pString); char string[MAX_MESSAGE_BUFFER]; vsprintf(string, pString, args); va_end(args); WriteRaw(string, mNoticeOptions); } //************************************ // Name: error // // Output a message at error level. // // Parameters: // pString - Text string // // Return: // //************************************ void GWLPLog::error(const char* pString, ...) { va_list args; va_start(args, pString); char string[MAX_MESSAGE_BUFFER]; vsprintf(string, pString, args); va_end(args); WriteRaw(string, mErrorOptions); } //************************************ // Name: always // // Output a message at always level. // // Parameters: // pString - Text string // // Return: // //************************************ void GWLPLog::always(const char* pString, ...) { va_list args; va_start(args, pString); char string[MAX_MESSAGE_BUFFER]; vsprintf(string, pString, args); va_end(args); WriteRaw(string, mAlwaysOptions); } //************************************ // Name: Section // // Output a section with the default formatting. // // Parameters: // c - Text string // // Return: // //************************************ void GWLPLog::Section(const char *c) { Section(c, mSectionOptions); } //************************************ // Name: SectionOk // // Output sectionOK using default formatting. // // Parameters: // // Return: // //************************************ void GWLPLog::SectionOk() { this->SectionOk(mSectionOkOptions); } //************************************ // Name: SectionFail // // Output sectionFail using default formatting // // Parameters: // // Return: // //************************************ void GWLPLog::SectionFail() { this->SectionFail(mSectionFailOptions); } //************************************ // Name: WriteCategory // // Write category using default formatting. // // Parameters: // pString - Text string // // Return: // //************************************ void GWLPLog::WriteCategory(char* pString) { this->WriteCategory(pString, mCategoryOptions); } //************************************ // Name: Close // // Clean-up and close // // Parameters: // // Return: // //************************************ void GWLPLog::Close() { if (mpFout!=NULL) { mpFout->close(); delete mpFout; } mpFout=NULL; } //************************************ // Name: StepUpCategory // // Moves up one category. // // Parameters: // // Return: // //************************************ void GWLPLog::StepUpCategory() { if (mLogLevel>0) { if (mIndentation-CAT_STEP>=0) { mIndentation-=CAT_STEP; } } } //************************************ // Name: WriteCategory // // Write a category using custom formatting. // // Parameters: // pString - Text string // options - Formatting options // // Return: // //************************************ void GWLPLog::WriteCategory(char* pString, bitset<32> options) { //Make sure logging is enabled if (mLogLevel>0) { mIndentation-=CAT_STEP; if (mIndentation<0){mIndentation=0;} options.set(OP_NEWLINE, true); WriteRaw("", options); WriteRaw(pString, options); mIndentation+=CAT_STEP; } } //************************************ // Name: Init // // Opens and writes the log file header. (Internal only) // // Parameters: // pFile - File name // // Return: // //************************************ bool GWLPLog::Init(char* pFile) { memset(mPName, 0, 150); //Open the RTF file mpFout->open(pFile, ios::app); //Output my file header xD Write(OPT_ALWAYS_LEVEL, " _____ __ __ _ _____ \n"); Write(OPT_ALWAYS_LEVEL, " / ____|\\ \\ / /| | | __ \\ \n"); Write(OPT_ALWAYS_LEVEL, "| | __ \\ \\ /\\ / / | | | |__) |\n"); Write(OPT_ALWAYS_LEVEL, "| | |_ | \\ \\/ \\/ / | | | ___/\n"); Write(OPT_ALWAYS_LEVEL, "| |__| | \\ /\\ / | |____ | | \n"); Write(OPT_ALWAYS_LEVEL, " \\_____| \\/ \\/ |______||_| \n"); Write(OPT_ALWAYS_LEVEL, "File: %s\n", pFile); Write(OPT_ALWAYS_LEVEL, "Program: %s\n", mFileHeader.program); Write(OPT_ALWAYS_LEVEL, "Version: %s\n", mFileHeader.version); return true; } //************************************ // Name: SetFileHeader // // Set file header program and version. // // Parameters: // pProgram - String, program name. // pVersion - String, program version. // // Return: // //************************************ void GWLPLog::SetFileHeader(char* pProgram, char* pVersion) { strcpy(mFileHeader.program, pProgram); strcpy(mFileHeader.version, pVersion); } //************************************ // Name: Open // // Opens a log file // // Parameters: // pFilename - File name // logLevel - Logging levels allowed bitfield. // // Return: // - zero //************************************ long GWLPLog::Open(char* pFilename, unsigned char logLevel) { this->mLogLevel=logLevel; mpFout=new ofstream; mIndentation=0; mUsedHeader=false; Init(pFilename); return 0; } //************************************ // Name: WriteRaw // // Handles actual output to streams using the custom formatting. (Internal use only) // // Parameters: // pString - Text string // options - Formatting options // // Return: // //************************************ void GWLPLog::WriteRaw(char* pString, bitset<32> options) { char css_class[10]; css_class[0]=0; char header[50]; header[0]=0; //Make sure logging is enabled if (mLogLevel>0) { //Verify that logging is enabled for this messages level //Check for error if (options.test(OP_ERROR_LEVEL) && (mLogLevel&LOG_ERRORS)==LOG_ERRORS){} //check for warning else if (options.test(OP_WARNING_LEVEL) && (mLogLevel&LOG_WARNINGS)==LOG_WARNINGS){} //check for notice else if (options.test(OP_NOTICE_LEVEL) && (mLogLevel&LOG_NOTICES)==LOG_NOTICES){} //check for debug else if (options.test(OP_DEBUG_LEVEL) && (mLogLevel&LOG_DEBUGS)==LOG_DEBUGS){} //check for always else if (options.test(OP_ALWAYS_LEVEL)){} else { return; } //Set the header if (options.test(OP_HEAD_DEBUG)){strcpy(header, LOGTH_DEBUG);} else if (options.test(OP_HEAD_WARNING)){strcpy(header, LOGTH_WARNING);} else if (options.test(OP_HEAD_NOTICE)){strcpy(header, LOGTH_NOTICE);} else if (options.test(OP_HEAD_ERROR)){strcpy(header, LOGTH_ERROR);} else if (options.test(OP_HEAD_ALWAYS)){strcpy(header, LOGTH_ALWAYS);} //output the current time if (options.test(OP_APP_TIME)) { if ((pString[0]!='\n')) { time_t ct=time(NULL); struct tm* time_info; time_info = localtime(&ct); char time[100]; strftime(time, 100, "[%H:%M %m/%d/%y] ", time_info); *mpFout << time; } } //If there is indentation write it out now! if (mIndentation>0 && !(options.test(OP_IGNORE_INDENT))) { for (int j=0; jflush(); } }