| | |
A Powerful Easy-To-Read Logging System
Please support our C++ advertiser: Intel Parallel Studio Home
Log files are useful things. They are used in programs to display the values or variables or other useful information such as what the program is doing. This is useful is the program crashes at some point or is not behaving as it should. By writing values and events to a log you can check back and see what happened! Most logs are text files, which take a while to search though and arent very easy to debug. HTML files however support different colours for different events (ie green text if it worked, red if its an error....) and images (so you can dump screenshots ect). The following code forms the basis to create a log file in HTML which has icons and font colours to show what type of event happened and also is tabulated so it is easy to read.
To make it work you need 3 bmps to use as icons (i recommend 16x16). I have files for download, check in the main forums. Run it and look at the resulting HTML file. Enjoy!
To make it work you need 3 bmps to use as icons (i recommend 16x16). I have files for download, check in the main forums. Run it and look at the resulting HTML file. Enjoy!
#include <fstream> #include <string> #define APP_NAME "Program name" #define COMPANY_NAME "CompanyX" #define NORMAL_COLOUR "#0000FF" // Blue #define WARNING_COLOUR "#FF8000" // orange #define ERROR_COLOUR "#FF0000" // red using namespace std; string logfile = "log.html"; bool logisopen = 0; void OpenLog(void) { if(!logisopen) { string str = "<html>\n<head>\n<title>"; str += APP_NAME; str += " Logging system ©"; str += COMPANY_NAME; str += "</title>\n</head>\n<body>\n<table>\n<caption><b>"; str += "LOG ENTRIES</b></caption>"; fstream file; file.open(logfile.c_str(), ios::out | ios::trunc); file.seekp(0, ios::beg); file.write(str.c_str(), str.length()); file.close(); logisopen = 1; } } void AddNormalEntryToLog(char* text) { if(logisopen) { string str = "<tr><td><img src=\"log_ok.bmp\"></td><td>"; str += "<font color=\""; str += NORMAL_COLOUR; str += "\"><b>"; str += text; str += "</b></font></td></tr>"; fstream file; file.open(logfile.c_str(), ios::app | ios::out); file.write(str.c_str(), str.length()); file.close(); } } void AddWarningEntryToLog(char* text) { if(logisopen) { string str = "<tr><td><img src=\"log_warning.bmp\"></td><td>"; str += "<font color=\""; str += WARNING_COLOUR; str += "\"><b>"; str += text; str += "</b></font></td></tr>"; fstream file; file.open(logfile.c_str(), ios::app | ios::out); file.write(str.c_str(), str.length()); file.close(); } } void AddErrorEntryToLog(char* text) { if(logisopen) { string str = "<tr><td><img src=\"log_error.bmp\"></td><td>"; str += "<font color=\""; str += ERROR_COLOUR; str += "\"><b>"; str += text; str += "</b></font></td></tr>"; fstream file; file.open(logfile.c_str(), ios::app | ios::out); file.write(str.c_str(), str.length()); file.close(); } } void CloseLog(void) { if(logisopen) { string str = "</table>\n</body>\n</html>"; fstream file; file.open(logfile.c_str(), ios::out | ios::app); file.write(str.c_str(), str.length()); file.close(); } logisopen = 0; } int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { char this_path[MAX_PATH]; char log_txt[MAX_PATH]; HINSTANCE hInstance = GetModuleHandle(NULL); GetModuleFileName(hInstance, this_path, MAX_PATH); strcat(log_txt, "Program started. EXE = "); strcat(log_txt, this_path); OpenLog(); AddNormalEntryToLog(log_txt); AddNormalEntryToLog("Registered Window Class"); AddWarningEntryToLog("WARNING: Could not load data!"); AddWarningEntryToLog("WARNING: Virtual Memory Low"); AddErrorEntryToLog("ERROR: Could not initialise DirectX"); CloseLog(); return 0; }
0
•
•
•
•
Its what the DevC++ Windows Template Gave me!!! i dont actually know yet what the significance of that parameter is and its only used to show the window... 

0
•
•
•
•
PS: This is a windows app! the three bmps you need to make should be called log_ok.bmp, log_warning.bmp and log_error.bmp. You could make these filenames #define'd constants so that someone else may change them....
0
•
•
•
•
Ah ha!! you mean whether its max/min/default! it makes sense now i should have seen it after programming a text editor and using the windows size message.....
0
•
•
•
•
They only take up minute disk space and minimal ram on the html docs! You can always NOT provide a file, then nothing is drawn...
Similar Threads
- How to easy get System Config and Info? (IT Professionals' Lounge)
- Book that is easy to read and understand (C++)
- System Freezes when logging in a power user (Windows NT / 2000 / XP)
- WealthHunt.com - Powerful and easy to remember! (Websites for Sale)
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets



