Added comments

This commit is contained in:
R. Eric Wheeler 2017-02-25 23:04:12 -08:00
parent f19f0ad275
commit 25f2ef8070
10 changed files with 140 additions and 52 deletions

View File

@ -36,6 +36,11 @@
{$codepage utf8}
{$h+}
{ 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;
interface
@ -87,7 +92,7 @@ begin
LOG_INFO: Result := 'Info';
LOG_DEBUG: Result := 'Debug';
else
Result := 'Unkown';
Result := 'Unknown';
end;
end;

View File

@ -35,6 +35,9 @@
{$mode objfpc}
{$codepage utf8}
{$h+}
{ namespace Renegade.Logger }
{ Console Log Handler }
{ This handler just write messages to the console or terminal. }
unit Logger.ConsoleHandler;
interface
@ -46,9 +49,9 @@ uses
type
ConsoleHandler = class(TObject, LoggingHandlerInterface)
Private
private
LogIdentifier: UTF8String;
Public
public
constructor Create();
destructor Destroy();
function Open(Identifier: UTF8String): boolean;
@ -71,6 +74,7 @@ end;
function ConsoleHandler.Open(Identifier: UTF8String): boolean;
begin
LogIdentifier := Identifier;
Result := True;
end;
function ConsoleHandler.Close(): boolean;

View File

@ -35,18 +35,26 @@
{$mode objfpc}
{$codepage utf8}
{$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
Uses
uses
Classes,
SysUtils,
Logger.HandlerInterface,
Logger.StreamHandler;
Type FileHandler = class(StreamHandler, LoggingHandlerInterface)
Public
type
FileHandler = class(StreamHandler, LoggingHandlerInterface)
public
constructor Create(const FileName: UTF8String);
function Open(Identifier: UTF8String): boolean;
function Close(): boolean;
@ -75,5 +83,4 @@ begin
Result := inherited Write(LogData);
end;
End.
end.

View File

@ -46,8 +46,19 @@ uses
type
LoggingHandlerInterface = interface
['{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;
{ This method is called to do any closing of the log,
an example would be closing a database or a file. }
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;
end;

View File

@ -60,19 +60,46 @@ const
LOG_PRIMASK = $07; // Internal Unix Use;
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
['{3220524c-fae0-11e6-8b70-9c5c8e742ab6}']
// Sets the Logging Handler to use. (e.g. SysLogHandler, FileHandler, etc.)
procedure SetHandler(Handler: LoggingHandlerInterface);
// System is unusable
procedure Emergency(Message: UTF8String; Context: array of const);
// Action must be taken immediately
procedure Alert(Message: UTF8String; Context: array of const);
// Critical conditions
procedure Critical(Message: UTF8String; Context: array of const);
// Error conditions
procedure Error(Message: UTF8String; Context: array of const);
// Warning conditions
procedure Warning(Message: UTF8String; Context: array of const);
// Normal, but significant, condition
procedure Notice(Message: UTF8String; Context: array of const);
// Informational message
procedure Info(Message: UTF8String; Context: array of const);
// Debug-level message
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;
Context: array of const);
// Holds the current logging handler.
property LoggingHandler: LoggingHandlerInterface write SetHandler;
end;

View File

@ -35,6 +35,14 @@
{$mode objfpc}
{$codepage utf8}
{$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;
interface
@ -46,7 +54,7 @@ uses
type
NullHandler = class(TObject, LoggingHandlerInterface)
Public
public
constructor Create();
destructor Destroy();
function Open(Identifier: UTF8String): boolean;

View File

@ -35,7 +35,12 @@
{$mode objfpc}
{$codepage utf8}
{$h+}
{ namespace Renegade.Logger }
{ StreamHandler Logger Class }
{
This class should take any TStream decendant and use it to
write log messages.
}
unit Logger.StreamHandler;
interface

View File

@ -33,28 +33,41 @@
{ `--- ---' }
{*******************************************************}
{$mode objfpc}
{$interfaces corba}
{$linklib c}
{$codepage utf8}
{$h+}
{
Unix System Log Handler.
The constructor takes one of the defined Logging Facilities.
}
unit Logger.SysLogHandler;
interface
uses
Classes,
FPJson,
Logger.HandlerInterface;
const
LOG_PID = $01; // log the pid with each message
LOG_CONS = $02; // log on the console if errors in sending
LOG_ODELAY = $04; // delay open until first syslog() (default)
LOG_NDELAY = $08; // don't delay open
LOG_NOWAIT = $10; // don't wait for console forks; (DEPRECATED)
LOG_PERROR = $20; // log to stderr as well
// log the pid with each message
LOG_PID = $01;
// Write directly to system console if there is an error
// while sending to system logger.
LOG_CONS = $02;
// 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_USER = 1 shl 3; // random user-level messages
LOG_MAIL = 2 shl 3; // mail system
@ -77,7 +90,6 @@ type
procedure SetFacility(const UnixFacility: longint);
public
constructor Create(const LoggingFacility: longint);
function Open(Identifier: UTF8String): boolean;
function Close(): boolean;
function Write(const LogData: UTF8String): boolean;
@ -86,7 +98,8 @@ type
end;
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;
procedure syslog(__pri: longint; __fmt: PChar; args: array of const); cdecl; external;

View File

@ -36,6 +36,12 @@
{$codepage utf8}
{$h+}
{ 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;
interface
@ -48,6 +54,7 @@ uses
Logger.LoggerInterface,
Logger.AbstractLogger;
type
RTLogger = class(AbstractLogger, LoggerInterface)
public

View File

@ -62,6 +62,7 @@ begin
//NullLogHandler := NullHandler.Create;
ConsoleLogHandler := ConsoleHandler.Create;
Log := RTLogger.Create(ConsoleLogHandler);
Log.Log(LOG_E, 'Test', []);
Log.Info('Testing', ['File', True, 'Error', True, 'Extended', 'Extend']);
Log.Debug('Debugging', []);
Log.Error('Error', []);