BLog.h 11 KB

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