print.h 7.1 KB

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