log_freertos.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdint.h>
  15. #include <time.h>
  16. #include <sys/time.h>
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "freertos/semphr.h"
  20. #include "soc/cpu.h" // for esp_cpu_get_ccount()
  21. #include "esp_log.h"
  22. #include "esp_log_private.h"
  23. // Maximum time to wait for the mutex in a logging statement.
  24. #define MAX_MUTEX_WAIT_MS 10
  25. #define MAX_MUTEX_WAIT_TICKS ((MAX_MUTEX_WAIT_MS + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS)
  26. static SemaphoreHandle_t s_log_mutex = NULL;
  27. void esp_log_impl_lock(void)
  28. {
  29. if (!s_log_mutex) {
  30. s_log_mutex = xSemaphoreCreateMutex();
  31. }
  32. xSemaphoreTake(s_log_mutex, portMAX_DELAY);
  33. }
  34. bool esp_log_impl_lock_timeout(void)
  35. {
  36. if (!s_log_mutex) {
  37. s_log_mutex = xSemaphoreCreateMutex();
  38. }
  39. return xSemaphoreTake(s_log_mutex, MAX_MUTEX_WAIT_TICKS) == pdTRUE;
  40. }
  41. void esp_log_impl_unlock(void)
  42. {
  43. xSemaphoreGive(s_log_mutex);
  44. }
  45. char *esp_log_system_timestamp(void)
  46. {
  47. static char buffer[18] = {0};
  48. static _lock_t bufferLock = 0;
  49. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  50. uint32_t timestamp = esp_log_early_timestamp();
  51. for (uint8_t i = 0; i < sizeof(buffer); i++) {
  52. if ((timestamp > 0) || (i == 0)) {
  53. for (uint8_t j = sizeof(buffer) - 1; j > 0; j--) {
  54. buffer[j] = buffer[j - 1];
  55. }
  56. buffer[0] = (char)(timestamp % 10) + '0';
  57. timestamp /= 10;
  58. } else {
  59. buffer[i] = 0;
  60. break;
  61. }
  62. }
  63. return buffer;
  64. } else {
  65. struct timeval tv;
  66. struct tm timeinfo;
  67. gettimeofday(&tv, NULL);
  68. localtime_r(&tv.tv_sec, &timeinfo);
  69. _lock_acquire(&bufferLock);
  70. snprintf(buffer, sizeof(buffer),
  71. "%02d:%02d:%02d.%03ld",
  72. timeinfo.tm_hour,
  73. timeinfo.tm_min,
  74. timeinfo.tm_sec,
  75. tv.tv_usec / 1000);
  76. _lock_release(&bufferLock);
  77. return buffer;
  78. }
  79. }
  80. uint32_t esp_log_timestamp(void)
  81. {
  82. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  83. return esp_log_early_timestamp();
  84. }
  85. static uint32_t base = 0;
  86. if (base == 0 && xPortGetCoreID() == 0) {
  87. base = esp_log_early_timestamp();
  88. }
  89. TickType_t tick_count = xPortInIsrContext() ? xTaskGetTickCountFromISR() : xTaskGetTickCount();
  90. return base + tick_count * (1000 / configTICK_RATE_HZ);
  91. }
  92. /* FIXME: define an API for getting the timestamp in soc/hal */
  93. uint32_t esp_log_early_timestamp(void)
  94. {
  95. extern uint32_t g_ticks_per_us_pro;
  96. return esp_cpu_get_ccount() / (g_ticks_per_us_pro * 1000);
  97. }