[FMX] 使用日志调试跟踪程序

可能许多人象我一样没有注意到,FMX.Types 提供了一个调试日志的管理类,用来在程序中输出日志。

  • Windows 下,日志被使用 OutputDebugString 一样,输出到调试器的 Event Log 里
  • Android 下,日志被输出给LogCat,你可以用Android 平台 SDK 的tools 目录下的 monitor.bat 来打开 Android Device Monitor 工具来查看日志。AndroidDeviceMonitor

如果没有特殊指定,monitor.bat 应该位于 C:\Users\Public\Documents\Embarcadero\Studio\16.0\PlatformSDKs\android-sdk-windows\tools 目录下。

  • iOS 需要到 xcode 的 Organizer 里的Console 查看日志,请参考:http://www.th7.cn/Program/IOS/2012/03/09/62910.shtml
  • OSX 好象需要到控制台去查看日志。

osxlog

在程序中,记录日志实际上在引用 FMX.Types 后,就很简单了,只需要一句话:

procedure TForm3.FormCreate(Sender: TObject);
begin
Log.d('Hello,world');
end;

当然,它还有几个方法,大家看下声明就一目了然了,俺不就啰嗦凑字数了:

    /// <summary>Log a debug message. Same arguments as Format.</summary>
    class procedure d(const Fmt: string; const Args: array of const); overload;
    /// <summary>Log a simple debug message.</summary>
    class procedure d(const Msg: string); overload; inline;
    /// <summary>Log a debug message with Tag, object data of Instance, Method that invokes the logger and message Msg.
    /// </summary>
    class procedure d(const Tag: string; const Instance: TObject; const Method, Msg: string); overload; inline;
    /// <summary>Log a debug message with Tag, object data of Instance and a message Msg</summary>
    class procedure d(const Tag: string; const Instance: TObject; const Msg: string); overload; inline;
    /// <summary>Log a time stamp with message Msg</summary>
    class procedure TimeStamp(const Msg: string); overload;
    /// <summary>Perform a timed execution of Func and print execution times, return function result.
    /// Proc receives a parameter TLogToken which can be used to mark specific points where timestamps should be taken
    /// in addition to complete procedure time.</summary>
    class function Trace<TResult>(const Tag: string; const Func: TFunc<TLogToken, TResult>;
      const Threshold: Integer = -1): TResult; overload;
    /// <summary>A convenience variant of Trace<TResult> when token is not needed.</summary>
    class function Trace<TResult>(const Tag: string; const Func: TFunc<TResult>; const Threshold: Integer = -1): TResult; overload;
    /// <summary>A convenience variant of Trace<TResult> for procedures.</summary>
    class procedure Trace(const Tag: string; const Proc: TProc<TLogToken>; const Threshold: Integer = -1); overload;
    /// <summary>A convenience variant of Trace<TResult> for procedures when token is not needed.</summary>
    class procedure Trace(const Tag: string; const Proc: TProc; const Threshold: Integer = -1); overload;
    /// <summary>Get a basic string representation of an object, consisting of ClassName and its pointer</summary>
    class function ObjToString(const Instance: TObject): string;
    /// <summary>Get a string representation of array using MakeStr function to convert individual elements.</summary>
    class function ArrayToString(const AArray: TEnumerable<TFmxObject>; const MakeStr: TToStringFunc): string; overload;
    /// <summary>Get a string representation of array using TObject.ToString to convert individual elements.</summary>
    class function ArrayToString(const AArray: TEnumerable<TFmxObject>): string; overload;
    /// <summary>Dump complete TFmxObject with all its children.</summary>
    class procedure DumpFmxObject(const AObject: TFmxObject; const Nest: Integer = 0);

实际上,在 VCL 程序下,你也可以引用 FMX.Types 单元,然后再调用 Log.d 函数来记录日志。

最后,个人建议您使用 QLog 记录日志,简单易用跨平台,而且日志可以还支持 SyslogD 协议,日志的输出途径多样,文件、控制台、SyslogD 兼容的远程日志服务器都可以,文件日志支持自动分卷压缩,还可以自行实现更多的 TQLogWriter。

分享到: