log.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. /*
  14. * Log library implementation notes.
  15. *
  16. * Log library stores all tags provided to esp_log_level_set as a linked
  17. * list. See uncached_tag_entry_t structure.
  18. *
  19. * To avoid looking up log level for given tag each time message is
  20. * printed, this library caches pointers to tags. Because the suggested
  21. * way of creating tags uses one 'TAG' constant per file, this caching
  22. * should be effective. Cache is a binary min-heap of cached_tag_entry_t
  23. * items, ordering is done on 'generation' member. In this context,
  24. * generation is an integer which is incremented each time an operation
  25. * with cache is performed. When cache is full, new item is inserted in
  26. * place of an oldest item (that is, with smallest 'generation' value).
  27. * After that, bubble-down operation is performed to fix ordering in the
  28. * min-heap.
  29. *
  30. * The potential problem with wrap-around of cache generation counter is
  31. * ignored for now. This will happen if someone happens to output more
  32. * than 4 billion log entries, at which point wrap-around will not be
  33. * the biggest problem.
  34. *
  35. */
  36. #ifndef BOOTLOADER_BUILD
  37. #include <freertos/FreeRTOS.h>
  38. #include <freertos/FreeRTOSConfig.h>
  39. #include <freertos/task.h>
  40. #include <freertos/semphr.h>
  41. #endif
  42. #include "esp_attr.h"
  43. #include "xtensa/hal.h"
  44. #include "soc/soc.h"
  45. #include <stdbool.h>
  46. #include <stdarg.h>
  47. #include <string.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <assert.h>
  51. #include <ctype.h>
  52. #include <time.h>
  53. #include <sys/time.h>
  54. #include "esp_log.h"
  55. #include "sys/queue.h"
  56. #include "soc/soc_memory_layout.h"
  57. //print number of bytes per line for esp_log_buffer_char and esp_log_buffer_hex
  58. #define BYTES_PER_LINE 16
  59. #ifndef BOOTLOADER_BUILD
  60. // Number of tags to be cached. Must be 2**n - 1, n >= 2.
  61. #define TAG_CACHE_SIZE 31
  62. // Maximum time to wait for the mutex in a logging statement.
  63. #define MAX_MUTEX_WAIT_MS 10
  64. #define MAX_MUTEX_WAIT_TICKS ((MAX_MUTEX_WAIT_MS + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS)
  65. // Uncomment this to enable consistency checks and cache statistics in this file.
  66. // #define LOG_BUILTIN_CHECKS
  67. typedef struct {
  68. const char* tag;
  69. uint32_t level : 3;
  70. uint32_t generation : 29;
  71. } cached_tag_entry_t;
  72. typedef struct uncached_tag_entry_{
  73. SLIST_ENTRY(uncached_tag_entry_) entries;
  74. uint8_t level; // esp_log_level_t as uint8_t
  75. char tag[0]; // beginning of a zero-terminated string
  76. } uncached_tag_entry_t;
  77. static esp_log_level_t s_log_default_level = ESP_LOG_VERBOSE;
  78. static SLIST_HEAD(log_tags_head , uncached_tag_entry_) s_log_tags = SLIST_HEAD_INITIALIZER(s_log_tags);
  79. static cached_tag_entry_t s_log_cache[TAG_CACHE_SIZE];
  80. static uint32_t s_log_cache_max_generation = 0;
  81. static uint32_t s_log_cache_entry_count = 0;
  82. static vprintf_like_t s_log_print_func = &vprintf;
  83. static SemaphoreHandle_t s_log_mutex = NULL;
  84. #ifdef LOG_BUILTIN_CHECKS
  85. static uint32_t s_log_cache_misses = 0;
  86. #endif
  87. static inline bool get_cached_log_level(const char* tag, esp_log_level_t* level);
  88. static inline bool get_uncached_log_level(const char* tag, esp_log_level_t* level);
  89. static inline void add_to_cache(const char* tag, esp_log_level_t level);
  90. static void heap_bubble_down(int index);
  91. static inline void heap_swap(int i, int j);
  92. static inline bool should_output(esp_log_level_t level_for_message, esp_log_level_t level_for_tag);
  93. static inline void clear_log_level_list(void);
  94. vprintf_like_t esp_log_set_vprintf(vprintf_like_t func)
  95. {
  96. if (!s_log_mutex) {
  97. s_log_mutex = xSemaphoreCreateMutex();
  98. }
  99. xSemaphoreTake(s_log_mutex, portMAX_DELAY);
  100. vprintf_like_t orig_func = s_log_print_func;
  101. s_log_print_func = func;
  102. xSemaphoreGive(s_log_mutex);
  103. return orig_func;
  104. }
  105. void esp_log_level_set(const char* tag, esp_log_level_t level)
  106. {
  107. if (!s_log_mutex) {
  108. s_log_mutex = xSemaphoreCreateMutex();
  109. }
  110. xSemaphoreTake(s_log_mutex, portMAX_DELAY);
  111. // for wildcard tag, remove all linked list items and clear the cache
  112. if (strcmp(tag, "*") == 0) {
  113. s_log_default_level = level;
  114. clear_log_level_list();
  115. xSemaphoreGive(s_log_mutex);
  116. return;
  117. }
  118. //searching exist tag
  119. uncached_tag_entry_t *it = NULL;
  120. SLIST_FOREACH( it, &s_log_tags, entries ) {
  121. if ( strcmp(it->tag, tag)==0 ) {
  122. //one tag in the linked list match, update the level
  123. it->level = level;
  124. //quit with it != NULL
  125. break;
  126. }
  127. }
  128. //no exist tag, append new one
  129. if ( it == NULL ) {
  130. // allocate new linked list entry and append it to the head of the list
  131. size_t entry_size = offsetof(uncached_tag_entry_t, tag) + strlen(tag) + 1;
  132. uncached_tag_entry_t* new_entry = (uncached_tag_entry_t*) malloc(entry_size);
  133. if (!new_entry) {
  134. xSemaphoreGive(s_log_mutex);
  135. return;
  136. }
  137. new_entry->level = (uint8_t) level;
  138. strcpy(new_entry->tag, tag);
  139. SLIST_INSERT_HEAD( &s_log_tags, new_entry, entries );
  140. }
  141. //search in the cache and update it if exist
  142. for (int i = 0; i < s_log_cache_entry_count; ++i) {
  143. #ifdef LOG_BUILTIN_CHECKS
  144. assert(i == 0 || s_log_cache[(i - 1) / 2].generation < s_log_cache[i].generation);
  145. #endif
  146. if (strcmp(s_log_cache[i].tag,tag) == 0) {
  147. s_log_cache[i].level = level;
  148. break;
  149. }
  150. }
  151. xSemaphoreGive(s_log_mutex);
  152. }
  153. void clear_log_level_list(void)
  154. {
  155. uncached_tag_entry_t *it;
  156. while((it = SLIST_FIRST(&s_log_tags)) != NULL) {
  157. SLIST_REMOVE_HEAD(&s_log_tags, entries );
  158. free(it);
  159. }
  160. s_log_cache_entry_count = 0;
  161. s_log_cache_max_generation = 0;
  162. #ifdef LOG_BUILTIN_CHECKS
  163. s_log_cache_misses = 0;
  164. #endif
  165. }
  166. void IRAM_ATTR esp_log_write(esp_log_level_t level,
  167. const char* tag,
  168. const char* format, ...)
  169. {
  170. if (!s_log_mutex) {
  171. s_log_mutex = xSemaphoreCreateMutex();
  172. }
  173. if (xSemaphoreTake(s_log_mutex, MAX_MUTEX_WAIT_TICKS) == pdFALSE) {
  174. return;
  175. }
  176. esp_log_level_t level_for_tag;
  177. // Look for the tag in cache first, then in the linked list of all tags
  178. if (!get_cached_log_level(tag, &level_for_tag)) {
  179. if (!get_uncached_log_level(tag, &level_for_tag)) {
  180. level_for_tag = s_log_default_level;
  181. }
  182. add_to_cache(tag, level_for_tag);
  183. #ifdef LOG_BUILTIN_CHECKS
  184. ++s_log_cache_misses;
  185. #endif
  186. }
  187. xSemaphoreGive(s_log_mutex);
  188. if (!should_output(level, level_for_tag)) {
  189. return;
  190. }
  191. va_list list;
  192. va_start(list, format);
  193. (*s_log_print_func)(format, list);
  194. va_end(list);
  195. }
  196. static inline bool get_cached_log_level(const char* tag, esp_log_level_t* level)
  197. {
  198. // Look for `tag` in cache
  199. int i;
  200. for (i = 0; i < s_log_cache_entry_count; ++i) {
  201. #ifdef LOG_BUILTIN_CHECKS
  202. assert(i == 0 || s_log_cache[(i - 1) / 2].generation < s_log_cache[i].generation);
  203. #endif
  204. if (s_log_cache[i].tag == tag) {
  205. break;
  206. }
  207. }
  208. if (i == s_log_cache_entry_count) { // Not found in cache
  209. return false;
  210. }
  211. // Return level from cache
  212. *level = (esp_log_level_t) s_log_cache[i].level;
  213. // If cache has been filled, start taking ordering into account
  214. // (other options are: dynamically resize cache, add "dummy" entries
  215. // to the cache; this option was chosen because code is much simpler,
  216. // and the unfair behavior of cache will show it self at most once, when
  217. // it has just been filled)
  218. if (s_log_cache_entry_count == TAG_CACHE_SIZE) {
  219. // Update item generation
  220. s_log_cache[i].generation = s_log_cache_max_generation++;
  221. // Restore heap ordering
  222. heap_bubble_down(i);
  223. }
  224. return true;
  225. }
  226. static inline void add_to_cache(const char* tag, esp_log_level_t level)
  227. {
  228. uint32_t generation = s_log_cache_max_generation++;
  229. // First consider the case when cache is not filled yet.
  230. // In this case, just add new entry at the end.
  231. // This happens to satisfy binary min-heap ordering.
  232. if (s_log_cache_entry_count < TAG_CACHE_SIZE) {
  233. s_log_cache[s_log_cache_entry_count] = (cached_tag_entry_t) {
  234. .generation = generation,
  235. .level = level,
  236. .tag = tag
  237. };
  238. ++s_log_cache_entry_count;
  239. return;
  240. }
  241. // Cache is full, so we replace the oldest entry (which is at index 0
  242. // because this is a min-heap) with the new one, and do bubble-down
  243. // operation to restore min-heap ordering.
  244. s_log_cache[0] = (cached_tag_entry_t) {
  245. .tag = tag,
  246. .level = level,
  247. .generation = generation
  248. };
  249. heap_bubble_down(0);
  250. }
  251. static inline bool get_uncached_log_level(const char* tag, esp_log_level_t* level)
  252. {
  253. // Walk the linked list of all tags and see if given tag is present in the list.
  254. // This is slow because tags are compared as strings.
  255. uncached_tag_entry_t *it;
  256. SLIST_FOREACH( it, &s_log_tags, entries ) {
  257. if (strcmp(tag, it->tag) == 0) {
  258. *level = it->level;
  259. return true;
  260. }
  261. }
  262. return false;
  263. }
  264. static inline bool should_output(esp_log_level_t level_for_message, esp_log_level_t level_for_tag)
  265. {
  266. return level_for_message <= level_for_tag;
  267. }
  268. static void heap_bubble_down(int index)
  269. {
  270. while (index < TAG_CACHE_SIZE / 2) {
  271. int left_index = index * 2 + 1;
  272. int right_index = left_index + 1;
  273. int next = (s_log_cache[left_index].generation < s_log_cache[right_index].generation) ? left_index : right_index;
  274. heap_swap(index, next);
  275. index = next;
  276. }
  277. }
  278. static inline void heap_swap(int i, int j)
  279. {
  280. cached_tag_entry_t tmp = s_log_cache[i];
  281. s_log_cache[i] = s_log_cache[j];
  282. s_log_cache[j] = tmp;
  283. }
  284. #endif //BOOTLOADER_BUILD
  285. #ifndef BOOTLOADER_BUILD
  286. #define ATTR IRAM_ATTR
  287. #else
  288. #define ATTR
  289. #endif // BOOTLOADER_BUILD
  290. //the variable defined in ROM is the cpu frequency in MHz.
  291. //as a workaround before the interface for this variable
  292. extern uint32_t g_ticks_per_us_pro;
  293. uint32_t ATTR esp_log_early_timestamp(void)
  294. {
  295. return xthal_get_ccount() / (g_ticks_per_us_pro * 1000);
  296. }
  297. #ifndef BOOTLOADER_BUILD
  298. char* IRAM_ATTR esp_log_system_timestamp(void)
  299. {
  300. static char buffer[18] = {0};
  301. static _lock_t bufferLock = 0;
  302. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  303. uint32_t timestamp = esp_log_early_timestamp();
  304. for (uint8_t i = 0; i < sizeof(buffer); i++) {
  305. if ((timestamp > 0) || (i == 0)) {
  306. for (uint8_t j = sizeof(buffer) - 1; j > 0; j--) {
  307. buffer[j] = buffer[j - 1];
  308. }
  309. buffer[0] = (char) (timestamp % 10) + '0';
  310. timestamp /= 10;
  311. } else {
  312. buffer[i] = 0;
  313. break;
  314. }
  315. }
  316. return buffer;
  317. } else {
  318. struct timeval tv;
  319. struct tm timeinfo;
  320. gettimeofday(&tv, NULL);
  321. localtime_r(&tv.tv_sec, &timeinfo);
  322. _lock_acquire(&bufferLock);
  323. snprintf(buffer, sizeof(buffer),
  324. "%02d:%02d:%02d.%03ld",
  325. timeinfo.tm_hour,
  326. timeinfo.tm_min,
  327. timeinfo.tm_sec,
  328. tv.tv_usec / 1000);
  329. _lock_release(&bufferLock);
  330. return buffer;
  331. }
  332. }
  333. uint32_t IRAM_ATTR esp_log_timestamp(void)
  334. {
  335. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  336. return esp_log_early_timestamp();
  337. }
  338. static uint32_t base = 0;
  339. if (base == 0 && xPortGetCoreID() == 0) {
  340. base = esp_log_early_timestamp();
  341. }
  342. return base + xTaskGetTickCount() * (1000 / configTICK_RATE_HZ);
  343. }
  344. #else
  345. uint32_t esp_log_timestamp(void) __attribute__((alias("esp_log_early_timestamp")));
  346. #endif //BOOTLOADER_BUILD
  347. void esp_log_buffer_hex_internal(const char *tag, const void *buffer, uint16_t buff_len,
  348. esp_log_level_t log_level)
  349. {
  350. if ( buff_len == 0 ) return;
  351. char temp_buffer[BYTES_PER_LINE+3]; //for not-byte-accessible memory
  352. char hex_buffer[3*BYTES_PER_LINE+1];
  353. const char *ptr_line;
  354. int bytes_cur_line;
  355. do {
  356. if ( buff_len > BYTES_PER_LINE ) {
  357. bytes_cur_line = BYTES_PER_LINE;
  358. } else {
  359. bytes_cur_line = buff_len;
  360. }
  361. if ( !esp_ptr_byte_accessible(buffer) ) {
  362. //use memcpy to get around alignment issue
  363. memcpy( temp_buffer, buffer, (bytes_cur_line+3)/4*4 );
  364. ptr_line = temp_buffer;
  365. } else {
  366. ptr_line = buffer;
  367. }
  368. for( int i = 0; i < bytes_cur_line; i ++ ) {
  369. sprintf( hex_buffer + 3*i, "%02x ", ptr_line[i] );
  370. }
  371. ESP_LOG_LEVEL( log_level, tag, "%s", hex_buffer );
  372. buffer += bytes_cur_line;
  373. buff_len -= bytes_cur_line;
  374. } while( buff_len );
  375. }
  376. void esp_log_buffer_char_internal(const char *tag, const void *buffer, uint16_t buff_len,
  377. esp_log_level_t log_level)
  378. {
  379. if ( buff_len == 0 ) return;
  380. char temp_buffer[BYTES_PER_LINE+3]; //for not-byte-accessible memory
  381. char char_buffer[BYTES_PER_LINE+1];
  382. const char *ptr_line;
  383. int bytes_cur_line;
  384. do {
  385. if ( buff_len > BYTES_PER_LINE ) {
  386. bytes_cur_line = BYTES_PER_LINE;
  387. } else {
  388. bytes_cur_line = buff_len;
  389. }
  390. if ( !esp_ptr_byte_accessible(buffer) ) {
  391. //use memcpy to get around alignment issue
  392. memcpy( temp_buffer, buffer, (bytes_cur_line+3)/4*4 );
  393. ptr_line = temp_buffer;
  394. } else {
  395. ptr_line = buffer;
  396. }
  397. for( int i = 0; i < bytes_cur_line; i ++ ) {
  398. sprintf( char_buffer + i, "%c", ptr_line[i] );
  399. }
  400. ESP_LOG_LEVEL( log_level, tag, "%s", char_buffer );
  401. buffer += bytes_cur_line;
  402. buff_len -= bytes_cur_line;
  403. } while( buff_len );
  404. }
  405. void esp_log_buffer_hexdump_internal( const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level)
  406. {
  407. if ( buff_len == 0 ) return;
  408. char temp_buffer[BYTES_PER_LINE+3]; //for not-byte-accessible memory
  409. const char *ptr_line;
  410. //format: field[length]
  411. // ADDR[10]+" "+DATA_HEX[8*3]+" "+DATA_HEX[8*3]+" |"+DATA_CHAR[8]+"|"
  412. char hd_buffer[10+3+BYTES_PER_LINE*3+3+BYTES_PER_LINE+1+1];
  413. char *ptr_hd;
  414. int bytes_cur_line;
  415. do {
  416. if ( buff_len > BYTES_PER_LINE ) {
  417. bytes_cur_line = BYTES_PER_LINE;
  418. } else {
  419. bytes_cur_line = buff_len;
  420. }
  421. if ( !esp_ptr_byte_accessible(buffer) ) {
  422. //use memcpy to get around alignment issue
  423. memcpy( temp_buffer, buffer, (bytes_cur_line+3)/4*4 );
  424. ptr_line = temp_buffer;
  425. } else {
  426. ptr_line = buffer;
  427. }
  428. ptr_hd = hd_buffer;
  429. ptr_hd += sprintf( ptr_hd, "%p ", buffer );
  430. for( int i = 0; i < BYTES_PER_LINE; i ++ ) {
  431. if ( (i&7)==0 ) {
  432. ptr_hd += sprintf( ptr_hd, " " );
  433. }
  434. if ( i < bytes_cur_line ) {
  435. ptr_hd += sprintf( ptr_hd, " %02x", ptr_line[i] );
  436. } else {
  437. ptr_hd += sprintf( ptr_hd, " " );
  438. }
  439. }
  440. ptr_hd += sprintf( ptr_hd, " |" );
  441. for( int i = 0; i < bytes_cur_line; i ++ ) {
  442. if ( isprint((int)ptr_line[i]) ) {
  443. ptr_hd += sprintf( ptr_hd, "%c", ptr_line[i] );
  444. } else {
  445. ptr_hd += sprintf( ptr_hd, "." );
  446. }
  447. }
  448. ptr_hd += sprintf( ptr_hd, "|" );
  449. ESP_LOG_LEVEL( log_level, tag, "%s", hd_buffer );
  450. buffer += bytes_cur_line;
  451. buff_len -= bytes_cur_line;
  452. } while( buff_len );
  453. }