print.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. *
  4. * Author: Steven Dake (sdake@mvista.com)
  5. *
  6. * Copyright (c) 2006 Ericsson AB.
  7. * Author: Hans Feldt
  8. * Description: Added support for runtime installed loggers, tags tracing,
  9. * and file & line printing.
  10. *
  11. * All rights reserved.
  12. *
  13. * This software licensed under BSD license, the text of which follows:
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  24. * contributors may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  37. * THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #ifndef PRINT_H_DEFINED
  40. #define PRINT_H_DEFINED
  41. #include <stdarg.h>
  42. #include <syslog.h>
  43. #include "mainconfig.h"
  44. #define LOG_MODE_DEBUG 1
  45. #define LOG_MODE_TIMESTAMP 2
  46. #define LOG_MODE_FILE 4
  47. #define LOG_MODE_SYSLOG 8
  48. #define LOG_MODE_STDERR 16
  49. #define LOG_MODE_FILELINE 32
  50. /*
  51. * Log levels, compliant with syslog and SA Forum Log spec.
  52. */
  53. #define LOG_LEVEL_EMERG LOG_EMERG
  54. #define LOG_LEVEL_ALERT LOG_ALERT
  55. #define LOG_LEVEL_CRIT LOG_CRIT
  56. #define LOG_LEVEL_ERROR LOG_ERR
  57. #define LOG_LEVEL_WARNING LOG_WARNING
  58. #define LOG_LEVEL_SECURITY LOG_WARNING // openais specific
  59. #define LOG_LEVEL_NOTICE LOG_NOTICE
  60. #define LOG_LEVEL_INFO LOG_INFO
  61. #define LOG_LEVEL_DEBUG LOG_DEBUG
  62. /*
  63. ** Log tags, used by trace macros, uses 32 bits => 32 different tags
  64. */
  65. #define TAG_LOG 1<<0
  66. #define TAG_ENTER 1<<1
  67. #define TAG_LEAVE 1<<2
  68. #define TAG_TRACE1 1<<3
  69. #define TAG_TRACE2 1<<4
  70. #define TAG_TRACE3 1<<5
  71. #define TAG_TRACE4 1<<6
  72. #define TAG_TRACE5 1<<7
  73. #define TAG_TRACE6 1<<8
  74. #define TAG_TRACE7 1<<9
  75. #define TAG_TRACE8 1<<10
  76. struct logger {
  77. char ident[6];
  78. int level;
  79. int tags;
  80. int mode;
  81. };
  82. extern struct logger loggers[];
  83. /*
  84. ** The logger_identifier variable holds the numerical identifier for a logger
  85. ** obtained with log_init() and hides it from the logger.
  86. */
  87. static int logger_identifier __attribute__((unused));
  88. extern void internal_log_printf (char *file, int line, int priority, char *format, ...);
  89. extern void internal_log_printf2 (char *file, int line, int priority, char *format, ...);
  90. #define LEVELMASK 0x07 /* 3 bits */
  91. #define LOG_LEVEL(p) ((p) & LEVELMASK)
  92. #define IDMASK (0x3f << 3) /* 6 bits */
  93. #define LOG_ID(p) (((p) & IDMASK) >> 3)
  94. #define _mkpri(lvl, id) (((id) << 3) | (lvl))
  95. static inline int mkpri (int level, int id)
  96. {
  97. return _mkpri (level, id);
  98. }
  99. int log_setup (char **error_string, struct main_config *config);
  100. extern int _log_init (const char *ident);
  101. static inline void log_init (const char *ident)
  102. {
  103. logger_identifier = _log_init (ident);
  104. }
  105. #define log_printf(lvl, format, args...) do { \
  106. if ((lvl) <= loggers[logger_identifier].level) { \
  107. internal_log_printf2 (__FILE__, __LINE__, _mkpri ((lvl), logger_identifier), format, ##args); \
  108. } \
  109. } while(0)
  110. #define dprintf(format, args...) do { \
  111. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  112. internal_log_printf2 (__FILE__, __LINE__, _mkpri (LOG_LEVEL_DEBUG, logger_identifier), format, ##args); \
  113. } \
  114. } while(0)
  115. #define ENTER() do { \
  116. if ((LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) && (TAG_ENTER & loggers[logger_identifier].tags)) { \
  117. internal_log_printf2 (__FILE__, __LINE__, _mkpri (LOG_LEVEL_DEBUG, logger_identifier), ">%s\n", __FUNCTION__); \
  118. } \
  119. } while(0)
  120. #define ENTER_ARGS(format, args...) do { \
  121. if ((LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) && (TAG_ENTER & loggers[logger_identifier].tags)) { \
  122. internal_log_printf2 (__FILE__, __LINE__, _mkpri (LOG_LEVEL_DEBUG, logger_identifier), ">%s: " format, __FUNCTION__, ##args); \
  123. } \
  124. } while(0)
  125. #define LEAVE() do { \
  126. if ((LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) && (TAG_LEAVE & loggers[logger_identifier].tags)) { \
  127. internal_log_printf2 (__FILE__, __LINE__, _mkpri (LOG_LEVEL_DEBUG, logger_identifier), "<%s\n", __FUNCTION__); \
  128. } \
  129. } while(0)
  130. #define TRACE8(format, args...) do { \
  131. if ((LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) && (TAG_TRACE8 & loggers[logger_identifier].tags)) { \
  132. internal_log_printf2 (__FILE__, __LINE__, _mkpri (LOG_LEVEL_DEBUG, logger_identifier), format, ##args); \
  133. } \
  134. } while(0)
  135. #endif /* PRINT_H_DEFINED */