log.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * @file log.c
  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 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @section DESCRIPTION
  29. *
  30. * Message logging using the BLog system provided by the BadVPN framework.
  31. * Each message has an associated loglevel, which must be one of: "error, "warning",
  32. * "notice", "info", "debug", or a numeric identifier (1=error to 5=debug).
  33. *
  34. * Synopsis:
  35. * log(string level [, string ...])
  36. *
  37. * Description:
  38. * On init, logs the concatenation of the given strings.
  39. *
  40. * Synopsis:
  41. * log_r(string level [, string ...])
  42. *
  43. * Description:
  44. * On deinit, logs the concatenation of the given strings.
  45. *
  46. * Synopsis:
  47. * log_fr(string level, list(string) strings_init, list(string) strings_deinit)
  48. *
  49. * Description:
  50. * On init, logs the concatenation of the strings in 'strings_init',
  51. * and on deinit, logs the concatenation of the strings in 'strings_deinit'.
  52. */
  53. #include <stdlib.h>
  54. #include <stdio.h>
  55. #include <misc/debug.h>
  56. #include <ncd/NCDModule.h>
  57. #include <ncd/extra/value_utils.h>
  58. #include <generated/blog_channel_ncd_log.h>
  59. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  60. #define ModuleString(i, id) ((i)->m->group->strings[(id)])
  61. struct rlog_instance {
  62. NCDModuleInst *i;
  63. int level;
  64. NCDValRef list;
  65. size_t start;
  66. };
  67. enum {STRING_ERROR, STRING_WARNING, STRING_NOTICE, STRING_INFO, STRING_DEBUG};
  68. static const char *strings[] = {
  69. "error", "warning", "notice", "info", "debug", NULL
  70. };
  71. static int check_strings (NCDValRef list, size_t start)
  72. {
  73. ASSERT(NCDVal_IsList(list))
  74. size_t count = NCDVal_ListCount(list);
  75. for (size_t j = start; j < count; j++) {
  76. NCDValRef string = NCDVal_ListGet(list, j);
  77. if (!NCDVal_IsString(string)) {
  78. return 0;
  79. }
  80. }
  81. return 1;
  82. }
  83. static void do_log (int level, NCDValRef list, size_t start)
  84. {
  85. ASSERT(level >= BLOG_ERROR)
  86. ASSERT(level <= BLOG_DEBUG)
  87. ASSERT(check_strings(list, start))
  88. if (!BLog_WouldLog(BLOG_CHANNEL_ncd_log_msg, level)) {
  89. return;
  90. }
  91. size_t count = NCDVal_ListCount(list);
  92. for (size_t j = start; j < count; j++) {
  93. NCDValRef string = NCDVal_ListGet(list, j);
  94. ASSERT(NCDVal_IsString(string))
  95. BLog_AppendBytes(NCDVal_StringData(string), NCDVal_StringLength(string));
  96. }
  97. BLog_Finish(BLOG_CHANNEL_ncd_log_msg, level);
  98. }
  99. static int parse_level (NCDModuleInst *i, NCDValRef level_arg, int *out_level)
  100. {
  101. if (!NCDVal_IsString(level_arg)) {
  102. return 0;
  103. }
  104. NCDStringIndex *string_index = i->params->iparams->string_index;
  105. uintmax_t level_numeric;
  106. if (ncd_read_uintmax(level_arg, &level_numeric) && level_numeric >= BLOG_ERROR && level_numeric <= BLOG_DEBUG) {
  107. *out_level = level_numeric;
  108. }
  109. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_ERROR), string_index)) {
  110. *out_level = BLOG_ERROR;
  111. }
  112. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_WARNING), string_index)) {
  113. *out_level = BLOG_WARNING;
  114. }
  115. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_NOTICE), string_index)) {
  116. *out_level = BLOG_NOTICE;
  117. }
  118. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_INFO), string_index)) {
  119. *out_level = BLOG_INFO;
  120. }
  121. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_DEBUG), string_index)) {
  122. *out_level = BLOG_DEBUG;
  123. }
  124. else {
  125. return 0;
  126. }
  127. return 1;
  128. }
  129. static void rlog_func_new_common (void *vo, NCDModuleInst *i, int level, NCDValRef list, size_t start)
  130. {
  131. ASSERT(level >= BLOG_ERROR)
  132. ASSERT(level <= BLOG_DEBUG)
  133. ASSERT(check_strings(list, start))
  134. struct rlog_instance *o = vo;
  135. o->i = i;
  136. o->level = level;
  137. o->list = list;
  138. o->start = start;
  139. NCDModuleInst_Backend_Up(i);
  140. }
  141. static void rlog_func_die (void *vo)
  142. {
  143. struct rlog_instance *o = vo;
  144. do_log(o->level, o->list, o->start);
  145. NCDModuleInst_Backend_Dead(o->i);
  146. }
  147. static void log_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  148. {
  149. if (NCDVal_ListCount(params->args) < 1) {
  150. ModuleLog(i, BLOG_ERROR, "missing level argument");
  151. goto fail0;
  152. }
  153. int level;
  154. if (!parse_level(i, NCDVal_ListGet(params->args, 0), &level)) {
  155. ModuleLog(i, BLOG_ERROR, "wrong level argument");
  156. goto fail0;
  157. }
  158. if (!check_strings(params->args, 1)) {
  159. ModuleLog(i, BLOG_ERROR, "wrong string arguments");
  160. goto fail0;
  161. }
  162. do_log(level, params->args, 1);
  163. NCDModuleInst_Backend_Up(i);
  164. return;
  165. fail0:
  166. NCDModuleInst_Backend_DeadError(i);
  167. }
  168. static void log_r_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  169. {
  170. if (NCDVal_ListCount(params->args) < 1) {
  171. ModuleLog(i, BLOG_ERROR, "missing level argument");
  172. goto fail0;
  173. }
  174. int level;
  175. if (!parse_level(i, NCDVal_ListGet(params->args, 0), &level)) {
  176. ModuleLog(i, BLOG_ERROR, "wrong level argument");
  177. goto fail0;
  178. }
  179. if (!check_strings(params->args, 1)) {
  180. ModuleLog(i, BLOG_ERROR, "wrong string arguments");
  181. goto fail0;
  182. }
  183. rlog_func_new_common(vo, i, level, params->args, 1);
  184. return;
  185. fail0:
  186. NCDModuleInst_Backend_DeadError(i);
  187. }
  188. static void log_fr_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  189. {
  190. NCDValRef level_arg;
  191. NCDValRef strings_init_arg;
  192. NCDValRef strings_deinit_arg;
  193. if (!NCDVal_ListRead(params->args, 3, &level_arg, &strings_init_arg, &strings_deinit_arg)) {
  194. ModuleLog(i, BLOG_ERROR, "wrong arity");
  195. goto fail0;
  196. }
  197. int level;
  198. if (!parse_level(i, level_arg, &level)) {
  199. ModuleLog(i, BLOG_ERROR, "wrong level argument");
  200. goto fail0;
  201. }
  202. if (!NCDVal_IsList(strings_init_arg) || !check_strings(strings_init_arg, 0)) {
  203. ModuleLog(i, BLOG_ERROR, "wrong string_init argument");
  204. goto fail0;
  205. }
  206. if (!NCDVal_IsList(strings_deinit_arg) || !check_strings(strings_deinit_arg, 0)) {
  207. ModuleLog(i, BLOG_ERROR, "wrong strings_deinit argument");
  208. goto fail0;
  209. }
  210. do_log(level, strings_init_arg, 0);
  211. rlog_func_new_common(vo, i, level, strings_deinit_arg, 0);
  212. return;
  213. fail0:
  214. NCDModuleInst_Backend_DeadError(i);
  215. }
  216. static struct NCDModule modules[] = {
  217. {
  218. .type = "log",
  219. .func_new2 = log_func_new
  220. }, {
  221. .type = "log_r",
  222. .func_new2 = log_r_func_new,
  223. .func_die = rlog_func_die,
  224. .alloc_size = sizeof(struct rlog_instance)
  225. }, {
  226. .type = "log_fr",
  227. .func_new2 = log_fr_func_new,
  228. .func_die = rlog_func_die,
  229. .alloc_size = sizeof(struct rlog_instance)
  230. }, {
  231. .type = NULL
  232. }
  233. };
  234. const struct NCDModuleGroup ncdmodule_log = {
  235. .modules = modules,
  236. .strings = strings
  237. };