Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-10

How to use tilog from tiopf2?

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi Felipe,

Felipe Monteiro de Carvalho wrote
Does anyone know how to use this? I tryed googling, but didn't have much luck ...

Rather late than never. ;-)

In your .lpr file, add the tiLog unit to the uses clause, and also the unit representing the logging method you want to use. For this example, lets say you want to log to a file, then include the tiLogToFile unit in the uses clause too.

eg:
uses

    {$IFDEF UNIX}
    cthreads,
    {$ENDIF}
    Classes
    ,tiLog
    ,tiLogToFile
    ;

Log ago that would have been enough, and the class would have registered itself. But we have since changed the design so the end-user can specific which logging method they want (if multiple logging options are enabled), and the developer can decide where for example a log file must be created. Anyway, now register the "log to file" class.

The most basic one is...

  gLog.RegisterLog(TtiLogToFile);

Or a more complex one could be...

  gLog.RegisterLog(TtiLogToFile.CreateWithFileName('.',
ExtractFileName(ParamStr(0))+'.log', True));

Now your application is all set. Oh, if you compile your project under Linux or FreeBSD, remember to enable threading support, because the tiOPF log functionality uses threads.

Now to output log message, the tiLog unit must be in a uses clause, then simply call:

  Log('Some cool message', lsDebug);

or

  LogError('Some big error occured', False);

Please see the various LogXXX methods in the tiLog unit. The parameter names will describe what each parameter does.

Here is a more useful example that I use in my desktop applications. The end-user can control what logging style they want. Multiple logging styles can be enabled at the same time too. eg: Log to File and Log To GUI.

------------[ project1.lpr ]---------------------
uses
Classes,
SysUtils,

  // tiOPF
  tiLog,
  tiLogToFile,
  tiLogToConsole,
  tiLogToGUI,
  tiLogToDebugSvr,
  ...other project units here...
  ;
begin
   {$I initialize.inc}

... other application startup code here ...
end.
-------------------[ end ]-------------------------

--------------[ initialize.inc ]-------------------------
  // I like the ISO 8601 international date format in my apps
  FormatSettings.ShortDateFormat := 'yyyy-mm-dd';
  // You can decide what log severity you actually want to log. This
  // is like a verbosity setting.
  gLog.SevToLog :=  [
    lsNormal
    ,lsUserInfo

// ,lsObjCreation
,lsVisitor
// ,lsConnectionPool
,lsAcceptVisitor
// ,lsQueryTiming
,lsDebug
,lsWarning
,lsError
,lsSQL

    ];

{ Do all parameter processing }

  // Help
  if gCommandLineParams.IsParam(['help', 'h', '?']) then
  begin
    if IsConsole then
    begin
        writeln(Format(cAppNameFormatStr, [ApplicationName,

cMajorVersion, cMinorVersion, uiVersionBuild, cBuildVersion]));

        writeln('Compiled on ' + cCompileDateTime);
        writeln('');
        writeln('The following parameters are available:');
        writeln('');
        writeln('   h, help, ?       Shows this help');
        writeln('   lc               Logs debug information to the

console');
writeln(' lv Logs debug information to a visual screen');
writeln(' ls Logs debug information to debug server');

        writeln('   l                Logs debug information to a file');
        writeln('   style            Applies a custom style to the

application. Available');
writeln(' options are: ''M2'', ''Win2000'', ''Motif''');

        writeln('');
        Exit;
    end
    else
    begin
      tiShowString(
      Format(cAppNameFormatStr, [cTAdminAppName, cMajorVersion,

cMinorVersion, uiVersionBuild, cBuildVersion])
+ LineEnding + 'Compiled on ' + cCompileDateTime
+ LineEnding + ''
+ LineEnding + 'The following parameters are available:'
+ LineEnding + ''
+ LineEnding + ' h, help, ? Shows this help'
+ LineEnding + ' lc Logs debug information to the console'
+ LineEnding + ' lv Logs debug information to a visual screen'
+ LineEnding + ' ls Logs debug information to debug server'
+ LineEnding + ' l Logs debug information to a file'
+ LineEnding + ' style Applies a custom style to the application. Available'
+ LineEnding + ' options are: ''M2'', ''Win2000'', ''Motif'''

      , 'Quick Help');
      fpgApplication.Run;
      Exit;
    end;
  end;
  // Logging
  if gCommandLineParams.IsParam(csLogConsole) then
    gLog.RegisterLog(TtiLogToConsole);
  if gCommandLineParams.IsParam(csLog) then
gLog.RegisterLog(TtiLogToFile.CreateWithFileName('.',fpgExtractFileName(ParamStr(0))+'.log',
True));
  if gCommandLineParams.IsParam(csLogVisual) then
    gLog.RegisterLog(TtiLogToGUI);
  if gCommandLineParams.IsParam(csLogDebugSvr) then
    gLog.RegisterLog(TtiLogToDebugSvr);
-------------------[ end ]-------------------------

Hope this was useful.

Regards,
Graeme