{ $Id: PDOLogging.pas 23 2007-01-19 22:06:28Z jmarino $

############################################################################################

	Pascal Data Objects Library
	Copyright © 2006 John Marino, http://www.synsport.com
	Project site: http://pdo.sourceforge.net

	Note: This file contains code fragments from Zeoslib
	Copyright © 1999-2004 Sergey Seroukhov

############################################################################################

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library 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
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

############################################################################################}


unit PDOLogging;

interface

{$I directives.inc}

uses SysUtils;

type

  {** Defines a time or the message. }
  TZLoggingCategory = (lcConnect, lcDisconnect, lcTransaction, lcExecute, lcOther,
  lcPrepStmt, lcExecPrepStmt);

  {** Defines a object for logging event. }
  TZLoggingEvent = class (TObject)
  private
    FCategory: TZLoggingCategory;
    FProtocol: string;
    FMessage: string;
    FErrorCode: Integer;
    FError: string;
    FTimestamp: TDateTime;
  public
    constructor Create(Category: TZLoggingCategory; Protocol: string;
      Msg: string; ErrorCode: Integer; Error: string);

    function AsString: string;

    property Category: TZLoggingCategory read FCategory;
    property Protocol: string read FProtocol;
    property Message: string read FMessage;
    property ErrorCode: Integer read FErrorCode;
    property Error: string read FError;
    property Timestamp: TDateTime read FTimestamp;
  end;

  {** Defines an interface to accept logging events. }
  IZLoggingListener = interface (IInterface)
    ['{53559F5F-AC22-4DDC-B2EA-45D21ADDD2D4}']

    procedure LogEvent(Event: TZLoggingEvent);
  end;

  TZAbstractLoggingListener = class(TInterfacedObject, IZLoggingListener)
  public
    procedure LogEvent (Event: TZLoggingEvent); virtual; abstract;
  end;

implementation

{ TZLoggingEvent }

{**
  Constructs this logging event.
  @param Protocol a DBC protocol.
  @param Msg a description message.
  @param ErrorCode an error code.
  @param Error an error message.
}
constructor TZLoggingEvent.Create(Category: TZLoggingCategory;
  Protocol: string; Msg: string; ErrorCode: Integer; Error: string);
begin
  FCategory := Category;
  FProtocol := Protocol;
  FMessage := Msg;
  FErrorCode := ErrorCode;
  FError := Error;
  FTimestamp := Now;
end;

{**
  Gets a string representation for this event.
  @returns a string representation.
}
function TZLoggingEvent.AsString: string;
begin
  Result := FormatDateTime('yyyy-mm-dd hh:mm:ss', FTimestamp) + ' cat: ';
  case FCategory of
    lcConnect: Result := Result + 'Connect';
    lcDisconnect: Result := Result + 'Disconnect';
    lcTransaction: Result := Result + 'Transaction';
    lcExecute: Result := Result + 'Execute';
    lcPrepStmt: Result := Result + 'Prepared_Stmt';
    lcExecPrepStmt: Result := Result + 'Exec_Prep_Stmt';
    else Result := Result + 'Other';
  end;
  if Protocol <> '' then
    Result := Result + ', proto: ' + FProtocol;
  Result := Result + ', msg: ' + FMessage;
  if (FErrorCode <> 0) or (FError <> '') then
  begin
    Result := Result + ', errcode: ' + IntToStr(FErrorCode)
      + ', error: ' + FError;
  end;
end;

end.

