Log Service

Last Updated on : 2023-12-19 08:36:58download

Device logs record the runtime process and exception information of a device. They are helpful for identifying the source of problems.

Feature description

  • Dynamic customization of log output destination, such as the terminal, file, serial port, and RS-485.
  • Dynamic setting of the log level.
  • Printing logs by module.
  • Printing logs in hex format.
  • Logs contain details such as log level, file, line number, and timestamp.
  • Thread-safe log output. The log output API has a lock to prevent data from getting mixed up when multiple threads execute log output in parallel.

Development guide

Runtime environment

Open tuya_iot_config.h and check if the following macro is defined.

#define ENABLE_LOG 1

Reference the header

  • tal_log.h

API call sequence

The log output API should be called after system service initialization.

API description

Default log output

Macro function Description
TAL_PR_TRACE (fmt, …) The trace log.
TAL_PR_DEBUG (fmt, …) The debug log, output during firmware development or troubleshooting.
TAL_PR_INFO (fmt, …) The info log.
TAL_PR_NOTICE (fmt, …) The notable event.
TAL_PR_WARN (fmt, …) The warning log, output when a recoverable error is thrown.
TAL_PR_ERR (fmt, …) The error log, output when a severe and unrecoverable error is thrown.
Parameter Description
fmt Formatted parameter
Variadic

Default log output in hex

Macro function Description
TAL_PR_HEXDUMP_TRACE (title, buf, size) The trace log.
TAL_PR_HEXDUMP_DEBUG (title, buf, size) The debug log, output during firmware development or troubleshooting.
TAL_PR_HEXDUMP_INFO (title, buf, size) The info log.
TAL_PR_HEXDUMP_NOTICE (title, buf, size) The notable event.
TAL_PR_HEXDUMP_WARN (title, buf, size) The warning log, output when a recoverable error is thrown.
TAL_PR_HEXDUMP_ERR (title, buf, size) The error log, output when a severe and unrecoverable error is thrown.
Parameter Description
title Custom label
buf Buffer to print
size Buffer size

Set log level

Once you set the log level, any logs below this level will not be printed.

  • The highest log level: TAL_LOG_LEVEL_ERR
  • The lowest log level: TAL_LOG_LEVEL_TRACE

This API is intended for the default log output, not for custom log output. That is, the setting only applies to TAL_PR_XXX and TAL_PR_HEXDUMP_XXX.

typedef enum {
    TAL_LOG_LEVEL_ERR,            //! Error
    TAL_LOG_LEVEL_WARN,            //! Warning
    TAL_LOG_LEVEL_NOTICE,        //! Notice
    TAL_LOG_LEVEL_INFO,            //! Info
    TAL_LOG_LEVEL_DEBUG,        //! Debug
    TAL_LOG_LEVEL_TRACE,        //! Trace
} TAL_LOG_LEVEL_E;

/**
 * @brief set the global log level.
 *
 * @param[in] curLogLevel , log level
 *
 * @note This API is used to set the global log level.
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_log_set_manage_attr(CONST TAL_LOG_LEVEL_E level);

Add custom log output destination

After the log is formatted, it will invoke the callback you registered. You can implement the callback as needed. This API allows you to output logs to the required destination, such as network and file.

typedef VOID (*TAL_LOG_OUTPUT_CB)(IN CONST CHAR_T *str);

/**
 * @brief add one output terminal.
 *
 * @param[in] name, terminal name
 * @param[in] term, output function pointer
 *
 * @note This API is used for adding one output terminal.
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_log_add_output_term(CONST CHAR_T *name, CONST TAL_LOG_OUTPUT_CB term);

Delete custom log output destination

/**
 * @brief delete one output terminal.
 *
 * @param[in] name , terminal name
 *
 * @note This API is used to delete one output terminal.
 *
 * @return NONE
 */
VOID tal_log_del_output_term(CONST CHAR_T *name);

Add a custom log print module

You can add a module for log print and set its log level. TAL_PR_XXX is the default log print module.

/**
 * @brief add one module's log level
 *
 * @param[in] module_name, module name
 * @param[in] logLevel, this module's log level
 *
 * @note This API is used for adding one module's log level.
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_log_add_module_level(CONST PCHAR_T module_name, CONST TAL_LOG_LEVEL_E level);

Set the custom module’s log level

/**
 * @brief add one module's log level
 *
 * @param[in] pModuleName, module name
 * @param[in] logLevel, this module's log level
 *
 * @note This API is used for adding one module's log level.
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_log_set_module_level(CONST PCHAR_T module_name, TAL_LOG_LEVEL_E level);

Delete the custom module’s log level

/**
 * @brief delete one module's log level
 *
 * @param[in] pModuleName, module name
 *
 * @note This API is used for deleting one module's log level.
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_log_delete_module_level(CONST PCHAR_T module_name);

Log output macros for custom module

Macro Description
TAL_MPR_TRACE (module, fmt, …) The trace log.
TAL_MPR_DEBUG (module, fmt, …) The debug log, output during firmware development or troubleshooting.
TAL_MPR_INFO (module, fmt, …) The info log.
TAL_MPR_NOTICE (module, fmt, …) The notable event.
TAL_MPR_WARN (module, fmt, …) The warning log, output when a recoverable error is thrown.
TAL_MPR_ERR (module, fmt, …) The error log, output when a severe and unrecoverable error is thrown.

FAQs

Why is the printed log not complete?

The default buffer size for log printing is 1,024 bytes. Any excess will not be printed.

Why is the program freezing when the log output API is used?

The log output API has a program lock, so do not use this API in an interrupt function.

When deleting a thread asynchronously, make sure the log output call is finished.