log.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "esp_log.h"
  52. //print number of bytes per line for esp_log_buffer_char and esp_log_buffer_hex
  53. #define BYTES_PER_LINE 16
  54. #ifndef BOOTLOADER_BUILD
  55. // Number of tags to be cached. Must be 2**n - 1, n >= 2.
  56. #define TAG_CACHE_SIZE 31
  57. // Maximum time to wait for the mutex in a logging statement.
  58. #define MAX_MUTEX_WAIT_MS 10
  59. #define MAX_MUTEX_WAIT_TICKS ((MAX_MUTEX_WAIT_MS + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS)
  60. // Uncomment this to enable consistency checks and cache statistics in this file.
  61. // #define LOG_BUILTIN_CHECKS
  62. typedef struct {
  63. const char* tag;
  64. uint32_t level : 3;
  65. uint32_t generation : 29;
  66. } cached_tag_entry_t;
  67. typedef struct uncached_tag_entry_{
  68. struct uncached_tag_entry_* next;
  69. uint8_t level; // esp_log_level_t as uint8_t
  70. char tag[0]; // beginning of a zero-terminated string
  71. } uncached_tag_entry_t;
  72. static esp_log_level_t s_log_default_level = ESP_LOG_VERBOSE;
  73. static uncached_tag_entry_t* s_log_tags_head = NULL;
  74. static uncached_tag_entry_t* s_log_tags_tail = NULL;
  75. static cached_tag_entry_t s_log_cache[TAG_CACHE_SIZE];
  76. static uint32_t s_log_cache_max_generation = 0;
  77. static uint32_t s_log_cache_entry_count = 0;
  78. static vprintf_like_t s_log_print_func = &vprintf;
  79. static SemaphoreHandle_t s_log_mutex = NULL;
  80. #ifdef LOG_BUILTIN_CHECKS
  81. static uint32_t s_log_cache_misses = 0;
  82. #endif
  83. static inline bool get_cached_log_level(const char* tag, esp_log_level_t* level);
  84. static inline bool get_uncached_log_level(const char* tag, esp_log_level_t* level);
  85. static inline void add_to_cache(const char* tag, esp_log_level_t level);
  86. static void heap_bubble_down(int index);
  87. static inline void heap_swap(int i, int j);
  88. static inline bool should_output(esp_log_level_t level_for_message, esp_log_level_t level_for_tag);
  89. static inline void clear_log_level_list();
  90. void esp_log_set_vprintf(vprintf_like_t func)
  91. {
  92. s_log_print_func = func;
  93. }
  94. void esp_log_level_set(const char* tag, esp_log_level_t level)
  95. {
  96. if (!s_log_mutex) {
  97. s_log_mutex = xSemaphoreCreateMutex();
  98. }
  99. xSemaphoreTake(s_log_mutex, portMAX_DELAY);
  100. // for wildcard tag, remove all linked list items and clear the cache
  101. if (strcmp(tag, "*") == 0) {
  102. s_log_default_level = level;
  103. clear_log_level_list();
  104. xSemaphoreGive(s_log_mutex);
  105. return;
  106. }
  107. // allocate new linked list entry and append it to the endo of the list
  108. size_t entry_size = offsetof(uncached_tag_entry_t, tag) + strlen(tag) + 1;
  109. uncached_tag_entry_t* new_entry = (uncached_tag_entry_t*) malloc(entry_size);
  110. if (!new_entry) {
  111. xSemaphoreGive(s_log_mutex);
  112. return;
  113. }
  114. new_entry->next = NULL;
  115. new_entry->level = (uint8_t) level;
  116. strcpy(new_entry->tag, tag);
  117. if (s_log_tags_tail) {
  118. s_log_tags_tail->next = new_entry;
  119. }
  120. s_log_tags_tail = new_entry;
  121. if (!s_log_tags_head) {
  122. s_log_tags_head = new_entry;
  123. }
  124. xSemaphoreGive(s_log_mutex);
  125. }
  126. void clear_log_level_list()
  127. {
  128. for (uncached_tag_entry_t* it = s_log_tags_head; it != NULL; ) {
  129. uncached_tag_entry_t* next = it->next;
  130. free(it);
  131. it = next;
  132. }
  133. s_log_tags_tail = NULL;
  134. s_log_tags_head = NULL;
  135. s_log_cache_entry_count = 0;
  136. s_log_cache_max_generation = 0;
  137. #ifdef LOG_BUILTIN_CHECKS
  138. s_log_cache_misses = 0;
  139. #endif
  140. }
  141. void IRAM_ATTR esp_log_write(esp_log_level_t level,
  142. const char* tag,
  143. const char* format, ...)
  144. {
  145. if (!s_log_mutex) {
  146. s_log_mutex = xSemaphoreCreateMutex();
  147. }
  148. if (xSemaphoreTake(s_log_mutex, MAX_MUTEX_WAIT_TICKS) == pdFALSE) {
  149. return;
  150. }
  151. esp_log_level_t level_for_tag;
  152. // Look for the tag in cache first, then in the linked list of all tags
  153. if (!get_cached_log_level(tag, &level_for_tag)) {
  154. if (!get_uncached_log_level(tag, &level_for_tag)) {
  155. level_for_tag = s_log_default_level;
  156. }
  157. add_to_cache(tag, level_for_tag);
  158. #ifdef LOG_BUILTIN_CHECKS
  159. ++s_log_cache_misses;
  160. #endif
  161. }
  162. xSemaphoreGive(s_log_mutex);
  163. if (!should_output(level, level_for_tag)) {
  164. return;
  165. }
  166. va_list list;
  167. va_start(list, format);
  168. (*s_log_print_func)(format, list);
  169. va_end(list);
  170. }
  171. static inline bool get_cached_log_level(const char* tag, esp_log_level_t* level)
  172. {
  173. // Look for `tag` in cache
  174. int i;
  175. for (i = 0; i < s_log_cache_entry_count; ++i) {
  176. #ifdef LOG_BUILTIN_CHECKS
  177. assert(i == 0 || s_log_cache[(i - 1) / 2].generation < s_log_cache[i].generation);
  178. #endif
  179. if (s_log_cache[i].tag == tag) {
  180. break;
  181. }
  182. }
  183. if (i == s_log_cache_entry_count) { // Not found in cache
  184. return false;
  185. }
  186. // Return level from cache
  187. *level = (esp_log_level_t) s_log_cache[i].level;
  188. // If cache has been filled, start taking ordering into account
  189. // (other options are: dynamically resize cache, add "dummy" entries
  190. // to the cache; this option was chosen because code is much simpler,
  191. // and the unfair behavior of cache will show it self at most once, when
  192. // it has just been filled)
  193. if (s_log_cache_entry_count == TAG_CACHE_SIZE) {
  194. // Update item generation
  195. s_log_cache[i].generation = s_log_cache_max_generation++;
  196. // Restore heap ordering
  197. heap_bubble_down(i);
  198. }
  199. return true;
  200. }
  201. static inline void add_to_cache(const char* tag, esp_log_level_t level)
  202. {
  203. uint32_t generation = s_log_cache_max_generation++;
  204. // First consider the case when cache is not filled yet.
  205. // In this case, just add new entry at the end.
  206. // This happens to satisfy binary min-heap ordering.
  207. if (s_log_cache_entry_count < TAG_CACHE_SIZE) {
  208. s_log_cache[s_log_cache_entry_count] = (cached_tag_entry_t) {
  209. .generation = generation,
  210. .level = level,
  211. .tag = tag
  212. };
  213. ++s_log_cache_entry_count;
  214. return;
  215. }
  216. // Cache is full, so we replace the oldest entry (which is at index 0
  217. // because this is a min-heap) with the new one, and do bubble-down
  218. // operation to restore min-heap ordering.
  219. s_log_cache[0] = (cached_tag_entry_t) {
  220. .tag = tag,
  221. .level = level,
  222. .generation = generation
  223. };
  224. heap_bubble_down(0);
  225. }
  226. static inline bool get_uncached_log_level(const char* tag, esp_log_level_t* level)
  227. {
  228. // Walk the linked list of all tags and see if given tag is present in the list.
  229. // This is slow because tags are compared as strings.
  230. for (uncached_tag_entry_t* it = s_log_tags_head; it != NULL; it = it->next) {
  231. if (strcmp(tag, it->tag) == 0) {
  232. *level = it->level;
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. static inline bool should_output(esp_log_level_t level_for_message, esp_log_level_t level_for_tag)
  239. {
  240. return level_for_message <= level_for_tag;
  241. }
  242. static void heap_bubble_down(int index)
  243. {
  244. while (index < TAG_CACHE_SIZE / 2) {
  245. int left_index = index * 2 + 1;
  246. int right_index = left_index + 1;
  247. int next = (s_log_cache[left_index].generation < s_log_cache[right_index].generation) ? left_index : right_index;
  248. heap_swap(index, next);
  249. index = next;
  250. }
  251. }
  252. static inline void heap_swap(int i, int j)
  253. {
  254. cached_tag_entry_t tmp = s_log_cache[i];
  255. s_log_cache[i] = s_log_cache[j];
  256. s_log_cache[j] = tmp;
  257. }
  258. #endif //BOOTLOADER_BUILD
  259. #ifndef BOOTLOADER_BUILD
  260. #define ATTR IRAM_ATTR
  261. #else
  262. #define ATTR
  263. #endif // BOOTLOADER_BUILD
  264. uint32_t ATTR esp_log_early_timestamp()
  265. {
  266. return xthal_get_ccount() / (CPU_CLK_FREQ_ROM / 1000);
  267. }
  268. #ifndef BOOTLOADER_BUILD
  269. uint32_t IRAM_ATTR esp_log_timestamp()
  270. {
  271. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  272. return esp_log_early_timestamp();
  273. }
  274. static uint32_t base = 0;
  275. if (base == 0) {
  276. base = esp_log_early_timestamp();
  277. }
  278. return base + xTaskGetTickCount() * (1000 / configTICK_RATE_HZ);
  279. }
  280. #else
  281. uint32_t esp_log_timestamp() __attribute__((alias("esp_log_early_timestamp")));
  282. #endif //BOOTLOADER_BUILD
  283. void esp_log_buffer_hex(const char *tag, const char *buffer, uint16_t buff_len)
  284. {
  285. char temp_buffer[3*BYTES_PER_LINE + 1]= {0};
  286. int line_len = 0;
  287. for (int i = 0; i < buff_len; i++) {
  288. line_len += sprintf(temp_buffer+line_len, "%02x ", buffer[i]);
  289. if (((i + 1) % BYTES_PER_LINE == 0) || (i == buff_len - 1)) {
  290. ESP_LOGI(tag, "%s", temp_buffer);
  291. line_len = 0;
  292. temp_buffer[0] = 0;
  293. }
  294. }
  295. }
  296. void esp_log_buffer_char(const char *tag, const char *buffer, uint16_t buff_len)
  297. {
  298. char temp_buffer[2*BYTES_PER_LINE + 1] = {0};
  299. int line_len = 0;
  300. for (int i = 0; i < buff_len; i++) {
  301. line_len += sprintf(temp_buffer+line_len, "%c ", buffer[i]);
  302. if (((i + 1) % BYTES_PER_LINE == 0) || (i == buff_len - 1)) {
  303. ESP_LOGI(tag, "%s", temp_buffer);
  304. line_len = 0;
  305. temp_buffer[0] = 0;
  306. }
  307. }
  308. }