BLog.h 7.9 KB

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