log.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Logging support
  3. *
  4. * Copyright (c) 2017 Google, Inc
  5. * Written by Simon Glass <sjg@chromium.org>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <log.h>
  11. #include <malloc.h>
  12. #include <dm/uclass.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static const char *log_cat_name[LOGC_COUNT - LOGC_NONE] = {
  15. "none",
  16. "arch",
  17. "board",
  18. "core",
  19. "driver-model",
  20. "device-tree",
  21. "efi",
  22. };
  23. static const char *log_level_name[LOGL_COUNT] = {
  24. "EMERG",
  25. "ALERT",
  26. "CRIT",
  27. "ERR",
  28. "WARNING",
  29. "NOTICE",
  30. "INFO",
  31. "DEBUG",
  32. "CONTENT",
  33. "IO",
  34. };
  35. const char *log_get_cat_name(enum log_category_t cat)
  36. {
  37. if (cat > LOGC_COUNT)
  38. return "invalid";
  39. if (cat >= LOGC_NONE)
  40. return log_cat_name[cat - LOGC_NONE];
  41. return uclass_get_name((enum uclass_id)cat);
  42. }
  43. enum log_category_t log_get_cat_by_name(const char *name)
  44. {
  45. enum uclass_id id;
  46. int i;
  47. for (i = LOGC_NONE; i < LOGC_COUNT; i++)
  48. if (!strcmp(name, log_cat_name[i - LOGC_NONE]))
  49. return i;
  50. id = uclass_get_by_name(name);
  51. if (id != UCLASS_INVALID)
  52. return (enum log_category_t)id;
  53. return LOGC_NONE;
  54. }
  55. const char *log_get_level_name(enum log_level_t level)
  56. {
  57. if (level >= LOGL_COUNT)
  58. return "INVALID";
  59. return log_level_name[level];
  60. }
  61. enum log_level_t log_get_level_by_name(const char *name)
  62. {
  63. int i;
  64. for (i = 0; i < LOGL_COUNT; i++) {
  65. if (!strcasecmp(log_level_name[i], name))
  66. return i;
  67. }
  68. return LOGL_NONE;
  69. }
  70. static struct log_device *log_device_find_by_name(const char *drv_name)
  71. {
  72. struct log_device *ldev;
  73. list_for_each_entry(ldev, &gd->log_head, sibling_node) {
  74. if (!strcmp(drv_name, ldev->drv->name))
  75. return ldev;
  76. }
  77. return NULL;
  78. }
  79. /**
  80. * log_has_cat() - check if a log category exists within a list
  81. *
  82. * @cat_list: List of categories to check, at most LOGF_MAX_CATEGORIES entries
  83. * long, terminated by LC_END if fewer
  84. * @cat: Category to search for
  85. * @return true if @cat is in @cat_list, else false
  86. */
  87. static bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
  88. {
  89. int i;
  90. for (i = 0; i < LOGF_MAX_CATEGORIES && cat_list[i] != LOGC_END; i++) {
  91. if (cat_list[i] == cat)
  92. return true;
  93. }
  94. return false;
  95. }
  96. /**
  97. * log_has_file() - check if a file is with a list
  98. *
  99. * @file_list: List of files to check, separated by comma
  100. * @file: File to check for. This string is matched against the end of each
  101. * file in the list, i.e. ignoring any preceding path. The list is
  102. * intended to consist of relative pathnames, e.g. common/main.c,cmd/log.c
  103. * @return true if @file is in @file_list, else false
  104. */
  105. static bool log_has_file(const char *file_list, const char *file)
  106. {
  107. int file_len = strlen(file);
  108. const char *s, *p;
  109. int substr_len;
  110. for (s = file_list; *s; s = p + (*p != '\0')) {
  111. p = strchrnul(s, ',');
  112. substr_len = p - s;
  113. if (file_len >= substr_len &&
  114. !strncmp(file + file_len - substr_len, s, substr_len))
  115. return true;
  116. }
  117. return false;
  118. }
  119. /**
  120. * log_passes_filters() - check if a log record passes the filters for a device
  121. *
  122. * @ldev: Log device to check
  123. * @rec: Log record to check
  124. * @return true if @rec is not blocked by the filters in @ldev, false if it is
  125. */
  126. static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec)
  127. {
  128. struct log_filter *filt;
  129. /* If there are no filters, filter on the default log level */
  130. if (list_empty(&ldev->filter_head)) {
  131. if (rec->level > gd->default_log_level)
  132. return false;
  133. return true;
  134. }
  135. list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
  136. if (rec->level > filt->max_level)
  137. continue;
  138. if ((filt->flags & LOGFF_HAS_CAT) &&
  139. !log_has_cat(filt->cat_list, rec->cat))
  140. continue;
  141. if (filt->file_list &&
  142. !log_has_file(filt->file_list, rec->file))
  143. continue;
  144. return true;
  145. }
  146. return false;
  147. }
  148. /**
  149. * log_dispatch() - Send a log record to all log devices for processing
  150. *
  151. * The log record is sent to each log device in turn, skipping those which have
  152. * filters which block the record
  153. *
  154. * @rec: Log record to dispatch
  155. * @return 0 (meaning success)
  156. */
  157. static int log_dispatch(struct log_rec *rec)
  158. {
  159. struct log_device *ldev;
  160. list_for_each_entry(ldev, &gd->log_head, sibling_node) {
  161. if (log_passes_filters(ldev, rec))
  162. ldev->drv->emit(ldev, rec);
  163. }
  164. return 0;
  165. }
  166. int _log(enum log_category_t cat, enum log_level_t level, const char *file,
  167. int line, const char *func, const char *fmt, ...)
  168. {
  169. char buf[CONFIG_SYS_CBSIZE];
  170. struct log_rec rec;
  171. va_list args;
  172. rec.cat = cat;
  173. rec.level = level;
  174. rec.file = file;
  175. rec.line = line;
  176. rec.func = func;
  177. va_start(args, fmt);
  178. vsnprintf(buf, sizeof(buf), fmt, args);
  179. va_end(args);
  180. rec.msg = buf;
  181. if (!gd || !(gd->flags & GD_FLG_LOG_READY)) {
  182. if (gd)
  183. gd->log_drop_count++;
  184. return -ENOSYS;
  185. }
  186. log_dispatch(&rec);
  187. return 0;
  188. }
  189. int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
  190. enum log_level_t max_level, const char *file_list)
  191. {
  192. struct log_filter *filt;
  193. struct log_device *ldev;
  194. int i;
  195. ldev = log_device_find_by_name(drv_name);
  196. if (!ldev)
  197. return -ENOENT;
  198. filt = (struct log_filter *)calloc(1, sizeof(*filt));
  199. if (!filt)
  200. return -ENOMEM;
  201. if (cat_list) {
  202. filt->flags |= LOGFF_HAS_CAT;
  203. for (i = 0; ; i++) {
  204. if (i == ARRAY_SIZE(filt->cat_list))
  205. return -ENOSPC;
  206. filt->cat_list[i] = cat_list[i];
  207. if (cat_list[i] == LOGC_END)
  208. break;
  209. }
  210. }
  211. filt->max_level = max_level;
  212. if (file_list) {
  213. filt->file_list = strdup(file_list);
  214. if (!filt->file_list)
  215. goto nomem;
  216. }
  217. filt->filter_num = ldev->next_filter_num++;
  218. list_add_tail(&filt->sibling_node, &ldev->filter_head);
  219. return filt->filter_num;
  220. nomem:
  221. free(filt);
  222. return -ENOMEM;
  223. }
  224. int log_remove_filter(const char *drv_name, int filter_num)
  225. {
  226. struct log_filter *filt;
  227. struct log_device *ldev;
  228. ldev = log_device_find_by_name(drv_name);
  229. if (!ldev)
  230. return -ENOENT;
  231. list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
  232. if (filt->filter_num == filter_num) {
  233. list_del(&filt->sibling_node);
  234. free(filt);
  235. return 0;
  236. }
  237. }
  238. return -ENOENT;
  239. }
  240. int log_init(void)
  241. {
  242. struct log_driver *drv = ll_entry_start(struct log_driver, log_driver);
  243. const int count = ll_entry_count(struct log_driver, log_driver);
  244. struct log_driver *end = drv + count;
  245. /*
  246. * We cannot add runtime data to the driver since it is likely stored
  247. * in rodata. Instead, set up a 'device' corresponding to each driver.
  248. * We only support having a single device.
  249. */
  250. INIT_LIST_HEAD((struct list_head *)&gd->log_head);
  251. while (drv < end) {
  252. struct log_device *ldev;
  253. ldev = calloc(1, sizeof(*ldev));
  254. if (!ldev) {
  255. debug("%s: Cannot allocate memory\n", __func__);
  256. return -ENOMEM;
  257. }
  258. INIT_LIST_HEAD(&ldev->filter_head);
  259. ldev->drv = drv;
  260. list_add_tail(&ldev->sibling_node,
  261. (struct list_head *)&gd->log_head);
  262. drv++;
  263. }
  264. gd->flags |= GD_FLG_LOG_READY;
  265. gd->default_log_level = LOGL_INFO;
  266. gd->log_fmt = LOGF_DEFAULT;
  267. return 0;
  268. }