BLog.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * @file BLog.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * A global object for logging.
  32. */
  33. #ifndef BADVPN_BLOG_H
  34. #define BADVPN_BLOG_H
  35. #include <stdarg.h>
  36. #include <string.h>
  37. #include <misc/debug.h>
  38. // auto-generated channel numbers and number of channels
  39. #include <generated/blog_channels_defines.h>
  40. // keep in sync with level names in BLog.c!
  41. #define BLOG_ERROR 1
  42. #define BLOG_WARNING 2
  43. #define BLOG_NOTICE 3
  44. #define BLOG_INFO 4
  45. #define BLOG_DEBUG 5
  46. #define BLog(...) BLog_LogToChannel(BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. typedef void (*_BLog_log_func) (int channel, int level, const char *msg);
  48. typedef void (*_BLog_free_func) (void);
  49. struct _BLog_channel {
  50. const char *name;
  51. int loglevel;
  52. };
  53. struct _BLog_global {
  54. #ifndef NDEBUG
  55. int initialized; // initialized statically
  56. #endif
  57. struct _BLog_channel channels[BLOG_NUM_CHANNELS];
  58. _BLog_log_func log_func;
  59. _BLog_free_func free_func;
  60. char logbuf[2048];
  61. int logbuf_pos;
  62. };
  63. extern struct _BLog_channel blog_channel_list[];
  64. extern struct _BLog_global blog_global;
  65. typedef void (*BLog_logfunc) (void *);
  66. static int BLogGlobal_GetChannelByName (const char *channel_name);
  67. static void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func);
  68. static void BLog_Free (void);
  69. static void BLog_SetChannelLoglevel (int channel, int loglevel);
  70. static int BLog_WouldLog (int channel, int level);
  71. static void BLog_AppendVarArg (const char *fmt, va_list vl);
  72. static void BLog_Append (const char *fmt, ...);
  73. static void BLog_Finish (int channel, int level);
  74. static void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl);
  75. static void BLog_LogToChannel (int channel, int level, const char *fmt, ...);
  76. static void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, va_list vl);
  77. static void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, ...);
  78. void BLog_InitStdout (void);
  79. void BLog_InitStderr (void);
  80. int BLogGlobal_GetChannelByName (const char *channel_name)
  81. {
  82. int i;
  83. for (i = 0; i < BLOG_NUM_CHANNELS; i++) {
  84. if (!strcmp(blog_channel_list[i].name, channel_name)) {
  85. return i;
  86. }
  87. }
  88. return -1;
  89. }
  90. void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func)
  91. {
  92. ASSERT(!blog_global.initialized)
  93. #ifndef NDEBUG
  94. blog_global.initialized = 1;
  95. #endif
  96. // initialize channels
  97. memcpy(blog_global.channels, blog_channel_list, BLOG_NUM_CHANNELS * sizeof(struct _BLog_channel));
  98. blog_global.log_func = log_func;
  99. blog_global.free_func = free_func;
  100. blog_global.logbuf_pos = 0;
  101. blog_global.logbuf[0] = '\0';
  102. }
  103. void BLog_Free (void)
  104. {
  105. ASSERT(blog_global.initialized)
  106. #ifndef NDEBUG
  107. blog_global.initialized = 0;
  108. #endif
  109. blog_global.free_func();
  110. }
  111. void BLog_SetChannelLoglevel (int channel, int loglevel)
  112. {
  113. ASSERT(blog_global.initialized)
  114. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  115. ASSERT(loglevel >= 0 && loglevel <= BLOG_DEBUG)
  116. blog_global.channels[channel].loglevel = loglevel;
  117. }
  118. int BLog_WouldLog (int channel, int level)
  119. {
  120. ASSERT(blog_global.initialized)
  121. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  122. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  123. return (level <= blog_global.channels[channel].loglevel);
  124. }
  125. void BLog_AppendVarArg (const char *fmt, va_list vl)
  126. {
  127. ASSERT(blog_global.initialized)
  128. ASSERT(blog_global.logbuf_pos >= 0 && blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  129. int w = vsnprintf(blog_global.logbuf + blog_global.logbuf_pos, sizeof(blog_global.logbuf) - blog_global.logbuf_pos, fmt, vl);
  130. if (w >= sizeof(blog_global.logbuf) - blog_global.logbuf_pos) {
  131. blog_global.logbuf_pos = sizeof(blog_global.logbuf) - 1;
  132. } else {
  133. blog_global.logbuf_pos += w;
  134. }
  135. }
  136. void BLog_Append (const char *fmt, ...)
  137. {
  138. ASSERT(blog_global.initialized)
  139. va_list vl;
  140. va_start(vl, fmt);
  141. BLog_AppendVarArg(fmt, vl);
  142. va_end(vl);
  143. }
  144. void BLog_Finish (int channel, int level)
  145. {
  146. ASSERT(blog_global.initialized)
  147. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  148. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  149. ASSERT(BLog_WouldLog(channel, level))
  150. ASSERT(blog_global.logbuf_pos >= 0 && blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  151. ASSERT(blog_global.logbuf[blog_global.logbuf_pos] == '\0')
  152. blog_global.log_func(channel, level, blog_global.logbuf);
  153. blog_global.logbuf_pos = 0;
  154. blog_global.logbuf[0] = '\0';
  155. }
  156. void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl)
  157. {
  158. ASSERT(blog_global.initialized)
  159. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  160. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  161. if (!BLog_WouldLog(channel, level)) {
  162. blog_global.logbuf_pos = 0;
  163. blog_global.logbuf[0] = '\0';
  164. return;
  165. }
  166. BLog_AppendVarArg(fmt, vl);
  167. BLog_Finish(channel, level);
  168. }
  169. void BLog_LogToChannel (int channel, int level, const char *fmt, ...)
  170. {
  171. ASSERT(blog_global.initialized)
  172. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  173. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  174. if (!BLog_WouldLog(channel, level)) {
  175. blog_global.logbuf_pos = 0;
  176. blog_global.logbuf[0] = '\0';
  177. return;
  178. }
  179. va_list vl;
  180. va_start(vl, fmt);
  181. BLog_AppendVarArg(fmt, vl);
  182. BLog_Finish(channel, level);
  183. va_end(vl);
  184. }
  185. void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, va_list vl)
  186. {
  187. ASSERT(blog_global.initialized)
  188. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  189. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  190. if (!BLog_WouldLog(channel, level)) {
  191. blog_global.logbuf_pos = 0;
  192. blog_global.logbuf[0] = '\0';
  193. return;
  194. }
  195. func(arg);
  196. BLog_AppendVarArg(fmt, vl);
  197. BLog_Finish(channel, level);
  198. }
  199. void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, ...)
  200. {
  201. ASSERT(blog_global.initialized)
  202. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  203. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  204. if (!BLog_WouldLog(channel, level)) {
  205. blog_global.logbuf_pos = 0;
  206. blog_global.logbuf[0] = '\0';
  207. return;
  208. }
  209. va_list vl;
  210. va_start(vl, fmt);
  211. func(arg);
  212. BLog_AppendVarArg(fmt, vl);
  213. BLog_Finish(channel, level);
  214. va_end(vl);
  215. }
  216. #endif