<p>George Joseph has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/14737">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">logger.c: Added a new log formatter called "debug"<br><br>Added a new log formatter called "debug" that always prints<br>function and line number if available and never prints color<br>control character. Most suitable for file output but can be<br>used for other channels as well.<br><br>You use it in logger.conf like so:<br>debug => [debug]debug<br>console => [debug]error,warning,debug,notice,pjsip_history<br>messages => [debug]warning,error,verbose<br><br>Change-Id: I4fdfe4089f66ce2f9cb29f3005522090dbb5243d<br>---<br>M configs/samples/logger.conf.sample<br>A doc/CHANGES-staging/logger_format.txt<br>M main/logger.c<br>3 files changed, 81 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/37/14737/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/configs/samples/logger.conf.sample b/configs/samples/logger.conf.sample</span><br><span>index 9c7dcca..9f673ff 100644</span><br><span>--- a/configs/samples/logger.conf.sample</span><br><span>+++ b/configs/samples/logger.conf.sample</span><br><span>@@ -109,6 +109,9 @@</span><br><span> ; levels, and is enclosed in square brackets. Valid formatters are:</span><br><span> ; - [default] - The default formatter, this outputs log messages using a</span><br><span> ; human readable format.</span><br><span style="color: hsl(120, 100%, 40%);">+; - [debug] - The debug formatter, this outputs log messages using a</span><br><span style="color: hsl(120, 100%, 40%);">+; human readable format with the addition of function name</span><br><span style="color: hsl(120, 100%, 40%);">+; and line number.</span><br><span> ; - [json] - Log the output in JSON. Note that JSON formatted log entries,</span><br><span> ; if specified for a logger type of 'console', will be formatted</span><br><span> ; per the 'default' formatter for log messages of type VERBOSE.</span><br><span>diff --git a/doc/CHANGES-staging/logger_format.txt b/doc/CHANGES-staging/logger_format.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..a373cd8</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/logger_format.txt</span><br><span>@@ -0,0 +1,11 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: logger</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Added a new log formatter called "debug" that always prints</span><br><span style="color: hsl(120, 100%, 40%);">+function and line number if available and never prints color</span><br><span style="color: hsl(120, 100%, 40%);">+control character. Most suitable for file output but can be</span><br><span style="color: hsl(120, 100%, 40%);">+used for other channels as well.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+You use it in logger.conf like so:</span><br><span style="color: hsl(120, 100%, 40%);">+debug => [debug]debug</span><br><span style="color: hsl(120, 100%, 40%);">+console => [debug]error,warning,debug,notice,pjsip_history</span><br><span style="color: hsl(120, 100%, 40%);">+messages => [debug]warning,error,verbose</span><br><span>diff --git a/main/logger.c b/main/logger.c</span><br><span>index 8815301..06d671d 100644</span><br><span>--- a/main/logger.c</span><br><span>+++ b/main/logger.c</span><br><span>@@ -423,6 +423,71 @@</span><br><span> .format_log = format_log_default,</span><br><span> };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static int format_log_debug(struct logchannel *chan, struct logmsg *msg, char *buf, size_t size)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char call_identifier_str[13];</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (msg->callid) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", msg->callid);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ call_identifier_str[0] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (chan->type) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case LOGTYPE_SYSLOG:</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, size, "%s[%d]%s: %s:%d in %s: %s",</span><br><span style="color: hsl(120, 100%, 40%);">+ levels[msg->level], msg->lwp, call_identifier_str, msg->file,</span><br><span style="color: hsl(120, 100%, 40%);">+ msg->line, msg->function, msg->message);</span><br><span style="color: hsl(120, 100%, 40%);">+ term_strip(buf, buf, size);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case LOGTYPE_FILE:</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, size, "[%s] %s[%d]%s %s: %s:%d %s",</span><br><span style="color: hsl(120, 100%, 40%);">+ msg->date, msg->level_name, msg->lwp, call_identifier_str,</span><br><span style="color: hsl(120, 100%, 40%);">+ msg->file, msg->function, msg->line, msg->message);</span><br><span style="color: hsl(120, 100%, 40%);">+ term_strip(buf, buf, size);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case LOGTYPE_CONSOLE:</span><br><span style="color: hsl(120, 100%, 40%);">+ {</span><br><span style="color: hsl(120, 100%, 40%);">+ char linestr[32];</span><br><span style="color: hsl(120, 100%, 40%);">+ int has_file = !ast_strlen_zero(msg->file);</span><br><span style="color: hsl(120, 100%, 40%);">+ int has_line = (msg->line > 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ int has_func = !ast_strlen_zero(msg->function);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Verbose messages are interpreted by console channels in their own</span><br><span style="color: hsl(120, 100%, 40%);">+ * special way</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (msg->level == __LOG_VERBOSE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return logger_add_verbose_magic(msg, buf, size);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Turn the numerical line number into a string */</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(linestr, sizeof(linestr), "%d", msg->line);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Build string to print out */</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, size, "[%s] %s[%d]%s: %s%s%s%s%s%s%s",</span><br><span style="color: hsl(120, 100%, 40%);">+ msg->date,</span><br><span style="color: hsl(120, 100%, 40%);">+ msg->level_name,</span><br><span style="color: hsl(120, 100%, 40%);">+ msg->lwp,</span><br><span style="color: hsl(120, 100%, 40%);">+ call_identifier_str,</span><br><span style="color: hsl(120, 100%, 40%);">+ has_file ? msg->file : "",</span><br><span style="color: hsl(120, 100%, 40%);">+ has_file ? ":" : "",</span><br><span style="color: hsl(120, 100%, 40%);">+ has_line ? linestr : "",</span><br><span style="color: hsl(120, 100%, 40%);">+ has_line ? " " : "",</span><br><span style="color: hsl(120, 100%, 40%);">+ has_func ? msg->function : "",</span><br><span style="color: hsl(120, 100%, 40%);">+ has_func ? ": " : "",</span><br><span style="color: hsl(120, 100%, 40%);">+ msg->message);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct logformatter logformatter_debug = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .name = "debug",</span><br><span style="color: hsl(120, 100%, 40%);">+ .format_log = format_log_debug,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static void make_components(struct logchannel *chan)</span><br><span> {</span><br><span> char *w;</span><br><span>@@ -449,6 +514,8 @@</span><br><span> memcpy(&chan->formatter, &logformatter_json, sizeof(chan->formatter));</span><br><span> } else if (!strcasecmp(formatter_name, "default")) {</span><br><span> memcpy(&chan->formatter, &logformatter_default, sizeof(chan->formatter));</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(formatter_name, "debug")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ memcpy(&chan->formatter, &logformatter_debug, sizeof(chan->formatter));</span><br><span> } else {</span><br><span> fprintf(stderr, "Logger Warning: Unknown formatter definition %s for %s in logger.conf; using 'default'\n",</span><br><span> formatter_name, chan->filename);</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/14737">change 14737</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/14737"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: certified/16.8 </div>
<div style="display:none"> Gerrit-Change-Id: I4fdfe4089f66ce2f9cb29f3005522090dbb5243d </div>
<div style="display:none"> Gerrit-Change-Number: 14737 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>