]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Verbose logging discrepancies
authorKevin Harwell <kharwell@digium.com>
Thu, 29 Aug 2013 22:16:41 +0000 (22:16 +0000)
committerKevin Harwell <kharwell@digium.com>
Thu, 29 Aug 2013 22:16:41 +0000 (22:16 +0000)
Refactored cases where a combination of ast_verbose/options_verbose were
present.  Also in general tried to eliminate, in as many places as possible,
where the options_verbose global variable was being used.  Refactored the way
local and remote consoles handle verbose message logging in an attempt to
solve the various discrepancies that sometimes would show between the two.

(closes issue AST-1193)
Reported by: Guenther Kelleter
Review: https://reviewboard.asterisk.org/r/2798/

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/11@397948 65c4cc65-6c06-0410-ace0-fbb531ad65f3

apps/app_dumpchan.c
apps/app_verbose.c
channels/chan_misdn.c
main/asterisk.c
main/features.c
main/logger.c

index 4a80a3d13ea269bebed442b03a7e8e12a824dde3..c14259c1e841f77c6515f8a936e6d12d1501c0d5 100644 (file)
@@ -138,10 +138,10 @@ static int serialize_showchan(struct ast_channel *c, char *buf, size_t size)
                S_OR(ast_channel_dialed(c)->number.str, "(N/A)"),
                S_COR(ast_channel_redirecting(c)->from.number.valid, ast_channel_redirecting(c)->from.number.str, "(N/A)"),
                ast_channel_parkinglot(c),
-               ast_channel_language(c),        
+               ast_channel_language(c),
                ast_state2str(ast_channel_state(c)),
                ast_channel_state(c),
-               ast_channel_rings(c), 
+               ast_channel_rings(c),
                ast_getformatname_multiple(nf, sizeof(nf), ast_channel_nativeformats(c)),
                ast_getformatname(ast_channel_writeformat(c)),
                ast_getformatname(ast_channel_readformat(c)),
@@ -159,7 +159,7 @@ static int serialize_showchan(struct ast_channel *c, char *buf, size_t size)
                min,
                sec,
                ast_channel_internal_bridged_channel(c) ? ast_channel_name(ast_channel_internal_bridged_channel(c)) : "<none>",
-               ast_bridged_channel(c) ? ast_channel_name(ast_bridged_channel(c)) : "<none>", 
+               ast_bridged_channel(c) ? ast_channel_name(ast_bridged_channel(c)) : "<none>",
                ast_channel_context(c),
                ast_channel_exten(c),
                ast_channel_priority(c),
@@ -182,17 +182,15 @@ static int dumpchan_exec(struct ast_channel *chan, const char *data)
        if (!ast_strlen_zero(data))
                level = atoi(data);
 
-       if (option_verbose >= level) {
-               serialize_showchan(chan, info, sizeof(info));
-               pbx_builtin_serialize_variables(chan, &vars);
-               ast_verbose("\n"
-                       "Dumping Info For Channel: %s:\n"
-                       "%s\n"
-                       "Info:\n"
-                       "%s\n"
-                       "Variables:\n"
-                       "%s%s\n", ast_channel_name(chan), line, info, ast_str_buffer(vars), line);
-       }
+       serialize_showchan(chan, info, sizeof(info));
+       pbx_builtin_serialize_variables(chan, &vars);
+       ast_verb(level, "\n"
+                "Dumping Info For Channel: %s:\n"
+                "%s\n"
+                "Info:\n"
+                "%s\n"
+                "Variables:\n"
+                "%s%s\n", ast_channel_name(chan), line, info, ast_str_buffer(vars), line);
 
        return 0;
 }
index b981fb9c86bcadd0c9a03c85fcfdffa7e342023f..394c489912c2279e2fe5da50b24cce05c3231ef7 100644 (file)
@@ -63,7 +63,7 @@ static char *app_log = "Log";
                <syntax>
                        <parameter name="level" required="true">
                                <para>Level must be one of <literal>ERROR</literal>, <literal>WARNING</literal>, <literal>NOTICE</literal>,
-                               <literal>DEBUG</literal>, <literal>VERBOSE</literal> or <literal>DTMF</literal>.</para> 
+                               <literal>DEBUG</literal>, <literal>VERBOSE</literal> or <literal>DTMF</literal>.</para>
                        </parameter>
                        <parameter name="message" required="true">
                                <para>Output text message.</para>
@@ -100,24 +100,8 @@ static int verbose_exec(struct ast_channel *chan, const char *data)
                vsize = 0;
                ast_log(LOG_WARNING, "'%s' is not a verboser number\n", args.level);
        }
