BUG #16108: Colorization to the output of command-line has unproperly behaviors at Windows platform
The following bug has been logged on the website:
Bug reference: 16108
Logged by: Haiying Tang
Email address: tanghy.fnst@cn.fujitsu.com
PostgreSQL version: 12.0
Operating system: Windows
Description:
Hello
I found the following release notes in PG12 is not working properly at
Windows.
•Add colorization to the output of command-line utilities
Following the release note, I've set the the environment variable PG_COLOR
to auto, then I run pg_dump command with an incorrect passwd.
However, the command-line output is not colorized as the release notes
said.
Before PG_COLOR=auto is set: pg_dump: error: connection to database
"tanghy.fnst" failed: FATAL:
After PG_COLOR=auto is set: [01mpg_dump: [0m[01;31merror: [0mconnection
to database "tanghy.fnst" failed: FATAL
I think the colorization to the output of command-line is not supported at
Windows.
Maybe function "pg_logging_init" at source "src\common\logging.c" should add
a platform check.
Besides, the related release note of PG12 should add some description about
it.
Best Regards,
Tang
On Tue, Nov 12, 2019 at 9:30 PM PG Bug reporting form
<noreply@postgresql.org> wrote:
The following bug has been logged on the website:
Bug reference: 16108
Logged by: Haiying Tang
Email address: tanghy.fnst@cn.fujitsu.com
PostgreSQL version: 12.0
Operating system: Windows
Description:Hello
I found the following release notes in PG12 is not working properly at
Windows.•Add colorization to the output of command-line utilities
Following the release note, I've set the the environment variable PG_COLOR
to auto, then I run pg_dump command with an incorrect passwd.
However, the command-line output is not colorized as the release notes
said.Before PG_COLOR=auto is set: pg_dump: error: connection to database
"tanghy.fnst" failed: FATAL:
After PG_COLOR=auto is set: [01mpg_dump: [0m [01;31merror: [0mconnection
to database "tanghy.fnst" failed: FATALI think the colorization to the output of command-line is not supported at
Windows.
Maybe function "pg_logging_init" at source "src\common\logging.c" should add
a platform check.
Besides, the related release note of PG12 should add some description about
it.
Based on this:
https://en.wikipedia.org/wiki/ANSI_escape_code#DOS_and_Windows
... I wonder if it works if you use the new Windows Terminal, and I
wonder if it would work on the older thing if we used the
SetConsoleMode() flag it mentions.
On Tue, Nov 12, 2019 at 9:39 AM Thomas Munro <thomas.munro@gmail.com> wrote:
... I wonder if it works if you use the new Windows Terminal, and I
wonder if it would work on the older thing if we used the
SetConsoleMode() flag it mentions.
In order to make it work both things are needed, setting the console mode
and a terminal that supports it. Please find attached a patch for so.
Regards,
Juan José Santamaría Flecha
Attachments:
0001-command-line-colorization-on-windows.patchapplication/octet-stream; name=0001-command-line-colorization-on-windows.patchDownload
diff --git a/src/common/logging.c b/src/common/logging.c
index 895da71..655db70 100644
--- a/src/common/logging.c
+++ b/src/common/logging.c
@@ -32,6 +32,31 @@ static const char *sgr_locus = NULL;
#define ANSI_ESCAPE_FMT "\x1b[%sm"
#define ANSI_ESCAPE_RESET "\x1b[0m"
+#ifdef WIN32
+/*
+ * Check Windows support for VT100
+ */
+static bool
+enable_vt_mode()
+{
+ /* Check stderr */
+ HANDLE hOut = GetStdHandle(STD_ERROR_HANDLE);
+ DWORD dwMode = 0;
+
+ if (hOut == INVALID_HANDLE_VALUE)
+ return false;
+
+ if (!GetConsoleMode(hOut, &dwMode))
+ return false;
+
+ dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
+ if (!SetConsoleMode(hOut, dwMode))
+ return false;
+
+ return true;
+}
+#endif
+
/*
* This should be called before any output happens.
*/
@@ -49,8 +74,13 @@ pg_logging_init(const char *argv0)
if (pg_color_env)
{
+#ifdef WIN32
+ bool vt_mode = enable_vt_mode();
+#else
+ bool vt_mode = isatty(fileno(stderr));
+#endif
if (strcmp(pg_color_env, "always") == 0 ||
- (strcmp(pg_color_env, "auto") == 0 && isatty(fileno(stderr))))
+ (strcmp(pg_color_env, "auto") == 0 && vt_mode))
log_color = true;
}
In order to make it work both things are needed, setting the console mode and a terminal that supports it.
Your patch worked fine on windows which supports VT100. But the bug still happened when set PG_COLOR="always" at Windows Terminal that not support VT100. Please see the attached file “Test_result.png” for the NG result. (I used win7 for this test)
To fix the above bug, I made some change to your patch. The new one works fine on my win7(VT100 not support) and win10(VT100 support).
Also, in this new patch(v1), I added some doc change for Windows not support Colorization. Please find the attached patch for so.
Regards,
Tang
From: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
Sent: Wednesday, November 13, 2019 4:00 AM
To: Thomas Munro <thomas.munro@gmail.com>
Cc: PG Bug reporting form <noreply@postgresql.org>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>; Tang, Haiying/唐 海英 <tanghy.fnst@cn.fujitsu.com>
Subject: Re: BUG #16108: Colorization to the output of command-line has unproperly behaviors at Windows platform
On Tue, Nov 12, 2019 at 9:39 AM Thomas Munro <thomas.munro@gmail.com<mailto:thomas.munro@gmail.com>> wrote:
... I wonder if it works if you use the new Windows Terminal, and I
wonder if it would work on the older thing if we used the
SetConsoleMode() flag it mentions.
In order to make it work both things are needed, setting the console mode and a terminal that supports it. Please find attached a patch for so.
Regards,
Juan José Santamaría Flecha
Attachments:
v1-0001-command-line-colorization-on-windows.patchapplication/octet-stream; name=v1-0001-command-line-colorization-on-windows.patchDownload
diff --git a/doc/src/sgml/release-12.sgml b/doc/src/sgml/release-12.sgml
index 68949b2026..26b94bf4e4 100644
--- a/doc/src/sgml/release-12.sgml
+++ b/doc/src/sgml/release-12.sgml
@@ -3825,6 +3825,10 @@ Author: Peter Eisentraut <peter@eisentraut.org>
For example, the default behavior is equivalent to
<literal>PG_COLORS="error=01;31:warning=01;35:locus=01"</literal>.
</para>
+
+ <para>
+ Notably, VT100 support is required for the colorization on Windows.
+ </para>
</listitem>
</itemizedlist>
diff --git a/src/common/logging.c b/src/common/logging.c
index 895da7150e..8cb59fb609 100644
--- a/src/common/logging.c
+++ b/src/common/logging.c
@@ -32,6 +32,31 @@ static const char *sgr_locus = NULL;
#define ANSI_ESCAPE_FMT "\x1b[%sm"
#define ANSI_ESCAPE_RESET "\x1b[0m"
+#ifdef WIN32
+/*
+ * Check Windows support for VT100
+ */
+static bool
+enable_vt_mode()
+{
+ /* Check stderr */
+ HANDLE hOut = GetStdHandle(STD_ERROR_HANDLE);
+ DWORD dwMode = 0;
+
+ if (hOut == INVALID_HANDLE_VALUE)
+ return false;
+
+ if (!GetConsoleMode(hOut, &dwMode))
+ return false;
+
+ dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
+ if (!SetConsoleMode(hOut, dwMode))
+ return false;
+
+ return true;
+}
+#endif
+
/*
* This should be called before any output happens.
*/
@@ -47,6 +72,13 @@ pg_logging_init(const char *argv0)
progname = get_progname(argv0);
__pg_log_level = PG_LOG_INFO;
+#ifdef WIN32
+ bool vt_mode = enable_vt_mode();
+
+ if(!vt_mode)
+ pg_color_env = NULL;
+#endif
+
if (pg_color_env)
{
if (strcmp(pg_color_env, "always") == 0 ||
Test_result.pngimage/png; name=Test_result.pngDownload
�PNG
IHDR � � /�J gAMA ���a pHYs � ��+ ��IDATx^����g�y���@��������+���=Q���R�$FQ9���^��;�����.�� �Ab��� �D�n LH��3y|>��O���s��������������������~U���kN���'��e�������>������w�d���w���{.9�K6Q�.���{.�����s������k\
�B��������������p��������+|��{��?�����k���k��S��R���������{��C?������=x��wO�?������?^�..����b�vttttttt�^|����/�����7=0|��w=������Y]
����������������������b�����������o���~h����r�vttttttt�N\���
'��������~����_��#�p��gT M�~)���������=�����c��0�����}�<?������S�����_
;:::::::v���W��������~�'/<7���F��B��aK���������qa��_����#^�����������K���K��B�/��� �[���m�1�:^~�K��o���qq�vttttttt�x)����W���^���/���w����������c� ?>��O_\}||�����R/��R���������;���������;?~~�������;�E�S���/��\o���/7������~9���>9���W�KaGGGGGGG���w�{vu<z�����'��mx��|�_
;:::::::.&�|���;�==\�����~>���7�_����p�������\ ��[����������=u9���39��������/��U_�k����-P�%K�}�6�Y�U^m��'��]������GT�)?��.�lE����^��^7?��o����?�KT�S�8�����:�]���T���M}|O��m���o�`]�S����S��r�����B]�T�u�wGGGG����G�Cw>4=��x)|x���W��x��s���#G��o849vt�������
�n��^�������~k���^��y��t�MMx����&u�?�el��Z��N}uj[�D����7�p�p���U��<�/<��x�5|���d�<u��7���?v�������_��2sJ^m����������������'�sAN��s��`���s�������-��E{�
/}��1���f�?����1�O{���9%j�S2��&��l���\����������&n>����W���p�?x�W����3�w
9V�����/���au)����MY�<� �j�D�������M9�K��A|�N�oQ}ph��������|�CK����PW���'��RH����W��q���:��'(rs���{I`LP�����n��������������K�5�����mi~�O� z9 Pt��9`,��-�_�WW;�!d�SV� /|B��[�K��� �@k������m�7�7���p�?������~��������p�����G��e���o���ar���&��&Z/�S��_������z���(�P�>�k�C�m_�zx�~���R�:P/P`�>�:�M�Z������9vh����;^�"k�o��/��k��V���:��k��&���~]s���*��\b�����>Z���&�}m����O���=2�>�����W_����N���s:7?�+��GGGGG���Rx�����w=�zS��'�\]
���l����S�������7\`�����=�z���Yn���������R>�����r�'��d��y���O;�)��2��X��3��M�s| xlZc���=r�i�/s��F���)����K�f;��1� �LZ��y�<���KV�*����H�\�h�<����M �o��m��6[��?6�R�>