///////////////////////////////////////////////////////////////////////
// Copyright (C) Andru 2007                                          //
// mail: dr.andru@gmail.com                                          //
// site: http://andru.2x4.ru                                         //
//                                                                   //
// OMEGA Engine is free software.                                    //
//                                                                   //
// You may redistribute it and/or modify it under the terms of the   //
// GNU General Public License, as published by the Free Software     //
// Foundation; either version 2 of the License, or (at your option)  //
// any later version.                                                //
//                                                                   //
// this module is distributed in the hope that it will be useful,    //
// but WITHOUT ANY WARRANTY; without even the implied warranty of    //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              //
// See the GNU General Public License for more details.              //
//                                                                   //
// You should have received a copy of the GNU General Public License //
// along with this module.  If not, write to:                        //
// 	The Free Software Foundation, Inc.,                              //
// 	51 Franklin Street, Fifth Floor                                  //
// 	Boston, MA  02110-1301, USA.                                     //
///////////////////////////////////////////////////////////////////////
unit ENGINE_LOG;

{$I DEFINES.pas}

interface
uses
  minClasses,
  minFile,
  Windows,

  ENGINE_VAR,
  ENGINE_CONST,
  
  UTILS;

procedure LOG_SetOptions(
                          clrBODY    : PChar;
                          clrTIME    : PChar;
                          clrENGINE  : PChar;
                          clrAPP     : PChar;
                          clrCONSOLE : PChar;
                          clrMESS    : PChar;
                          clrWARNING : PChar;
                          clrERROR   : PChar;
                          sTIME      : Byte;
                          sENGINE    : Byte;
                          sAPP       : Byte;
                          sCONSOLE   : Byte;
                          sMESS      : Byte;
                          sWARNING   : Byte;
                          sERROR     : Byte ) stdcall;
procedure LOG_Init( FileName : PChar; Create : Boolean ); stdcall;
procedure LOG_AddLine( Size : Integer; Color : PChar ); stdcall;
procedure LOG_AddMessage( MessageType : Byte; Text : PChar ); stdcall;

procedure LOG_Flush;

procedure htmAddText( Text, Color : String; Size : Integer );
procedure htmAddEnter;
procedure htmEndText;

const
  LOGT_ERROR   = 0;
  LOGT_WARNING = 1;
  LOGT_APP     = 2;
  LOGT_CONSOLE = 3;
  LOGT_ENGINE  = 13;

var
  LogString  : String;
  LogName    : String;
  LogFile    : TextFile;
  LogRewrite : Boolean;

  Color_TIME    : String = 'yellowgreen';
  Color_BODY    : String = '808080';
  Color_ENGINE  : String = '00ff00';
  Color_APP     : String = 'blue';
  Color_CONSOLE : String = 'blue';
  Color_MESS    : String = 'black';
  Color_WARNING : String = 'orange';
  Color_ERROR   : String = 'red';

  Size_TIME     : Byte = 1;
  Size_ENGINE   : Byte = 3;
  Size_APP      : Byte = 3;
  Size_CONSOLE  : Byte = 3;
  Size_MESS     : Byte = 2;
  Size_WARNING  : Byte = 3;
  Size_ERROR    : Byte = 3;

//----------------------------------------------------------------------------//
                                 IMPLEMENTATION
//----------------------------------------------------------------------------//
uses
  ENGINE_UTILS;

procedure LOG_EnableEngineLog( Enable : Boolean );
Begin
app_Log := Enable;
End;

procedure LOG_SetOptions;
Begin

Color_TIME    := clrTIME;
Color_BODY    := clrBODY;
Color_ENGINE  := clrENGINE;
Color_APP     := clrAPP;
Color_CONSOLE := clrCONSOLE;
Color_MESS    := clrMESS;
Color_WARNING := clrWARNING;
Color_ERROR   := clrERROR;

Size_TIME    := sTIME;
Size_ENGINE  := sENGINE;
Size_APP     := sAPP;
Size_CONSOLE := sCONSOLE;
Size_MESS    := sMESS;
Size_WARNING := sWARNING;
Size_ERROR   := sERROR;

End;

