BLog.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @file BLog.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * A global object for logging.
  25. */
  26. #ifndef BADVPN_BLOG_H
  27. #define BADVPN_BLOG_H
  28. #include <stdarg.h>
  29. #include <string.h>
  30. #include <misc/debug.h>
  31. // auto-generated channel numbers and number of channels
  32. #include <generated/blog_channels_defines.h>
  33. #define BLOG_ERROR 1
  34. #define BLOG_WARNING 2
  35. #define BLOG_NOTICE 3
  36. #define BLOG_INFO 4
  37. #define BLOG_DEBUG 5
  38. #define BLog(...) BLog_LogToChannel(BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  39. typedef void (*_BLog_log_func) (int channel, int level, const char *msg);
  40. typedef void (*_BLog_free_func) (void);
  41. struct _BLog_channel {
  42. const char *name;
  43. int loglevel;
  44. };
  45. struct _BLog_global {
  46. #ifndef NDEBUG
  47. int initialized; // initialized statically
  48. #endif
  49. struct _BLog_channel channels[BLOG_NUM_CHANNELS];
  50. _BLog_log_func log_func;
  51. _BLog_free_func free_func;
  52. char logbuf[2048];
  53. int logbuf_pos;
  54. };
  55. extern struct _BLog_channel blog_channel_list[];
  56. extern struct _BLog_global blog_global;
  57. typedef void (*BLog_logfunc) (void *);
  58. static int BLogGlobal_GetChannelByName (const char *channel_name);
  59. static void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func);
  60. static void BLog_Free (void);
  61. static void BLog_SetChannelLoglevel (int channel, int loglevel);
  62. static int BLog_WouldLog (int channel, int level);
  63. static void BLog_AppendVarArg (const char *fmt, va_list vl);
  64. static void BLog_Append (const char *fmt, ...);
  65. static void BLog_Finish (int channel, int level);
  66. static void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl);
  67. static void BLog_LogToChannel (int channel, int level, const char *fmt, ...);
  68. static void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, va_list vl);
  69. static void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, ...);
  70. void BLog_InitStdout (void);
  71. int BLogGlobal_GetChannelByName (const char *channel_name)
  72. {
  73. int i;
  74. for (i = 0; i < BLOG_NUM_CHANNELS; i++) {
  75. if (!strcmp(blog_channel_list[i].name, channel_name)) {
  76. return i;
  77. }
  78. }
  79. return -1;
  80. }
  81. void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func)
  82. {
  83. ASSERT(!blog_global.initialized)
  84. #ifndef NDEBUG
  85. blog_global.initialized = 1;
  86. #endif
  87. // initialize channels
  88. memcpy(blog_global.channels, blog_channel_list, BLOG_NUM_CHANNELS * sizeof(struct _BLog_channel));
  89. blog_global.log_func = log_func;
  90. blog_global.free_func = free_func;
  91. blog_global.logbuf_pos = 0;
  92. blog_global.logbuf[0] = '\0';
  93. }
  94. void BLog_Free (void)
  95. {
  96. ASSERT(blog_global.initialized)
  97. #ifndef NDEBUG
  98. blog_global.initialized = 0;
  99. #endif
  100. blog_global.free_func();
  101. }
  102. void BLog_SetChannelLoglevel (int channel, int loglevel)
  103. {
  104. ASSERT(blog_global.initialized)
  105. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  106. ASSERT(loglevel >= 0 && loglevel <= BLOG_DEBUG)
  107. blog_global.channels[channel].loglevel = loglevel;
  108. }
  109. int BLog_WouldLog (int channel, int level)
  110. {
  111. ASSERT(blog_global.initialized)
  112. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  113. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  114. return (level <= blog_global.channels[channel].loglevel);
  115. }
  116. void BLog_AppendVarArg (const char *fmt, va_list vl)
  117. {
  118. ASSERT(blog_global.initialized)
  119. ASSERT(blog_global.logbuf_pos >= 0 && blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  120. int w = vsnprintf(blog_global.logbuf + blog_global.logbuf_pos, sizeof(blog_global.logbuf) - blog_global.logbuf_pos, fmt, vl);
  121. if (w >= sizeof(blog_global.logbuf) - blog_global.logbuf_pos) {
  122. blog_global.logbuf_pos = sizeof(blog_global.logbuf) - 1;
  123. } else {
  124. blog_global.logbuf_pos += w;
  125. }
  126. }
  127. void BLog_Append (const char *fmt, ...)
  128. {
  129. ASSERT(blog_global.initialized)
  130. va_list vl;
  131. va_start(vl, fmt);
  132. BLog_AppendVarArg(fmt, vl);
  133. va_end(vl);
  134. }
  135. void BLog_Finish (int channel, int level)
  136. {
  137. ASSERT(blog_global.initialized)
  138. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  139. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  140. ASSERT(BLog_WouldLog(channel, level))
  141. ASSERT(blog_global.logbuf_pos >= 0 && blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  142. ASSERT(blog_global.logbuf[blog_global.logbuf_pos] == '\0')
  143. blog_global.log_func(channel, level, blog_global.logbuf);
  144. blog_global.logbuf_pos = 0;
  145. blog_global.logbuf[0] = '\0';
  146. }
  147. void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl)
  148. {
  149. ASSERT(blog_global.initialized)
  150. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  151. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  152. if (!BLog_WouldLog(channel, level)) {
  153. blog_global.logbuf_pos = 0;
  154. blog_global.logbuf[0] = '\0';
  155. return;
  156. }
  157. BLog_AppendVarArg(fmt, vl);
  158. BLog_Finish(channel, level);
  159. }
  160. void BLog_LogToChannel (int channel, int level, const char *fmt, ...)
  161. {
  162. ASSERT(blog_global.initialized)
  163. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  164. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  165. if (!BLog_WouldLog(channel, level)) {
  166. blog_global.logbuf_pos = 0;
  167. blog_global.logbuf[0] = '\0';
  168. return;
  169. }
  170. va_list vl;
  171. va_start(vl, fmt);
  172. BLog_AppendVarArg(fmt, vl);
  173. BLog_Finish(channel, level);
  174. va_end(vl);
  175. }
  176. void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, va_list vl)
  177. {
  178. ASSERT(blog_global.initialized)
  179. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  180. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  181. if (!BLog_WouldLog(channel, level)) {
  182. blog_global.logbuf_pos = 0;
  183. blog_global.logbuf[0] = '\0';
  184. return;
  185. }
  186. func(arg);
  187. BLog_AppendVarArg(fmt, vl);
  188. BLog_Finish(channel, level);
  189. }
  190. void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, ...)
  191. {
  192. ASSERT(blog_global.initialized)
  193. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  194. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  195. if (!BLog_WouldLog(channel, level)) {
  196. blog_global.logbuf_pos = 0;
  197. blog_global.logbuf[0] = '\0';
  198. return;
  199. }
  200. va_list vl;
  201. va_start(vl, fmt);
  202. func(arg);
  203. BLog_AppendVarArg(fmt, vl);
  204. BLog_Finish(channel, level);
  205. va_end(vl);
  206. }
  207. #endif