4
0

print.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #define LOG_MODE_DEBUG 1
  44. #define LOG_MODE_TIMESTAMP 2
  45. #define LOG_MODE_FILE 4
  46. #define LOG_MODE_SYSLOG 8
  47. #define LOG_MODE_STDERR 16
  48. #define LOG_MODE_FILELINE 32
  49. #define LOG_MODE_BUFFER 64
  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. unsigned int level;
  79. unsigned int tags;
  80. unsigned 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 level, int id, char *format, ...);
  90. extern void trace (char *file, int line, int tag, int id, char *format, ...);
  91. extern void log_flush(void);
  92. #define LEVELMASK 0x07 /* 3 bits */
  93. #define LOG_LEVEL(p) ((p) & LEVELMASK)
  94. #define IDMASK (0x3f << 3) /* 6 bits */
  95. #define LOG_ID(p) (((p) & IDMASK) >> 3)
  96. #define _mkpri(lvl, id) (((id) << 3) | (lvl))
  97. static inline int mkpri (int level, int id)
  98. {
  99. return _mkpri (level, id);
  100. }
  101. #ifndef main_config
  102. struct main_config;
  103. #endif
  104. extern int log_setup (char **error_string, struct main_config *config);
  105. extern int _log_init (const char *ident);
  106. static inline void log_init (const char *ident)
  107. {
  108. logger_identifier = _log_init (ident);
  109. }
  110. #define log_printf(lvl, format, args...) do { \
  111. if ((lvl) <= loggers[logger_identifier].level) { \
  112. internal_log_printf2 (__FILE__, __LINE__, lvl, logger_identifier, format, ##args); \
  113. } \
  114. } while(0)
  115. #define dprintf(format, args...) do { \
  116. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  117. internal_log_printf2 (__FILE__, __LINE__, LOG_LEVEL_DEBUG, logger_identifier, format, ##args); \
  118. } \
  119. } while(0)
  120. #define ENTER_VOID() do { \
  121. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  122. trace (__FILE__, __LINE__, TAG_ENTER, logger_identifier, ">%s\n", __FUNCTION__); \
  123. } \
  124. } while(0)
  125. #define ENTER(format, args...) do { \
  126. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  127. trace (__FILE__, __LINE__, TAG_ENTER, logger_identifier, ">%s: " format, __FUNCTION__, ##args); \
  128. } \
  129. } while(0)
  130. #define LEAVE_VOID() do { \
  131. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  132. trace (__FILE__, __LINE__, TAG_LEAVE, logger_identifier, "<%s\n", __FUNCTION__); \
  133. } \
  134. } while(0)
  135. #define LEAVE(format, args...) do { \
  136. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  137. trace (__FILE__, __LINE__, TAG_LEAVE, logger_identifier, "<%s: " format, __FUNCTION__, ##args); \
  138. } \
  139. } while(0)
  140. #define TRACE1(format, args...) do { \
  141. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  142. trace (__FILE__, __LINE__, TAG_TRACE1, logger_identifier, format, ##args); \
  143. } \
  144. } while(0)
  145. #define TRACE2(format, args...) do { \
  146. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  147. trace (__FILE__, __LINE__, TAG_TRACE2, logger_identifier, format, ##args); \
  148. } \
  149. } while(0)
  150. #define TRACE3(format, args...) do { \
  151. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  152. trace (__FILE__, __LINE__, TAG_TRACE3, logger_identifier, format, ##args); \
  153. } \
  154. } while(0)
  155. #define TRACE4(format, args...) do { \
  156. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  157. trace (__FILE__, __LINE__, TAG_TRACE4, logger_identifier, format, ##args); \
  158. } \
  159. } while(0)
  160. #define TRACE5(format, args...) do { \
  161. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  162. trace (__FILE__, __LINE__, TAG_TRACE5, logger_identifier, format, ##args); \
  163. } \
  164. } while(0)
  165. #define TRACE6(format, args...) do { \
  166. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  167. trace (__FILE__, __LINE__, TAG_TRACE6, logger_identifier, format, ##args); \
  168. } \
  169. } while(0)
  170. #define TRACE7(format, args...) do { \
  171. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  172. trace (__FILE__, __LINE__, TAG_TRACE7, logger_identifier, format, ##args); \
  173. } \
  174. } while(0)
  175. #define TRACE8(format, args...) do { \
  176. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  177. trace (__FILE__, __LINE__, TAG_TRACE8, logger_identifier, format, ##args); \
  178. } \
  179. } while(0)
  180. #endif /* PRINT_H_DEFINED */