BLog.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #include <base/BMutex.h>
  39. // auto-generated channel numbers and number of channels
  40. #include <generated/blog_channels_defines.h>
  41. // keep in sync with level names in BLog.c!
  42. #define BLOG_ERROR 1
  43. #define BLOG_WARNING 2
  44. #define BLOG_NOTICE 3
  45. #define BLOG_INFO 4
  46. #define BLOG_DEBUG 5
  47. #define BLog(...) BLog_LogToChannel(BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  48. typedef void (*_BLog_log_func) (int channel, int level, const char *msg);
  49. typedef void (*_BLog_free_func) (void);
  50. struct _BLog_channel {
  51. const char *name;
  52. int loglevel;
  53. };
  54. struct _BLog_global {
  55. #ifndef NDEBUG
  56. int initialized; // initialized statically
  57. #endif
  58. struct _BLog_channel channels[BLOG_NUM_CHANNELS];
  59. _BLog_log_func log_func;
  60. _BLog_free_func free_func;
  61. BMutex mutex;
  62. #ifndef NDEBUG
  63. int logging;
  64. #endif
  65. char logbuf[2048];
  66. int logbuf_pos;
  67. };
  68. extern struct _BLog_channel blog_channel_list[];
  69. extern struct _BLog_global blog_global;
  70. typedef void (*BLog_logfunc) (void *);
  71. static int BLogGlobal_GetChannelByName (const char *channel_name);
  72. static void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func);
  73. static void BLog_Free (void);
  74. static void BLog_SetChannelLoglevel (int channel, int loglevel);
  75. static int BLog_WouldLog (int channel, int level);
  76. static void BLog_Begin (void);
  77. static void BLog_AppendVarArg (const char *fmt, va_list vl);
  78. static void BLog_Append (const char *fmt, ...);
  79. static void BLog_AppendBytes (const char *data, size_t len);
  80. static void BLog_Finish (int channel, int level);
  81. static void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl);
  82. static void BLog_LogToChannel (int channel, int level, const char *fmt, ...);
  83. static void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, va_list vl);
  84. static void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, ...);
  85. void BLog_InitStdout (void);
  86. void BLog_InitStderr (void);
  87. int BLogGlobal_GetChannelByName (const char *channel_name)
  88. {
  89. int i;
  90. for (i = 0; i < BLOG_NUM_CHANNELS; i++) {
  91. if (!strcmp(blog_channel_list[i].name, channel_name)) {
  92. return i;
  93. }
  94. }
  95. return -1;
  96. }
  97. void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func)
  98. {
  99. ASSERT(!blog_global.initialized)
  100. #ifndef NDEBUG
  101. blog_global.initialized = 1;
  102. #endif
  103. // initialize channels
  104. memcpy(blog_global.channels, blog_channel_list, BLOG_NUM_CHANNELS * sizeof(struct _BLog_channel));
  105. blog_global.log_func = log_func;
  106. blog_global.free_func = free_func;
  107. #ifndef NDEBUG
  108. blog_global.logging = 0;
  109. #endif
  110. blog_global.logbuf_pos = 0;
  111. blog_global.logbuf[0] = '\0';
  112. ASSERT_FORCE(BMutex_Init(&blog_global.mutex))
  113. }
  114. void BLog_Free (void)
  115. {
  116. ASSERT(blog_global.initialized)
  117. #ifndef NDEBUG
  118. ASSERT(!blog_global.logging)
  119. #endif
  120. BMutex_Free(&blog_global.mutex);
  121. #ifndef NDEBUG
  122. blog_global.initialized = 0;
  123. #endif
  124. blog_global.free_func();
  125. }
  126. void BLog_SetChannelLoglevel (int channel, int loglevel)
  127. {
  128. ASSERT(blog_global.initialized)
  129. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  130. ASSERT(loglevel >= 0 && loglevel <= BLOG_DEBUG)
  131. blog_global.channels[channel].loglevel = loglevel;
  132. }
  133. int BLog_WouldLog (int channel, int level)
  134. {
  135. ASSERT(blog_global.initialized)
  136. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  137. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  138. return (level <= blog_global.channels[channel].loglevel);
  139. }
  140. void BLog_Begin (void)
  141. {
  142. ASSERT(blog_global.initialized)
  143. BMutex_Lock(&blog_global.mutex);
  144. #ifndef NDEBUG
  145. ASSERT(!blog_global.logging)
  146. blog_global.logging = 1;
  147. #endif
  148. }
  149. void BLog_AppendVarArg (const char *fmt, va_list vl)
  150. {
  151. ASSERT(blog_global.initialized)
  152. #ifndef NDEBUG
  153. ASSERT(blog_global.logging)
  154. #endif
  155. ASSERT(blog_global.logbuf_pos >= 0)
  156. ASSERT(blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  157. int w = vsnprintf(blog_global.logbuf + blog_global.logbuf_pos, sizeof(blog_global.logbuf) - blog_global.logbuf_pos, fmt, vl);
  158. if (w >= sizeof(blog_global.logbuf) - blog_global.logbuf_pos) {
  159. blog_global.logbuf_pos = sizeof(blog_global.logbuf) - 1;
  160. } else {
  161. blog_global.logbuf_pos += w;
  162. }
  163. }
  164. void BLog_Append (const char *fmt, ...)
  165. {
  166. ASSERT(blog_global.initialized)
  167. #ifndef NDEBUG
  168. ASSERT(blog_global.logging)
  169. #endif
  170. va_list vl;
  171. va_start(vl, fmt);
  172. BLog_AppendVarArg(fmt, vl);
  173. va_end(vl);
  174. }
  175. void BLog_AppendBytes (const char *data, size_t len)
  176. {
  177. ASSERT(blog_global.initialized)
  178. #ifndef NDEBUG
  179. ASSERT(blog_global.logging)
  180. #endif
  181. ASSERT(blog_global.logbuf_pos >= 0)
  182. ASSERT(blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  183. size_t avail = (sizeof(blog_global.logbuf) - 1) - blog_global.logbuf_pos;
  184. len = (len > avail ? avail : len);
  185. memcpy(blog_global.logbuf + blog_global.logbuf_pos, data, len);
  186. blog_global.logbuf_pos += len;
  187. blog_global.logbuf[blog_global.logbuf_pos] = '\0';
  188. }
  189. void BLog_Finish (int channel, int level)
  190. {
  191. ASSERT(blog_global.initialized)
  192. #ifndef NDEBUG
  193. ASSERT(blog_global.logging)
  194. #endif
  195. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  196. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  197. ASSERT(BLog_WouldLog(channel, level))
  198. ASSERT(blog_global.logbuf_pos >= 0)
  199. ASSERT(blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  200. ASSERT(blog_global.logbuf[blog_global.logbuf_pos] == '\0')
  201. blog_global.log_func(channel, level, blog_global.logbuf);
  202. #ifndef NDEBUG
  203. blog_global.logging = 0;
  204. #endif
  205. blog_global.logbuf_pos = 0;
  206. blog_global.logbuf[0] = '\0';
  207. BMutex_Unlock(&blog_global.mutex);
  208. }
  209. void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl)
  210. {
  211. ASSERT(blog_global.initialized)
  212. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  213. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  214. if (!BLog_WouldLog(channel, level)) {
  215. return;
  216. }
  217. BLog_Begin();
  218. BLog_AppendVarArg(fmt, vl);
  219. BLog_Finish(channel, level);
  220. }
  221. void BLog_LogToChannel (int channel, int level, const char *fmt, ...)
  222. {
  223. ASSERT(blog_global.initialized)
  224. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  225. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  226. if (!BLog_WouldLog(channel, level)) {
  227. return;
  228. }
  229. va_list vl;
  230. va_start(vl, fmt);
  231. BLog_Begin();
  232. BLog_AppendVarArg(fmt, vl);
  233. BLog_Finish(channel, level);
  234. va_end(vl);
  235. }
  236. void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, va_list vl)
  237. {
  238. ASSERT(blog_global.initialized)
  239. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  240. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  241. if (!BLog_WouldLog(channel, level)) {
  242. return;
  243. }
  244. BLog_Begin();
  245. func(arg);
  246. BLog_AppendVarArg(fmt, vl);
  247. BLog_Finish(channel, level);
  248. }
  249. void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, const char *fmt, ...)
  250. {
  251. ASSERT(blog_global.initialized)
  252. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  253. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  254. if (!BLog_WouldLog(channel, level)) {
  255. return;
  256. }
  257. va_list vl;
  258. va_start(vl, fmt);
  259. BLog_Begin();
  260. func(arg);
  261. BLog_AppendVarArg(fmt, vl);
  262. BLog_Finish(channel, level);
  263. va_end(vl);
  264. }
  265. #endif