log.c 9.5 KB

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