Log Management

Last Updated on : 2023-09-06 10:40:14

TuyaOS allows you to set the logging level and logging destination (such as serial port, file, terminal, and network). 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 gateway. It is used to log information about devices in the field, helping you identify problems. It is recommended to enable this feature for your gateway products.

Log management

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.

For 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 file
    	fp = ufopen("log.txt", "w+");
    	if(NULL == fp) {
    		return ;
    	}
    
    	// Write to file
    	uiWriteCnt = ufwrite(fp, str, strlen(str));
    	if(uiWriteCnt != strlen(str)) {
    		TAL_PR_ERR("log uf file write data error!");
    		return ;
    	}
    
    	// Close file
    	ufclose(fp);
    
    	return;
    }
    

Set logging level

After TuyaOS is run, call tal_log_set_manage_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.

Macro definition Description
TAL_LOG_LEVEL_ERR Logs a message with the ERROR level.
TAL_LOG_LEVEL_WARN Logs a message with the WARN level.
TAL_LOG_LEVEL_NOTICE Logs a message with the NOTICE level.
TAL_LOG_LEVEL_INFO Logs a message with the INFO level.
TAL_LOG_LEVEL_DEBUG Logs a message with the DEBUG level.
TAL_LOG_LEVEL_TRACE Logs a message with the TRACE level.

Example:

#include "tal_log.h"

int main(int argc, char **argv)
{
	OPERATE_RET rt = OPRT_OK;

	// Initialize TuyaOS
	TUYA_CALL_ERR_RETURN(tuya_iot_init("./"));

	// Set logging level to DEBUG
	tal_log_set_manage_attr(TAL_LOG_LEVEL_DEBUG);

	// ...

	return 0;
}

Log output macros

TuyaOS provides interfaces to output application logs with different severity levels.

Macro definition Description
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};

	tal_log_set_manage_attr(TAL_LOG_LEVEL_TRACE);
	PR_TRACE("This is a trace message");

	tal_log_set_manage_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;
}

Local logs

On the device side, you save the log output to a file. When you pull the local logs on the device management platform, the device will upload the log file to the cloud so that you can view local logs remotely.

Local logs can assist you in troubleshooting. You can set the logging level to DEBUG to log additional information that can help identify problems.

If your application logs all messages, there will be insufficient storage space on the device as the logs grow.

To resolve this problem, you can reserve some persistent storage such as 2 MB to back up logs. The logs are output to the memory. When their size exceeds the specified value, such as 128 KB or 256 KB, compress the log file and migrate it to the persistent storage, with the current date and time as its name. Regularly check the size of the persistent storage. When it exceeds the specified limit, delete the old backup to free space.

Register the callback for pulling local logs. In the callback, compress the latest log file and the log backup in the persistent storage into the same file and send this file to the SDK. The SDK then uploaded it to the cloud.

Example:

STATIC VOID __gw_log_path_cb(OUT CHAR_T *path, IN CONST INT_T len)
{
	CHAR_T cmd[128] = {0};
	snprintf(cmd, SIZEOF(cmd), "cp -fr /tmp/tuya.log ./log_dir/; tar zcvf /tmp/tuya_log.tgz ./log_dir/*");
	system(cmd);

	strncpy(path, "/tmp/tuya_log.tgz", len);
}

VOID test_get_log_file(VOID)
{
	TY_GW_APP_CBS_S __gw_app_cbs = {
		.gw_log_path_cb = __gw_log_path_cb,
	};
	tuya_iot_reg_gw_app_cb(&__gw_app_cbs);
}