客户错误处理

本模块提供自定义日志记录、错误和警告处理功能,允许用户记录消息、清理日志文件以及向UI发送错误和警告信号。

日志管理

记录日志消息

log_message(message, logger='default', include_timestamp=False)

Log a message to a user log file.

Log files are automatically named with timestamp: {logger}_{YYYYMMDD_HHMMSS}.log

Parameters:
  • message (str) – The message to log

  • logger (str) – Logger name (used as filename base), defaults to “default”

  • include_timestamp (bool) – Whether to include timestamp in log content, defaults to False

Returns:

True if logging succeeded

Return type:

bool

Examples:

log_message("Task started")
log_message("Processing item 1", "process_log", True)

清理日志文件

clear_log(logger_name='', log_dir='/root/data/daystar_api/user_logs/')

Clear user log files.

This function works independently and doesn’t require initialization.

Parameters:
  • logger_name (str) – Optional logger name. If provided, only delete logs for this logger. If empty (default), delete all log files.

  • log_dir (str) – Optional log directory. Defaults to /root/data/daystar_api/user_logs/

Returns:

Number of log files deleted

Return type:

int

Examples:

# Delete all log files
deleted = clear_log()
print(f"Deleted {deleted} log files")

# Delete only 'my_task' logs
deleted = clear_log("my_task")
print(f"Deleted {deleted} my_task log files")

错误和警告处理

触发错误信号

raise_error(message)

Raise an error signal. The error will be displayed in the UI.

Parameters:

message (str) – Error message

Returns:

Response containing state and service response

Return type:

RaiseErrorResponse

Examples:

response = raise_error("Failed to connect to sensor")
if response.state.code == StateCode.success:
    print("Error raised successfully")

触发警告信号

raise_warn(message)

Raise a warning signal. The warning will be displayed in the UI.

Parameters:

message (str) – Warning message

Returns:

Response containing state and service response

Return type:

RaiseWarnResponse

Examples:

response = raise_warn("Battery level is low")
if response.state.code == StateCode.success:
    print("Warning raised successfully")