log.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. * Log library implementation notes.
  8. *
  9. * Log library stores all tags provided to esp_log_level_set as a linked
  10. * list. See uncached_tag_entry_t structure.
  11. *
  12. * To avoid looking up log level for given tag each time message is
  13. * printed, this library caches pointers to tags. Because the suggested
  14. * way of creating tags uses one 'TAG' constant per file, this caching
  15. * should be effective. Cache is a binary min-heap of cached_tag_entry_t
  16. * items, ordering is done on 'generation' member. In this context,
  17. * generation is an integer which is incremented each time an operation
  18. * with cache is performed. When cache is full, new item is inserted in
  19. * place of an oldest item (that is, with smallest 'generation' value).
  20. * After that, bubble-down operation is performed to fix ordering in the
  21. * min-heap.
  22. *
  23. * The potential problem with wrap-around of cache generation counter is
  24. * ignored for now. This will happen if someone happens to output more
  25. * than 4 billion log entries, at which point wrap-around will not be
  26. * the biggest problem.
  27. *
  28. */
  29. #include <stdbool.h>
  30. #include <stdarg.h>
  31. #include <stddef.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <assert.h>
  36. #include "esp_log.h"
  37. #include "esp_log_private.h"
  38. #ifndef NDEBUG
  39. // Enable built-in checks in queue.h in debug builds
  40. #define INVARIANTS
  41. // Enable consistency checks and cache statistics in this file.
  42. #define LOG_BUILTIN_CHECKS
  43. #endif
  44. #include "sys/queue.h"
  45. // Number of tags to be cached. Must be 2**n - 1, n >= 2.
  46. #define TAG_CACHE_SIZE 31
  47. typedef struct {
  48. const char *tag;
  49. uint32_t level : 3;
  50. uint32_t generation : 29;
  51. } cached_tag_entry_t;
  52. typedef struct uncached_tag_entry_ {
  53. SLIST_ENTRY(uncached_tag_entry_) entries;
  54. uint8_t level; // esp_log_level_t as uint8_t
  55. char tag[0]; // beginning of a zero-terminated string
  56. } uncached_tag_entry_t;
  57. esp_log_level_t esp_log_default_level = CONFIG_LOG_DEFAULT_LEVEL;
  58. static SLIST_HEAD(log_tags_head, uncached_tag_entry_) s_log_tags = SLIST_HEAD_INITIALIZER(s_log_tags);
  59. static cached_tag_entry_t s_log_cache[TAG_CACHE_SIZE];
  60. static uint32_t s_log_cache_max_generation = 0;
  61. static uint32_t s_log_cache_entry_count = 0;
  62. static vprintf_like_t s_log_print_func = &vprintf;
  63. #ifdef LOG_BUILTIN_CHECKS
  64. static uint32_t s_log_cache_misses = 0;
  65. #endif
  66. static inline bool get_cached_log_level(const char *tag, esp_log_level_t *level);
  67. static inline bool get_uncached_log_level(const char *tag, esp_log_level_t *level);
  68. static inline void add_to_cache(const char *tag, esp_log_level_t level);
  69. static void heap_bubble_down(int index);
  70. static inline void heap_swap(int i, int j);
  71. static inline bool should_output(esp_log_level_t level_for_message, esp_log_level_t level_for_tag);
  72. static inline void clear_log_level_list(void);
  73. vprintf_like_t esp_log_set_vprintf(vprintf_like_t func)
  74. {
  75. esp_log_impl_lock();
  76. vprintf_like_t orig_func = s_log_print_func;
  77. s_log_print_func = func;
  78. esp_log_impl_unlock();
  79. return orig_func;
  80. }
  81. void esp_log_level_set(const char *tag, esp_log_level_t level)
  82. {
  83. esp_log_impl_lock();
  84. // for wildcard tag, remove all linked list items and clear the cache
  85. if (strcmp(tag, "*") == 0) {
  86. esp_log_default_level = level;
  87. clear_log_level_list();
  88. esp_log_impl_unlock();
  89. return;
  90. }
  91. // search for existing tag
  92. uncached_tag_entry_t *it = NULL;
  93. SLIST_FOREACH(it, &s_log_tags, entries) {
  94. if (strcmp(it->tag, tag) == 0) {
  95. // one tag in the linked list matched, update the level
  96. it->level = level;
  97. // quit with it != NULL
  98. break;
  99. }
  100. }
  101. // no existing tag, append new one
  102. if (it == NULL) {
  103. // allocate new linked list entry and append it to the head of the list
  104. size_t tag_len = strlen(tag) + 1;
  105. size_t entry_size = offsetof(uncached_tag_entry_t, tag) + tag_len;
  106. uncached_tag_entry_t *new_entry = (uncached_tag_entry_t *) malloc(entry_size);
  107. if (!new_entry) {
  108. esp_log_impl_unlock();
  109. return;
  110. }
  111. new_entry->level = (uint8_t) level;
  112. memcpy(new_entry->tag, tag, tag_len); // we know the size and strncpy would trigger a compiler warning here
  113. SLIST_INSERT_HEAD(&s_log_tags, new_entry, entries);
  114. }
  115. // search in the cache and update the entry it if exists
  116. for (uint32_t i = 0; i < s_log_cache_entry_count; ++i) {
  117. #ifdef LOG_BUILTIN_CHECKS
  118. assert(i == 0 || s_log_cache[(i - 1) / 2].generation < s_log_cache[i].generation);
  119. #endif
  120. if (strcmp(s_log_cache[i].tag, tag) == 0) {
  121. s_log_cache[i].level = level;
  122. break;
  123. }
  124. }
  125. esp_log_impl_unlock();
  126. }
  127. void clear_log_level_list(void)
  128. {
  129. uncached_tag_entry_t *it;
  130. while ((it = SLIST_FIRST(&s_log_tags)) != NULL) {
  131. SLIST_REMOVE_HEAD(&s_log_tags, entries);
  132. free(it);
  133. }
  134. s_log_cache_entry_count = 0;
  135. s_log_cache_max_generation = 0;
  136. #ifdef LOG_BUILTIN_CHECKS
  137. s_log_cache_misses = 0;
  138. #endif
  139. }
  140. void esp_log_writev(esp_log_level_t level,
  141. const char *tag,
  142. const char *format,
  143. va_list args)
  144. {
  145. if (!esp_log_impl_lock_timeout()) {
  146. return;
  147. }
  148. esp_log_level_t level_for_tag;
  149. // Look for the tag in cache first, then in the linked list of all tags
  150. if (!get_cached_log_level(tag, &level_for_tag)) {
  151. if (!get_uncached_log_level(tag, &level_for_tag)) {
  152. level_for_tag = esp_log_default_level;
  153. }
  154. add_to_cache(tag, level_for_tag);
  155. #ifdef LOG_BUILTIN_CHECKS
  156. ++s_log_cache_misses;
  157. #endif
  158. }
  159. esp_log_impl_unlock();
  160. if (!should_output(level, level_for_tag)) {
  161. return;
  162. }
  163. (*s_log_print_func)(format, args);
  164. }
  165. void esp_log_write(esp_log_level_t level,
  166. const char *tag,
  167. const char *format, ...)
  168. {
  169. va_list list;
  170. va_start(list, format);
  171. esp_log_writev(level, tag, format, list);
  172. va_end(list);
  173. }
  174. static inline bool get_cached_log_level(const char *tag, esp_log_level_t *level)
  175. {
  176. // Look for `tag` in cache
  177. uint32_t i;
  178. for (i = 0; i < s_log_cache_entry_count; ++i) {
  179. #ifdef LOG_BUILTIN_CHECKS
  180. assert(i == 0 || s_log_cache[(i - 1) / 2].generation < s_log_cache[i].generation);
  181. #endif
  182. if (s_log_cache[i].tag == tag) {
  183. break;
  184. }
  185. }
  186. if (i == s_log_cache_entry_count) { // Not found in cache
  187. return false;
  188. }
  189. // Return level from cache
  190. *level = (esp_log_level_t) s_log_cache[i].level;
  191. // If cache has been filled, start taking ordering into account
  192. // (other options are: dynamically resize cache, add "dummy" entries
  193. // to the cache; this option was chosen because code is much simpler,
  194. // and the unfair behavior of cache will show it self at most once, when
  195. // it has just been filled)
  196. if (s_log_cache_entry_count == TAG_CACHE_SIZE) {
  197. // Update item generation
  198. s_log_cache[i].generation = s_log_cache_max_generation++;
  199. // Restore heap ordering
  200. heap_bubble_down(i);
  201. }
  202. return true;
  203. }
  204. static inline void add_to_cache(const char *tag, esp_log_level_t level)
  205. {
  206. uint32_t generation = s_log_cache_max_generation++;
  207. // First consider the case when cache is not filled yet.
  208. // In this case, just add new entry at the end.
  209. // This happens to satisfy binary min-heap ordering.
  210. if (s_log_cache_entry_count < TAG_CACHE_SIZE) {
  211. s_log_cache[s_log_cache_entry_count] = (cached_tag_entry_t) {
  212. .generation = generation,
  213. .level = level,
  214. .tag = tag
  215. };
  216. ++s_log_cache_entry_count;
  217. return;
  218. }
  219. // Cache is full, so we replace the oldest entry (which is at index 0
  220. // because this is a min-heap) with the new one, and do bubble-down
  221. // operation to restore min-heap ordering.
  222. s_log_cache[0] = (cached_tag_entry_t) {
  223. .tag = tag,
  224. .level = level,
  225. .generation = generation
  226. };
  227. heap_bubble_down(0);
  228. }
  229. static inline bool get_uncached_log_level(const char *tag, esp_log_level_t *level)
  230. {
  231. // Walk the linked list of all tags and see if given tag is present in the list.
  232. // This is slow because tags are compared as strings.
  233. uncached_tag_entry_t *it;
  234. SLIST_FOREACH(it, &s_log_tags, entries) {
  235. if (strcmp(tag, it->tag) == 0) {
  236. *level = it->level;
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. static inline bool should_output(esp_log_level_t level_for_message, esp_log_level_t level_for_tag)
  243. {
  244. return level_for_message <= level_for_tag;
  245. }
  246. static void heap_bubble_down(int index)
  247. {
  248. while (index < TAG_CACHE_SIZE / 2) {
  249. int left_index = index * 2 + 1;
  250. int right_index = left_index + 1;
  251. int next = (s_log_cache[left_index].generation < s_log_cache[right_index].generation) ? left_index : right_index;
  252. heap_swap(index, next);
  253. index = next;
  254. }
  255. }
  256. static inline void heap_swap(int i, int j)
  257. {
  258. cached_tag_entry_t tmp = s_log_cache[i];
  259. s_log_cache[i] = s_log_cache[j];
  260. s_log_cache[j] = tmp;
  261. }