BLog.h 11 KB

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