In this topic, the ./demos/logpost_basic_demo.c sample code file is used to show you how to use the Link SDK for C to upload local device log files to IoT Platform.
Background information
Before you upload device logs, make sure that the following prerequisite is met: On the Device Details page of IoT Platform, the Device local log reporting switch is turned on. For more information, see Overview.
You must use the MQTT protocol to upload log files. For more information about MQTT connection-related code when you configure the feature, see Connection over MQTT.
Step 1: Initialize the environment
Add header files
... ... #include "aiot_logpost_api.h"Add the underlying dependency and configure the log output feature.
aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile); aiot_state_set_logcb(demo_state_logcb);Call the aiot_logpost_init operation to create a
data-modelclient instance and initialize its default parameters.logpost_handle = aiot_logpost_init(); if (logpost_handle == NULL) { demo_mqtt_stop(&mqtt_handle); printf("aiot_logpost_init failed\r\n"); return -1; }
Step 2: Configure features
Call the aiot_logpost_setopt operation to create a LogPost client instance and initialize its default parameters.
Associate with an MQTT connection handle.
ImportantBefore you set log uploading-specific parameters, make sure that device authentication information is specified. For more information, see Example.
Sample code:
aiot_logpost_setopt(logpost_handle, AIOT_LOGPOSTOPT_MQTT_HANDLE, mqtt_handle);Relevant parameter:
Parameter
Example
Description
AIOT_LOGPOSTOPT_MQTT_HANDLE
mqtt_handle
You must establish an MQTT connection. This parameter is used to associate with the MQTT connection handle.
Turn on the log uploading switch.
Sample code:
sys_log_switch = 1 ... ... aiot_logpost_setopt(logpost_handle, AIOT_LOGPOSTOPT_SYS_LOG, (void *)&sys_log_switch);Relevant parameter:
Parameter
Example
Description
AIOT_LOGPOSTOPT_SYS_LOG
sys_log_switch
The system log switch of the device. If you turn on the switch, the duration required to establish a connection and communication latency is submitted.
Valid values:
1 (default): turns on the switch.
0: turns off the switch.
Configure status monitoring.
Define a callback function to monitor status.
When you specify the processing logic of the callback, take note of the following items:
aiot_logpost_event_t indicates the format of the event message. This parameter is an input parameter of the callback.
AIOT_LOGPOSTEVT_CONFIG_DATA indicates the message type.
In this example, the message is printed.
void demo_logpost_event_handler(void *handle, const aiot_logpost_event_t *event, void *userdata) { switch (event->type) { /* The event of log configuration. This event is received when the device connects with IoT Platform or you turn on the log uploading switch in the IoT Platform console. */ case AIOT_LOGPOSTEVT_CONFIG_DATA: { printf("user log switch state is: %d\r\n", event->data.config_data.on_off); printf("toggle it using the switch in device detail page in https://iot.console.alibabacloud.com\r\n"); } default: break; } }Configure the callback function to monitor status.
Sample code:
aiot_logpost_setopt(logpost_handle, AIOT_LOGPOSTOPT_EVENT_HANDLER, (void *)aiot_logpost_event_handler);Relevant parameter:
Parameter
Example
Description
AIOT_LOGPOSTOPT_MQTT_HANDLE
aiot_logpost_event_handlere
After you connect a device to IoT Platform by using Link SDK, Link SDK queries whether the log uploading switch is turned on in the IoT Platform console. When IoT Platform returns a response, this callback is called to perform the required operations.
Step 3: Send a request
Call the aiot_logpost_send operation to upload business logs to IoT Platform. For more information about the parameters, see the previous step.
When you upload logs, take note of the following items:
aiot_logpost_msg_t indicates the data format.
For more information about the Alink format of messages, see Device log reporting.
In this example, the uploaded log data is
log in while(1).
void demo_send_log(void *handle, char *log)
{
int32_t res = 0;
aiot_logpost_msg_t msg;
memset(&msg, 0, sizeof(aiot_logpost_msg_t));
msg.timestamp = 0; /* The timestamp in milliseconds. The value 0 indicates that the SDK uses the current timestamp. */
msg.loglevel = AIOT_LOGPOST_LEVEL_DEBUG; /* The log level. */
msg.module_name = "APP"; /* The log module. */
msg.code = 200; /* The status code. */
msg.msg_id = 0; /* The ID of the message sent from IoT Platform. If no message is sent from IoT Platform, set the value to 0. */
msg.content = log; /* The log content. */
res = aiot_logpost_send(handle, &msg);
if (res < 0) {
printf("aiot_logpost_send failed: -0x%04X\r\n", -res);
}
}
……
...
while (1) {
sleep(10);
/* TODO: You can delete comments when you use the sample code to upload logs. Note: After the log module is initialized, the log uploading switch is off by default. The log module synchronizes the switch status with the IoT Platform console after the device is connected to IoT Platform. */
demo_send_log(logpost_handle, "log in while(1)");
}Step 4: Exit the program
Call the aiot_logpost_deinit operation to destroy the LogPost client instance and release the resources.
res = aiot_logpost_deinit(&logpost_handle);
if (res < STATE_SUCCESS) {
demo_mqtt_stop(&mqtt_handle);
printf("aiot_logpost_deinit failed: -0x%04X\r\n", -res);
return -1;
}Next steps
After you configure the sample code file, compile the file to generate an executable file. In this example, the ./demos/logpost-basic-demo executable file is generated.
For more information, see Prepare an environment.
For more information about the execution results, see View logs.