qcloud_iot_log.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub
  3. available.
  4. * Copyright (C) 2018-2020 THL A29 Limited, a Tencent company. All rights
  5. reserved.
  6. * Licensed under the MIT License (the "License"); you may not use this file
  7. except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * http://opensource.org/licenses/MIT
  10. * Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is
  12. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND,
  14. * either express or implied. See the License for the specific language
  15. governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #include <string.h>
  23. #include "log_upload.h"
  24. #include "qcloud_iot_export_log.h"
  25. #include "qcloud_iot_import.h"
  26. //static char *level_str[] = {"DIS", "ERR", "WRN", "INF", "DBG"};
  27. static LogMessageHandler sg_log_message_handler = NULL;
  28. LOG_LEVEL g_log_print_level = eLOG_INFO;
  29. #ifdef LOG_UPLOAD
  30. LOG_LEVEL g_log_upload_level = eLOG_ERROR;
  31. #else
  32. LOG_LEVEL g_log_upload_level = eLOG_DISABLE;
  33. #endif
  34. //static const char *_get_filename(const char *p)
  35. //{
  36. //#ifdef WIN32
  37. // char ch = '\\';
  38. //#else
  39. // char ch = '/';
  40. //#endif
  41. // const char *q = strrchr(p, ch);
  42. // if (q == NULL) {
  43. // q = p;
  44. // } else {
  45. // q++;
  46. // }
  47. // return q;
  48. //}
  49. void IOT_Log_Set_Level(LOG_LEVEL logLevel)
  50. {
  51. g_log_print_level = logLevel;
  52. }
  53. LOG_LEVEL IOT_Log_Get_Level(void)
  54. {
  55. return g_log_print_level;
  56. }
  57. void IOT_Log_Set_MessageHandler(LogMessageHandler handler)
  58. {
  59. sg_log_message_handler = handler;
  60. }
  61. void IOT_Log_Set_Upload_Level(LOG_LEVEL logLevel)
  62. {
  63. g_log_upload_level = logLevel;
  64. }
  65. LOG_LEVEL IOT_Log_Get_Upload_Level(void)
  66. {
  67. return g_log_upload_level;
  68. }
  69. int IOT_Log_Init_Uploader(LogUploadInitParams *init_params)
  70. {
  71. #ifdef LOG_UPLOAD
  72. return init_log_uploader(init_params);
  73. #else
  74. return 0;
  75. #endif
  76. }
  77. void IOT_Log_Fini_Uploader(void)
  78. {
  79. #ifdef LOG_UPLOAD
  80. fini_log_uploader();
  81. return;
  82. #else
  83. return;
  84. #endif
  85. }
  86. int IOT_Log_Upload(bool force_upload)
  87. {
  88. #ifdef LOG_UPLOAD
  89. return do_log_upload(force_upload);
  90. #else
  91. return 0;
  92. #endif
  93. }
  94. //void IOT_Log_Gen(const char *file, const char *func, const int line, const int level, const char *fmt, ...)
  95. //{
  96. // if (level > g_log_print_level && level > g_log_upload_level) {
  97. // return;
  98. // }
  99. //
  100. // /* format log content */
  101. // const char *file_name = _get_filename(file);
  102. //
  103. // char sg_text_buf[MAX_LOG_MSG_LEN + 1];
  104. // char *tmp_buf = sg_text_buf;
  105. // char *o = tmp_buf;
  106. // memset(tmp_buf, 0, sizeof(sg_text_buf));
  107. // char time_str[TIME_FORMAT_STR_LEN] = {0};
  108. //
  109. // o += HAL_Snprintf(o, sizeof(sg_text_buf), "%s|%s|%s|%s(%d): ", level_str[level], HAL_Timer_current(time_str),
  110. // file_name, func, line);
  111. //
  112. // va_list ap;
  113. // va_start(ap, fmt);
  114. // HAL_Vsnprintf(o, MAX_LOG_MSG_LEN - 2 - strlen(tmp_buf), fmt, ap);
  115. // va_end(ap);
  116. //
  117. // strcat(tmp_buf, "\r\n");
  118. //
  119. //#ifdef LOG_UPLOAD
  120. // /* append to upload buffer */
  121. // if (level <= g_log_upload_level) {
  122. // append_to_upload_buffer(tmp_buf, strlen(tmp_buf));
  123. // }
  124. //#endif
  125. //
  126. // if (level <= g_log_print_level) {
  127. // /* customer defined log print handler */
  128. // if (sg_log_message_handler != NULL && sg_log_message_handler(tmp_buf)) {
  129. // return;
  130. // }
  131. //
  132. // /* default log handler: print to console */
  133. // HAL_Printf("%s", tmp_buf);
  134. // }
  135. //
  136. // return;
  137. //}
  138. #ifdef __cplusplus
  139. }
  140. #endif