log.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifndef __ELL_LOG_H
  23. #define __ELL_LOG_H
  24. #include <stdarg.h>
  25. #include <stdbool.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #define L_LOG_ERR 3
  30. #define L_LOG_WARNING 4
  31. #define L_LOG_INFO 6
  32. #define L_LOG_DEBUG 7
  33. typedef void (*l_log_func_t) (int priority, const char *file, const char *line,
  34. const char *func, const char *format, va_list ap);
  35. void l_log_set_ident(const char *ident);
  36. void l_log_set_handler(l_log_func_t function);
  37. void l_log_set_null(void);
  38. void l_log_set_stderr(void);
  39. void l_log_set_syslog(void);
  40. void l_log_set_journal(void);
  41. void l_log_with_location(int priority, const char *file, const char *line,
  42. const char *func, const char *format, ...)
  43. __attribute__((format(printf, 5, 6)));
  44. #define l_log(priority, format, ...) l_log_with_location(priority, \
  45. __FILE__, L_STRINGIFY(__LINE__), \
  46. __func__, format "\n", ##__VA_ARGS__)
  47. struct l_debug_desc {
  48. const char *file;
  49. const char *func;
  50. #define L_DEBUG_FLAG_DEFAULT (0)
  51. #define L_DEBUG_FLAG_PRINT (1 << 0)
  52. unsigned int flags;
  53. } __attribute__((aligned(8)));
  54. #define L_DEBUG_SYMBOL(symbol, format, ...) do { \
  55. static struct l_debug_desc symbol \
  56. __attribute__((used, section("__ell_debug"), aligned(8))) = { \
  57. .file = __FILE__, .func = __func__, \
  58. .flags = L_DEBUG_FLAG_DEFAULT, \
  59. }; \
  60. if (symbol.flags & L_DEBUG_FLAG_PRINT) \
  61. l_log(L_LOG_DEBUG, "%s:%s() " format, __FILE__, \
  62. __func__ , ##__VA_ARGS__); \
  63. } while (0)
  64. void l_debug_enable_full(const char *pattern,
  65. struct l_debug_desc *start,
  66. struct l_debug_desc *end);
  67. void l_debug_add_section(struct l_debug_desc *start,
  68. struct l_debug_desc *end);
  69. #define l_debug_enable(pattern) do { \
  70. _Pragma("GCC diagnostic push") \
  71. _Pragma("GCC diagnostic ignored \"-Wredundant-decls\"") \
  72. extern struct l_debug_desc __start___ell_debug[]; \
  73. extern struct l_debug_desc __stop___ell_debug[]; \
  74. l_debug_enable_full(pattern, __start___ell_debug, __stop___ell_debug); \
  75. _Pragma("GCC diagnostic pop") \
  76. } while (0)
  77. void l_debug_disable(void);
  78. #define l_error(format, ...) l_log(L_LOG_ERR, format, ##__VA_ARGS__)
  79. #define l_warn(format, ...) l_log(L_LOG_WARNING, format, ##__VA_ARGS__)
  80. #define l_info(format, ...) l_log(L_LOG_INFO, format, ##__VA_ARGS__)
  81. #define l_debug(format, ...) L_DEBUG_SYMBOL(__debug_desc, format, ##__VA_ARGS__)
  82. #ifdef __cplusplus
  83. }
  84. #endif
  85. #endif /* __ELL_LOG_H */