log.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2017-2019 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * System logging.
  19. */
  20. #ifndef KERN_LOG_H
  21. #define KERN_LOG_H
  22. #include <stdarg.h>
  23. #include <kern/init.h>
  24. #include <kern/stream.h>
  25. enum
  26. {
  27. LOG_EMERG,
  28. LOG_ALERT,
  29. LOG_CRIT,
  30. LOG_ERR,
  31. LOG_WARNING,
  32. LOG_NOTICE,
  33. LOG_INFO,
  34. LOG_DEBUG,
  35. LOG_NR_LEVELS,
  36. };
  37. /*
  38. * Generate a message and send it to the log thread.
  39. *
  40. * The arguments and return value are similar to printf(), with
  41. * these exceptions :
  42. * - a level is associated to each log message
  43. * - processing stops at the first terminating null byte or newline
  44. * character, whichever occurs first
  45. *
  46. * This function may safely be called in interrupt context.
  47. */
  48. int log_msg (unsigned int level, const char *format, ...)
  49. __attribute__ ((format (printf, 2, 3)));
  50. int log_vmsg (unsigned int level, const char *format, va_list ap)
  51. __attribute__ ((format (printf, 2, 0)));
  52. // Convenience wrappers.
  53. #define log_emerg(format, ...) \
  54. log_msg (LOG_EMERG, (format), ##__VA_ARGS__)
  55. #define log_alert(format, ...) \
  56. log_msg (LOG_ALERT, (format), ##__VA_ARGS__)
  57. #define log_crit(format, ...) \
  58. log_msg (LOG_CRIT, (format), ##__VA_ARGS__)
  59. #define log_err(format, ...) \
  60. log_msg (LOG_ERR, (format), ##__VA_ARGS__)
  61. #define log_warning(format, ...) \
  62. log_msg (LOG_WARNING, (format), ##__VA_ARGS__)
  63. #define log_notice(format, ...) \
  64. log_msg (LOG_NOTICE, (format), ##__VA_ARGS__)
  65. #define log_info(format, ...) \
  66. log_msg (LOG_INFO, (format), ##__VA_ARGS__)
  67. #define log_debug(format, ...) \
  68. log_msg (LOG_DEBUG, (format), ##__VA_ARGS__)
  69. /*
  70. * The bulletin returned by this function is used to notify the initial log
  71. * dump so that console output is well ordered.
  72. */
  73. struct bulletin* log_get_bulletin (void);
  74. // Get the logger stream for a particular level.
  75. struct stream* log_stream (unsigned int level);
  76. // Accesors for the above.
  77. #define log_stream_emerg() log_stream (LOG_EMERG)
  78. #define log_stream_alert() log_stream (LOG_ALERT)
  79. #define log_stream_crit() log_stream (LOG_CRIT)
  80. #define log_stream_err() log_stream (LOG_ERR)
  81. #define log_stream_warning() log_stream (LOG_WARNING)
  82. #define log_stream_notice() log_stream (LOG_NOTICE)
  83. #define log_stream_info() log_stream (LOG_INFO)
  84. #define log_stream_debug() log_stream (LOG_DEBUG)
  85. /*
  86. * This init operation provides :
  87. * - message logging
  88. *
  89. * The log thread isn't yet started and messages are merely stored in an
  90. * internal buffer.
  91. */
  92. INIT_OP_DECLARE (log_setup);
  93. #endif