YOUSEFUL / DELPHI / COMPONENTS

TAtlasCallbackSink

Hands each event to an event handler, on the thread that logged.

Non-visual component Palette: Atlas Log Unit Atlas.LogSinks · Inherits TAtlasLogSink · Product Atlas Log

Description

The quickest way to react to events in application code: count them, forward them, raise an alarm. The handler runs on the logging thread (or on the background thread when the sink is the target of a TAtlasAsyncSink), under the sink's lock, so calls never overlap but must not touch VCL controls directly; use TThread.Queue. An exception in the handler is counted by the logger. Keep the handler short, since the thread that logged waits for it, and do not log to the same logger from inside it.

Members

MemberKindWhat it does
OnEventeventCalled once per event that passes MinLevel.
Createconstructor from TAtlasLogSinkCreates the sink, not yet attached to a logger: set Logger to start receiving events.
Destroydestructor from TAtlasLogSinkLeaves the logger first, so no event arrives during destruction, then frees the sink.
Loggerproperty from TAtlasLogSinkThe logger this sink receives events from.
MinLevelproperty from TAtlasLogSinkThe sink's own threshold: events below it are ignored by this sink only.
Writemethod from TAtlasLogSinkWrites one event.
Flushmethod from TAtlasLogSinkPushes buffered output out, under the sink's lock.

Constructors

Create constructorinherited from TAtlasLogSink

constructor Create(AOwner: TComponent); override;

Creates the sink, not yet attached to a logger: set Logger to start receiving events. With an owner (a form, a data module or the logger itself) the sink is freed with it.

Destructors

Destroy destructorinherited from TAtlasLogSink

destructor Destroy; override;

Leaves the logger first, so no event arrives during destruction, then frees the sink.

Properties

Logger propertyinherited from TAtlasLogSink

property Logger: TAtlasLogger read FLogger write SetLogger;

The logger this sink receives events from. Setting it registers the sink with that logger (and removes it from the previous one); nil detaches it. The same as calling TAtlasLogger.AddSink and RemoveSink. Either side may be destroyed first: the other is told and lets go.

pascal
Sink.Logger := Log;

MinLevel propertyinherited from TAtlasLogSink

property MinLevel: TAtlasLogLevel read FMinLevel write FMinLevel default llTrace;

The sink's own threshold: events below it are ignored by this sink only. It applies after the logger's threshold, so it can only narrow what the logger lets through. Default llTrace.

pascal
Files.MinLevel := llInfo;   // the file skips debug and trace

Events

OnEvent event

property OnEvent: TAtlasLogEventHandler read FOnEvent write FOnEvent;

Called once per event that passes MinLevel. Without a handler, events are ignored.

Methods

Write methodinherited from TAtlasLogSink

procedure Write(const Event: TAtlasLogEvent);

Writes one event. The logger calls it; you may call it too, for example to replay events from a TAtlasMemorySink into another sink. Events below MinLevel and events at llOff are ignored; otherwise DoWrite runs under the sink's lock. Unlike the logger, Write itself may raise when the output fails.

Flush methodinherited from TAtlasLogSink

procedure Flush;

Pushes buffered output out, under the sink's lock. TAtlasLogger.Flush calls it on every sink and counts failures; called directly it may raise. A callback sink has nothing to flush.

Examples

Show the last error in the user interface

Errors from any thread reach the main thread through TThread.Queue.

pascal
uses System.Classes, System.SysUtils, Atlas.Log, Atlas.LogSinks;

type
  TErrorForwarder = class
  public
    LastError: string;
    procedure EventLogged(Sender: TObject; const Event: TAtlasLogEvent);
  end;

procedure TErrorForwarder.EventLogged(Sender: TObject; const Event: TAtlasLogEvent);
var
  Text: string;
begin
  Text := AtlasLevelCodes[Event.Level] + ' ' + Event.Message;
  // On the logging thread: hand the text to the main thread.
  TThread.Queue(nil, procedure begin LastError := Text; end);
end;

procedure WatchErrors(Log: TAtlasLogger; Forwarder: TErrorForwarder);
var
  Sink: TAtlasCallbackSink;
begin
  Sink := TAtlasCallbackSink.Create(Log);
  Sink.MinLevel := llError;
  Sink.OnEvent := Forwarder.EventLogged;
  Sink.Logger := Log;
end;

TAtlasLogEventHandler · TAtlasLogSink · TAtlasAsyncSink

Also in Atlas Log

TAtlasLogger · TAtlasMemorySink · TAtlasConsoleSink · TAtlasDebugSink · TAtlasFileSink · TAtlasAsyncSink · TAtlasLogView