[Asterisk-code-review] logger.c: Added a new log formatter called "debug" (asterisk[certified/16.8])

George Joseph asteriskteam at digium.com
Thu Aug 20 08:39:29 CDT 2020


George Joseph has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/14737 )


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

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

Added a new log formatter called "debug" that always prints
function and line number if available and never prints color
control character.  Most suitable for file output but can be
used for other channels as well.

You use it in logger.conf like so:
debug => [debug]debug
console => [debug]error,warning,debug,notice,pjsip_history
messages => [debug]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, 81 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/37/14737/1

diff --git a/configs/samples/logger.conf.sample b/configs/samples/logger.conf.sample
index 9c7dcca..9f673ff 100644
--- a/configs/samples/logger.conf.sample
+++ b/configs/samples/logger.conf.sample
@@ -109,6 +109,9 @@
 ; levels, and is enclosed in square brackets. Valid formatters are:
 ;   - [default] - The default formatter, this outputs log messages using a
 ;                 human readable format.
+;   - [debug]   - The debug formatter, this outputs log messages using a
+;                 human readable format with the addition of function name
+;                 and line number.
 ;   - [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..a373cd8
--- /dev/null
+++ b/doc/CHANGES-staging/logger_format.txt
@@ -0,0 +1,11 @@
+Subject: logger
+
+Added a new log formatter called "debug" that always prints
+function and line number if available and never prints color
+control character.  Most suitable for file output but can be
+used for other channels as well.
+
+You use it in logger.conf like so:
+debug => [debug]debug
+console => [debug]error,warning,debug,notice,pjsip_history
+messages => [debug]warning,error,verbose
diff --git a/main/logger.c b/main/logger.c
index 8815301..06d671d 100644
--- a/main/logger.c
+++ b/main/logger.c
@@ -423,6 +423,71 @@
 	.format_log = format_log_default,
 };
 
+static int format_log_debug(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);
+	} 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:
+		snprintf(buf, size, "[%s] %s[%d]%s %s: %s:%d %s",
+		      msg->date, msg->level_name, msg->lwp, call_identifier_str,
+		      msg->file, msg->function, msg->line, msg->message);
+		term_strip(buf, buf, size);
+		break;
+	case LOGTYPE_CONSOLE:
+		{
+			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);
+
+			/*
+			 * Verbose messages are interpreted by console channels in their own
+			 * special way
+			 */
+			if (msg->level == __LOG_VERBOSE) {
+				return logger_add_verbose_magic(msg, buf, size);
+			}
+
+			/* 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);
+		}
+		break;
+	}
+
+	return 0;
+}
+
+static struct logformatter logformatter_debug = {
+	.name = "debug",
+	.format_log = format_log_debug,
+};
+
 static void make_components(struct logchannel *chan)
 {
 	char *w;
@@ -449,6 +514,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, "debug")) {
+				memcpy(&chan->formatter, &logformatter_debug, 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/+/14737
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: certified/16.8
Gerrit-Change-Id: I4fdfe4089f66ce2f9cb29f3005522090dbb5243d
Gerrit-Change-Number: 14737
Gerrit-PatchSet: 1
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200820/b7673abb/attachment-0001.html>


More information about the asterisk-code-review mailing list