app_verbose.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/module.h"
  31. #include "asterisk/app.h"
  32. #include "asterisk/channel.h"
  33. static char *app_verbose = "Verbose";
  34. static char *app_log = "Log";
  35. /*** DOCUMENTATION
  36. <application name="Verbose" language="en_US">
  37. <synopsis>
  38. Send arbitrary text to verbose output.
  39. </synopsis>
  40. <syntax>
  41. <parameter name="level">
  42. <para>Must be an integer value. If not specified, defaults to 0.</para>
  43. </parameter>
  44. <parameter name="message" required="true">
  45. <para>Output text message.</para>
  46. </parameter>
  47. </syntax>
  48. <description>
  49. <para>Sends an arbitrary text message to verbose output.</para>
  50. </description>
  51. </application>
  52. <application name="Log" language="en_US">
  53. <synopsis>
  54. Send arbitrary text to a selected log level.
  55. </synopsis>
  56. <syntax>
  57. <parameter name="level" required="true">
  58. <para>Level must be one of <literal>ERROR</literal>, <literal>WARNING</literal>, <literal>NOTICE</literal>,
  59. <literal>DEBUG</literal>, <literal>VERBOSE</literal> or <literal>DTMF</literal>.</para>
  60. </parameter>
  61. <parameter name="message" required="true">
  62. <para>Output text message.</para>
  63. </parameter>
  64. </syntax>
  65. <description>
  66. <para>Sends an arbitrary text message to a selected log level.</para>
  67. </description>
  68. </application>
  69. ***/
  70. static int verbose_exec(struct ast_channel *chan, const char *data)
  71. {
  72. unsigned int vsize;
  73. char *parse;
  74. AST_DECLARE_APP_ARGS(args,
  75. AST_APP_ARG(level);
  76. AST_APP_ARG(msg);
  77. );
  78. if (ast_strlen_zero(data)) {
  79. return 0;
  80. }
  81. parse = ast_strdupa(data);
  82. AST_STANDARD_APP_ARGS(args, parse);
  83. if (args.argc == 1) {
  84. args.msg = args.level;
  85. args.level = "0";
  86. }
  87. if (sscanf(args.level, "%30u", &vsize) != 1) {
  88. vsize = 0;
  89. ast_log(LOG_WARNING, "'%s' is not a verboser number\n", args.level);
  90. } else if (4 < vsize) {
  91. vsize = 4;
  92. }
  93. ast_verb(vsize, "%s\n", args.msg);
  94. return 0;
  95. }
  96. static int log_exec(struct ast_channel *chan, const char *data)
  97. {
  98. char *parse;
  99. int lnum = -1;
  100. char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
  101. AST_DECLARE_APP_ARGS(args,
  102. AST_APP_ARG(level);
  103. AST_APP_ARG(msg);
  104. );
  105. if (ast_strlen_zero(data))
  106. return 0;
  107. parse = ast_strdupa(data);
  108. AST_STANDARD_APP_ARGS(args, parse);
  109. if (!strcasecmp(args.level, "ERROR")) {
  110. lnum = __LOG_ERROR;
  111. } else if (!strcasecmp(args.level, "WARNING")) {
  112. lnum = __LOG_WARNING;
  113. } else if (!strcasecmp(args.level, "NOTICE")) {
  114. lnum = __LOG_NOTICE;
  115. } else if (!strcasecmp(args.level, "DEBUG")) {
  116. lnum = __LOG_DEBUG;
  117. } else if (!strcasecmp(args.level, "VERBOSE")) {
  118. lnum = __LOG_VERBOSE;
  119. } else if (!strcasecmp(args.level, "DTMF")) {
  120. lnum = __LOG_DTMF;
  121. } else {
  122. ast_log(LOG_ERROR, "Unknown log level: '%s'\n", args.level);
  123. }
  124. if (lnum > -1) {
  125. snprintf(context, sizeof(context), "@ %s", ast_channel_context(chan));
  126. snprintf(extension, sizeof(extension), "Ext. %s", ast_channel_exten(chan));
  127. ast_log(lnum, extension, ast_channel_priority(chan), context, "%s\n", args.msg);
  128. }
  129. return 0;
  130. }
  131. static int unload_module(void)
  132. {
  133. int res;
  134. res = ast_unregister_application(app_verbose);
  135. res |= ast_unregister_application(app_log);
  136. return res;
  137. }
  138. static int load_module(void)
  139. {
  140. int res;
  141. res = ast_register_application_xml(app_log, log_exec);
  142. res |= ast_register_application_xml(app_verbose, verbose_exec);
  143. return res;
  144. }
  145. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send verbose output");