A Powerful Easy-To-Read Logging System

1o0oBhP 0 Tallied Votes 182 Views Share

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! :)

#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 &copy";
        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

Sorry! couldnt work out how to upload fiiles so ull have to draw some icons yourself!

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This idea is good. FunsterStil sounds Dutch to me!

1o0oBhP 4 Posting Pro in Training

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... :)

1o0oBhP 4 Posting Pro in Training

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....

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

After three bottles of Dutch beer I figured it out, FunsterStil means Windows Style.

1o0oBhP 4 Posting Pro in Training

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.....

teddy 0 Newbie Poster

The logs are always good. I would not implement icons, they take a lot of resources.

1o0oBhP 4 Posting Pro in Training

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...

Rodan 0 Newbie Poster

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?

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.