惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
J
Java Code Geeks
V
V2EX
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
博客园 - Franky
爱范儿
爱范儿
T
Tailwind CSS Blog
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
博客园_首页
B
Blog RSS Feed
博客园 - 司徒正美
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 秋·风

修复lazarus datetimepicker控件在linux下拉不能选择日期的bug 用fpc trunk编译lazarus控件找不到TTimeZone 在银河麒麟v10操作系统应用程序执行遇到“权限不够”的提示 修复pl_cindy的Panel系列控件在gtk3出错的bug 龙芯 3A4000工具链的编译(mingw64) lazarus 4.8及之前的版本QT5中文输入多字词组时只输入前2个中文 修正lazarus在GTK3下的Synedit只能输入2个中文字符的Bug lazarus trunk linux从2026.07.02开始,LCLWidgetType默认使用gtk3,怎样改回gtk2 【aarch64-win64】DonAlfredo终于修复fpc将TSmallPoint赋值为一个负数,并将TSmallPoint赋值给TPoint时得到一个655xx的数值的bug lazarus编译时提示链接出错的处理方法 mORMot2两个BUG 【aarch64-win64】发现fpc aarch64-win64 将TSmallPoint赋值为一个负数,并赋值给TPoint时得到一个655xx的数值的bug 修复aarch64 win64下lazarus不弹出SourceTabPopUpMenu fpc aarch64-win64版编译应用时遇到一个奇怪的bug 在windows 11 on arm64系统编译lazarus出错的处理方法 修复TDBMemo只读不能使用Ctrl+C复制文字的Bug freepascal打补丁后已支持windows 11 on arm64(lazarus aarch64 windows版也可以运行了) 查看libc.so.6的版本号 Lazarus移动版启动程序 fpc 3.2.2交叉编译i386-linux链接出错 lazarus鸿蒙开发13:使用鸿蒙原生API画图--lazarus侧关键代码 lazarus鸿蒙开发12:使用鸿蒙原生API画图--鸿蒙侧关键代码 lazarus鸿蒙开发11:使用鸿蒙原生接口画图 lazarus鸿蒙开发10:lazarus一键编译、打包助手 lazarus鸿蒙开发9:使用命令行编译DevEco Studio应用 lazarus鸿蒙开发7:在 Windows 上为 OpenHarmony (LoongArch64) 编译 Qt 5.15.12 SDK 指南 lazarus鸿蒙开发6:鸿蒙project配置 lazarus鸿蒙开发5:编译ohos_hap_project lazarus鸿蒙开发4:编译OHOS_QT_Lazarus lazarus鸿蒙开发3:编译libLazarusOHOS_Wrapper.so
lazarus鸿蒙开发8:在DevEco Studio的日志窗口显示日志信息
秋·风 · 2026-06-08 · via 博客园 - 秋·风

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.

日志输出截图:

QQ_1780893268481

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.