[PATCH] libosmocore[master]: logging: color the log level

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Tue Jan 16 02:34:08 UTC 2018


Review at  https://gerrit.osmocom.org/5810

logging: color the log level

When log_set_use_color() is enabled, color the log level string according to
the log level. The log line before and after the log level is printed in the
category's color.

ERROR and FATAL are red, NOTICE is yellow, INFO is green and DEBUG is blue.

The default behavior remains unchanged.

Change-Id: If2e52ae9ab83e538e04321c338e3fdffb2c7f9d3
---
M src/logging.c
1 file changed, 26 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/10/5810/1

diff --git a/src/logging.c b/src/logging.c
index 80fc7d2..c01294c 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -298,6 +298,23 @@
 	return NULL;
 }
 
+static const struct value_string level_colors[] = {
+	{ LOGL_DEBUG, "\033[1;34m" },
+	{ LOGL_INFO, "\033[1;32m" },
+	{ LOGL_NOTICE, "\033[1;33m" },
+	{ LOGL_ERROR, "\033[1;31m" },
+	{ LOGL_FATAL, "\033[1;31m" },
+	{ 0, NULL }
+};
+
+static const char *level_color(int level)
+{
+	const char *c = get_value_string_or_null(level_colors, level);
+	if (!c)
+		return get_value_string(level_colors, LOGL_FATAL);
+	return c;
+}
+
 const char* log_category_name(int subsys)
 {
 	if (subsys < osmo_log_info->num_cat)
@@ -312,12 +329,13 @@
 {
 	char buf[4096];
 	int ret, len = 0, offset = 0, rem = sizeof(buf);
+	const char *c_subsys = NULL;
 
 	/* are we using color */
 	if (target->use_color) {
-		const char *c = color(subsys);
-		if (c) {
-			ret = snprintf(buf + offset, rem, "%s", c);
+		c_subsys = color(subsys);
+		if (c_subsys) {
+			ret = snprintf(buf + offset, rem, c_subsys);
 			if (ret < 0)
 				goto err;
 			OSMO_SNPRINTF_RET(ret, rem, offset, len);
@@ -354,7 +372,11 @@
 			OSMO_SNPRINTF_RET(ret, rem, offset, len);
 		}
 		if (target->print_level) {
-			ret = snprintf(buf + offset, rem, "%s ", log_level_str(level));
+			ret = snprintf(buf + offset, rem, "%s%s%s%s ",
+				       target->use_color ? level_color(level) : "",
+				       log_level_str(level),
+				       target->use_color ? "\033[0;m" : "",
+				       c_subsys ? c_subsys : "");
 			if (ret < 0)
 				goto err;
 			OSMO_SNPRINTF_RET(ret, rem, offset, len);

-- 
To view, visit https://gerrit.osmocom.org/5810
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If2e52ae9ab83e538e04321c338e3fdffb2c7f9d3
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list