procedure LOG_Init;
Begin

FileName := PChar( String( app_Dir ) + String( FileName ) );

LogString := '';
LogString := LogString + '<html>'+ Chr( 13 );
LogString := LogString + '<head><title>' + wnd_ClassName + '</title></head>' + Chr( 13 );
LogString := LogString + '<body bgcolor=' + Color_BODY + '>' + Chr( 13 );
LogString := LogString +
'<table width="100%" border=0 bgcolor=0><tr>' + Chr( 13 ) +
'<td width="100%"><center><font size=4 color=red>&Omega;<font color=white>MEGA Engine ' + OE_VERSION + '</font></center></td>' + Chr( 13 ) +
'</tr></table>' + Chr( 13 );

LogName := FileName;

LogRewrite := Create;
Log_Flush;
LogRewrite := FALSE;

End;

procedure LOG_AddMessage;
function Mili( V : Double ) : String;
Begin
if V < 100000000 Then Result :=             U_IntToStr( Round( V ) );
if V < 10000000  Then Result := '0'       + U_IntToStr( Round( V ) );
if V < 1000000   Then Result := '00'      + U_IntToStr( Round( V ) );
if V < 100000    Then Result := '000'     + U_IntToStr( Round( V ) );
if V < 10000     Then Result := '0000'    + U_IntToStr( Round( V ) );
if V < 1000      Then Result := '00000'   + U_IntToStr( Round( V ) );
if V < 100       Then Result := '000000'  + U_IntToStr( Round( V ) );
if V < 10        Then Result := '0000000' + U_IntToStr( Round( V ) );
End;
  var
    ST : _SYSTEMTIME;
Begin

GetSystemTime( ST );
htmAddText(
            PChar( '[' + Mili( U_GetTime ){
                   U_IntToStr( ST.wHour )   + ':' +
                   U_IntToStr( ST.wMinute ) + ':' +
                   U_IntToStr( ST.wSecond )} + 'ms]' ),
            Color_TIME, Size_TIME );

case MessageType of
LOGT_ERROR  :
  begin
    htmAddText( LOG_ERROR, Color_ERROR, Size_ERROR );
    htmAddText( Text, Color_MESS, Size_MESS );
  end;

LOGT_WARNING  :
  begin
    htmAddText( LOG_WARNING, Color_WARNING, Size_WARNING );
    htmAddText( Text, Color_MESS, Size_MESS );
  end;

LOGT_ENGINE :
  begin
    htmAddText( LOG_ENGINE, Color_ENGINE, Size_ENGINE );
    htmAddText( Text, Color_MESS, Size_MESS );
  end;

LOGT_APP    :
  begin
    htmAddText( LOG_APP, COlor_APP, Size_APP );
    htmAddText( Text, Color_MESS, Size_MESS );
  end;

LOGT_CONSOLE:
  begin
    htmAddText( LOG_CONSOLE, COlor_CONSOLE, Size_CONSOLE );
    htmAddText( Text, Color_MESS, Size_MESS );
  end;
end;

htmEndText;
htmAddEnter;

Log_Flush;

End;

procedure LOG_Flush;
  var
    i  : Integer;
    lF : TMemoryStream;
Begin

lF := TMemoryStream.Create;
if not LogRewrite Then
  lF.LoadFromFile( LogName );
lF.Seek( 0, soEnd );
for i := 1 to Length( LogString ) do
  lF.Write( LogString[ i ], Length( LogString[ i ] ) );
lF.SaveToFile( LogName );
lF.Free;

LogString := '';

End;

procedure htmAddText;
Begin

LogString := LogString +
             '<font size=' + U_IntToStr( Size )  +
             ' color='     + Color + '>' + Text;

End;

procedure htmAddEnter;
Begin

LogString := LogString + '<br>' + Chr( 13 );

End;

procedure htmEndText;
Begin

LogString := LogString + '</font>' + Chr( 13 );

End;

procedure LOG_AddLine;
Begin

LogString := LogString + '<hr color=' + Color + ' size=' + U_IntToStr( Size ) + ' noshade>' + Chr( 13 );
Log_Flush;

End;

end.
