944,155 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 5887
  • C++ RSS
0

A Powerful Easy-To-Read Logging System

by on Dec 19th, 2004
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!
C++ Code Snippet (Toggle Plain Text)
  1. #include <fstream>
  2. #include <string>
  3.  
  4. #define APP_NAME "Program name"
  5. #define COMPANY_NAME "CompanyX"
  6.  
  7. #define NORMAL_COLOUR "#0000FF" // Blue
  8. #define WARNING_COLOUR "#FF8000" // orange
  9. #define ERROR_COLOUR "#FF0000" // red
  10.  
  11. using namespace std;
  12. string logfile = "log.html";
  13. bool logisopen = 0;
  14.  
  15. void OpenLog(void)
  16. {
  17. if(!logisopen)
  18. {
  19. string str = "<html>\n<head>\n<title>";
  20. str += APP_NAME;
  21. str += " Logging system &copy";
  22. str += COMPANY_NAME;
  23. str += "</title>\n</head>\n<body>\n<table>\n<caption><b>";
  24. str += "LOG ENTRIES</b></caption>";
  25.  
  26. fstream file;
  27. file.open(logfile.c_str(), ios::out | ios::trunc);
  28. file.seekp(0, ios::beg);
  29. file.write(str.c_str(), str.length());
  30. file.close();
  31.  
  32. logisopen = 1;
  33. }
  34. }
  35.  
  36. void AddNormalEntryToLog(char* text)
  37. {
  38. if(logisopen)
  39. {
  40. string str = "<tr><td><img src=\"log_ok.bmp\"></td><td>";
  41. str += "<font color=\"";
  42. str += NORMAL_COLOUR;
  43. str += "\"><b>";
  44. str += text;
  45. str += "</b></font></td></tr>";
  46.  
  47. fstream file;
  48. file.open(logfile.c_str(), ios::app | ios::out);
  49. file.write(str.c_str(), str.length());
  50. file.close();
  51. }
  52. }
  53. void AddWarningEntryToLog(char* text)
  54. {
  55. if(logisopen)
  56. {
  57. string str = "<tr><td><img src=\"log_warning.bmp\"></td><td>";
  58. str += "<font color=\"";
  59. str += WARNING_COLOUR;
  60. str += "\"><b>";
  61. str += text;
  62. str += "</b></font></td></tr>";
  63.  
  64. fstream file;
  65. file.open(logfile.c_str(), ios::app | ios::out);
  66. file.write(str.c_str(), str.length());
  67. file.close();
  68. }
  69. }
  70. void AddErrorEntryToLog(char* text)
  71. {
  72. if(logisopen)
  73. {
  74. string str = "<tr><td><img src=\"log_error.bmp\"></td><td>";
  75. str += "<font color=\"";
  76. str += ERROR_COLOUR;
  77. str += "\"><b>";
  78. str += text;
  79. str += "</b></font></td></tr>";
  80.  
  81. fstream file;
  82. file.open(logfile.c_str(), ios::app | ios::out);
  83. file.write(str.c_str(), str.length());
  84. file.close();
  85. }
  86. }
  87.  
  88. void CloseLog(void)
  89. {
  90. if(logisopen)
  91. {
  92. string str = "</table>\n</body>\n</html>";
  93.  
  94. fstream file;
  95. file.open(logfile.c_str(), ios::out | ios::app);
  96. file.write(str.c_str(), str.length());
  97. file.close();
  98. }
  99.  
  100. logisopen = 0;
  101. }
  102.  
  103. int WINAPI WinMain (HINSTANCE hThisInstance,
  104. HINSTANCE hPrevInstance,
  105. LPSTR lpszArgument,
  106. int nFunsterStil)
  107.  
  108. {
  109.  
  110. char this_path[MAX_PATH];
  111. char log_txt[MAX_PATH];
  112. HINSTANCE hInstance = GetModuleHandle(NULL);
  113. GetModuleFileName(hInstance, this_path, MAX_PATH);
  114. strcat(log_txt, "Program started. EXE = ");
  115. strcat(log_txt, this_path);
  116.  
  117. OpenLog();
  118. AddNormalEntryToLog(log_txt);
  119. AddNormalEntryToLog("Registered Window Class");
  120. AddWarningEntryToLog("WARNING: Could not load data!");
  121. AddWarningEntryToLog("WARNING: Virtual Memory Low");
  122. AddErrorEntryToLog("ERROR: Could not initialise DirectX");
  123.  
  124. CloseLog();
  125. return 0;
  126. }
Comments on this Code Snippet
Dec 19th, 2004
0

Re: A Powerful Easy-To-Read Logging System

Sorry! couldnt work out how to upload fiiles so ull have to draw some icons yourself!
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 19th, 2004
0

Re: A Powerful Easy-To-Read Logging System

This idea is good. FunsterStil sounds Dutch to me!
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Dec 20th, 2004
0

Re: A Powerful Easy-To-Read Logging System

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...
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 20th, 2004
0

Re: A Powerful Easy-To-Read Logging System

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....
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 21st, 2004
0

Re: A Powerful Easy-To-Read Logging System

After three bottles of Dutch beer I figured it out, FunsterStil means Windows Style.
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Dec 21st, 2004
0

Re: A Powerful Easy-To-Read Logging System

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.....
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Jan 19th, 2005
0

Re: A Powerful Easy-To-Read Logging System

The logs are always good. I would not implement icons, they take a lot of resources.
Newbie Poster
Teddy is offline Offline
2 posts
since Jan 2005
Mar 23rd, 2005
0

Re: A Powerful Easy-To-Read Logging System

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...
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Jun 17th, 2005
0

Re: A Powerful Easy-To-Read Logging System

I get the following error code when I try to complie this source code??? I did a straight copy and past into Dev-C++.

"103 C:\Programs I Made\Logging File through HTML.cpp `WINAPI' does not name a type"

Any ideas?
Newbie Poster
Rodan is offline Offline
3 posts
since Jun 2005
Message:
Previous Thread in C++ Forum Timeline: High School student need help for C++ on final project
Next Thread in C++ Forum Timeline: Total Newbie Could Use A Pointer In The Right Direction





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC