print.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #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, ...);
  90. extern void internal_log_printf2 (char *file, int line, int level, int id, char *format, ...);
  91. extern void trace (char *file, int line, int tag, int id, char *format, ...);
  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. int log_setup (char **error_string, struct main_config *config);
  103. extern int _log_init (const char *ident);
  104. static inline void log_init (const char *ident)
  105. {
  106. logger_identifier = _log_init (ident);
  107. }
  108. #define log_printf(lvl, format, args...) do { \
  109. if ((lvl) <= loggers[logger_identifier].level) { \
  110. internal_log_printf2 (__FILE__, __LINE__, lvl, logger_identifier, format, ##args); \
  111. } \
  112. } while(0)
  113. #define dprintf(format, args...) do { \
  114. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  115. internal_log_printf2 (__FILE__, __LINE__, LOG_LEVEL_DEBUG, logger_identifier, format, ##args); \
  116. } \
  117. } while(0)
  118. #define ENTER_VOID() do { \
  119. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  120. trace (__FILE__, __LINE__, TAG_ENTER, logger_identifier, ">%s\n", __FUNCTION__); \
  121. } \
  122. } while(0)
  123. #define ENTER(format, args...) do { \
  124. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  125. trace (__FILE__, __LINE__, TAG_ENTER, logger_identifier, ">%s: " format, __FUNCTION__, ##args); \
  126. } \
  127. } while(0)
  128. #define LEAVE_VOID() do { \
  129. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  130. trace (__FILE__, __LINE__, TAG_LEAVE, logger_identifier, "<%s\n", __FUNCTION__); \
  131. } \
  132. } while(0)
  133. #define LEAVE(format, args...) do { \
  134. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  135. trace (__FILE__, __LINE__, TAG_LEAVE, logger_identifier, "<%s: " format, __FUNCTION__, ##args); \
  136. } \
  137. } while(0)
  138. #define TRACE1(format, args...) do { \
  139. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  140. trace (__FILE__, __LINE__, TAG_TRACE1, logger_identifier, format, ##args); \
  141. } \
  142. } while(0)
  143. #define TRACE2(format, args...) do { \
  144. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  145. trace (__FILE__, __LINE__, TAG_TRACE2, logger_identifier, format, ##args); \
  146. } \
  147. } while(0)
  148. #define TRACE3(format, args...) do { \
  149. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  150. trace (__FILE__, __LINE__, TAG_TRACE3, logger_identifier, format, ##args); \
  151. } \
  152. } while(0)
  153. #define TRACE4(format, args...) do { \
  154. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  155. trace (__FILE__, __LINE__, TAG_TRACE4, logger_identifier, format, ##args); \
  156. } \
  157. } while(0)
  158. #define TRACE5(format, args...) do { \
  159. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  160. trace (__FILE__, __LINE__, TAG_TRACE5, logger_identifier, format, ##args); \
  161. } \
  162. } while(0)
  163. #define TRACE6(format, args...) do { \
  164. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  165. trace (__FILE__, __LINE__, TAG_TRACE6, logger_identifier, format, ##args); \
  166. } \
  167. } while(0)
  168. #define TRACE7(format, args...) do { \
  169. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  170. trace (__FILE__, __LINE__, TAG_TRACE7, logger_identifier, format, ##args); \
  171. } \
  172. } while(0)
  173. #define TRACE8(format, args...) do { \
  174. if (LOG_LEVEL_DEBUG <= loggers[logger_identifier].level) { \
  175. trace (__FILE__, __LINE__, TAG_TRACE8, logger_identifier, format, ##args); \
  176. } \
  177. } while(0)
  178. #endif /* PRINT_H_DEFINED */