1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #include "freertos/FreeRTOS.h"
11 #include "freertos/task.h"
12 #include "freertos/semphr.h"
13 #include "esp_cpu.h" // for esp_cpu_get_cycle_count()
14 #include "esp_compiler.h"
15 #include "esp_log.h"
16 #include "esp_log_private.h"
17 
18 
19 // Maximum time to wait for the mutex in a logging statement.
20 //
21 // We don't expect this to happen in most cases, as contention is low. The most likely case is if a
22 // log function is called from an ISR (technically caller should use the ISR-friendly logging macros but
23 // possible they use the normal one instead and disable the log type by tag).
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 
27 static SemaphoreHandle_t s_log_mutex = NULL;
28 
esp_log_impl_lock(void)29 void esp_log_impl_lock(void)
30 {
31     if (unlikely(!s_log_mutex)) {
32         s_log_mutex = xSemaphoreCreateMutex();
33     }
34     if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
35         return;
36     }
37     xSemaphoreTake(s_log_mutex, portMAX_DELAY);
38 }
39 
esp_log_impl_lock_timeout(void)40 bool esp_log_impl_lock_timeout(void)
41 {
42     if (unlikely(!s_log_mutex)) {
43         s_log_mutex = xSemaphoreCreateMutex();
44     }
45     if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
46         return true;
47     }
48     return xSemaphoreTake(s_log_mutex, MAX_MUTEX_WAIT_TICKS) == pdTRUE;
49 }
50 
esp_log_impl_unlock(void)51 void esp_log_impl_unlock(void)
52 {
53     if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
54         return;
55     }
56     xSemaphoreGive(s_log_mutex);
57 }
58 
esp_log_system_timestamp(void)59 char *esp_log_system_timestamp(void)
60 {
61     static char buffer[18] = {0};
62     static _lock_t bufferLock = 0;
63 
64     if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
65         uint32_t timestamp = esp_log_early_timestamp();
66         for (uint8_t i = 0; i < sizeof(buffer); i++) {
67             if ((timestamp > 0) || (i == 0)) {
68                 for (uint8_t j = sizeof(buffer) - 1; j > 0; j--) {
69                     buffer[j] = buffer[j - 1];
70                 }
71                 buffer[0] = (char)(timestamp % 10) + '0';
72                 timestamp /= 10;
73             } else {
74                 buffer[i] = 0;
75                 break;
76             }
77         }
78         return buffer;
79     } else {
80         struct timeval tv;
81         struct tm timeinfo;
82 
83         gettimeofday(&tv, NULL);
84         localtime_r(&tv.tv_sec, &timeinfo);
85 
86         _lock_acquire(&bufferLock);
87         snprintf(buffer, sizeof(buffer),
88                  "%02d:%02d:%02d.%03ld",
89                  timeinfo.tm_hour,
90                  timeinfo.tm_min,
91                  timeinfo.tm_sec,
92                  tv.tv_usec / 1000);
93         _lock_release(&bufferLock);
94 
95         return buffer;
96     }
97 }
98 
esp_log_timestamp(void)99 uint32_t esp_log_timestamp(void)
100 {
101     if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
102         return esp_log_early_timestamp();
103     }
104     static uint32_t base = 0;
105     if (base == 0 && xPortGetCoreID() == 0) {
106         base = esp_log_early_timestamp();
107     }
108     TickType_t tick_count = xPortInIsrContext() ? xTaskGetTickCountFromISR() : xTaskGetTickCount();
109     return base + tick_count * (1000 / configTICK_RATE_HZ);
110 }
111 
112 /* FIXME: define an API for getting the timestamp in soc/hal IDF-2351 */
esp_log_early_timestamp(void)113 uint32_t esp_log_early_timestamp(void)
114 {
115 #if CONFIG_IDF_TARGET_ESP32
116     /* ESP32 ROM stores separate clock rate values for each CPU, but we want the PRO CPU value always */
117     extern uint32_t g_ticks_per_us_pro;
118     return esp_cpu_get_cycle_count() / (g_ticks_per_us_pro * 1000);
119 #else
120     return esp_cpu_get_cycle_count() / (esp_rom_get_cpu_ticks_per_us() * 1000);
121 #endif
122 }
123