BLog.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #define BLOG_ERROR 1
  41. #define BLOG_WARNING 2
  42. #define BLOG_NOTICE 3
  43. #define BLOG_INFO 4
  44. #define BLOG_DEBUG 5
  45. #define BLog(...) BLog_LogToChannel(BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  46. typedef void (*_BLog_log_func) (int channel, int level, const char *msg);
  47. typedef void (*_BLog_free_func) (void);
  48. struct _BLog_channel {
  49. const char *name;
  50. int loglevel;
  51. };
  52. struct _BLog_global {
  53. #ifndef NDEBUG
  54. int initialized; // initialized statically
  55. #endif
  56. struct _BLog_channel channels[BLOG_NUM_CHANNELS];
  57. _BLog_log_func log_func;
  58. _BLog_free_func free_func;
  59. char logbuf[2048];
  60. int logbuf_pos;
  61. };
  62. extern struct _BLog_channel blog_channel_list[];
  63. extern struct _BLog_global blog_global;
  64. typedef void (*BLog_logfunc) (void *);
  65. static int BLogGlobal_GetChannelByName (const char *channel_name);
  66. static void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func);
  67. static void BLog_Free (void);
  68. static void BLog_SetChannelLoglevel (int channel, int loglevel);
  69. static int BLog_WouldLog (int channel, int level);
  70. static void BLog_AppendVarArg (const char *fmt, va_list vl);
  71. static void BLog_Append (const char *fmt, ...);
  72. static void BLog_Finish (int channel, int level);
  73. static void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl);
  74. static void BLog_LogToChannel (int channel, int level, const char *fmt, ...);
  75. static void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, va_list vl);
  76. static void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, ...);
  77. void BLog_InitStdout (void);
  78. int BLogGlobal_GetChannelByName (const char *channel_name)
  79. {
  80. int i;
  81. for (i = 0; i < BLOG_NUM_CHANNELS; i++) {
  82. if (!strcmp(blog_channel_list[i].name, channel_name)) {
  83. return i;
  84. }
  85. }
  86. return -1;
  87. }
  88. void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func)
  89. {
  90. ASSERT(!blog_global.initialized)
  91. #ifndef NDEBUG
  92. blog_global.initialized = 1;
  93. #endif
  94. // initialize channels
  95. memcpy(blog_global.channels, blog_channel_list, BLOG_NUM_CHANNELS * sizeof(struct _BLog_channel));
  96. blog_global.log_func = log_func;
  97. blog_global.free_func = free_func;
  98. blog_global.logbuf_pos = 0;
  99. blog_global.logbuf[0] = '\0';
  100. }
  101. void BLog_Free (void)
  102. {
  103. ASSERT(blog_global.initialized)
  104. #ifndef NDEBUG
  105. blog_global.initialized = 0;
  106. #endif
  107. blog_global.free_func();
  108. }
  109. void BLog_SetChannelLoglevel (int channel, int loglevel)
  110. {
  111. ASSERT(blog_global.initialized)
  112. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  113. ASSERT(loglevel >= 0 && loglevel <= BLOG_DEBUG)
  114. blog_global.channels[channel].loglevel = loglevel;
  115. }
  116. int BLog_WouldLog (int channel, int level)
  117. {
  118. ASSERT(blog_global.initialized)
  119. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  120. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  121. return (level <= blog_global.channels[channel].loglevel);
  122. }
  123. void BLog_AppendVarArg (const char *fmt, va_list vl)
  124. {
  125. ASSERT(blog_global.initialized)
  126. ASSERT(blog_global.logbuf_pos >= 0 && blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  127. int w = vsnprintf(blog_global.logbuf + blog_global.logbuf_pos, sizeof(blog_global.logbuf) - blog_global.logbuf_pos, fmt, vl);
  128. if (w >= sizeof(blog_global.logbuf) - blog_global.logbuf_pos) {
  129. blog_global.logbuf_pos = sizeof(blog_global.logbuf) - 1;
  130. } else {
  131. blog_global.logbuf_pos += w;
  132. }
  133. }
  134. void BLog_Append (const char *fmt, ...)
  135. {
  136. ASSERT(blog_global.initialized)
  137. va_list vl;
  138. va_start(vl, fmt);
  139. BLog_AppendVarArg(fmt, vl);
  140. va_end(vl);
  141. }
  142. void BLog_Finish (int channel, int level)
  143. {
  144. ASSERT(blog_global.initialized)
  145. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  146. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  147. ASSERT(BLog_WouldLog(channel, level))
  148. ASSERT(blog_global.logbuf_pos >= 0 && blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  149. ASSERT(blog_global.logbuf[blog_global.logbuf_pos] == '\0')
  150. blog_global.log_func(channel, level, blog_global.logbuf);
  151. blog_global.logbuf_pos = 0;
  152. blog_global.logbuf[0] = '\0';
  153. }
  154. void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl)
  155. {
  156. ASSERT(blog_global.initialized)
  157. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  158. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  159. if (!BLog_WouldLog(channel, level)) {
  160. blog_global.logbuf_pos = 0;
  161. blog_global.logbuf[0] = '\0';
  162. return;
  163. }
  164. BLog_AppendVarArg(fmt, vl);
  165. BLog_Finish(channel, level);
  166. }
  167. void BLog_LogToChannel (int channel, int level, const char *fmt, ...)
  168. {
  169. ASSERT(blog_global.initialized)
  170. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  171. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  172. if (!BLog_WouldLog(channel, level)) {
  173. blog_global.logbuf_pos = 0;
  174. blog_global.logbuf[0] = '\0';
  175. return;
  176. }
  177. va_list vl;
  178. va_start(vl, fmt);
  179. BLog_AppendVarArg(fmt, vl);
  180. BLog_Finish(channel, level);
  181. va_end(vl);
  182. }
  183. void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, va_list vl)
  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. func(arg);
  194. BLog_AppendVarArg(fmt, vl);
  195. BLog_Finish(channel, level);
  196. }
  197. void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, ...)
  198. {
  199. ASSERT(blog_global.initialized)
  200. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  201. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  202. if (!BLog_WouldLog(channel, level)) {
  203. blog_global.logbuf_pos = 0;
  204. blog_global.logbuf[0] = '\0';
  205. return;
  206. }
  207. va_list vl;
  208. va_start(vl, fmt);
  209. func(arg);
  210. BLog_AppendVarArg(fmt, vl);
  211. BLog_Finish(channel, level);
  212. va_end(vl);
  213. }
  214. #endif