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! :)
A Powerful Easy-To-Read Logging System
#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;
}
1o0oBhP 4 Posting Pro in Training
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
1o0oBhP 4 Posting Pro in Training
1o0oBhP 4 Posting Pro in Training
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
1o0oBhP 4 Posting Pro in Training
teddy 0 Newbie Poster
1o0oBhP 4 Posting Pro in Training
Rodan 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.