diff mbox series

logging: colorize terminal output log messages

Message ID 20180223143939.25898-1-christian.storm@siemens.com
State Accepted
Headers show
Series logging: colorize terminal output log messages | expand

Commit Message

Storm, Christian Feb. 23, 2018, 2:39 p.m. UTC
Make the notification system's console notifier use ANSI
escape sequences to colorize the output of the messages
according to their level, however, only if stdout as well
as stderr is output to a terminal.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 core/notifier.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

Comments

Stefano Babic Feb. 23, 2018, 3:20 p.m. UTC | #1
On 23/02/2018 15:39, Christian Storm wrote:
> Make the notification system's console notifier use ANSI
> escape sequences to colorize the output of the messages
> according to their level, however, only if stdout as well
> as stderr is output to a terminal.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  core/notifier.c | 26 ++++++++++++++++++++------
>  1 file changed, 20 insertions(+), 6 deletions(-)
> 
> diff --git a/core/notifier.c b/core/notifier.c
> index 48f1575..14a6052 100644
> --- a/core/notifier.c
> +++ b/core/notifier.c
> @@ -60,6 +60,7 @@ static struct sockaddr_un notify_client;
>  static struct sockaddr_un notify_server;
>  static int notifyfd = -1;
>  static bool console_priority_prefix = false;
> +static bool console_ansi_colors = false;
>  
>  /*
>   * This allows to extend the list of notifier.
> @@ -149,24 +150,34 @@ static void console_notifier (RECOVERY_STATUS status, int error, int level, cons
>  
>  	switch (level) {
>  	case ERRORLEVEL:
> -		fprintf(stderr, "%s[ERROR]", console_priority_prefix ? "<3>" : "");
> +		fprintf(stderr, "%s%s[ERROR]",
> +				console_ansi_colors ? "\033[01;31m" : "",
> +				console_priority_prefix ? "<3>" : "");
>  		break;
>  	case WARNLEVEL:
> -		fprintf(stdout, "%s[WARN ]", console_priority_prefix ? "<4>" : "");
> +		fprintf(stdout, "%s%s[WARN ]",
> +				console_ansi_colors ? "\033[01;33m" : "",
> +				console_priority_prefix ? "<4>" : "");
>  		break;
>  	case INFOLEVEL:
> -		fprintf(stdout, "%s[INFO ]", console_priority_prefix ? "<6>" : "");
> +		fprintf(stdout, "%s%s[INFO ]",
> +				console_ansi_colors ? "\033[01;32m" : "",
> +				console_priority_prefix ? "<6>" : "");
>  		break;
>  	case DEBUGLEVEL:
> -		fprintf(stdout, "%s[DEBUG]", console_priority_prefix ? "<7>" : "");
> +		fprintf(stdout, "%s%s[DEBUG]",
> +				console_ansi_colors ? "\033[01;30m" : "",
> +				console_priority_prefix ? "<7>" : "");
>  		break;
>  	case TRACELEVEL:
> -		fprintf(stdout, "%s[TRACE]", console_priority_prefix ? "<7>" : "");
> +		fprintf(stdout, "%s%s[TRACE]",
> +				console_ansi_colors ? "\033[01;30m" : "",
> +				console_priority_prefix ? "<7>" : "");
>  		break;
>  	}
>  
>  	fprintf(level == ERRORLEVEL ? stderr : stdout,
> -			" : %s %s\n", current, msg ? msg : "");
> +			" : %s %s%s\n", current, msg ? msg : "", console_ansi_colors ? "\x1b[0m" : "");
>  	fflush(stdout);
>  }
>  
> @@ -275,6 +286,9 @@ void notify_init(void)
>  	}
>  #endif
>  
> +	console_ansi_colors = (isatty(fileno(stdout)) && isatty(fileno(stderr)))
> +		? true : false;
> +
>  	if (pid == getpid()) {
>  		char buf[60];
>  		snprintf(buf, sizeof(buf), "Notify%d", pid);
> 


Nice !

Acked-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
Stefano Babic March 2, 2018, 1:56 p.m. UTC | #2
On 23/02/2018 16:20, Stefano Babic wrote:
> On 23/02/2018 15:39, Christian Storm wrote:
>> Make the notification system's console notifier use ANSI
>> escape sequences to colorize the output of the messages
>> according to their level, however, only if stdout as well
>> as stderr is output to a terminal.
>>
>> Signed-off-by: Christian Storm <christian.storm@siemens.com>
>> ---
>>  core/notifier.c | 26 ++++++++++++++++++++------
>>  1 file changed, 20 insertions(+), 6 deletions(-)
>>
>> diff --git a/core/notifier.c b/core/notifier.c
>> index 48f1575..14a6052 100644
>> --- a/core/notifier.c
>> +++ b/core/notifier.c
>> @@ -60,6 +60,7 @@ static struct sockaddr_un notify_client;
>>  static struct sockaddr_un notify_server;
>>  static int notifyfd = -1;
>>  static bool console_priority_prefix = false;
>> +static bool console_ansi_colors = false;
>>  
>>  /*
>>   * This allows to extend the list of notifier.
>> @@ -149,24 +150,34 @@ static void console_notifier (RECOVERY_STATUS status, int error, int level, cons
>>  
>>  	switch (level) {
>>  	case ERRORLEVEL:
>> -		fprintf(stderr, "%s[ERROR]", console_priority_prefix ? "<3>" : "");
>> +		fprintf(stderr, "%s%s[ERROR]",
>> +				console_ansi_colors ? "\033[01;31m" : "",
>> +				console_priority_prefix ? "<3>" : "");
>>  		break;
>>  	case WARNLEVEL:
>> -		fprintf(stdout, "%s[WARN ]", console_priority_prefix ? "<4>" : "");
>> +		fprintf(stdout, "%s%s[WARN ]",
>> +				console_ansi_colors ? "\033[01;33m" : "",
>> +				console_priority_prefix ? "<4>" : "");
>>  		break;
>>  	case INFOLEVEL:
>> -		fprintf(stdout, "%s[INFO ]", console_priority_prefix ? "<6>" : "");
>> +		fprintf(stdout, "%s%s[INFO ]",
>> +				console_ansi_colors ? "\033[01;32m" : "",
>> +				console_priority_prefix ? "<6>" : "");
>>  		break;
>>  	case DEBUGLEVEL:
>> -		fprintf(stdout, "%s[DEBUG]", console_priority_prefix ? "<7>" : "");
>> +		fprintf(stdout, "%s%s[DEBUG]",
>> +				console_ansi_colors ? "\033[01;30m" : "",
>> +				console_priority_prefix ? "<7>" : "");
>>  		break;
>>  	case TRACELEVEL:
>> -		fprintf(stdout, "%s[TRACE]", console_priority_prefix ? "<7>" : "");
>> +		fprintf(stdout, "%s%s[TRACE]",
>> +				console_ansi_colors ? "\033[01;30m" : "",
>> +				console_priority_prefix ? "<7>" : "");
>>  		break;
>>  	}
>>  
>>  	fprintf(level == ERRORLEVEL ? stderr : stdout,
>> -			" : %s %s\n", current, msg ? msg : "");
>> +			" : %s %s%s\n", current, msg ? msg : "", console_ansi_colors ? "\x1b[0m" : "");
>>  	fflush(stdout);
>>  }
>>  
>> @@ -275,6 +286,9 @@ void notify_init(void)
>>  	}
>>  #endif
>>  
>> +	console_ansi_colors = (isatty(fileno(stdout)) && isatty(fileno(stderr)))
>> +		? true : false;
>> +
>>  	if (pid == getpid()) {
>>  		char buf[60];
>>  		snprintf(buf, sizeof(buf), "Notify%d", pid);
>>
> 
> 
> Nice !
> 
> Acked-by: Stefano Babic <sbabic@denx.de>
> 

Tested both on tty and serial consoles:

Tested-by: Stefano Babic <sbabic@denx.de>

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/core/notifier.c b/core/notifier.c
index 48f1575..14a6052 100644
--- a/core/notifier.c
+++ b/core/notifier.c
@@ -60,6 +60,7 @@  static struct sockaddr_un notify_client;
 static struct sockaddr_un notify_server;
 static int notifyfd = -1;
 static bool console_priority_prefix = false;
+static bool console_ansi_colors = false;
 
 /*
  * This allows to extend the list of notifier.
@@ -149,24 +150,34 @@  static void console_notifier (RECOVERY_STATUS status, int error, int level, cons
 
 	switch (level) {
 	case ERRORLEVEL:
-		fprintf(stderr, "%s[ERROR]", console_priority_prefix ? "<3>" : "");
+		fprintf(stderr, "%s%s[ERROR]",
+				console_ansi_colors ? "\033[01;31m" : "",
+				console_priority_prefix ? "<3>" : "");
 		break;
 	case WARNLEVEL:
-		fprintf(stdout, "%s[WARN ]", console_priority_prefix ? "<4>" : "");
+		fprintf(stdout, "%s%s[WARN ]",
+				console_ansi_colors ? "\033[01;33m" : "",
+				console_priority_prefix ? "<4>" : "");
 		break;
 	case INFOLEVEL:
-		fprintf(stdout, "%s[INFO ]", console_priority_prefix ? "<6>" : "");
+		fprintf(stdout, "%s%s[INFO ]",
+				console_ansi_colors ? "\033[01;32m" : "",
+				console_priority_prefix ? "<6>" : "");
 		break;
 	case DEBUGLEVEL:
-		fprintf(stdout, "%s[DEBUG]", console_priority_prefix ? "<7>" : "");
+		fprintf(stdout, "%s%s[DEBUG]",
+				console_ansi_colors ? "\033[01;30m" : "",
+				console_priority_prefix ? "<7>" : "");
 		break;
 	case TRACELEVEL:
-		fprintf(stdout, "%s[TRACE]", console_priority_prefix ? "<7>" : "");
+		fprintf(stdout, "%s%s[TRACE]",
+				console_ansi_colors ? "\033[01;30m" : "",
+				console_priority_prefix ? "<7>" : "");
 		break;
 	}
 
 	fprintf(level == ERRORLEVEL ? stderr : stdout,
-			" : %s %s\n", current, msg ? msg : "");
+			" : %s %s%s\n", current, msg ? msg : "", console_ansi_colors ? "\x1b[0m" : "");
 	fflush(stdout);
 }
 
@@ -275,6 +286,9 @@  void notify_init(void)
 	}
 #endif
 
+	console_ansi_colors = (isatty(fileno(stdout)) && isatty(fileno(stderr)))
+		? true : false;
+
 	if (pid == getpid()) {
 		char buf[60];
 		snprintf(buf, sizeof(buf), "Notify%d", pid);