












lazarus编写的应用如果用writeln输出日志信息,在DevEco Studio的日志窗口是没有相关信息的,为方便调试跟踪,在AI帮助下,将鸿蒙原生Hilog.inc转为pascal格式,只需用HILogInfo替换writeLn就可以。
在uses 加上hilog这个单元就可以
library OHOS_QT_Lazarus; {$mode objfpc}{$H+} {$longstrings on} uses {$IFDEF UNIX} cthreads, {$ENDIF} {$IFDEF HASAMIGA} athreads, {$ENDIF} Classes, SysUtils, CTypes, Hilog,//加上这个单元
begin OH_LOG_Print(LOG_APP, LOG_INFO, 1, 'MyTag', '这是用lazarus编译的鸿蒙程序'); HILogInfo('鸿蒙你好,这是lazarus编译的程序'); HILogInfo('=== OpenHarmony Drawing Canvas Demo ==='); end.
日志输出截图:

Hilog.pas:
unit Hilog; {$mode objfpc}{$H+} {$packrecords c} interface uses SysUtils; //============================================================================== // 类型定义 //============================================================================== type // 日志类型 TLogType = ( LOG_APP = 0 ); // 日志级别 TLogLevel = ( LOG_DEBUG = 3, LOG_INFO = 4, LOG_WARN = 5, LOG_ERROR = 6, LOG_FATAL = 7 ); // 日志级别设定策略(API 21+) TPreferStrategy = ( UNSET_LOGLEVEL = 0, PREFER_CLOSE_LOG = 1, PREFER_OPEN_LOG = 2 ); // 回调函数类型(用于 OH_LOG_SetCallback) TLogCallback = procedure ( atype: TLogType; level: TLogLevel; domain: LongWord; tag: PChar; msg: PChar ); cdecl; //============================================================================== // 外部函数声明(来自 libhilog_ndk.z.so) //============================================================================== // 基础打印函数(支持格式化) function OH_LOG_Print( atype: TLogType; level: TLogLevel; domain: LongWord; tag: PChar; fmt: PChar ): Integer; cdecl; varargs; external 'libhilog_ndk.z.so' name 'OH_LOG_Print'; // 打印纯文本消息(API 18+) function OH_LOG_PrintMsg( atype: TLogType; level: TLogLevel; domain: LongWord; tag: PChar; message: PChar ): Integer; cdecl; external 'libhilog_ndk.z.so' name 'OH_LOG_PrintMsg'; // 打印带长度的消息(API 18+) function OH_LOG_PrintMsgByLen( atype: TLogType; level: TLogLevel; domain: LongWord; tag: PChar; tagLen: NativeUInt; message: PChar; messageLen: NativeUInt ): Integer; cdecl; external 'libhilog_ndk.z.so' name 'OH_LOG_PrintMsgByLen'; // 使用 va_list 的打印函数(API 18+) function OH_LOG_VPrint( atype: TLogType; level: TLogLevel; domain: LongWord; tag: PChar; fmt: PChar; ap: Pointer // va_list 实际是不透明指针,此处用 Pointer 代替 ): Integer; cdecl; external 'libhilog_ndk.z.so' name 'OH_LOG_VPrint'; // 检查给定域、标签、级别的日志是否可输出 function OH_LOG_IsLoggable( domain: LongWord; tag: PChar; level: TLogLevel ): Boolean; cdecl; external 'libhilog_ndk.z.so' name 'OH_LOG_IsLoggable'; // 设置自定义日志处理回调(API 11+) procedure OH_LOG_SetCallback(callback: TLogCallback); cdecl; external 'libhilog_ndk.z.so' name 'OH_LOG_SetCallback'; // 设置当前进程的最低日志级别(API 15+) procedure OH_LOG_SetMinLogLevel(level: TLogLevel); cdecl; external 'libhilog_ndk.z.so' name 'OH_LOG_SetMinLogLevel'; // 设置带策略的日志级别(API 21+) procedure OH_LOG_SetLogLevel(level: TLogLevel; prefer: TPreferStrategy); cdecl; external 'libhilog_ndk.z.so' name 'OH_LOG_SetLogLevel'; //============================================================================== // 简化的日志过程(使用全局默认域和标签) //============================================================================== var // 全局默认服务域,默认为 0,用户可修改 DefaultLogDomain: LongWord = 0; // 全局默认日志标签,默认为 nil,用户可设置为字符串常量,如 'MyApp' DefaultLogTag: PChar = 'MyApp'; // 以下过程使用 DefaultLogDomain 和 DefaultLogTag,自动格式化消息 procedure HILogDebug(const Msg: string); overload; procedure HILogInfo(const Msg: string); overload; procedure HILogWarn(const Msg: string); overload; procedure HILogError(const Msg: string); overload; procedure HILogFatal(const Msg: string); overload; // 带格式化的版本(使用 SysUtils.Format) procedure HILogDebug(const Fmt: string; const Args: array of const); overload; procedure HILogInfo(const Fmt: string; const Args: array of const); overload; procedure HILogWarn(const Fmt: string; const Args: array of const); overload; procedure HILogError(const Fmt: string; const Args: array of const); overload; procedure HILogFatal(const Fmt: string; const Args: array of const); overload; //============================================================================== // 与原始 C 宏等价的过程(需要显式传入域和标签) //============================================================================== procedure OH_LOG_DEBUG(atype: TLogType; const Fmt: string; const Args: array of const); procedure OH_LOG_INFO(atype: TLogType; const Fmt: string; const Args: array of const); procedure OH_LOG_WARN(atype: TLogType; const Fmt: string; const Args: array of const); procedure OH_LOG_ERROR(atype: TLogType; const Fmt: string; const Args: array of const); procedure OH_LOG_FATAL(atype: TLogType; const Fmt: string; const Args: array of const); implementation // 内部辅助函数:将格式化后的字符串通过 OH_LOG_PrintMsg 输出 { procedure DoLog(atype: TLogType; level: TLogLevel; domain: LongWord; tag: PChar; const msg: string); begin if (msg = '') then Exit; OH_LOG_PrintMsg(atype, level, domain, tag, PChar(msg)); end; } procedure DoLog(atype: TLogType; level: TLogLevel; domain: LongWord; tag: PChar; const msg: string); begin if msg = '' then Exit; // 使用 OH_LOG_Print 并传入 %s 格式符,将 msg 作为字符串参数 OH_LOG_Print(atype, level, domain, tag, PChar(msg)); end; //============================================================================== // 简化日志过程实现 //============================================================================== procedure HILogDebug(const Msg: string); begin DoLog(LOG_APP, LOG_DEBUG, DefaultLogDomain, DefaultLogTag, Msg); end; procedure HILogInfo(const Msg: string); begin DoLog(LOG_APP, LOG_INFO, DefaultLogDomain, DefaultLogTag, Msg); end; procedure HILogWarn(const Msg: string); begin DoLog(LOG_APP, LOG_WARN, DefaultLogDomain, DefaultLogTag, Msg); end; procedure HILogError(const Msg: string); begin DoLog(LOG_APP, LOG_ERROR, DefaultLogDomain, DefaultLogTag, Msg); end; procedure HILogFatal(const Msg: string); begin DoLog(LOG_APP, LOG_FATAL, DefaultLogDomain, DefaultLogTag, Msg); end; procedure HILogDebug(const Fmt: string; const Args: array of const); begin HILogDebug(Format(Fmt, Args)); end; procedure HILogInfo(const Fmt: string; const Args: array of const); begin HILogInfo(Format(Fmt, Args)); end; procedure HILogWarn(const Fmt: string; const Args: array of const); begin HILogWarn(Format(Fmt, Args)); end; procedure HILogError(const Fmt: string; const Args: array of const); begin HILogError(Format(Fmt, Args)); end; procedure HILogFatal(const Fmt: string; const Args: array of const); begin HILogFatal(Format(Fmt, Args)); end; //============================================================================== // 宏等价过程实现 //============================================================================== procedure OH_LOG_DEBUG(atype: TLogType; const Fmt: string; const Args: array of const); begin DoLog(atype, LOG_DEBUG, DefaultLogDomain, DefaultLogTag, Format(Fmt, Args)); end; procedure OH_LOG_INFO(atype: TLogType; const Fmt: string; const Args: array of const); begin DoLog(atype, LOG_INFO, DefaultLogDomain, DefaultLogTag, Format(Fmt, Args)); end; procedure OH_LOG_WARN(atype: TLogType; const Fmt: string; const Args: array of const); begin DoLog(atype, LOG_WARN, DefaultLogDomain, DefaultLogTag, Format(Fmt, Args)); end; procedure OH_LOG_ERROR(atype: TLogType; const Fmt: string; const Args: array of const); begin DoLog(atype, LOG_ERROR, DefaultLogDomain, DefaultLogTag, Format(Fmt, Args)); end; procedure OH_LOG_FATAL(atype: TLogType; const Fmt: string; const Args: array of const); begin DoLog(atype, LOG_FATAL, DefaultLogDomain, DefaultLogTag, Format(Fmt, Args)); end; end.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。