diff --git a/include/asterisk/logger.h b/include/asterisk/logger.h index 6ab55f76da..d823ed4e44 100644 --- a/include/asterisk/logger.h +++ b/include/asterisk/logger.h @@ -614,20 +614,25 @@ an entry/exit message. To do so, you can use the ast_trace macros... */ /*! * \brief Get the trace level for a module * \param module the name of module * \return the trace level */ unsigned int ast_trace_get_by_module(const char *module); +/*! + * \brief load logger.conf configuration for console socket connections + */ +void ast_init_logger_for_socket_console(void); + #define TRACE_ATLEAST(level) \ (option_trace >= (level) \ || (ast_opt_trace_module \ && ((int)ast_trace_get_by_module(AST_MODULE) >= (level) \ || (int)ast_trace_get_by_module(__FILE__) >= (level)))) /*! * \brief Controls if and when indenting is applied. */ enum ast_trace_indent_type { diff --git a/main/asterisk.c b/main/asterisk.c index 1061fd0b31..431c08e3e2 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -3244,20 +3244,22 @@ static void ast_remotecontrol(char *data) /* No non-verbose output in 60 seconds. */ if (not_written) { break; } } return; } ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid); + ast_init_logger_for_socket_console(); + remotehostname = hostname; if (el_hist == NULL || el == NULL) ast_el_initialize(); ast_el_read_default_histfile(); el_set(el, EL_GETCFN, ast_el_read_char); for (;;) { ebuf = (char *)el_gets(el, &num); diff --git a/main/logger.c b/main/logger.c index 067f9a9919..6105a14da6 100644 --- a/main/logger.c +++ b/main/logger.c @@ -255,21 +255,21 @@ AST_THREADSTORAGE(log_buf); #define LOG_BUF_INIT_SIZE 256 static int format_log_json(struct logchannel *channel, struct logmsg *msg, char *buf, size_t size) { struct ast_json *json; char *str; char call_identifier_str[13]; size_t json_str_len; if (msg->callid) { - snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", msg->callid); + ast_callid_strnprint(call_identifier_str, sizeof(call_identifier_str), msg->callid); } else { call_identifier_str[0] = '\0'; } json = ast_json_pack("{s: s, s: s, " "s: {s: i, s: s} " "s: {s: {s: s, s: s, s: i}, " "s: s, s: s} }", "hostname", ast_config_AST_SYSTEM_NAME, "timestamp", msg->date, @@ -358,21 +358,21 @@ static int logger_add_verbose_magic(struct logmsg *logmsg, char *buf, size_t siz snprintf(buf, size, "%s", ast_str_buffer(prefixed)); return 0; } static int format_log_default(struct logchannel *chan, struct logmsg *msg, char *buf, size_t size) { char call_identifier_str[13]; if (msg->callid) { - snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", msg->callid); + ast_callid_strnprint(call_identifier_str, sizeof(call_identifier_str), msg->callid); } else { call_identifier_str[0] = '\0'; } switch (chan->type) { case LOGTYPE_SYSLOG: snprintf(buf, size, "%s[%d]%s: %s:%d in %s: %s", levels[msg->level], msg->lwp, call_identifier_str, msg->file, msg->line, msg->function, msg->message); term_strip(buf, buf, size); @@ -426,21 +426,21 @@ static struct logformatter logformatter_default = { static int format_log_plain(struct logchannel *chan, struct logmsg *msg, char *buf, size_t size) { char call_identifier_str[13]; char linestr[32]; int has_file = !ast_strlen_zero(msg->file); int has_line = (msg->line > 0); int has_func = !ast_strlen_zero(msg->function); if (msg->callid) { - snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", msg->callid); + ast_callid_strnprint(call_identifier_str, sizeof(call_identifier_str), msg->callid); } else { call_identifier_str[0] = '\0'; } switch (chan->type) { case LOGTYPE_SYSLOG: snprintf(buf, size, "%s[%d]%s: %s:%d in %s: %s", levels[msg->level], msg->lwp, call_identifier_str, msg->file, msg->line, msg->function, msg->message); term_strip(buf, buf, size); @@ -672,20 +672,42 @@ static struct logchannel *make_logchannel(const char *channel, const char *compo ast_build_machine, ast_build_os, ast_build_date); fflush(chan->fileptr); } chan->type = LOGTYPE_FILE; } make_components(chan); return chan; } +void ast_init_logger_for_socket_console(void) +{ + struct ast_config *cfg; + const char *s; + struct ast_flags config_flags = { 0 }; + + if (!(cfg = ast_config_load2("logger.conf", "logger", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) { + return; + } + + /* Set defaults for THIS session*/ + display_callids = 1; + + if ((s = ast_variable_retrieve(cfg, "general", "display_callids"))) { + display_callids = ast_true(s); + } + + if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) { + ast_copy_string(dateformat, s, sizeof(dateformat)); + } +} + /*! * \brief Read config, setup channels. * \param altconf Alternate configuration file to read. * * \pre logchannels list is write locked * * \retval 0 Success * \retval -1 No config found or Failed */ static int init_logger_chain(const char *altconf) @@ -1670,32 +1692,41 @@ static struct logmsg * __attribute__((format(printf, 7, 0))) format_log_message_ /* If the build failed, then abort and free this structure */ if (res == AST_DYNSTR_BUILD_FAILED) { return NULL; } /* Create a new logging message */ if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128))) { return NULL; } - /* Copy string over */ - ast_string_field_set(logmsg, message, ast_str_buffer(buf)); - /* Set type */ if (level == __LOG_VERBOSE) { logmsg->type = LOGMSG_VERBOSE; } else { logmsg->type = LOGMSG_NORMAL; } if (display_callids && callid) { + struct ast_str *with_call_id = ast_str_create(100); logmsg->callid = callid; + + /* !!! There should be a better way... The log formatters should have already put the call-id in, but they haven't at this point */ + ast_str_set(&with_call_id, 0, "[C-%08x] %s", logmsg->callid, ast_str_buffer(buf)); + + /* Copy string over */ + ast_string_field_set(logmsg, message, ast_str_buffer(with_call_id)); + ast_free(with_call_id); + } + else { + /* Copy string over */ + ast_string_field_set(logmsg, message, ast_str_buffer(buf)); } /* Create our date/time */ ast_localtime(&now, &tm, NULL); ast_strftime(datestring, sizeof(datestring), dateformat, &tm); ast_string_field_set(logmsg, date, datestring); /* Copy over data */ logmsg->level = level; logmsg->sublevel = sublevel;