-       if (option_verbose >= vsize) {
-               switch (vsize) {
-               case 0:
-                       ast_verb(0, "%s\n", args.msg);
-                       break;
-               case 1:
-                       ast_verb(1, "%s\n", args.msg);
-                       break;
-               case 2:
-                       ast_verb(2, "%s\n", args.msg);
-                       break;
-               case 3:
-                       ast_verb(3, "%s\n", args.msg);
-                       break;
-               default:
-                       ast_verb(4, "%s\n", args.msg);
-               }
-       }
+
+       ast_verb(vsize, "%s\n", args.msg);
 
        return 0;
 }
@@ -171,7 +155,7 @@ static int unload_module(void)
        res = ast_unregister_application(app_verbose);
        res |= ast_unregister_application(app_log);
 
-       return res;     
+       return res;
 }
 
 static int load_module(void)
index 8c6ba9a8f0b010eb26a81dd14f166f9d62dd57a6..41cc714e0519f060b0b0e3542fec7b21d2de57f6 100644 (file)
@@ -10203,7 +10203,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
                        struct ast_format_cap *cap = ast_format_cap_alloc_nolock();
                        struct ast_format tmpfmt;
                        if (!(cap)) {
-                               return RESPONSE_ERR; 
+                               return RESPONSE_ERR;
                        }
                        ast_format_cap_add(cap, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0));
                        chan = misdn_new(ch, AST_STATE_RESERVED, bc->dialed.number, bc->caller.number, cap, NULL, bc->port, bc->channel);
