From patchwork Fri Feb 23 14:39:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Storm, Christian" X-Patchwork-Id: 877108 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:400c:c0c::23b; helo=mail-wr0-x23b.google.com; envelope-from=swupdate+bncbdd6bwv65qpbbyopydkakgqerir2xoi@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b="gpAeqlVx"; dkim-atps=neutral Received: from mail-wr0-x23b.google.com (mail-wr0-x23b.google.com [IPv6:2a00:1450:400c:c0c::23b]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3znv5y278wz9sW5 for ; Sat, 24 Feb 2018 01:40:36 +1100 (AEDT) Received: by mail-wr0-x23b.google.com with SMTP id f16sf3020774wre.0 for ; Fri, 23 Feb 2018 06:40:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1519396834; cv=pass; d=google.com; s=arc-20160816; b=uWYuKmODNCcLemKScyt07kUkzgWPpBF290lZpHey9v33XhWhSDxH2g+c+0ndROY3mu ZM+0N12o06JWp4a1/keG6eeN6JB6rvbw/SMLBqdGa7Jjl/FIPXzPxlJr44KV4ZXUk5TL MQyIeswzYApJV74eVH4GsVTEqmT+eehJZKunEFQX06+5EyHi02Wu6X27TC0cqawoihvI 3EylzVpKz9Yn/fhbcaq9tc4SzPTm0t34a+Yl6SGKZzkBn1wupGfmZVR15gPdoQEwWHnE r2oiunR0IutkIvP1uV7+3E/u3Jtw9iIUTI7km8CPFQhf6xyXRsL9xImclEIz4WOHUZ85 WzDA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:message-id:date:subject:cc:to:from :arc-authentication-results:arc-message-signature:mime-version :sender:dkim-signature:arc-authentication-results; bh=wYzJbgWRmm8FN5kV+reSltxxzv5W9N4HWh1DXqVAKDo=; b=NRgvK6v59CqWZQpP1hkk0n9J/LI4JFAw0FtXFt7eIWELi5QOCWczZpDDwAvdW0PUN3 S4X81khpGRUW7Q76vB8o6MZtWqvKRz3YIASudsCdQI2vcsnZkXBm0hgztPbsxyOiiSl/ CokZnecwemZWe1nRfp55hZMjIcBwGW7n4iKIx0vBgoFaY9p7wU+8aXLHR0PdyqTdIehq CV7eKvm8RGJBojUYLpxIZIhMWcuwFP8K7naELUlOKFB2uFumvj7IxToFkm/eHuX5FQGf 4aw4ysb2Bm5fCdUs309aj04WBj1bxaAWpGnZF0xqEXx/ui/wiKf9a3rwI9OP7ySrbGMq esrw== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) smtp.mailfrom=christian.storm@siemens.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:mime-version:from:to:cc:subject:date:message-id :x-original-sender:x-original-authentication-results:precedence :mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=wYzJbgWRmm8FN5kV+reSltxxzv5W9N4HWh1DXqVAKDo=; b=gpAeqlVxeSeiCHYWiXPIaOP3/Lmb8P5LIX87+1Bvq03PUNfLJPA2/PLTT2fskdDO6N COwEc7vwSvnur4aOJ092HIC0AkV3RRJa1Yy0T/suJhMjY4DqkvzHV3CWXxvgEYIzRY3n 380xmVXMQRAfzh61+JvUnQuPzLRV5sKPZIl99Au0N9PBrAxaJxkRZnZnZkYLhkzY2C8v EoyyOkxvmixCBWZkh3ZtouZwGqmAW6TvTGKbXsyDxPslMLEE7TlPpO2lxlPnjpCGeb0v 2pRXoRoMKgHWNC588VKeJoZKs2IsTd52Q0zx8vLkmZ7J4AYBHIoihrqjDEOnHdzvfEdZ EP0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:mime-version:from:to:cc:subject:date :message-id:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=wYzJbgWRmm8FN5kV+reSltxxzv5W9N4HWh1DXqVAKDo=; b=g5YchduZ2PZW9kklxP+wFmIA/ryCzrTaqkMFq25P5RnHwpL1wHoUHCnGqLGw1cI3qW aXk/vDfq8aCoRCZjEW3YFjrhQDjwhbTi6FbYIH+KdDswMNh/vDJPz4uWyu21j7nLJCJc +E4VgbdGy8jN7vn6kJb85e4NALpKLKg47+TMnvYHSt7xiokmoBfk1hQUhwgo6Tt45D0T MrCv25NbQaos/7yMaDKLCeY+XWQn0R69JzepqutiVNEALCNwRHS7Cdb2ktsDpC9q/VSY LEE1vzW7oFFn7gb7tAMuyKhUIoPNEfxWjA+lkZdiCuBRY177bK3cmKWk8SwPCB41Z4/s 8GUQ== Sender: swupdate@googlegroups.com X-Gm-Message-State: APf1xPCwj/dfbeSrKMw9RFerEYJFHi1UHqa90eOeXQFCvpaMkDVnHWeQ HuEAv9uitIAGTrbFj42Ensw= X-Google-Smtp-Source: AH8x2250J60tVfwNMMLxh/bthLeKHhiQT+9Zj5L0m257+KoDB8GGVLWUdQ8jR2bkiKvoRnyoCas23w== X-Received: by 10.28.138.66 with SMTP id m63mr39189wmd.6.1519396834135; Fri, 23 Feb 2018 06:40:34 -0800 (PST) MIME-Version: 1.0 X-BeenThere: swupdate@googlegroups.com Received: by 10.28.45.2 with SMTP id t2ls443459wmt.2.canary-gmail; Fri, 23 Feb 2018 06:40:33 -0800 (PST) X-Received: by 10.28.153.194 with SMTP id b185mr185815wme.17.1519396833224; Fri, 23 Feb 2018 06:40:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519396833; cv=none; d=google.com; s=arc-20160816; b=lAKwGa5d2L0UkowFjl/9c7dvIHj7oqiEUJYS8Y0r+2O5vpZQIOqBEjLjLnfLhB3SRB L0I6OXS4IKjZmbgkKQPorHl1RhzpWOGz+RmfDKa4u26YjEx9Tac/n4PAHdbhRMFm5V9N Z7T44sETDGkmA4K1+Mmr1ihRuNzQIxE/kYOd1ftwxTmrOzSHhOwHFWF4auMfd8Wgvszy MWo7PoduGh7At1dqCHYGR7QkM0+z9HwHHA4h66sCENWA1pRqXJigPKga7s/Nu9cYpZzU xm1zEtcrw0lfJRKXxeZtoMaq+r6RPg+1j/3XWogvbRWXKmM6PbraLYv+IsV6++QW2E9J x78w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:arc-authentication-results; bh=Z7YchGb+dQ9b4iG3cMclIH8LVHuCZQcxpExzNEkbcxI=; b=fYkZa6b6ufjqg8tm0heL3JTng5/zc9NdVoY5o1Dkv772EfHCWOxfPv/4gMJnvtCGcg Xb8Jig/vUna8QztbqTWX/IE/98WKOHRSm47LhxH60gHZNI6mRt2V/gnpD03Ln8bjPHVL qxTV8COuFwNUcROxdf1RV24lZW9k7I44szAsLQfR7xCsWFMKZQyW9utp5EjyVWCE7QqP s9LLa62BHyAFm34DC0q3+1WQVx8AOnatQQPA7VHCmXSHhnSsVQ36J6mqiK6C/W76Umtd GKceH3e3AwvNzy5+K0pKB3zPcByfDJ3kkD9d4qYw6yw6tE8GKuaftoa9e9WNEb7cSONz I4Dw== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) smtp.mailfrom=christian.storm@siemens.com Received: from thoth.sbs.de (thoth.sbs.de. [192.35.17.2]) by gmr-mx.google.com with ESMTPS id r7si189256wmc.4.2018.02.23.06.40.33 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Feb 2018 06:40:33 -0800 (PST) Received-SPF: pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) client-ip=192.35.17.2; Received: from mail2.siemens.de (mail2.siemens.de [139.25.208.11]) by thoth.sbs.de (8.15.2/8.15.2) with ESMTPS id w1NEeW5N001996 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 23 Feb 2018 15:40:32 +0100 Received: from MD1KR9XC.ww002.siemens.net ([139.25.69.202]) by mail2.siemens.de (8.15.2/8.15.2) with ESMTP id w1NEeWpg016863; Fri, 23 Feb 2018 15:40:32 +0100 From: Christian Storm To: swupdate@googlegroups.com Cc: Christian Storm Subject: [swupdate] [PATCH] logging: colorize terminal output log messages Date: Fri, 23 Feb 2018 15:39:39 +0100 Message-Id: <20180223143939.25898-1-christian.storm@siemens.com> X-Mailer: git-send-email 2.16.2 X-Original-Sender: christian.storm@siemens.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) smtp.mailfrom=christian.storm@siemens.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , 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 Acked-by: Stefano Babic Tested-by: Stefano Babic --- 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);