Log Management

Last Updated on : 2024-12-17 06:24:15download

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 IP camera (IPC). It is used to log information about devices in the field, helping you identify problems. It is recommended to enable this feature for your IPC 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.

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, ...)
    	{
    		FILE * fp = NULL;
     		// Open the file.
    		fp = fopen("log.txt", "w+");
    		if(NULL == fp) {
    			return ;
    		}
     		// Write to the file.
    		size_t WriteCnt = fwrite(str, sizeof(char), strlen(str), fp);
     		// Close the file.
    		fclose(fp);
    
    		return;
     	}
    

Set logging level

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

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 ret = OPRT_OK;
 	// Initialize TuyaOS
	ret = tuya_ipc_init_sdk(&env);
 	// Set logging level to debug
	tuya_ipc_set_log_attr(TAL_LOG_LEVEL_DEBUG, NULL);

	// ...

	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};
        tuya_ipc_set_log_attr(TAL_LOG_LEVEL_TRACE, NULL);
	PR_TRACE("This is a trace message");

        tuya_ipc_set_log_attr(TAL_LOG_LEVEL_DEBUG, NULL);
 	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");

        tuya_ipc_set_log_attr(PR_DEBUG_RAW, NULL);
 	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 to the file, there might be insufficient storage space on the device as the logs grow.

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.

cloud_operation_set_log_path_cb or cloud_operation_set_log_path_and_fin_cb registers the callback for retrieving logs. In the callback, notify TuyaOS of the full path to the log. TuyaOS will upload the file to the Tuya Developer Platform. If you need to perform subsequent operations after the log is uploaded, such as deleting the log file, you can use cloud_operation_set_log_path_and_fin_cb to register a callback function after the log is retrieved.

Example:

STATIC VOID __sdk_log_path_cb(OUT CHAR_T *path, IN CONST INT_T len)
{
    strncpy(path, "/tmp/test.log", len);
}
 //Optional
STATIC VOID __sdk_log_finish_cb(OUT CHAR_T *path, IN CONST INT_T len)
{
	remove("/tmp/test.log");
}

VOID test_get_log_file(VOID)
 {
        //Use this API if you do not need to perform subsequent operations after the log is successfully uploaded
	cloud_operation_set_log_path_cb(__sdk_log_path_cb);
	 	//Use this API if you need to perform subsequent operations after the log is successfully uploaded
	cloud_operation_set_log_path_and_fin_cb(__sdk_log_path_cb, __sdk_log_finish_cb);
}

Many schemes are available to achieve circular overwriting.

  • For example, allocate some persistent storage, such as 2 MB, to store logs. Log files can be sliced for storage efficiency. When the log files exceed the specified value, such as 128 KB or 256 KB, 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.
  • Alternatively, you can save the logs to a circular buffer in tkl_log_output. When the platform triggers the log upload feature, the buffer data is written to the flash memory.