log.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_StringData(string), NCDVal_StringLength(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. if (!NCDVal_IsString(level_arg)) {
  100. return 0;
  101. }
  102. NCDStringIndex *string_index = i->params->iparams->string_index;
  103. uintmax_t level_numeric;
  104. if (ncd_read_uintmax(level_arg, &level_numeric) && level_numeric >= BLOG_ERROR && level_numeric <= BLOG_DEBUG) {
  105. *out_level = level_numeric;
  106. }
  107. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_ERROR), string_index)) {
  108. *out_level = BLOG_ERROR;
  109. }
  110. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_WARNING), string_index)) {
  111. *out_level = BLOG_WARNING;
  112. }
  113. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_NOTICE), string_index)) {
  114. *out_level = BLOG_NOTICE;
  115. }
  116. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_INFO), string_index)) {
  117. *out_level = BLOG_INFO;
  118. }
  119. else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_DEBUG), string_index)) {
  120. *out_level = BLOG_DEBUG;
  121. }
  122. else {
  123. return 0;
  124. }
  125. return 1;
  126. }
  127. static void rlog_func_new_common (void *vo, NCDModuleInst *i, int level, NCDValRef list, size_t start)
  128. {
  129. ASSERT(level >= BLOG_ERROR)
  130. ASSERT(level <= BLOG_DEBUG)
  131. ASSERT(check_strings(list, start))
  132. struct rlog_instance *o = vo;
  133. o->i = i;
  134. o->level = level;
  135. o->list = list;
  136. o->start = start;
  137. NCDModuleInst_Backend_Up(i);
  138. }
  139. static void rlog_func_die (void *vo)
  140. {
  141. struct rlog_instance *o = vo;
  142. do_log(o->level, o->list, o->start);
  143. NCDModuleInst_Backend_Dead(o->i);
  144. }
  145. static void log_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  146. {
  147. if (NCDVal_ListCount(params->args) < 1) {
  148. ModuleLog(i, BLOG_ERROR, "missing level argument");
  149. goto fail0;
  150. }
  151. int level;
  152. if (!parse_level(i, NCDVal_ListGet(params->args, 0), &level)) {
  153. ModuleLog(i, BLOG_ERROR, "wrong level argument");
  154. goto fail0;
  155. }
  156. if (!check_strings(params->args, 1)) {
  157. ModuleLog(i, BLOG_ERROR, "wrong string arguments");
  158. goto fail0;
  159. }
  160. do_log(level, params->args, 1);
  161. NCDModuleInst_Backend_Up(i);
  162. return;
  163. fail0:
  164. NCDModuleInst_Backend_DeadError(i);
  165. }
  166. static void log_r_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  167. {
  168. if (NCDVal_ListCount(params->args) < 1) {
  169. ModuleLog(i, BLOG_ERROR, "missing level argument");
  170. goto fail0;
  171. }
  172. int level;
  173. if (!parse_level(i, NCDVal_ListGet(params->args, 0), &level)) {
  174. ModuleLog(i, BLOG_ERROR, "wrong level argument");
  175. goto fail0;
  176. }
  177. if (!check_strings(params->args, 1)) {
  178. ModuleLog(i, BLOG_ERROR, "wrong string arguments");
  179. goto fail0;
  180. }
  181. rlog_func_new_common(vo, i, level, params->args, 1);
  182. return;
  183. fail0:
  184. NCDModuleInst_Backend_DeadError(i);
  185. }
  186. static void log_fr_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  187. {
  188. NCDValRef level_arg;
  189. NCDValRef strings_init_arg;
  190. NCDValRef strings_deinit_arg;
  191. if (!NCDVal_ListRead(params->args, 3, &level_arg, &strings_init_arg, &strings_deinit_arg)) {
  192. ModuleLog(i, BLOG_ERROR, "wrong arity");
  193. goto fail0;
  194. }
  195. int level;
  196. if (!parse_level(i, level_arg, &level)) {
  197. ModuleLog(i, BLOG_ERROR, "wrong level argument");
  198. goto fail0;
  199. }
  200. if (!NCDVal_IsList(strings_init_arg) || !check_strings(strings_init_arg, 0)) {
  201. ModuleLog(i, BLOG_ERROR, "wrong string_init argument");
  202. goto fail0;
  203. }
  204. if (!NCDVal_IsList(strings_deinit_arg) || !check_strings(strings_deinit_arg, 0)) {
  205. ModuleLog(i, BLOG_ERROR, "wrong strings_deinit argument");
  206. goto fail0;
  207. }
  208. do_log(level, strings_init_arg, 0);
  209. rlog_func_new_common(vo, i, level, strings_deinit_arg, 0);
  210. return;
  211. fail0:
  212. NCDModuleInst_Backend_DeadError(i);
  213. }
  214. static struct NCDModule modules[] = {
  215. {
  216. .type = "log",
  217. .func_new2 = log_func_new
  218. }, {
  219. .type = "log_r",
  220. .func_new2 = log_r_func_new,
  221. .func_die = rlog_func_die,
  222. .alloc_size = sizeof(struct rlog_instance)
  223. }, {
  224. .type = "log_fr",
  225. .func_new2 = log_fr_func_new,
  226. .func_die = rlog_func_die,
  227. .alloc_size = sizeof(struct rlog_instance)
  228. }, {
  229. .type = NULL
  230. }
  231. };
  232. const struct NCDModuleGroup ncdmodule_log = {
  233. .modules = modules,
  234. .strings = strings
  235. };