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

  • 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 establish an MQTT connection. For more information, see Overview.

Step 1: Initialize a client.

  1. Add header files.
    ...
    ...
    
    #include "aiot_logpost_api.h"
  2. 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);
  3. Call the aiot_logpost_init operation to create a data-model client instance and initialize the 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 required features

Call the aiot_logpost_setopt operation to create a LogPost client instance and initialize the default parameters.

  1. Associate with an MQTT connection handle.
    Notice Before 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);                      
    • Parameters:
      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.
  2. 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);                    
    • Parameters:
      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.
  3. Configure status monitoring.
    1. Define the callback 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.aliyun.com\r\n");
              }
              default:
                  break;
          }
      }
    2. Specify the callbacks to monitor status.
      • Sample code
            aiot_logpost_setopt(logpost_handle, AIOT_LOGPOSTOPT_EVENT_HANDLER, (void *)aiot_logpost_event_handler);                     
      • Parameters:
        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:

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.