[Asterisk-code-review] logger.c: Added a new log formatter called "plain" (asterisk[master])

Friendly Automation asteriskteam at digium.com
Fri Aug 28 12:29:38 CDT 2020


Friendly Automation has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/14779 )

Change subject: logger.c: Added a new log formatter called "plain"
......................................................................

logger.c: Added a new log formatter called "plain"

Added a new log formatter called "plain" that always prints
file, function and line number if available (even for verbose
messages) and never prints color control characters.  It also
doesn't apply any special formatting for verbose messages.
Most suitable for file output but can be used for other channels
as well.

You use it in logger.conf like so:
debug => [plain]debug
console => [plain]error,warning,debug,notice,pjsip_history
messages => [plain]warning,error,verbose

Change-Id: I4fdfe4089f66ce2f9cb29f3005522090dbb5243d
---
M configs/samples/logger.conf.sample
A doc/CHANGES-staging/logger_format.txt
M main/logger.c
3 files changed, 68 insertions(+), 0 deletions(-)

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



diff --git a/configs/samples/logger.conf.sample b/configs/samples/logger.conf.sample
index 9c7dcca..d917e63 100644
--- a/configs/samples/logger.conf.sample
+++ b/configs/samples/logger.conf.sample
@@ -109,6 +109,10 @@
 ; levels, and is enclosed in square brackets. Valid formatters are:
 ;   - [default] - The default formatter, this outputs log messages using a
 ;                 human readable format.
+;   - [plain]   - The plain formatter, this outputs log messages using a
+;                 human readable format with the addition of function name
+;                 and line number. No color escape codes are ever printed
+                  nor are verbose messages treated specially.
 ;   - [json]    - Log the output in JSON. Note that JSON formatted log entries,
 ;                 if specified for a logger type of 'console', will be formatted
 ;                 per the 'default' formatter for log messages of type VERBOSE.
diff --git a/doc/CHANGES-staging/logger_format.txt b/doc/CHANGES-staging/logger_format.txt
new file mode 100644
index 0000000..58d864d
--- /dev/null
+++ b/doc/CHANGES-staging/logger_format.txt
@@ -0,0 +1,12 @@
+Subject: logger
+
+Added a new log formatter called "plain" that always prints
+file, function and line number if available (even for verbose
+messages) and never prints color control characters.  Most
+suitable for file output but can be used for other channels
+as well.
+
+You use it in logger.conf like so:
+debug => [plain]debug
+console => [plain]error,warning,debug,notice,pjsip_history
+messages => [plain]warning,error,verbose
diff --git a/main/logger.c b/main/logger.c
index 8894b05..d7d1807 100644
--- a/main/logger.c
+++ b/main/logger.c
@@ -423,6 +423,56 @@
 	.format_log = format_log_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);
+	} 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);
+		break;
+	case LOGTYPE_FILE:
+	case LOGTYPE_CONSOLE:
+		/* Turn the numerical line number into a string */
+		snprintf(linestr, sizeof(linestr), "%d", msg->line);
+		/* Build string to print out */
+		snprintf(buf, size, "[%s] %s[%d]%s: %s%s%s%s%s%s%s",
+			msg->date,
+			msg->level_name,
+			msg->lwp,
+			call_identifier_str,
+			has_file ? msg->file : "",
+			has_file ? ":" : "",
+			has_line ? linestr : "",
+			has_line ? " " : "",
+			has_func ? msg->function : "",
+			has_func ? ": " : "",
+			msg->message);
+		term_strip(buf, buf, size);
+		break;
+	}
+
+	return 0;
+}
+
+static struct logformatter logformatter_plain = {
+	.name = "plain",
+	.format_log = format_log_plain,
+};
+
 static void make_components(struct logchannel *chan)
 {
 	char *w;
@@ -449,6 +499,8 @@
 				memcpy(&chan->formatter, &logformatter_json, sizeof(chan->formatter));
 			} else if (!strcasecmp(formatter_name, "default")) {
 				memcpy(&chan->formatter, &logformatter_default, sizeof(chan->formatter));
+			} else if (!strcasecmp(formatter_name, "plain")) {
+				memcpy(&chan->formatter, &logformatter_plain, sizeof(chan->formatter));
 			} else {
 				fprintf(stderr, "Logger Warning: Unknown formatter definition %s for %s in logger.conf; using 'default'\n",
 					formatter_name, chan->filename);

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/14779
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I4fdfe4089f66ce2f9cb29f3005522090dbb5243d
Gerrit-Change-Number: 14779
Gerrit-PatchSet: 6
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200828/c2eb831c/attachment.html>


More information about the asterisk-code-review mailing list