1
6
7
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
71
72
87
89
90
91
92
93
94
95
96
97
99
100
101
102
103
104
105
106
107
108
115
116
117
118
119
120
121
122
123
124
125
127
128
129
/* ... */
#include <assert.h>
#include <time.h>
#include <sys/time.h>
#include "esp_log_timestamp.h"
#include "esp_private/log_util.h"
#include "esp_private/log_timestamp.h"
#include "sdkconfig.h"7 includes
#ifndef NON_OS_BUILD
#include <sys/lock.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
char *esp_log_system_timestamp(void)
{
static char buffer[18] = {0};
static _lock_t bufferLock = 0;
#if CONFIG_IDF_TARGET_LINUX
const bool early_log = false;
#else
const bool early_log = xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED;
#endif
if (early_log) {
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;
}{...}
}{...}
}{...} else {
struct timeval tv;
struct tm timeinfo;
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &timeinfo);
_lock_acquire(&bufferLock);
unsigned msec = tv.tv_usec / 1000;
snprintf(buffer, sizeof(buffer),
"%02d:%02d:%02d.%03d",
timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec,
msec);
_lock_release(&bufferLock);
}{...}
return buffer;
}{ ... }
#endif/* ... */
uint64_t esp_log_timestamp64(bool critical)
{
uint64_t timestamp_ms;
#if CONFIG_BOOTLOADER_LOG_TIMESTAMP_SOURCE_NONE || CONFIG_LOG_TIMESTAMP_SOURCE_NONE
(void) critical;
timestamp_ms = 0;/* ... */
#elif CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM || CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM_FULL
if (critical) {
timestamp_ms = esp_log_early_timestamp();
}{...} else {
#if CONFIG_IDF_TARGET_LINUX
struct timespec tv;
int result = clock_gettime(CLOCK_MONOTONIC, &tv);
assert(result == 0);
timestamp_ms = tv.tv_sec * 1000 + tv.tv_nsec / 1000000;/* ... */
#else
struct timeval tv;
gettimeofday(&tv, NULL);
timestamp_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;/* ... */
#endif
}{...}
#else/* ... */
(void) critical;
timestamp_ms = esp_log_timestamp();/* ... */
#endif
return timestamp_ms;
}{ ... }
char* esp_log_timestamp_str(bool critical, uint64_t timestamp_ms, char* buffer)
{
char* out_buffer = buffer;
#if CONFIG_BOOTLOADER_LOG_TIMESTAMP_SOURCE_NONE || CONFIG_LOG_TIMESTAMP_SOURCE_NONE
(void)critical;
*buffer = '\0';/* ... */
#elif CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM || CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM_FULL
if (critical) {
esp_log_util_cvt_dec(timestamp_ms, 0, buffer);
}{...} else {
struct tm timeinfo;
time_t sec = timestamp_ms / 1000;
uint64_t msec = timestamp_ms % 1000;
localtime_r(&sec, &timeinfo);
#if CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM_FULL
buffer += esp_log_util_cvt_dec(timeinfo.tm_year, 2, buffer);
*buffer++ = '-';
buffer += esp_log_util_cvt_dec(timeinfo.tm_mon + 1, 2, buffer);
*buffer++ = '-';
buffer += esp_log_util_cvt_dec(timeinfo.tm_mday, 2, buffer);
*buffer++ = ' ';/* ... */
#endif
buffer += esp_log_util_cvt_dec(timeinfo.tm_hour, 2, buffer);
*buffer++ = ':';
buffer += esp_log_util_cvt_dec(timeinfo.tm_min, 2, buffer);
*buffer++ = ':';
buffer += esp_log_util_cvt_dec(timeinfo.tm_sec, 2, buffer);
*buffer++ = '.';
esp_log_util_cvt_dec(msec, 3, buffer);
}{...}
#else/* ... */
(void)critical;
esp_log_util_cvt_dec(timestamp_ms, 0, buffer);/* ... */
#endif
return out_buffer;
}{ ... }