<p>Friendly Automation <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/14779">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, but someone else must approve
Kevin Harwell: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved
Friendly Automation: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">logger.c: Added a new log formatter called "plain"<br><br>Added a new log formatter called "plain" that always prints<br>file, function and line number if available (even for verbose<br>messages) and never prints color control characters. It also<br>doesn't apply any special formatting for verbose messages.<br>Most suitable for file output but can be used for other channels<br>as well.<br><br>You use it in logger.conf like so:<br>debug => [plain]debug<br>console => [plain]error,warning,debug,notice,pjsip_history<br>messages => [plain]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, 68 insertions(+), 0 deletions(-)<br><br></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..d917e63 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,10 @@</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%);">+; - [plain] - The plain 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. No color escape codes are ever printed</span><br><span style="color: hsl(120, 100%, 40%);">+ nor are verbose messages treated specially.</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..58d864d</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/logger_format.txt</span><br><span>@@ -0,0 +1,12 @@</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 "plain" that always prints</span><br><span style="color: hsl(120, 100%, 40%);">+file, function and line number if available (even for verbose</span><br><span style="color: hsl(120, 100%, 40%);">+messages) and never prints color control characters. Most</span><br><span style="color: hsl(120, 100%, 40%);">+suitable for file output but can be used for other channels</span><br><span style="color: hsl(120, 100%, 40%);">+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 => [plain]debug</span><br><span style="color: hsl(120, 100%, 40%);">+console => [plain]error,warning,debug,notice,pjsip_history</span><br><span style="color: hsl(120, 100%, 40%);">+messages => [plain]warning,error,verbose</span><br><span>diff --git a/main/logger.c b/main/logger.c</span><br><span>index 8894b05..d7d1807 100644</span><br><span>--- a/main/logger.c</span><br><span>+++ b/main/logger.c</span><br><span>@@ -423,6 +423,56 @@</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_plain(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%);">+ 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%);">+ 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%);">+ case LOGTYPE_CONSOLE:</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%);">+ term_strip(buf, buf, size);</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_plain = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .name = "plain",</span><br><span style="color: hsl(120, 100%, 40%);">+ .format_log = format_log_plain,</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 +499,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, "plain")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ memcpy(&chan->formatter, &logformatter_plain, 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/+/14779">change 14779</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/+/14779"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I4fdfe4089f66ce2f9cb29f3005522090dbb5243d </div>
<div style="display:none"> Gerrit-Change-Number: 14779 </div>
<div style="display:none"> Gerrit-PatchSet: 6 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>