This article describes how to call the API operations of Link SDK for C to to upload local device logs to IoT Platform. In this example, the ./demos/logpost_basic_demo.c sample code file is used.
Background information
Step 1: Initialize a client.
Step 2: Configure required features
Call the aiot_logpost_setopt operation to create a LogPost
client instance and initialize the default parameters.
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 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;
}
What to do next
-
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 Compilation and running.
-
For more information about running results, see View logs.