Added comments
This commit is contained in:
parent
f19f0ad275
commit
25f2ef8070
|
@ -36,6 +36,11 @@
|
||||||
{$codepage utf8}
|
{$codepage utf8}
|
||||||
{$h+}
|
{$h+}
|
||||||
{ namespace Renegade.Logger }
|
{ namespace Renegade.Logger }
|
||||||
|
{ Abstract Logger Class }
|
||||||
|
{
|
||||||
|
If you don't need any custom code for the procedures Emergency to Debug
|
||||||
|
you can extend this class and only implement the Log procedure.
|
||||||
|
}
|
||||||
unit Logger.AbstractLogger;
|
unit Logger.AbstractLogger;
|
||||||
|
|
||||||
interface
|
interface
|
||||||
|
@ -87,7 +92,7 @@ begin
|
||||||
LOG_INFO: Result := 'Info';
|
LOG_INFO: Result := 'Info';
|
||||||
LOG_DEBUG: Result := 'Debug';
|
LOG_DEBUG: Result := 'Debug';
|
||||||
else
|
else
|
||||||
Result := 'Unkown';
|
Result := 'Unknown';
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,9 @@
|
||||||
{$mode objfpc}
|
{$mode objfpc}
|
||||||
{$codepage utf8}
|
{$codepage utf8}
|
||||||
{$h+}
|
{$h+}
|
||||||
|
{ namespace Renegade.Logger }
|
||||||
|
{ Console Log Handler }
|
||||||
|
{ This handler just write messages to the console or terminal. }
|
||||||
unit Logger.ConsoleHandler;
|
unit Logger.ConsoleHandler;
|
||||||
|
|
||||||
interface
|
interface
|
||||||
|
@ -46,14 +49,14 @@ uses
|
||||||
|
|
||||||
type
|
type
|
||||||
ConsoleHandler = class(TObject, LoggingHandlerInterface)
|
ConsoleHandler = class(TObject, LoggingHandlerInterface)
|
||||||
Private
|
private
|
||||||
LogIdentifier : UTF8String;
|
LogIdentifier: UTF8String;
|
||||||
Public
|
public
|
||||||
constructor Create();
|
constructor Create();
|
||||||
destructor Destroy();
|
destructor Destroy();
|
||||||
function Open(Identifier: UTF8String): boolean;
|
function Open(Identifier: UTF8String): boolean;
|
||||||
function Close(): boolean;
|
function Close(): boolean;
|
||||||
function Write(const LogData: UTF8String): boolean;
|
function Write(const LogData: UTF8String): boolean;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
@ -68,9 +71,10 @@ begin
|
||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function ConsoleHandler.Open(Identifier: UTF8String) : boolean;
|
function ConsoleHandler.Open(Identifier: UTF8String): boolean;
|
||||||
begin
|
begin
|
||||||
LogIdentifier := Identifier;
|
LogIdentifier := Identifier;
|
||||||
|
Result := True;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function ConsoleHandler.Close(): boolean;
|
function ConsoleHandler.Close(): boolean;
|
||||||
|
@ -80,7 +84,7 @@ end;
|
||||||
|
|
||||||
function ConsoleHandler.Write(const LogData: UTF8String): boolean;
|
function ConsoleHandler.Write(const LogData: UTF8String): boolean;
|
||||||
var
|
var
|
||||||
LogMessage : UTF8String;
|
LogMessage: UTF8String;
|
||||||
begin
|
begin
|
||||||
LogMessage := Format('%S[%D] %S', [LogIdentifier, GetProcessId(), LogData]);
|
LogMessage := Format('%S[%D] %S', [LogIdentifier, GetProcessId(), LogData]);
|
||||||
Writeln(LogMessage);
|
Writeln(LogMessage);
|
||||||
|
|
|
@ -35,45 +35,52 @@
|
||||||
{$mode objfpc}
|
{$mode objfpc}
|
||||||
{$codepage utf8}
|
{$codepage utf8}
|
||||||
{$h+}
|
{$h+}
|
||||||
Unit Logger.FileHandler;
|
{ namespace Renegade.Logger }
|
||||||
|
{ FileHandler Log Class }
|
||||||
|
{
|
||||||
|
This class write a file to the filename passed to the constructor,
|
||||||
|
it just implements the StreamHandler class and passes a filename
|
||||||
|
to the StreamHandler filename constructor.
|
||||||
|
}
|
||||||
|
unit Logger.FileHandler;
|
||||||
|
|
||||||
interface
|
interface
|
||||||
|
|
||||||
Uses
|
uses
|
||||||
Classes,
|
Classes,
|
||||||
SysUtils,
|
SysUtils,
|
||||||
Logger.HandlerInterface,
|
Logger.HandlerInterface,
|
||||||
Logger.StreamHandler;
|
Logger.StreamHandler;
|
||||||
|
|
||||||
Type FileHandler = class(StreamHandler, LoggingHandlerInterface)
|
type
|
||||||
Public
|
FileHandler = class(StreamHandler, LoggingHandlerInterface)
|
||||||
constructor Create( const FileName: UTF8String);
|
public
|
||||||
function Open(Identifier: UTF8String): boolean;
|
constructor Create(const FileName: UTF8String);
|
||||||
function Close(): boolean;
|
function Open(Identifier: UTF8String): boolean;
|
||||||
function Write(const LogData: UTF8String): boolean;
|
function Close(): boolean;
|
||||||
end;
|
function Write(const LogData: UTF8String): boolean;
|
||||||
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
constructor FileHandler.Create( const FileName: UTF8String);
|
constructor FileHandler.Create(const FileName: UTF8String);
|
||||||
begin
|
begin
|
||||||
inherited Create(FileName);
|
inherited Create(FileName);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function FileHandler.Open(Identifier: UTF8String): boolean;
|
function FileHandler.Open(Identifier: UTF8String): boolean;
|
||||||
begin
|
begin
|
||||||
Result := inherited Open(Identifier);
|
Result := inherited Open(Identifier);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function FileHandler.Close() : boolean;
|
function FileHandler.Close(): boolean;
|
||||||
begin
|
begin
|
||||||
Result := inherited Close;
|
Result := inherited Close;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function FileHandler.Write(const LogData: UTF8String): boolean;
|
function FileHandler.Write(const LogData: UTF8String): boolean;
|
||||||
begin
|
begin
|
||||||
Result := inherited Write(LogData);
|
Result := inherited Write(LogData);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
End.
|
end.
|
||||||
|
|
||||||
|
|
|
@ -46,8 +46,19 @@ uses
|
||||||
type
|
type
|
||||||
LoggingHandlerInterface = interface
|
LoggingHandlerInterface = interface
|
||||||
['{C6E95830-5B13-4A51-BC03-757A3A1C779F}']
|
['{C6E95830-5B13-4A51-BC03-757A3A1C779F}']
|
||||||
|
|
||||||
|
{ This method is called to do anything special to
|
||||||
|
open the log, an example would be making a
|
||||||
|
connection to a database or opening a file. }
|
||||||
function Open(Identifier: UTF8String): boolean;
|
function Open(Identifier: UTF8String): boolean;
|
||||||
|
|
||||||
|
{ This method is called to do any closing of the log,
|
||||||
|
an example would be closing a database or a file. }
|
||||||
function Close(): boolean;
|
function Close(): boolean;
|
||||||
|
|
||||||
|
{ This method is called to write log data,
|
||||||
|
and example would be inserting database records
|
||||||
|
or writing a line to a file. }
|
||||||
function Write(const LogData: UTF8String): boolean;
|
function Write(const LogData: UTF8String): boolean;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
|
@ -60,19 +60,46 @@ const
|
||||||
LOG_PRIMASK = $07; // Internal Unix Use;
|
LOG_PRIMASK = $07; // Internal Unix Use;
|
||||||
|
|
||||||
type
|
type
|
||||||
|
{ Every procedure takes an array of const as Context, this is for any
|
||||||
|
extra information that doesn't fit well in the message,
|
||||||
|
(e.g. User id, menu name, etc.) }
|
||||||
LoggerInterface = interface
|
LoggerInterface = interface
|
||||||
['{3220524c-fae0-11e6-8b70-9c5c8e742ab6}']
|
['{3220524c-fae0-11e6-8b70-9c5c8e742ab6}']
|
||||||
|
|
||||||
|
// Sets the Logging Handler to use. (e.g. SysLogHandler, FileHandler, etc.)
|
||||||
procedure SetHandler(Handler: LoggingHandlerInterface);
|
procedure SetHandler(Handler: LoggingHandlerInterface);
|
||||||
|
|
||||||
|
// System is unusable
|
||||||
procedure Emergency(Message: UTF8String; Context: array of const);
|
procedure Emergency(Message: UTF8String; Context: array of const);
|
||||||
|
|
||||||
|
// Action must be taken immediately
|
||||||
procedure Alert(Message: UTF8String; Context: array of const);
|
procedure Alert(Message: UTF8String; Context: array of const);
|
||||||
|
|
||||||
|
// Critical conditions
|
||||||
procedure Critical(Message: UTF8String; Context: array of const);
|
procedure Critical(Message: UTF8String; Context: array of const);
|
||||||
|
|
||||||
|
// Error conditions
|
||||||
procedure Error(Message: UTF8String; Context: array of const);
|
procedure Error(Message: UTF8String; Context: array of const);
|
||||||
|
|
||||||
|
// Warning conditions
|
||||||
procedure Warning(Message: UTF8String; Context: array of const);
|
procedure Warning(Message: UTF8String; Context: array of const);
|
||||||
|
|
||||||
|
// Normal, but significant, condition
|
||||||
procedure Notice(Message: UTF8String; Context: array of const);
|
procedure Notice(Message: UTF8String; Context: array of const);
|
||||||
|
|
||||||
|
// Informational message
|
||||||
procedure Info(Message: UTF8String; Context: array of const);
|
procedure Info(Message: UTF8String; Context: array of const);
|
||||||
|
|
||||||
|
// Debug-level message
|
||||||
procedure Debug(Message: UTF8String; Context: array of const);
|
procedure Debug(Message: UTF8String; Context: array of const);
|
||||||
|
|
||||||
|
{ General logging procedure
|
||||||
|
LogLevel : array of const containing the different log levels
|
||||||
|
Message : The Log Message }
|
||||||
procedure Log(LogLevel: LogLevels; Message: UTF8String;
|
procedure Log(LogLevel: LogLevels; Message: UTF8String;
|
||||||
Context: array of const);
|
Context: array of const);
|
||||||
|
|
||||||
|
// Holds the current logging handler.
|
||||||
property LoggingHandler: LoggingHandlerInterface write SetHandler;
|
property LoggingHandler: LoggingHandlerInterface write SetHandler;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,14 @@
|
||||||
{$mode objfpc}
|
{$mode objfpc}
|
||||||
{$codepage utf8}
|
{$codepage utf8}
|
||||||
{$h+}
|
{$h+}
|
||||||
|
{ Namespace Renegade.Logger }
|
||||||
|
{ Null Handler Log Class }
|
||||||
|
{ This class doesn't do anything. }
|
||||||
|
{
|
||||||
|
If you want to implement a logger, but you aren't sure what kind of logger
|
||||||
|
you need, you can implement this. You could probably also use it for testing
|
||||||
|
purposes
|
||||||
|
}
|
||||||
unit Logger.NullHandler;
|
unit Logger.NullHandler;
|
||||||
|
|
||||||
interface
|
interface
|
||||||
|
@ -46,12 +54,12 @@ uses
|
||||||
|
|
||||||
type
|
type
|
||||||
NullHandler = class(TObject, LoggingHandlerInterface)
|
NullHandler = class(TObject, LoggingHandlerInterface)
|
||||||
Public
|
public
|
||||||
constructor Create();
|
constructor Create();
|
||||||
destructor Destroy();
|
destructor Destroy();
|
||||||
function Open(Identifier: UTF8String): boolean;
|
function Open(Identifier: UTF8String): boolean;
|
||||||
function Close(): boolean;
|
function Close(): boolean;
|
||||||
function Write(const LogData: UTF8String): boolean;
|
function Write(const LogData: UTF8String): boolean;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
@ -66,7 +74,7 @@ begin
|
||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function NullHandler.Open(Identifier: UTF8String) : boolean;
|
function NullHandler.Open(Identifier: UTF8String): boolean;
|
||||||
begin
|
begin
|
||||||
Result := True;
|
Result := True;
|
||||||
end;
|
end;
|
||||||
|
|
|
@ -35,7 +35,12 @@
|
||||||
{$mode objfpc}
|
{$mode objfpc}
|
||||||
{$codepage utf8}
|
{$codepage utf8}
|
||||||
{$h+}
|
{$h+}
|
||||||
|
{ namespace Renegade.Logger }
|
||||||
|
{ StreamHandler Logger Class }
|
||||||
|
{
|
||||||
|
This class should take any TStream decendant and use it to
|
||||||
|
write log messages.
|
||||||
|
}
|
||||||
unit Logger.StreamHandler;
|
unit Logger.StreamHandler;
|
||||||
|
|
||||||
interface
|
interface
|
||||||
|
|
|
@ -33,28 +33,41 @@
|
||||||
{ `--- ---' }
|
{ `--- ---' }
|
||||||
{*******************************************************}
|
{*******************************************************}
|
||||||
{$mode objfpc}
|
{$mode objfpc}
|
||||||
{$interfaces corba}
|
|
||||||
{$linklib c}
|
{$linklib c}
|
||||||
{$codepage utf8}
|
{$codepage utf8}
|
||||||
{$h+}
|
{$h+}
|
||||||
|
{
|
||||||
|
Unix System Log Handler.
|
||||||
|
The constructor takes one of the defined Logging Facilities.
|
||||||
|
}
|
||||||
unit Logger.SysLogHandler;
|
unit Logger.SysLogHandler;
|
||||||
|
|
||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes,
|
Classes,
|
||||||
FPJson,
|
|
||||||
Logger.HandlerInterface;
|
Logger.HandlerInterface;
|
||||||
|
|
||||||
const
|
const
|
||||||
LOG_PID = $01; // log the pid with each message
|
// log the pid with each message
|
||||||
LOG_CONS = $02; // log on the console if errors in sending
|
LOG_PID = $01;
|
||||||
LOG_ODELAY = $04; // delay open until first syslog() (default)
|
// Write directly to system console if there is an error
|
||||||
LOG_NDELAY = $08; // don't delay open
|
// while sending to system logger.
|
||||||
LOG_NOWAIT = $10; // don't wait for console forks; (DEPRECATED)
|
LOG_CONS = $02;
|
||||||
LOG_PERROR = $20; // log to stderr as well
|
// The converse of LOG_NDELAY; opening of the connection is delayed until
|
||||||
|
// syslog() is called. (This is the default, and need not be specified.)
|
||||||
|
LOG_ODELAY = $04;
|
||||||
|
// Open the connection immediately (normally, the connection is opened when
|
||||||
|
// the first message is logged).
|
||||||
|
LOG_NDELAY = $08;
|
||||||
|
// Don't wait for child processes that may have been created while logging
|
||||||
|
// the message. (The GNU C library does not create a child process, so this
|
||||||
|
// option has no effect on Linux.)
|
||||||
|
LOG_NOWAIT = $10;
|
||||||
|
// log to stderr as well
|
||||||
|
LOG_PERROR = $20;
|
||||||
|
|
||||||
|
{ Logging Facilities }
|
||||||
LOG_KERN = 0 shl 3; // kernel messages
|
LOG_KERN = 0 shl 3; // kernel messages
|
||||||
LOG_USER = 1 shl 3; // random user-level messages
|
LOG_USER = 1 shl 3; // random user-level messages
|
||||||
LOG_MAIL = 2 shl 3; // mail system
|
LOG_MAIL = 2 shl 3; // mail system
|
||||||
|
@ -77,7 +90,6 @@ type
|
||||||
procedure SetFacility(const UnixFacility: longint);
|
procedure SetFacility(const UnixFacility: longint);
|
||||||
public
|
public
|
||||||
constructor Create(const LoggingFacility: longint);
|
constructor Create(const LoggingFacility: longint);
|
||||||
|
|
||||||
function Open(Identifier: UTF8String): boolean;
|
function Open(Identifier: UTF8String): boolean;
|
||||||
function Close(): boolean;
|
function Close(): boolean;
|
||||||
function Write(const LogData: UTF8String): boolean;
|
function Write(const LogData: UTF8String): boolean;
|
||||||
|
@ -86,7 +98,8 @@ type
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure closelog; cdecl; external;
|
procedure closelog; cdecl; external;
|
||||||
procedure openlog(__ident: PChar; __option: longint; __facilit: longint); cdecl; external;
|
procedure openlog(__ident: PChar; __option: longint; __facilit: longint);
|
||||||
|
cdecl; external;
|
||||||
function setlogmask(__mask: longint): longint; cdecl; external;
|
function setlogmask(__mask: longint): longint; cdecl; external;
|
||||||
procedure syslog(__pri: longint; __fmt: PChar; args: array of const); cdecl; external;
|
procedure syslog(__pri: longint; __fmt: PChar; args: array of const); cdecl; external;
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,12 @@
|
||||||
{$codepage utf8}
|
{$codepage utf8}
|
||||||
{$h+}
|
{$h+}
|
||||||
{ namespace Renegade.Logger }
|
{ namespace Renegade.Logger }
|
||||||
|
{ The Renegade logger class }
|
||||||
|
{
|
||||||
|
This takes a HandlerInterface object to create the method of
|
||||||
|
writing logs. Inspiration was taken from the php psr/3 LoggerInterface.
|
||||||
|
@link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
|
||||||
|
}
|
||||||
unit Renegade.Logger;
|
unit Renegade.Logger;
|
||||||
|
|
||||||
interface
|
interface
|
||||||
|
@ -48,6 +54,7 @@ uses
|
||||||
Logger.LoggerInterface,
|
Logger.LoggerInterface,
|
||||||
Logger.AbstractLogger;
|
Logger.AbstractLogger;
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
RTLogger = class(AbstractLogger, LoggerInterface)
|
RTLogger = class(AbstractLogger, LoggerInterface)
|
||||||
public
|
public
|
||||||
|
|
|
@ -48,12 +48,12 @@ uses
|
||||||
Logger.ConsoleHandler;
|
Logger.ConsoleHandler;
|
||||||
|
|
||||||
var
|
var
|
||||||
StreamLogHandler : StreamHandler;
|
StreamLogHandler: StreamHandler;
|
||||||
Log: RTLogger;
|
Log: RTLogger;
|
||||||
LogFileHandler : FileHandler;
|
LogFileHandler: FileHandler;
|
||||||
MemoryStream : TMemoryStream;
|
MemoryStream: TMemoryStream;
|
||||||
NullLogHandler : NullHandler;
|
NullLogHandler: NullHandler;
|
||||||
ConsoleLogHandler : ConsoleHandler;
|
ConsoleLogHandler: ConsoleHandler;
|
||||||
begin
|
begin
|
||||||
//MemoryStream := TMemoryStream.Create;
|
//MemoryStream := TMemoryStream.Create;
|
||||||
//StreamLogHandler := StreamHandler.Create('test.log');
|
//StreamLogHandler := StreamHandler.Create('test.log');
|
||||||
|
@ -62,6 +62,7 @@ begin
|
||||||
//NullLogHandler := NullHandler.Create;
|
//NullLogHandler := NullHandler.Create;
|
||||||
ConsoleLogHandler := ConsoleHandler.Create;
|
ConsoleLogHandler := ConsoleHandler.Create;
|
||||||
Log := RTLogger.Create(ConsoleLogHandler);
|
Log := RTLogger.Create(ConsoleLogHandler);
|
||||||
|
Log.Log(LOG_E, 'Test', []);
|
||||||
Log.Info('Testing', ['File', True, 'Error', True, 'Extended', 'Extend']);
|
Log.Info('Testing', ['File', True, 'Error', True, 'Extended', 'Extend']);
|
||||||
Log.Debug('Debugging', []);
|
Log.Debug('Debugging', []);
|
||||||
Log.Error('Error', []);
|
Log.Error('Error', []);
|
||||||
|
|
Loading…
Reference in New Issue