Quick start

Last Updated: Apr 19, 2017

The Alibaba Cloud Log Service C SDK is used to access logs from various platforms, such as MIPS and OpenWrt systems. The C SDK supports RAM temporary identity (STS).

The C SDK uses cURL as a network library. The apr/apr-util library is intended for memory management and cross-platform interconnection. You only need to simply compile the source code to use the C SDK.

GitHub project address and more details

Example

  1. #include "aos_log.h"
  2. #include "aos_util.h"
  3. #include "aos_string.h"
  4. #include "aos_status.h"
  5. #include "log_auth.h"
  6. #include "log_util.h"
  7. #include "log_api.h"
  8. #include "log_config.h"
  9. void log_post_logs_sample()
  10. {
  11. aos_pool_t *p = NULL;
  12. aos_status_t *s = NULL;
  13. //Create a memory pool
  14. aos_pool_create(&p, NULL);
  15. //Create log data
  16. cJSON *root = cJSON_CreateObject();
  17. cJSON_AddStringToObject(root, "__source__", "127.0.0.1");
  18. cJSON_AddStringToObject(root, "__topic__", "topic");
  19. cJSON *logs = cJSON_CreateArray();
  20. cJSON_AddItemToObject(root, "__logs__", logs);
  21. cJSON *log1 = cJSON_CreateObject();
  22. cJSON_AddStringToObject(log1, "level", "error1");
  23. cJSON_AddStringToObject(log1, "message", "c sdk test message1");
  24. cJSON_AddNumberToObject(log1, "__time__", apr_time_now()/1000000);
  25. cJSON *log2 = cJSON_CreateObject();
  26. cJSON_AddStringToObject(log2, "level", "error2");
  27. cJSON_AddStringToObject(log2, "message", "c sdk test message2");
  28. cJSON_AddNumberToObject(log2, "__time__", apr_time_now()/1000000);
  29. cJSON_AddItemToArray(logs, log1);
  30. cJSON_AddItemToArray(logs, log2);
  31. //Call the interface to send data
  32. s = log_post_logs(p, LOG_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET, PROJECT_NAME, LOGSTORE_NAME, root);
  33. if (aos_status_is_ok(s)) {
  34. printf("post logs succeeded\n");
  35. } else {
  36. printf("put logs failed\n");
  37. }
  38. //Delete JSON resources
  39. cJSON_Delete(root);
  40. //Destroy the memory pool
  41. aos_pool_destroy(p);
  42. }
  43. int main(int argc, char *argv[])
  44. {
  45. //Initialize HTTP I/O resources
  46. if (aos_http_io_initialize("linux-x86_64", 0) != AOSE_OK) {
  47. exit(1);
  48. }
  49. log_post_logs_sample();
  50. //Release HTTP I/O resources
  51. aos_http_io_deinitialize();
  52. return 0;
  53. }

The JSON format is as follows:

  1. {
  2. "__source__":"you host ip address",
  3. # Optional
  4. "__topic__":"you topic",
  5. #Log data, JSON array
  6. "__logs__":
  7. [
  8. {
  9. # Unix timestamp of the log, in seconds
  10. "__time__":1464949581,
  11. #Other fields of the log
  12. "key1":"value1",
  13. "key2":"value2"
  14. },
  15. {
  16. "__time__":1464949581,
  17. "key1":"value11",
  18. "key2":"value22"
  19. }
  20. ]
  21. }
Thank you! We've received your feedback.