log.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/module_common.h>
  57. #include <generated/blog_channel_ncd_log.h>
  58. struct rlog_instance {
  59. NCDModuleInst *i;
  60. int level;
  61. NCDValRef list;
  62. size_t start;
  63. };
  64. enum {STRING_ERROR, STRING_WARNING, STRING_NOTICE, STRING_INFO, STRING_DEBUG};
  65. static const char *strings[] = {
  66. "error", "warning", "notice", "info", "debug", NULL
  67. };
  68. static int check_strings (NCDValRef list, size_t start)
  69. {
  70. ASSERT(NCDVal_IsList(list))
  71. size_t count = NCDVal_ListCount(list);
  72. for (size_t j = start; j < count; j++) {
  73. NCDValRef string = NCDVal_ListGet(list, j);
  74. if (!NCDVal_IsString(string)) {
  75. return 0;
  76. }
  77. }
  78. return 1;
  79. }
  80. static void do_log (int level, NCDValRef list, size_t start)
  81. {
  82. ASSERT(level >= BLOG_ERROR)
  83. ASSERT(level <= BLOG_DEBUG)
  84. ASSERT(check_strings(list, start))
  85. if (!BLog_WouldLog(BLOG_CHANNEL_ncd_log_msg, level)) {
  86. return;
  87. }
  88. size_t count = NCDVal_ListCount(list);
  89. BLog_Begin();
  90. for (size_t j = start; j < count; j++) {
  91. NCDValRef string = NCDVal_ListGet(list, j);
  92. ASSERT(NCDVal_IsString(string))
  93. BLog_AppendBytes(NCDVal_StringMemRef(string));
  94. }
  95. BLog_Finish(BLOG_CHANNEL_ncd_log_msg, level);
  96. }
  97. static int parse_level (NCDModuleInst *i, NCDValRef level_arg, int *out_level)
  98. {
  99. int found = 0;
  100. if (NCDVal_IsString(level_arg)) {
  101. NCDStringIndex *string_index = i->params->iparams->string_index;
  102. found = 1;
  103. if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_ERROR), string_index)) {
  104. *out_level = BLOG_ERROR;
  105. }
  106. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_WARNING), string_index)) {
  107. *out_level = BLOG_WARNING;
  108. }
  109. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_NOTICE), string_index)) {
  110. *out_level = BLOG_NOTICE;
  111. }
  112. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_INFO), string_index)) {
  113. *out_level = BLOG_INFO;
  114. }
  115. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_DEBUG), string_index)) {
  116. *out_level = BLOG_DEBUG;
  117. }
  118. else {
  119. found = 0;
  120. }
  121. }
  122. if (!found) {
  123. uintmax_t level_numeric;
  124. if (!ncd_read_uintmax(level_arg, &level_numeric) || !(level_numeric >= BLOG_ERROR && level_numeric <= BLOG_DEBUG)) {
  125. return 0;
  126. }
  127. *out_level = level_numeric;
  128. }
  129. return 1;
  130. }
  131. static void rlog_func_new_common (void *vo, NCDModuleInst *i, int level, NCDValRef list, size_t start)
  132. {
  133. ASSERT(level >= BLOG_ERROR)
  134. ASSERT(level <= BLOG_DEBUG)
  135. ASSERT(check_strings(list, start))
  136. struct rlog_instance *o = vo;
  137. o->i = i;
  138. o->level = level;
  139. o->list = list;
  140. o->start = start;
  141. NCDModuleInst_Backend_Up(i);
  142. }
  143. static void rlog_func_die (void *vo)
  144. {
  145. struct rlog_instance *o = vo;
  146. do_log(o->level, o->list, o->start);
  147. NCDModuleInst_Backend_Dead(o->i);
  148. }
  149. static void log_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  150. {
  151. if (NCDVal_ListCount(params->args) < 1) {
  152. ModuleLog(i, BLOG_ERROR, "missing level argument");
  153. goto fail0;
  154. }
  155. int level;
  156. if (!parse_level(i, NCDVal_ListGet(params->args, 0), &level)) {
  157. ModuleLog(i, BLOG_ERROR, "wrong level argument");
  158. goto fail0;
  159. }
  160. if (!check_strings(params->args, 1)) {
  161. ModuleLog(i, BLOG_ERROR, "wrong string arguments");
  162. goto fail0;
  163. }
  164. do_log(level, params->args, 1);
  165. NCDModuleInst_Backend_Up(i);
  166. return;
  167. fail0:
  168. NCDModuleInst_Backend_DeadError(i);
  169. }
  170. static void log_r_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  171. {
  172. if (NCDVal_ListCount(params->args) < 1) {
  173. ModuleLog(i, BLOG_ERROR, "missing level argument");
  174. goto fail0;
  175. }
  176. int level;
  177. if (!parse_level(i, NCDVal_ListGet(params->args, 0), &level)) {
  178. ModuleLog(i, BLOG_ERROR, "wrong level argument");
  179. goto fail0;
  180. }
  181. if (!check_strings(params->args, 1)) {
  182. ModuleLog(i, BLOG_ERROR, "wrong string arguments");
  183. goto fail0;
  184. }
  185. rlog_func_new_common(vo, i, level, params->args, 1);
  186. return;
  187. fail0:
  188. NCDModuleInst_Backend_DeadError(i);
  189. }
  190. static void log_fr_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  191. {
  192. NCDValRef level_arg;
  193. NCDValRef strings_init_arg;
  194. NCDValRef strings_deinit_arg;
  195. if (!NCDVal_ListRead(params->args, 3, &level_arg, &strings_init_arg, &strings_deinit_arg)) {
  196. ModuleLog(i, BLOG_ERROR, "wrong arity");
  197. goto fail0;
  198. }
  199. int level;
  200. if (!parse_level(i, level_arg, &level)) {
  201. ModuleLog(i, BLOG_ERROR, "wrong level argument");
  202. goto fail0;
  203. }
  204. if (!NCDVal_IsList(strings_init_arg) || !check_strings(strings_init_arg, 0)) {
  205. ModuleLog(i, BLOG_ERROR, "wrong string_init argument");
  206. goto fail0;
  207. }
  208. if (!NCDVal_IsList(strings_deinit_arg) || !check_strings(strings_deinit_arg, 0)) {
  209. ModuleLog(i, BLOG_ERROR, "wrong strings_deinit argument");
  210. goto fail0;
  211. }
  212. do_log(level, strings_init_arg, 0);
  213. rlog_func_new_common(vo, i, level, strings_deinit_arg, 0);
  214. return;
  215. fail0:
  216. NCDModuleInst_Backend_DeadError(i);
  217. }
  218. static struct NCDModule modules[] = {
  219. {
  220. .type = "log",
  221. .func_new2 = log_func_new
  222. }, {
  223. .type = "log_r",
  224. .func_new2 = log_r_func_new,
  225. .func_die = rlog_func_die,
  226. .alloc_size = sizeof(struct rlog_instance)
  227. }, {
  228. .type = "log_fr",
  229. .func_new2 = log_fr_func_new,
  230. .func_die = rlog_func_die,
  231. .alloc_size = sizeof(struct rlog_instance)
  232. }, {
  233. .type = NULL
  234. }
  235. };
  236. const struct NCDModuleGroup ncdmodule_log = {
  237. .modules = modules,
  238. .strings = strings
  239. };