@@ -11232,7 +11232,7 @@ static struct ast_custom_function misdn_cc_function = {
 static int unload_module(void)
 {
        /* First, take us out of the channel loop */
-       ast_log(LOG_VERBOSE, "-- Unregistering mISDN Channel Driver --\n");
+       ast_verb(0, "-- Unregistering mISDN Channel Driver --\n");
 
        misdn_tasks_destroy();
 
index 4493445aec506a5d1e93b6801c6b203cf43c7711..86825f487c0cd078e484bb3cc6b5322dcbe68aee 100644 (file)
@@ -1721,8 +1721,8 @@ static int can_safely_quit(shutdown_nice_t niceness, int restart)
                time_t s, e;
                /* Begin shutdown routine, hanging up active channels */
                ast_begin_shutdown(1);
-               if (option_verbose && ast_opt_console) {
-                       ast_verbose("Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
+               if (ast_opt_console) {
+                       ast_verb(0, "Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
                }
                time(&s);
                for (;;) {
@@ -1738,7 +1738,7 @@ static int can_safely_quit(shutdown_nice_t niceness, int restart)
                if (niceness != SHUTDOWN_REALLY_NICE) {
                        ast_begin_shutdown(0);
                }
-               if (option_verbose && ast_opt_console) {
+               if (ast_opt_console) {
                        ast_verb(0, "Waiting for inactivity to perform %s...\n", restart ? "restart" : "halt");
                }
                for (;;) {
@@ -1753,7 +1753,7 @@ static int can_safely_quit(shutdown_nice_t niceness, int restart)
         * case someone else has taken over the shutdown. */
        ast_mutex_lock(&safe_system_lock);
        if (shuttingdown != niceness) {
-               if (shuttingdown == NOT_SHUTTING_DOWN && option_verbose && ast_opt_console) {
+               if (shuttingdown == NOT_SHUTTING_DOWN && ast_opt_console) {
                        ast_verb(0, "Asterisk %s cancelled.\n", restart ? "restart" : "shutdown");
                }
                ast_mutex_unlock(&safe_system_lock);
@@ -1892,49 +1892,124 @@ static void __remote_quit_handler(int num)
        sig_flags.need_quit = 1;
 }
 
-static const char *fix_header(char *outbuf, int maxout, const char *s, char *cmp)
+static const char *fix_header(char *outbuf, int maxout, const char *s, char level)
 {
-       const char *c;
+       const char *cmp;
+
+       switch (level) {
+       case 0: *outbuf = '\0';
+               return s;
+       case 1: cmp = VERBOSE_PREFIX_1;
+               break;
+       case 2: cmp = VERBOSE_PREFIX_2;
+               break;
+       case 3: cmp = VERBOSE_PREFIX_3;
+               break;
+       default: cmp = VERBOSE_PREFIX_4;
+               break;
+       }
 
        if (!strncmp(s, cmp, strlen(cmp))) {
-               c = s + strlen(cmp);
-               term_color(outbuf, cmp, COLOR_GRAY, 0, maxout);
-               return c;
+               s += strlen(cmp);
        }
-       return NULL;
+       term_color(outbuf, cmp, COLOR_GRAY, 0, maxout);
+
+       return s;
+}
+
+struct console_state_data {
+       int newline;
+       char verbose_line_level;
+};
+
+static int console_state_init(void *ptr)
+{
+       struct console_state_data *state = ptr;
+       state->newline = 1;
+       state->verbose_line_level = 0;
+       return 0;
 }
 
+AST_THREADSTORAGE_CUSTOM(console_state, console_state_init, ast_free_ptr);
+
 /* These gymnastics are due to platforms which designate char as unsigned by
  * default. Level is the negative character -- offset by 1, because \0 is the
  * EOS delimiter. */
 #define VERBOSE_MAGIC2LEVEL(x) (((char) -*(signed char *) (x)) - 1)
 #define VERBOSE_HASMAGIC(x)    (*(signed char *) (x) < 0)
 
-static void console_verboser(const char *s)
+static int console_log_verbose(const char *s)
 {
-       char tmp[80];
-       const char *c = NULL;
-       char level = 0;
+       /* verbose level of 0 evaluates to a magic of -1, 1 to -2, etc...
+          search up to -7 (level = 6) as this is currently the largest
+          level used */
+       static const char find_set[9] = { -1, -2, -3, -4, -5, -6, -7, '\n'};
 
-       if (VERBOSE_HASMAGIC(s)) {
-               level = VERBOSE_MAGIC2LEVEL(s);
-               s++;
-               if (level > option_verbose) {
-                       return;
+       struct console_state_data *state =
+               ast_threadstorage_get(&console_state, sizeof(*state));
+
+       char prefix[80];
+       const char *c = s;
+       int res = 0;
+
+       do {
+               if (VERBOSE_HASMAGIC(s)) {
+                       /* if it has one always use the given line's level,
+                          otherwise we'll use the last line's level */
+                       state->verbose_line_level = VERBOSE_MAGIC2LEVEL(s);
+                       /* move past magic */
+                       s++;
                }
-       }
 
-       if ((c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_4)) ||
-           (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_3)) ||
-           (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_2)) ||
-           (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_1))) {
-               fputs(tmp, stdout);
-               fputs(c, stdout);
-       } else {
-               fputs(s, stdout);
+               c = fix_header(prefix, sizeof(prefix), s,
+                              state->verbose_line_level);
+
+               if (!state->newline) {
+                       /* don't use the prefix if line continuation */
+                       *prefix = '\0';
+               }
+
+               /* for a given line separate on verbose magic and newlines */
+               if (!(s = strpbrk(c, find_set))) {
+                       s = strchr(c, '\0');
+               } else if (*s == '\n') {
+                       ++s;
+               }
+
+               /* check if we should write this line after calculating begin/end
+                  so we process the case of a higher level line embedded within
+                  two lower level lines */
+               if (state->verbose_line_level > option_verbose) {
+                       continue;
+               }
+
+               state->newline = *(s - 1) == '\n';
+               if (!ast_strlen_zero(prefix)) {
+                       fputs(prefix, stdout);
+               }
+
+               fwrite(c, sizeof(char), s - c, stdout);
+
+               if (!res) {
+                       /* if at least some info has been written
+                          we'll want to return true */
+                       res = 1;
+               }
+               c = s;
+       } while (*s);
+
+       if (res) {
+               fflush(stdout);
        }
 
-       fflush(stdout);
+       return res;
+}
+
+static void console_verboser(const char *s)
+{
+       if (!console_log_verbose(s)) {
+               return;
+       }
 
        /* Wake up a poll()ing console */
        if (ast_opt_console && consolethread != AST_PTHREADT_NULL) {
@@ -2342,23 +2417,6 @@ static struct ast_cli_entry cli_asterisk[] = {
 #endif /* ! LOW_MEMORY */
 };
 
-struct el_read_char_state_struct {
-       unsigned int line_full:1;
-       unsigned int prev_line_full:1;
-       char prev_line_verbosity;
-};
-
-static int el_read_char_state_init(void *ptr)
-{
-       struct el_read_char_state_struct *state = ptr;
-       state->line_full = 1;
-       state->prev_line_full = 1;
-       state->prev_line_verbosity = 0;
-       return 0;
-}
-
-AST_THREADSTORAGE_CUSTOM(el_read_char_state, el_read_char_state_init, ast_free_ptr);
-
 static int ast_el_read_char(EditLine *editline, char *cp)
 {
        int num_read = 0;
@@ -2368,7 +2426,6 @@ static int ast_el_read_char(EditLine *editline, char *cp)
        int max;
 #define EL_BUF_SIZE 512
        char buf[EL_BUF_SIZE];
-       struct el_read_char_state_struct *state = ast_threadstorage_get(&el_read_char_state, sizeof(*state));
 
        for (;;) {
                max = 1;
@@ -2398,8 +2455,6 @@ static int ast_el_read_char(EditLine *editline, char *cp)
                        }
                }
                if (fds[0].revents) {
-                       char level = 0;
-                       char *curline = buf, *nextline;
                        res = read(ast_consock, buf, sizeof(buf) - 1);
                        /* if the remote side disappears exit */
                        if (res < 1) {
@@ -2439,33 +2494,7 @@ static int ast_el_read_char(EditLine *editline, char *cp)
                                }
                        }
 
-                       do {
-                               state->prev_line_full = state->line_full;
-                               if ((nextline = strchr(curline, '\n'))) {
-                                       state->line_full = 1;
-                                       nextline++;
-                               } else {
-                                       state->line_full = 0;
-                                       nextline = strchr(curline, '\0');
-                               }
-
-                               if (state->prev_line_full && VERBOSE_HASMAGIC(curline)) {
-                                       level = VERBOSE_MAGIC2LEVEL(curline);
-                                       curline++;
-                               } else if (state->prev_line_full && !VERBOSE_HASMAGIC(curline)) {
-                                       /* Non-verbose output */
-                                       level = 0;
-                               } else {
-                                       level = state->prev_line_verbosity;
-                               }
-                               if ((!state->prev_line_full && state->prev_line_verbosity <= option_verbose) || (state->prev_line_full && level <= option_verbose)) {
-                                       if (write(STDOUT_FILENO, curline, nextline - curline) < 0) {
-                                       }
-                               }
-
-                               state->prev_line_verbosity = level;
-                               curline = nextline;
-                       } while (!ast_strlen_zero(curline));
+                       console_log_verbose(buf);
 
                        if ((res < EL_BUF_SIZE - 1) && ((buf[res-1] == '\n') || (buf[res-2] == '\n'))) {
                                *cp = CC_REFRESH;
@@ -3477,7 +3506,7 @@ static void env_init(void)
 
 static void print_intro_message(const char *runuser, const char *rungroup)
 {
-       if (ast_opt_console || option_verbose || (ast_opt_remote && !ast_opt_exec)) {
+       if (ast_opt_console || option_verbose || (ast_opt_remote && !ast_opt_exec)) {
                if (ast_register_verbose(console_verboser)) {
                        fprintf(stderr, "Unable to register console verboser?\n");
                        return;
@@ -3969,8 +3998,8 @@ int main(int argc, char *argv[])
 
        print_intro_message(runuser, rungroup);
 
-       if (ast_opt_console && !option_verbose) {
-               ast_verbose("[ Initializing Custom Configuration Options ]\n");
+       if (ast_opt_console) {
+               ast_verb(0, "[ Initializing Custom Configuration Options ]\n");
        }
        /* custom config setup */
        register_config_cli();
index c97426b7305db4c013f6bbf13520d9b4281f4826..3e824752f9e22a8102537c753ba8a10b529e0deb 100644 (file)
@@ -1145,7 +1145,7 @@ static void *bridge_call_thread(void *data)
 
        if (tobj->return_to_pbx) {
                if (!ast_check_hangup(tobj->peer)) {
-                       ast_log(LOG_VERBOSE, "putting peer %s into PBX again\n", ast_channel_name(tobj->peer));
+                       ast_verb(0, "putting peer %s into PBX again\n", ast_channel_name(tobj->peer));
                        if (ast_pbx_start(tobj->peer)) {
                                ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", ast_channel_name(tobj->peer));
                                ast_autoservice_chan_hangup_peer(tobj->chan, tobj->peer);
@@ -1154,7 +1154,7 @@ static void *bridge_call_thread(void *data)
                        ast_autoservice_chan_hangup_peer(tobj->chan, tobj->peer);
                }
                if (!ast_check_hangup(tobj->chan)) {
-                       ast_log(LOG_VERBOSE, "putting chan %s into PBX again\n", ast_channel_name(tobj->chan));
+                       ast_verb(0, "putting chan %s into PBX again\n", ast_channel_name(tobj->chan));
                        if (ast_pbx_start(tobj->chan)) {
                                ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", ast_channel_name(tobj->chan));
                                ast_hangup(tobj->chan);
@@ -9082,4 +9082,3 @@ int ast_features_init(void)
 
        return res;
 }
-
index 1100f785d57fe79bdd58f9346b138f223026d9e2..ee0070a27348fd00820c108e89247d164f26ae51 100644 (file)
@@ -1542,7 +1542,11 @@ void ast_log(int level, const char *file, int line, const char *function, const
        callid = ast_read_threadstorage_callid();
 
        va_start(ap, fmt);
-       ast_log_full(level, file, line, function, callid, fmt, ap);
+       if (level == __LOG_VERBOSE) {
+               __ast_verbose_ap(file, line, function, 0, callid, fmt, ap);
+       } else {
+               ast_log_full(level, file, line, function, callid, fmt, ap);
+       }
        va_end(ap);
 
        if (callid) {
@@ -2005,4 +2009,3 @@ void ast_logger_unregister_level(const char *name)
                AST_RWLIST_UNLOCK(&logchannels);
        }
 }
-