Annotation of DewFind/LogFile.cpp, revision 1.1
1.1 ! nick 1: // LogFile.cpp: implementation of the LogFile class.
! 2: //
! 3: //////////////////////////////////////////////////////////////////////
! 4: #include "stdafx.h"
! 5: #include "LogFile.h"
! 6:
! 7: #ifdef _DEBUG
! 8: #undef THIS_FILE
! 9: static char THIS_FILE[]=__FILE__;
! 10: #define new DEBUG_NEW
! 11: #endif
! 12:
! 13: //*****************************************************************************************************************************
! 14: //---------------------------------------------------------------PUBLIC-------------------------------------------------------
! 15: //*****************************************************************************************************************************
! 16: //
! 17:
! 18: LogFile::LogFile()
! 19: {
! 20: loaded_ = false;
! 21: }
! 22:
! 23: LogFile::~LogFile()
! 24: {
! 25: Close();
! 26: }
! 27:
! 28: void LogFile::ToggleStatus(bool onoff)
! 29: {
! 30: status_ = onoff;
! 31: }
! 32:
! 33: int LogFile::Init(const CString sFile)
! 34: {
! 35:
! 36: //Make sure log file is not already open
! 37: if (loaded_)
! 38: return false;
! 39:
! 40: //Open file
! 41: if (status_ == true)
! 42: {
! 43:
! 44: hFile_ = CreateFile (sFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,0);
! 45:
! 46:
! 47: if (hFile_ == INVALID_HANDLE_VALUE)
! 48: return false;
! 49:
! 50:
! 51: //Set file loaded flag
! 52: loaded_ = true;
! 53:
! 54: //Set filename
! 55: fileName_ = sFile;
! 56:
! 57: //Write RTF header
! 58: WriteString ("{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0 Courier New;}}\\fs20\n");
! 59:
! 60: //Write colour table
! 61: WriteString ("{\\colortbl;\n"); //Black
! 62: WriteString ("\\red255\\green255\\blue255;\n"); //White
! 63: WriteString ("\\red128\\green128\\blue128;\n"); //Grey
! 64: WriteString ("\\red255\\green0\\blue0;\n"); //Red
! 65: WriteString ("\\red0\\green255\\blue0;\n"); //Green
! 66: WriteString ("\\red0\\green0\\blue255;\n"); //Blue
! 67: WriteString ("\\red0\\green255\\blue255;\n"); //Cyan
! 68: WriteString ("\\red255\\green255\\blue0;\n"); //Yellow
! 69: WriteString ("\\red255\\green0\\blue255;\n"); //Magenta
! 70: WriteString ("\\red128\\green0\\blue0;\n"); //Dark Red
! 71: WriteString ("\\red0\\green128\\blue0;\n"); //Dark Green
! 72: WriteString ("\\red0\\green0\\blue128;\n"); //Dark Blue
! 73: WriteString ("\\red255\\green128\\blue128;\n"); //Light Red
! 74: WriteString ("\\red128\\green255\\blue128;\n"); //Light Green
! 75: WriteString ("\\red128\\green128\\blue255;\n"); //Light Blue
! 76: WriteString ("\\red255\\green128\\blue0;\n"); //Orange
! 77: WriteString ("}\n");
! 78:
! 79: //Success
! 80: };
! 81: return true;
! 82: };
! 83:
! 84: int LogFile::Close()
! 85: {
! 86: if (!loaded_)
! 87: return false;
! 88:
! 89: //Write closing line
! 90: //LogString ("");
! 91: //LogString ("[B]Log Closed.[/B]");
! 92:
! 93: //Write closing brace
! 94: WriteString ("}");
! 95:
! 96: //Close file
! 97: CloseHandle (hFile_);
! 98:
! 99: //Clear file loaded flag
! 100: loaded_ = FALSE;
! 101:
! 102: //Success
! 103: return true;
! 104:
! 105: };
! 106:
! 107:
! 108: bool LogFile::GetState()
! 109: {
! 110: return(loaded_);
! 111: };
! 112:
! 113:
! 114:
! 115: bool LogFile::LogString(CString sText)
! 116: {
! 117: if (!loaded_)
! 118: return false;
! 119:
! 120: //Format string
! 121: sText = DoFormatting (sText);
! 122:
! 123: //Write string to file
! 124: WriteString (sText);
! 125:
! 126: //Success
! 127: return true;
! 128: };
! 129:
! 130: bool LogFile::LogMessage(const CString sSource, CString sMessage, const int x,MESSAGETYPE messageType)
! 131: {
! 132: CString tmp;
! 133: tmp.Format(" %i",x);
! 134: sMessage = sMessage + " " + tmp;
! 135:
! 136: return(LogMessage(sSource,sMessage,messageType));
! 137: }
! 138:
! 139: bool LogFile::LogMessage(const CString sSource, const CString sMessage, MESSAGETYPE messageType)
! 140: {
! 141: CString str;
! 142:
! 143: switch(messageType)
! 144: {
! 145: case MSG_SUCCESS:
! 146: str = "[BLUE]";
! 147: break;
! 148: case MSG_INFO:
! 149: str = "[GREY]";
! 150: break;
! 151: case MSG_WARN:
! 152: str = "[ORANGE]";
! 153: break;
! 154: case MSG_ERROR:
! 155: str = "[RED]";
! 156: break;
! 157: };
! 158:
! 159: str += "[B]<";
! 160: str += sSource;
! 161: str += ">[/B]";
! 162: str += sMessage;
! 163:
! 164: return(LogString(str));
! 165: };
! 166:
! 167: //-------------------------------------PRIVATE-------------------------------------------------------
! 168:
! 169:
! 170: void LogFile::WriteString(const CString sText)
! 171: {
! 172: if (status_ == true)
! 173: {
! 174: DWORD bytesWritten;
! 175: char sTextX[100];
! 176: strcpy(sTextX,sText);
! 177: WriteFile (hFile_, sTextX, (int) sText.GetLength(), &bytesWritten, NULL);
! 178: };
! 179: };
! 180:
! 181:
! 182: CString LogFile::DoFormatting (CString sText)
! 183: {
! 184: //Special Characters
! 185: sText.Replace("\\","\\\\");
! 186: sText.Replace("{","\\{");
! 187: sText.Replace("}","\\}");
! 188:
! 189: //Colors
! 190: sText.Replace("[BLACK]", "\\cf0 ");
! 191: sText.Replace("[WHITE]", "\\cf1 ");
! 192: sText.Replace("[GREY]", "\\cf2 ");
! 193:
! 194: sText.Replace("[RED]", "\\cf3 ");
! 195: sText.Replace("[GREEN]", "\\cf4 ");
! 196: sText.Replace("[BLUE]", "\\cf5 ");
! 197:
! 198: sText.Replace("[CYAN]", "\\cf6 ");
! 199: sText.Replace("[YELLOW]", "\\cf7 ");
! 200: sText.Replace("[MAGENTA]", "\\cf8 ");
! 201: sText.Replace("[DARK_RED]", "\\cf9 ");
! 202: sText.Replace("[DARK_GREEN]", "\\cf10 ");
! 203: sText.Replace("[DARK_BLUE]", "\\cf11 ");
! 204: sText.Replace("[LIGHT_RED]", "\\cf12 ");
! 205: sText.Replace("[LIGHT_GREEN]", "\\cf13 ");
! 206: sText.Replace("[LIGHT_BLUE]", "\\cf14 ");
! 207: sText.Replace("[ORANGE]", "\\cf15 ");
! 208:
! 209: // Text Style
! 210: sText.Replace("[PLAIN]","\\plain ");
! 211: sText.Replace("[B]","\\b ");
! 212: sText.Replace("[/B]","\\b0 ");
! 213: sText.Replace("[I]","\\i ");
! 214: sText.Replace("[/I]","\\i0 ");
! 215: sText.Replace("[U]","\\ul ");
! 216: sText.Replace("[/U]","\\ul0 ");
! 217: sText.Replace("[S]","\\strike ");
! 218: sText.Replace("[/S]","\\strike0 ");
! 219: sText.Replace("[SUB]","\\sub ");
! 220: sText.Replace("[/SUB]","\\sub0 ");
! 221: sText.Replace("[SUPER]","\\super ");
! 222: sText.Replace("[/SUPER]","\\super0 ");
! 223: sText.Replace("[LEFT]", "\\ql ");
! 224: sText.Replace("[RIGHT]", "\\qr ");
! 225: sText.Replace("[CENTER]", "\\qc ");
! 226: sText.Replace("[FULL]", "\\qj ");
! 227: sText.Replace("[TITLE]", "\\fs40 ");
! 228: sText.Replace("[/TITLE]", "\\fs20 ");
! 229: sText.Replace("[H]", "\\fs24 ");
! 230: sText.Replace("[/H]", "\\fs20 ");
! 231:
! 232:
! 233: sText = "{\\pard " + sText;
! 234: sText = sText + "\\par}\n";
! 235: return(sText);
! 236: };
! 237:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>