YOUSEFUL / DELPHI / COMPONENTS

TAtlasMemorySink

Keeps the most recent events in a ring buffer, for the log viewer, diagnostics and tests.

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

Description

A fixed-size ring of TAtlasLogEvent records: when it is full, each new event pushes out the oldest and Discarded counts it. Every member is thread safe; Snapshot returns a copy, oldest first, that the caller may keep. Version changes whenever the content does, so a timer can poll it cheaply and take a snapshot only when something changed; TAtlasLogView works this way.

Members

MemberKindWhat it does
CreateconstructorCreates an empty sink with Capacity = 1000.
Destroydestructor from TAtlasLogSinkLeaves the logger first, so no event arrives during destruction, then frees the sink.
SnapshotmethodA copy of the events held, oldest first.
ClearmethodRemoves every event and changes Version.
CountpropertyThe number of events held, at most Capacity.
VersionpropertyChanges whenever the content does (a write, Clear, a capacity change); cheap to poll.
DiscardedpropertyHow many events were pushed out by newer ones since the sink was created.
CapacitypropertyHow many events are kept, from 1 to 10,000,000; other values raise EAtlasLogError.
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 constructor

constructor Create(AOwner: TComponent); override;

Creates an empty sink with Capacity = 1000.

Destructors

Destroy destructorinherited from TAtlasLogSink

destructor Destroy; override;

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

Properties

Count property

property Count: Integer read GetCount;

The number of events held, at most Capacity.

Version property

property Version: Int64 read GetVersion;

Changes whenever the content does (a write, Clear, a capacity change); cheap to poll.

Discarded property

property Discarded: Int64 read FDiscarded;

How many events were pushed out by newer ones since the sink was created.

Capacity property

property Capacity: Integer read FCapacity write SetCapacity default 1000;

How many events are kept, from 1 to 10,000,000; other values raise EAtlasLogError. Shrinking keeps the newest events; growing keeps them all.

pascal
Memory.Capacity := 5000;

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

Methods

Snapshot method

function Snapshot: TArray<TAtlasLogEvent>;

A copy of the events held, oldest first.

Clear method

procedure Clear;

Removes every event and changes Version. Discarded is not reset.

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 memory sink has nothing to flush.

Examples

Recent errors on demand

Keeps the last 500 warnings and errors for a diagnostics command.

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

function AddRecentProblems(Log: TAtlasLogger): TAtlasMemorySink;
begin
  Result := TAtlasMemorySink.Create(Log);
  Result.Capacity := 500;
  Result.MinLevel := llWarning;
  Result.Logger := Log;
end;

procedure PrintRecentProblems(Memory: TAtlasMemorySink);
var
  Event: TAtlasLogEvent;
begin
  for Event in Memory.Snapshot do
    Writeln(FormatDateTime('hh:nn:ss', Event.LocalTime), ' ', AtlasLevelCodes[Event.Level], ' ', Event.Message);
  Writeln(Memory.Count, ' kept, ', Memory.Discarded, ' pushed out');
end;

Poll for changes

Takes a snapshot only when the sink changed, as the viewer does.

pascal
uses Atlas.Log, Atlas.LogSinks;

var
  SeenVersion: Int64 = -1;

function Changed(Memory: TAtlasMemorySink; var Events: TArray<TAtlasLogEvent>): Boolean;
begin
  Result := Memory.Version <> SeenVersion;
  if Result then
  begin
    SeenVersion := Memory.Version;   // read before the snapshot: a late event only causes one more refresh
    Events := Memory.Snapshot;
  end;
end;

TAtlasLogView · TAtlasLogEvent · TAtlasLogSink

Also in Atlas Log

TAtlasLogger · TAtlasCallbackSink · TAtlasConsoleSink · TAtlasDebugSink · TAtlasFileSink · TAtlasAsyncSink · TAtlasLogView