BLog.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_AppendBytes (const char *data, size_t len);
  74. static void BLog_Finish (int channel, int level);
  75. static void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl);
  76. static void BLog_LogToChannel (int channel, int level, const char *fmt, ...);
  77. static void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, va_list vl);
  78. static void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, ...);
  79. void BLog_InitStdout (void);
  80. void BLog_InitStderr (void);
  81. int BLogGlobal_GetChannelByName (const char *channel_name)
  82. {
  83. int i;
  84. for (i = 0; i < BLOG_NUM_CHANNELS; i++) {
  85. if (!strcmp(blog_channel_list[i].name, channel_name)) {
  86. return i;
  87. }
  88. }
  89. return -1;
  90. }
  91. void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func)
  92. {
  93. ASSERT(!blog_global.initialized)
  94. #ifndef NDEBUG
  95. blog_global.initialized = 1;
  96. #endif
  97. // initialize channels
  98. memcpy(blog_global.channels, blog_channel_list, BLOG_NUM_CHANNELS * sizeof(struct _BLog_channel));
  99. blog_global.log_func = log_func;
  100. blog_global.free_func = free_func;
  101. blog_global.logbuf_pos = 0;
  102. blog_global.logbuf[0] = '\0';
  103. }
  104. void BLog_Free (void)
  105. {
  106. ASSERT(blog_global.initialized)
  107. #ifndef NDEBUG
  108. blog_global.initialized = 0;
  109. #endif
  110. blog_global.free_func();
  111. }
  112. void BLog_SetChannelLoglevel (int channel, int loglevel)
  113. {
  114. ASSERT(blog_global.initialized)
  115. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  116. ASSERT(loglevel >= 0 && loglevel <= BLOG_DEBUG)
  117. blog_global.channels[channel].loglevel = loglevel;
  118. }
  119. int BLog_WouldLog (int channel, int level)
  120. {
  121. ASSERT(blog_global.initialized)
  122. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  123. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  124. return (level <= blog_global.channels[channel].loglevel);
  125. }
  126. void BLog_AppendVarArg (const char *fmt, va_list vl)
  127. {
  128. ASSERT(blog_global.initialized)
  129. ASSERT(blog_global.logbuf_pos >= 0)
  130. ASSERT(blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  131. int w = vsnprintf(blog_global.logbuf + blog_global.logbuf_pos, sizeof(blog_global.logbuf) - blog_global.logbuf_pos, fmt, vl);
  132. if (w >= sizeof(blog_global.logbuf) - blog_global.logbuf_pos) {
  133. blog_global.logbuf_pos = sizeof(blog_global.logbuf) - 1;
  134. } else {
  135. blog_global.logbuf_pos += w;
  136. }
  137. }
  138. void BLog_Append (const char *fmt, ...)
  139. {
  140. ASSERT(blog_global.initialized)
  141. va_list vl;
  142. va_start(vl, fmt);
  143. BLog_AppendVarArg(fmt, vl);
  144. va_end(vl);
  145. }
  146. void BLog_AppendBytes (const char *data, size_t len)
  147. {
  148. ASSERT(blog_global.initialized)
  149. ASSERT(blog_global.logbuf_pos >= 0)
  150. ASSERT(blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  151. size_t avail = (sizeof(blog_global.logbuf) - 1) - blog_global.logbuf_pos;
  152. len = (len > avail ? avail : len);
  153. memcpy(blog_global.logbuf + blog_global.logbuf_pos, data, len);
  154. blog_global.logbuf_pos += len;
  155. blog_global.logbuf[blog_global.logbuf_pos] = '\0';
  156. }
  157. void BLog_Finish (int channel, int level)
  158. {
  159. ASSERT(blog_global.initialized)
  160. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  161. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  162. ASSERT(BLog_WouldLog(channel, level))
  163. ASSERT(blog_global.logbuf_pos >= 0)
  164. ASSERT(blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  165. ASSERT(blog_global.logbuf[blog_global.logbuf_pos] == '\0')
  166. blog_global.log_func(channel, level, blog_global.logbuf);
  167. blog_global.logbuf_pos = 0;
  168. blog_global.logbuf[0] = '\0';
  169. }
  170. void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl)
  171. {
  172. ASSERT(blog_global.initialized)
  173. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  174. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  175. if (!BLog_WouldLog(channel, level)) {
  176. blog_global.logbuf_pos = 0;
  177. blog_global.logbuf[0] = '\0';
  178. return;
  179. }
  180. BLog_AppendVarArg(fmt, vl);
  181. BLog_Finish(channel, level);
  182. }
  183. void BLog_LogToChannel (int channel, int level, const char *fmt, ...)
  184. {
  185. ASSERT(blog_global.initialized)
  186. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  187. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  188. if (!BLog_WouldLog(channel, level)) {
  189. blog_global.logbuf_pos = 0;
  190. blog_global.logbuf[0] = '\0';
  191. return;
  192. }
  193. va_list vl;
  194. va_start(vl, fmt);
  195. BLog_AppendVarArg(fmt, vl);
  196. BLog_Finish(channel, level);
  197. va_end(vl);
  198. }
  199. void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, va_list vl)
  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. func(arg);
  210. BLog_AppendVarArg(fmt, vl);
  211. BLog_Finish(channel, level);
  212. }
  213. void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, ...)
  214. {
  215. ASSERT(blog_global.initialized)
  216. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  217. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  218. if (!BLog_WouldLog(channel, level)) {
  219. blog_global.logbuf_pos = 0;
  220. blog_global.logbuf[0] = '\0';
  221. return;
  222. }
  223. va_list vl;
  224. va_start(vl, fmt);
  225. func(arg);
  226. BLog_AppendVarArg(fmt, vl);
  227. BLog_Finish(channel, level);
  228. va_end(vl);
  229. }
  230. #endif