Log Management

Last Updated on : 2025-02-20 02:54:19download

TuyaOS allows you to set the logging level and logging destination (such as file and terminal). On the device management platform, you can get the local logs of devices for troubleshooting.
Local logging is one of the most important features of an IoT robot vacuum. It is used to log information about devices in the field, helping you identify problems. It is recommended to enable this feature for your robot vacuum products.

Manage logs

Adapt to logging interfaces

TuyaOS provides the TuyaOS Kernel interface tkl_output.h for formatting the log messages. The application implements where to output the logs.

The default output terminal calls tkl_log_output. The application should implement log output in this interface.

Example:

  • Output logs to the terminal:

    VOID_T tkl_log_output(IN CONST CHAR_T *str, ...)
    {
        printf("%s", str);
        fflush(stdout);
        return;
    }
    
  • Output logs to a file:

    VOID_T tkl_log_output(IN CONST CHAR_T *str, ...)
    {
        uFILE * fp = NULL;
    
        // Open the file.
        fp = ufopen("log.txt", "w+");
        if(NULL == fp) {
            return ;
        }
    
        // Write to the file.
         uiWriteCnt = ufwrite(fp, str, strlen(str));
         if(uiWriteCnt != strlen(str)) {
             TAL_PR_ERR("log uf file write data error!");
             return ;
         }
    
        // Close the file.
        ufclose(fp);
    
        return;
    }
    

Set logging level

After TuyaOS is run, call tuya_iot_set_log_attr to set a logging threshold. The message is logged only if its logging level is less than or equal to the threshold. The following table lists the logging levels.

#define TY_LOG_LEVEL_ERR       Logs a message with the ERROR level.
#define TY_LOG_LEVEL_WARN      Logs a message with the WARN level.
#define TY_LOG_LEVEL_NOTICE    Logs a message with the NOTICE level.
#define TY_LOG_LEVEL_INFO      Logs a message with the INFO level.
#define TY_LOG_LEVEL_DEBUG     Logs a message with the DEBUG level.
#define TY_LOG_LEVEL_TRACE     Logs a message with the TRACE level.

Generally, during the device debugging phase, you must set the logging level to TY_LOG_LEVEL_DEBUG mode to facilitate troubleshooting.

Example:

// Set the logging level
	tuya_iot_set_log_attr(TY_LOG_LEVEL_DEBUG);

Log output

PR_DEBUG	Outputs DEBUG logs.
PR_DEBUG_RAW	Outputs DEBUG logs (RAW data without formatting).
PR_ERR	        Outputs ERROR logs.
PR_WARN	        Outputs WARN logs.
PR_INFO	        Outputs INFO logs.
PR_TRACE	Outputs TRACE logs.

Example:

VOID test_log(VOID)
{
	BYTE_T test_buf[8] = {0};
        tuya_iot_set_log_attrattr(TAL_LOG_LEVEL_TRACE);
	PR_TRACE("This is a trace message");

	tuya_iot_set_log_attr(TAL_LOG_LEVEL_DEBUG);
	PR_DEBUG("This is a debug message");
	PR_ERR("This is an error message");
	PR_WARN("This is a warning message");
	PR_INFO("This is an info message");

	PR_DEBUG("PR_DEBUG_RAW:");
	for (INT_T i = 0; i < sizeof(test_buf); i++) {
		PR_DEBUG_RAW("%02x ", test_buf[i]);
	}
	PR_DEBUG_RAW("\r\n");

    return;
}

Upload logs

Upload logs stored on the device to the Tuya Developer Platform. When anomalies occur on the device, device logs can help you troubleshoot problems.

  • If your application logs all messages, there will be insufficient storage space on the device as the logs grow.
  • It is recommended to allocate some persistent storage, such as 10 MB, to store logs. For storage efficiency, log files can be sliced. When the log files exceed the specified value, such as 10 MB or 20 MB, compress and back up them. When the total size of all log files exceeds a threshold, delete the oldest backup to create room for storing new logs.

Platform triggered

Trigger log upload through the Tuya Operations Platform. To trigger log upload, the device must be online. Otherwise, this feature is not available. Additionally, device authorization is required to determine whether logs can be uploaded. The platform can only trigger the log upload feature if you have enabled Device Logs on the panel.

Log Management

tuya_iot_app_cbs_init registers the callback for retrieving logs. In the callback, compress log files and notify TuyaOS of the full path to the compressed file. TuyaOS will upload the file to the Tuya Developer Platform.

Example

/**
 * @brief Handle device local log reporting.
 * @param [char*] path The path to the reported log.
 * @param  [int] len
 * @return [*]
 */
void cloud_log_path_cb(OUT char* path, IN CONST int len)
{
    char cmd[128] = { 0 };
    //You can package local logs such as test.log into log_all.tar (the name can be defined by yourself). Note that the file size of one package should not exceed 30 MB. If the file is large enough, you can package and pass it to the path in batches.
    snprintf(cmd, sizeof(cmd), "tar -cvf /tmp/log_all.tar /tmp/test.log"); 
    system(cmd);
    strcpy(path, "/tmp/log_all.tar");   // Pass the packaged logs and path to the path parameter. The SDK will obtain it through cloud_log_path_cb.
    PR_DEBUG("goto upload local log finish");

    return;
}

/**
 * @brief The callback function invoked when the device local log is reported.
 * @param  [int] result: The result of handling. 0: OK.
 * @return [*]
 */
void cloud_log_deal_cb(OUT INT_T result)
{
    char cmd[128] = { 0 };

    if (OPRT_OK == result) { // After receiving the callback of successful reporting, you can delete the local file.
        snprintf(cmd, sizeof(cmd), "rm -rf /tmp/log_all.tar");    // Delete the file.
        system(cmd);
    }
    PR_DEBUG("upload app log %d, goto delete data! ", result);
    return;
}

/**
 * @brief Initialize device local log reporting.
 * @param  [*]
 * @return [*]
 */
void ty_log_upload_init(void)    
{
    TY_IOT_APP_CBS_S app_log_deal = {
        cloud_log_path_cb,
        cloud_log_deal_cb
    };

    tuya_iot_app_cbs_init(&app_log_deal); // Register the callback function for cloud logs.
}

Download logs

Currently, your account must be allowlisted to use the RVC R&D and support system. If you want to use this feature, contact your account manager.

Perform the following steps:

  1. Log in to the RVC R&D and support system, click Product Management on the left-side navigation pane, and then click Add.

    Log Management

  2. In the Add product dialog that appears, click the drop-down list to select Product ID, and fill in your product ID (PID).

    Log Management

  3. Select your added product and click Confirm to add the product to the Product Management page.

    Log Management

  4. On the left-side navigation pane, select Device Management.

  5. In the Device ID field, fill in the PID of your device and click Search on the right. In the list, click Device Log in the Operation column.

    Log Management

  6. On the Device Local Log tab, click Upload Log.
    When the log status is Uploaded Successfully, you can click Download to get the local logs.

    Log Management