Difference between revisions of "LazLogger"

From Lazarus wiki
Jump to navigationJump to search
Line 67: Line 67:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
--debug-enable= takes a comma separated list
 
--debug-enable= takes a comma separated list
 +
 +
An already enable group ( DebugLogger.FindOrRegisterLogGroup('grpname', True) ) can be disabled by specifying the group name with a minus.
 +
<syntaxhighlight lang=dos>
 +
--debug-enable=-fancy_name_for_mylog
 +
</syntaxhighlight>
  
 
The parameter name can be set via DebugLogger.ParamForEnabledLogGroups='--debug-enable'
 
The parameter name can be set via DebugLogger.ParamForEnabledLogGroups='--debug-enable'

Revision as of 13:40, 5 July 2018

Overview

The unit LazLogger is supplied with Lazarus and provides logging to file.

Usage

Adding logging support to your source code

(For historical reasons some parts are also accessible through LclProc (removed with Lazarus 1.10); this access might be removed in future)

By just including the unit lazlogger you can use the following:

  • DebugLn: which works about the same as WriteLn.
  • DebugLnEnter/DebugLnExit: same as debugln, but increase/decrease indent.
  • DbgOut: which works about the same as Write.

All of them can be called with either:

  • A single string, or list of (up to 15) strings: DebugLn('Foo'); DbgOut('a','b');
  • An array of const: DebugLn(['Foo=', 1, ' Bar=', anInteger]);
  • Same arguments as Format(). String + array of const: DbgOut('a %d',[1]);

Also there are tons of dbgs functions that converts common types to a string. For example dbgs(aRect) converts a variable of type TRect to a string.

Activate/Deactivate LazLogger by using the correct unit

LazLogger provides 3 units for your "uses" clause

LazLogger
Use this unit in your main program only. Using this units will activate the functionality of LazLogger.
LazLoggerBase
Use this unit, whenever you want to use debugln. What LazLoggerBase does depends on whether the unit LazLogger is used in any other unit of your application. If unit LazLogger is not used, the LazLoggerBase will install a "blackhole logger" that discards all log messages. If LazLogger is used anywhere, then all units using LazLoggerBase will write the log messages as described under "Logging Output"
LazLoggerDummy
This unit provides you with dummy calls of debugln (and all other LazLagger functions). They are just calls to empty methods. This allows you to disable all logging in a unit, without the need to remove any debugln statement.

Logging output

Stdout

In normal circumstances, the output is written to stdout. No output is written if stdout is closed - for example when the application is {$AppType Gui}, or compiled with -WG on Windows (see Compiler options / Linking / Target OS specific options).

File

Debug output can also be written to file. The initialization code of the logging unit

  • checks your program's command line parameters for --debug-log=<file> (In Lazarus: Run / Run Parameters / Command line parameters). On finding this parameter, any subsequent debug output is sent to <file>.
  • If no '--debug-log' command line parameter has been given, it next checks if an operating system environment variable xxx_debuglog exists, where xxx is the program file name without extension. E.g. for Lazarus this would be lazarus_debuglog. If such an environment variable exists, it uses the file specified in that environment variable as file to receive debug output.

Example: if you do:

set lazarus_debuglog=c:\lazarus\debug.txt

and run Lazarus, debug output will be written to c:\lazarus\debug.txt.

Debug server

Not available in LazLogger; see DebugServer.

Log Groups

You can add log groups.

var MY_LOG_GROUP: PLazLoggerLogGroup; 

initialization
  MY_LOG_GROUP := DebugLogger.FindOrRegisterLogGroup('fancy_name_for_mylog' {$IFDEF MY_LOG_GROUP_ON_BY_DEFAULT} , True {$ENDIF} );

In your code use

debugln(MY_LOG_GROUP, 'log this text');
debugln(MY_LOG_GROUP, ['a=',a,' b=',b]); // a,b must be basic types: integer, byte, ansistring. See types available for "array of const"

and to enable it call your app with

--debug-enable=fancy_name_for_mylog

--debug-enable= takes a comma separated list

An already enable group ( DebugLogger.FindOrRegisterLogGroup('grpname', True) ) can be disabled by specifying the group name with a minus.

--debug-enable=-fancy_name_for_mylog

The parameter name can be set via DebugLogger.ParamForEnabledLogGroups='--debug-enable'

Documentation

For more features, see the unit itself and the LCL documentation.

Using LazLogger/LazLoggerBase your code can access the DebugLogger instance. This offers some properties to fine tune logging.

You can also write your own logger class and use it with LazLogger/Base.

Multithreading

debugln is thread safe, since Lazarus 1.10.

See also