BLog.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @file BLog.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * A global object for logging.
  25. */
  26. #ifndef BADVPN_BLOG_H
  27. #define BADVPN_BLOG_H
  28. #include <stdarg.h>
  29. #include <string.h>
  30. #include <misc/debug.h>
  31. // auto-generated channel numbers and number of channels
  32. #include <generated/blog_channels_defines.h>
  33. #define BLOG_ERROR 1
  34. #define BLOG_WARNING 2
  35. #define BLOG_NOTICE 3
  36. #define BLOG_INFO 4
  37. #define BLOG_DEBUG 5
  38. #define BLog(...) BLog_LogToChannel(BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  39. typedef void (*_BLog_log_func) (int channel, int level, const char *msg);
  40. typedef void (*_BLog_free_func) (void);
  41. struct _BLog_channel {
  42. const char *name;
  43. int loglevel;
  44. };
  45. struct _BLog_global {
  46. #ifndef NDEBUG
  47. int initialized; // initialized statically
  48. #endif
  49. struct _BLog_channel channels[BLOG_NUM_CHANNELS];
  50. _BLog_log_func log_func;
  51. _BLog_free_func free_func;
  52. char logbuf[2048];
  53. int logbuf_pos;
  54. };
  55. extern struct _BLog_channel blog_channel_list[];
  56. extern struct _BLog_global blog_global;
  57. static int BLogGlobal_GetChannelByName (const char *channel_name);
  58. static void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func);
  59. static void BLog_Free (void);
  60. static void BLog_SetChannelLoglevel (int channel, int loglevel);
  61. static void BLog_AppendVarArg (const char *fmt, va_list vl);
  62. static void BLog_Append (const char *fmt, ...);
  63. static void BLog_Finish (int channel, int level);
  64. static void BLog_LogToChannelStr (int channel, int level, const char *msg);
  65. static void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl);
  66. static void BLog_LogToChannel (int channel, int level, const char *fmt, ...);
  67. void BLog_InitStdout (void);
  68. int BLogGlobal_GetChannelByName (const char *channel_name)
  69. {
  70. int i;
  71. for (i = 0; i < BLOG_NUM_CHANNELS; i++) {
  72. if (!strcmp(blog_channel_list[i].name, channel_name)) {
  73. return i;
  74. }
  75. }
  76. return -1;
  77. }
  78. void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func)
  79. {
  80. ASSERT(!blog_global.initialized)
  81. #ifndef NDEBUG
  82. blog_global.initialized = 1;
  83. #endif
  84. // initialize channels
  85. memcpy(blog_global.channels, blog_channel_list, BLOG_NUM_CHANNELS * sizeof(struct _BLog_channel));
  86. blog_global.log_func = log_func;
  87. blog_global.free_func = free_func;
  88. blog_global.logbuf_pos = 0;
  89. blog_global.logbuf[0] = '\0';
  90. }
  91. void BLog_Free (void)
  92. {
  93. ASSERT(blog_global.initialized)
  94. #ifndef NDEBUG
  95. blog_global.initialized = 0;
  96. #endif
  97. blog_global.free_func();
  98. }
  99. void BLog_SetChannelLoglevel (int channel, int loglevel)
  100. {
  101. ASSERT(blog_global.initialized)
  102. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  103. ASSERT(loglevel >= 0 && loglevel <= BLOG_DEBUG)
  104. blog_global.channels[channel].loglevel = loglevel;
  105. }
  106. void BLog_AppendVarArg (const char *fmt, va_list vl)
  107. {
  108. ASSERT(blog_global.initialized)
  109. ASSERT(blog_global.logbuf_pos >= 0 && blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  110. int w = vsnprintf(blog_global.logbuf + blog_global.logbuf_pos, sizeof(blog_global.logbuf) - blog_global.logbuf_pos, fmt, vl);
  111. if (w >= sizeof(blog_global.logbuf) - blog_global.logbuf_pos) {
  112. blog_global.logbuf_pos = sizeof(blog_global.logbuf) - 1;
  113. } else {
  114. blog_global.logbuf_pos += w;
  115. }
  116. }
  117. void BLog_Append (const char *fmt, ...)
  118. {
  119. ASSERT(blog_global.initialized)
  120. va_list vl;
  121. va_start(vl, fmt);
  122. BLog_AppendVarArg(fmt, vl);
  123. va_end(vl);
  124. }
  125. void BLog_Finish (int channel, int level)
  126. {
  127. ASSERT(blog_global.initialized)
  128. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  129. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  130. ASSERT(blog_global.logbuf_pos >= 0 && blog_global.logbuf_pos < sizeof(blog_global.logbuf))
  131. ASSERT(blog_global.logbuf[blog_global.logbuf_pos] == '\0')
  132. if (level <= blog_global.channels[channel].loglevel) {
  133. blog_global.log_func(channel, level, blog_global.logbuf);
  134. }
  135. blog_global.logbuf_pos = 0;
  136. blog_global.logbuf[0] = '\0';
  137. }
  138. void BLog_LogToChannelStr (int channel, int level, const char *msg)
  139. {
  140. ASSERT(blog_global.initialized)
  141. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  142. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  143. BLog_Append("%s", msg);
  144. BLog_Finish(channel, level);
  145. }
  146. void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl)
  147. {
  148. ASSERT(blog_global.initialized)
  149. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  150. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  151. BLog_AppendVarArg(fmt, vl);
  152. BLog_Finish(channel, level);
  153. }
  154. void BLog_LogToChannel (int channel, int level, const char *fmt, ...)
  155. {
  156. ASSERT(blog_global.initialized)
  157. ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
  158. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  159. va_list vl;
  160. va_start(vl, fmt);
  161. BLog_LogToChannelVarArg(channel, level, fmt, vl);
  162. va_end(vl);
  163. }
  164. #endif