Miscelaneous
TMS Logging provides some additional features and capabilities, assorted listed below.
Helper procedures
In TMSLoggingUtils
there are some
general-purpose procedures and functions that you can use:
Name | Description |
---|---|
AddBackslash(const AValue: string): string | Returns the string with a backslash if the string does not contain one. |
AppendStream(const AFileName: string; const AStream: TStringStream) | Appends a string stream to a file. If the file does not exist, the file is created. |
ColorToHTML(const AValue: TAlphaColor): string | Converts the TAlphaColor value to a HTML color string. |
CreateFileFromResource(AFileName: string; AResourceName: string) | Creates a file from a resource. |
Decode64Bytes(const AValue: string): TBytes | Decodes a base 64 string into an array of bytes. |
Decode64String(const AValue: string): string | Decodes a base 64 string into a string. |
Encode64Bytes(const AValue: TBytes): string | Encodes an array of bytes into a base 64 string. |
Encode64String(const AValue: string): string | Encodes a string into a base 64 string. |
ExtractPicture(const AValue: string): string | Extracts the picture data from a string that begins with #BEGINPIC# and ends with #ENDPIC# tags. |
GetConcatenatedLogMessage(const AOutputInformation: TTMSLoggerOutputInformation; const AIndent: Boolean = False): string | Returns a concatenated log message based on the output information received from the logger. Optional parameters specify if indenting needs to be applied. |
GetCurrentLangID: string | Returns the current language identifier. |
GetDefaultOutputFileName: string | Returns the default output file name used inside an output handler that logs to a separate plain text or HTML file. |
GetHTMLFormattedMessage(const AOutputInformation: TTMSLoggerOutputInformation; const AMode: TTMSLoggerHTMLOutputHandlerMode; const AApplyOutputParameters: Boolean; const ADocumentWrite: Boolean; const AEven: Boolean): string | Returns a HTML formatted message based on the output information received from the logger. Optional parameters specify the mode, if output parameters need to be applied such as the global color of the string, specifies whether document.write needs to be included and if the style applied to an element needs to include the even style. |
GetIndent(const AIndent: Integer): string | Returns a string containing the AIndent amount of spaces. |
GetProcessID: Cardinal | Returns the process id. |
GetResourceStream(const AResourceName: string): TResourceStream | Returns a resource stream based on a resource name. |
GetSystemInformation: string | Returns information on the operating system. |
GetThreadID: Cardinal | Returns the thread id. |
GetTickCountX: Integer | Returns the current tick count. |
GetTimer(const AMode: TTMSLoggerTimerMode = lsmMilliseconds): Int64 | Returns an elapsed value in ticks or milliseconds based on the mode parameters after a timer has been started with StartTimer. |
GetUsedMemory: Cardinal | Returns the amount of memory the application is using. |
HexStringToByteStream(const AValue: string; const ADigits: Integer = 2): TBytesStream | Returns a stream of bytes based on a hex string. |
HexStrToBytes(const AValue: string; const ADigits: Integer = 2): TBytes | Returns an array of bytes based on a hex string. |
IntToBinByte(const AValue: Byte; const ADecimals: Integer): string | Converts an integer to a byte with an optional amount of decimals. |
IsTimerRunning | Returns a Boolean if the timer is still running after it was started with the StartTimer call. |
LogToConsole(const AValue: string) | Logs a string value to the console window of the IDE or the log monitor / console of the device (iOS, Android, Mac OSX). |
ReplaceTextInFile(AFileName, AText, AReplaceText: string) | Replaces a string inside a file. |
StartTimer | Starts a timer, needs to be paired with StopTimer. |
StopTimer(const AMode: TTMSLoggerTimerMode = lsmMilliseconds): Int64 | Returns an elapsed value in ticks or milliseconds based on the mode parameters and stops the timer, needs to be paired with StartTimer. |
StripHTML(const AValue: string): string | Strips HTML from a string. |
When adding the unit TMSLoggingCore
, there is a helper method TMSLog()
to quickly log a value with optional format and level parameters. Below
is a sample that demonstrates this.
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
i := 100;
TMSLog(i);
end;
Record and Class Helpers
The core packages provides an additional unit which provides record and
class helpers for a set of types available in Delphi. When adding the
unit TMSLoggingHelpers
, the default
record / class helpers for the type you wish the use will be hidden.
Unfortunately Delphi doesn't allow record / class helper inheritance, so
the use of it is completely optional. Below is a sample what can be
achieved when using this unit.
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
fmt: string;
begin
fmt := 'The value is {%g}';
for I := 1 to 10 do
I.LogInfoFormat(fmt);
end;
Note that the logging starts from the value itself, instead of passing it as a parameter to the logger log statements. This unit makes use of the custom logger instance retrieved with TMSDefaultLogger. The default logger instance retrieved with TMSLogger will be ignored, thus implying that by default, the TMSDefaultLogger will only log to the console output handler. When this technique is used, instead of the default TMSLogger functionality, the registration of output handlers need to be applied separately.
Persistence
The logger has the ability to save its configuration, registered output handlers and properties to a file, stream or registry (Windows only), so to save the logger instance, simply call one of the Save* methods.
To load, the Load* equivalent of the Save methods can be used. Please note that this will override any registered output handlers, or properties set.
Each Save* and Load* call will automatically call the Save* and Load* calls on output handler instances. The TTMSLoggerBaseOutputHandler class provides a set of read and write calls for registry and ini file save / load instructions.
Initialization of the logger, such as the output handlers, output formats properties can be done once when saving the configuration. Simply loading the configuration again will automatically create any registered output handlers with their settings. For creation of the TTMSLoggerBrowserOutputHandler and TTMSLoggerTCPOutputHandler, the default constructor has an AOwner: TComponent parameter that needs to be set in order to successfully destroy the HTTP or TCP server instance. The parameter needs to be a form as demonstrated in the following sample, which registers a TTMSLoggerBrowserOutputHandler, saves the configuration to a stream and then reloads the configuration after unregistering all output handlers:
var
ms: TMemoryStream;
begin
TMSLogger.RegisterOutputHandlerClass(TTMSLoggerBrowserOutputHandler, [Self]);
ms := TMemoryStream.Create;
try
TMSLogger.SaveConfigurationToStream(ms);
TMSLogger.UnregisterAllOutputHandlers;
ms.Position := 0;
TMSLogger.LoadConfigurationFromStream(Self, ms);
finally
ms.Free;
end;
end;
IDE Plugin
After installing TMS Logging through the automated installer, the IDE is updated with a "TMS Logging" helper menu that can be used to execute various operation when implementing logging in your application. Note that each action is only applied to the active source editor window, not application wide and the plugin is only supported in Delphi.
Add Missing Units (Keyboard shortcut ALT+M+A)
When adding logging to your application with the TMS Logging units, or copy and use the code snippets from this documentation, which possibly demonstrates the uses of an output handler, you might encounter compilation issues which indicates that there are missing units. This menu item will look into the active source file editor window and will automatically add missing units in order to compile your application. Optionally, to quickly add missing units, the shortcut ALT+M+A can be used.
Register Output Handlers
This menu option has a set of sub menu items that inserts registration code at the cursor position. Each output handler has a unique signature and the "Register Output Handler" menu item will help you setup the registration code needed to use the output handler.
Comment / Uncomment Log Calls / Units
This menu option will look for logger specific calls / units and will comment/uncomment them. This is designed to quickly eliminate any logger calls when you want to test your application without logging capabilities.
Remove Log Calls / Units
This menu option will look for logger specific calls / units and will remove them.