app_verbose.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (c) 2004 - 2005 Tilghman Lesher. All rights reserved.
  5. *
  6. * Tilghman Lesher <app_verbose_v001@the-tilghman.com>
  7. *
  8. * This code is released by the author with no restrictions on usage.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. */
  17. /*! \file
  18. *
  19. * \brief Verbose logging application
  20. *
  21. * \author Tilghman Lesher <app_verbose_v001@the-tilghman.com>
  22. *
  23. * \ingroup applications
  24. */
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include "asterisk/module.h"
  28. #include "asterisk/app.h"
  29. #include "asterisk/channel.h"
  30. static char *app_verbose = "Verbose";
  31. static char *verbose_synopsis = "Send arbitrary text to verbose output";
  32. static char *verbose_descrip =
  33. "Verbose([<level>,]<message>)\n"
  34. " level must be an integer value. If not specified, defaults to 0.\n";
  35. static char *app_log = "Log";
  36. static char *log_synopsis = "Send arbitrary text to a selected log level";
  37. static char *log_descrip =
  38. "Log(<level>,<message>)\n"
  39. " level must be one of ERROR, WARNING, NOTICE, DEBUG, VERBOSE, DTMF\n";
  40. static int verbose_exec(struct ast_channel *chan, void *data)
  41. {
  42. int vsize;
  43. char *parse;
  44. AST_DECLARE_APP_ARGS(args,
  45. AST_APP_ARG(level);
  46. AST_APP_ARG(msg);
  47. );
  48. if (ast_strlen_zero(data)) {
  49. return 0;
  50. }
  51. parse = ast_strdupa(data);
  52. AST_STANDARD_APP_ARGS(args, parse);
  53. if (args.argc == 1) {
  54. args.msg = args.level;
  55. args.level = "0";
  56. }
  57. if (sscanf(args.level, "%30d", &vsize) != 1) {
  58. vsize = 0;
  59. ast_log(LOG_WARNING, "'%s' is not a verboser number\n", args.level);
  60. }
  61. if (option_verbose >= vsize) {
  62. switch (vsize) {
  63. case 0:
  64. ast_verbose("%s\n", args.msg);
  65. break;
  66. case 1:
  67. ast_verbose(VERBOSE_PREFIX_1 "%s\n", args.msg);
  68. break;
  69. case 2:
  70. ast_verbose(VERBOSE_PREFIX_2 "%s\n", args.msg);
  71. break;
  72. case 3:
  73. ast_verbose(VERBOSE_PREFIX_3 "%s\n", args.msg);
  74. break;
  75. default:
  76. ast_verbose(VERBOSE_PREFIX_4 "%s\n", args.msg);
  77. }
  78. }
  79. return 0;
  80. }
  81. static int log_exec(struct ast_channel *chan, void *data)
  82. {
  83. char *parse;
  84. int lnum = -1;
  85. char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
  86. AST_DECLARE_APP_ARGS(args,
  87. AST_APP_ARG(level);
  88. AST_APP_ARG(msg);
  89. );
  90. if (ast_strlen_zero(data))
  91. return 0;
  92. parse = ast_strdupa(data);
  93. AST_STANDARD_APP_ARGS(args, parse);
  94. if (!strcasecmp(args.level, "ERROR")) {
  95. lnum = __LOG_ERROR;
  96. } else if (!strcasecmp(args.level, "WARNING")) {
  97. lnum = __LOG_WARNING;
  98. } else if (!strcasecmp(args.level, "NOTICE")) {
  99. lnum = __LOG_NOTICE;
  100. } else if (!strcasecmp(args.level, "DEBUG")) {
  101. lnum = __LOG_DEBUG;
  102. } else if (!strcasecmp(args.level, "VERBOSE")) {
  103. lnum = __LOG_VERBOSE;
  104. } else if (!strcasecmp(args.level, "DTMF")) {
  105. lnum = __LOG_DTMF;
  106. } else if (!strcasecmp(args.level, "EVENT")) {
  107. lnum = __LOG_EVENT;
  108. } else {
  109. ast_log(LOG_ERROR, "Unknown log level: '%s'\n", args.level);
  110. }
  111. if (lnum > -1) {
  112. snprintf(context, sizeof(context), "@ %s", chan->context);
  113. snprintf(extension, sizeof(extension), "Ext. %s", chan->exten);
  114. ast_log(lnum, extension, chan->priority, context, "%s\n", args.msg);
  115. }
  116. return 0;
  117. }
  118. static int unload_module(void)
  119. {
  120. int res;
  121. res = ast_unregister_application(app_verbose);
  122. res |= ast_unregister_application(app_log);
  123. return res;
  124. }
  125. static int load_module(void)
  126. {
  127. int res;
  128. res = ast_register_application(app_log, log_exec, log_synopsis, log_descrip);
  129. res |= ast_register_application(app_verbose, verbose_exec, verbose_synopsis, verbose_descrip);
  130. return res;
  131. }
  132. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send verbose output");