File:  [Local Repository] / DewFind / LogFile.cpp
Revision 1.1: download - view: text, annotated - select for diffs
Wed Mar 2 17:51:54 2011 UTC (13 years, 8 months ago) by nick
CVS tags: MAIN, HEAD
Initial revision

// LogFile.cpp: implementation of the LogFile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LogFile.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//*****************************************************************************************************************************
//---------------------------------------------------------------PUBLIC-------------------------------------------------------
//*****************************************************************************************************************************
//

LogFile::LogFile()
{
	loaded_ = false;
}

LogFile::~LogFile()
{
	Close();
}

void LogFile::ToggleStatus(bool onoff)
{
  status_ = onoff;
}

int LogFile::Init(const CString sFile)
{

    //Make sure log file is not already open
    if (loaded_)
        return false;

    //Open file
    if (status_ == true)
	{

		hFile_ = CreateFile (sFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,0);

		
		if (hFile_ == INVALID_HANDLE_VALUE)
			return false;


		//Set file loaded flag
		loaded_ = true;

		//Set filename
		fileName_ = sFile;

		//Write RTF header
		WriteString ("{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0 Courier New;}}\\fs20\n");

		//Write colour table
		WriteString ("{\\colortbl;\n");                     //Black
		WriteString ("\\red255\\green255\\blue255;\n");     //White
		WriteString ("\\red128\\green128\\blue128;\n");     //Grey
		WriteString ("\\red255\\green0\\blue0;\n");         //Red
		WriteString ("\\red0\\green255\\blue0;\n");         //Green
		WriteString ("\\red0\\green0\\blue255;\n");         //Blue
		WriteString ("\\red0\\green255\\blue255;\n");       //Cyan
		WriteString ("\\red255\\green255\\blue0;\n");       //Yellow
		WriteString ("\\red255\\green0\\blue255;\n");       //Magenta
		WriteString ("\\red128\\green0\\blue0;\n");         //Dark Red
		WriteString ("\\red0\\green128\\blue0;\n");         //Dark Green
		WriteString ("\\red0\\green0\\blue128;\n");         //Dark Blue
		WriteString ("\\red255\\green128\\blue128;\n");     //Light Red
		WriteString ("\\red128\\green255\\blue128;\n");     //Light Green
		WriteString ("\\red128\\green128\\blue255;\n");     //Light Blue
		WriteString ("\\red255\\green128\\blue0;\n");       //Orange
		WriteString ("}\n");

		//Success
	};
    return true;
};
	
int LogFile::Close()
{
	if (!loaded_)
        return false;

    //Write closing line
    //LogString ("");
    //LogString ("[B]Log Closed.[/B]");

    //Write closing brace
    WriteString ("}");

    //Close file
    CloseHandle (hFile_);

    //Clear file loaded flag
    loaded_ = FALSE;

    //Success
    return true;

};


bool LogFile::GetState()
{
    return(loaded_);
};



bool LogFile::LogString(CString sText)
{
	if (!loaded_)
        return false;

    //Format string
    sText = DoFormatting (sText);

    //Write string to file
    WriteString (sText);

    //Success
    return true;
};

bool LogFile::LogMessage(const CString sSource, CString sMessage, const int x,MESSAGETYPE messageType)
{
	CString tmp;
	tmp.Format(" %i",x);
	sMessage = sMessage + " " + tmp;

	return(LogMessage(sSource,sMessage,messageType));
}

bool LogFile::LogMessage(const CString sSource, const CString sMessage, MESSAGETYPE messageType)
{
	CString str;

	switch(messageType)
	{
	case MSG_SUCCESS:
		str = "[BLUE]";
		break;
	case MSG_INFO:
		str = "[GREY]";
		break;
	case MSG_WARN:
		str = "[ORANGE]";
		break;
	case MSG_ERROR:
		str = "[RED]";
		break;
	};

	str += "[B]<";
	str += sSource;
	str += ">[/B]";
	str += sMessage;

	return(LogString(str));
};

//-------------------------------------PRIVATE-------------------------------------------------------


void LogFile::WriteString(const CString sText)
{
    if (status_ == true)
	{
		DWORD bytesWritten;
		char sTextX[100];
		strcpy(sTextX,sText);
		WriteFile (hFile_, sTextX, (int) sText.GetLength(), &bytesWritten, NULL);
	};
};


CString LogFile::DoFormatting (CString sText)
{
	//Special Characters
	sText.Replace("\\","\\\\");
	sText.Replace("{","\\{");
	sText.Replace("}","\\}");
	
	//Colors
    sText.Replace("[BLACK]", "\\cf0 ");
    sText.Replace("[WHITE]", "\\cf1 ");
    sText.Replace("[GREY]", "\\cf2 ");
    
	sText.Replace("[RED]", "\\cf3 ");
    sText.Replace("[GREEN]", "\\cf4 ");
    sText.Replace("[BLUE]", "\\cf5 ");
    
	sText.Replace("[CYAN]", "\\cf6 ");
    sText.Replace("[YELLOW]", "\\cf7 ");
    sText.Replace("[MAGENTA]", "\\cf8 ");
    sText.Replace("[DARK_RED]", "\\cf9 ");
    sText.Replace("[DARK_GREEN]", "\\cf10 ");
    sText.Replace("[DARK_BLUE]", "\\cf11 ");
    sText.Replace("[LIGHT_RED]", "\\cf12 ");
    sText.Replace("[LIGHT_GREEN]", "\\cf13 ");
    sText.Replace("[LIGHT_BLUE]", "\\cf14 ");
    sText.Replace("[ORANGE]", "\\cf15 ");

	// Text Style
    sText.Replace("[PLAIN]","\\plain ");
	sText.Replace("[B]","\\b ");
    sText.Replace("[/B]","\\b0 ");
	sText.Replace("[I]","\\i ");
    sText.Replace("[/I]","\\i0 ");
    sText.Replace("[U]","\\ul ");
    sText.Replace("[/U]","\\ul0 ");
    sText.Replace("[S]","\\strike ");
    sText.Replace("[/S]","\\strike0 ");
	sText.Replace("[SUB]","\\sub ");
    sText.Replace("[/SUB]","\\sub0 ");
    sText.Replace("[SUPER]","\\super ");
    sText.Replace("[/SUPER]","\\super0 ");
    sText.Replace("[LEFT]", "\\ql ");
    sText.Replace("[RIGHT]", "\\qr ");
    sText.Replace("[CENTER]", "\\qc ");
    sText.Replace("[FULL]", "\\qj ");
    sText.Replace("[TITLE]", "\\fs40 ");
    sText.Replace("[/TITLE]", "\\fs20 ");
    sText.Replace("[H]", "\\fs24 ");
    sText.Replace("[/H]", "\\fs20 ");
	
	
	sText = "{\\pard " + sText;
	sText = sText + "\\par}\n";
	return(sText);
};


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>