2021-04-12 06:02:59 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-11-21 17:40:59 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/semphr.h"
|
2022-07-21 11:24:42 +00:00
|
|
|
#include "esp_cpu.h" // for esp_cpu_get_cycle_count()
|
2021-03-11 09:17:21 +00:00
|
|
|
#include "esp_compiler.h"
|
2019-11-21 17:40:59 +00:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_log_private.h"
|
|
|
|
|
|
|
|
// Maximum time to wait for the mutex in a logging statement.
|
2021-03-11 09:17:21 +00:00
|
|
|
//
|
|
|
|
// We don't expect this to happen in most cases, as contention is low. The most likely case is if a
|
|
|
|
// log function is called from an ISR (technically caller should use the ISR-friendly logging macros but
|
|
|
|
// possible they use the normal one instead and disable the log type by tag).
|
2019-11-21 17:40:59 +00:00
|
|
|
#define MAX_MUTEX_WAIT_MS 10
|
|
|
|
#define MAX_MUTEX_WAIT_TICKS ((MAX_MUTEX_WAIT_MS + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS)
|
|
|
|
|
|
|
|
static SemaphoreHandle_t s_log_mutex = NULL;
|
|
|
|
|
|
|
|
void esp_log_impl_lock(void)
|
|
|
|
{
|
2021-03-11 09:17:21 +00:00
|
|
|
if (unlikely(!s_log_mutex)) {
|
2019-11-21 17:40:59 +00:00
|
|
|
s_log_mutex = xSemaphoreCreateMutex();
|
|
|
|
}
|
2021-03-11 09:17:21 +00:00
|
|
|
if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-21 17:40:59 +00:00
|
|
|
xSemaphoreTake(s_log_mutex, portMAX_DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool esp_log_impl_lock_timeout(void)
|
|
|
|
{
|
2021-03-11 09:17:21 +00:00
|
|
|
if (unlikely(!s_log_mutex)) {
|
2019-11-21 17:40:59 +00:00
|
|
|
s_log_mutex = xSemaphoreCreateMutex();
|
|
|
|
}
|
2021-03-11 09:17:21 +00:00
|
|
|
if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-21 17:40:59 +00:00
|
|
|
return xSemaphoreTake(s_log_mutex, MAX_MUTEX_WAIT_TICKS) == pdTRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_log_impl_unlock(void)
|
|
|
|
{
|
2021-03-11 09:17:21 +00:00
|
|
|
if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-21 17:40:59 +00:00
|
|
|
xSemaphoreGive(s_log_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *esp_log_system_timestamp(void)
|
|
|
|
{
|
|
|
|
static char buffer[18] = {0};
|
|
|
|
static _lock_t bufferLock = 0;
|
|
|
|
|
|
|
|
if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
|
|
|
|
uint32_t timestamp = esp_log_early_timestamp();
|
|
|
|
for (uint8_t i = 0; i < sizeof(buffer); i++) {
|
|
|
|
if ((timestamp > 0) || (i == 0)) {
|
|
|
|
for (uint8_t j = sizeof(buffer) - 1; j > 0; j--) {
|
|
|
|
buffer[j] = buffer[j - 1];
|
|
|
|
}
|
|
|
|
buffer[0] = (char)(timestamp % 10) + '0';
|
|
|
|
timestamp /= 10;
|
|
|
|
} else {
|
|
|
|
buffer[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
} else {
|
|
|
|
struct timeval tv;
|
|
|
|
struct tm timeinfo;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
localtime_r(&tv.tv_sec, &timeinfo);
|
|
|
|
|
|
|
|
_lock_acquire(&bufferLock);
|
|
|
|
snprintf(buffer, sizeof(buffer),
|
|
|
|
"%02d:%02d:%02d.%03ld",
|
|
|
|
timeinfo.tm_hour,
|
|
|
|
timeinfo.tm_min,
|
|
|
|
timeinfo.tm_sec,
|
|
|
|
tv.tv_usec / 1000);
|
|
|
|
_lock_release(&bufferLock);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t esp_log_timestamp(void)
|
|
|
|
{
|
2021-03-11 09:17:21 +00:00
|
|
|
if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
|
2019-11-21 17:40:59 +00:00
|
|
|
return esp_log_early_timestamp();
|
|
|
|
}
|
|
|
|
static uint32_t base = 0;
|
|
|
|
if (base == 0 && xPortGetCoreID() == 0) {
|
|
|
|
base = esp_log_early_timestamp();
|
|
|
|
}
|
2020-02-05 13:09:14 +00:00
|
|
|
TickType_t tick_count = xPortInIsrContext() ? xTaskGetTickCountFromISR() : xTaskGetTickCount();
|
|
|
|
return base + tick_count * (1000 / configTICK_RATE_HZ);
|
2019-11-21 17:40:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 04:00:07 +00:00
|
|
|
/* FIXME: define an API for getting the timestamp in soc/hal IDF-2351 */
|
2019-11-21 17:40:59 +00:00
|
|
|
uint32_t esp_log_early_timestamp(void)
|
|
|
|
{
|
2020-11-06 04:00:07 +00:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
/* ESP32 ROM stores separate clock rate values for each CPU, but we want the PRO CPU value always */
|
2019-11-21 17:40:59 +00:00
|
|
|
extern uint32_t g_ticks_per_us_pro;
|
2022-07-21 11:24:42 +00:00
|
|
|
return esp_cpu_get_cycle_count() / (g_ticks_per_us_pro * 1000);
|
2020-11-06 04:00:07 +00:00
|
|
|
#else
|
2022-07-21 11:24:42 +00:00
|
|
|
return esp_cpu_get_cycle_count() / (esp_rom_get_cpu_ticks_per_us() * 1000);
|
2020-11-06 04:00:07 +00:00
|
|
|
#endif
|
2019-11-21 17:40:59 +00:00
|
|
|
}
|