* This should be called before any output happens. */
| 78 | * This should be called before any output happens. |
| 79 | */ |
| 80 | void |
| 81 | pg_logging_init(const char *argv0) |
| 82 | { |
| 83 | const char *pg_color_env = getenv("PG_COLOR"); |
| 84 | bool log_color = false; |
| 85 | bool color_terminal = isatty(fileno(stderr)); |
| 86 | |
| 87 | #ifdef WIN32 |
| 88 | |
| 89 | /* |
| 90 | * On Windows, check if environment is VT100-compatible if using a |
| 91 | * terminal. |
| 92 | */ |
| 93 | if (color_terminal) |
| 94 | color_terminal = enable_vt_processing(); |
| 95 | #endif |
| 96 | |
| 97 | /* usually the default, but not on Windows */ |
| 98 | setvbuf(stderr, NULL, _IONBF, 0); |
| 99 | |
| 100 | progname = get_progname(argv0); |
| 101 | __pg_log_level = PG_LOG_INFO; |
| 102 | |
| 103 | if (pg_color_env) |
| 104 | { |
| 105 | if (strcmp(pg_color_env, "always") == 0 || |
| 106 | (strcmp(pg_color_env, "auto") == 0 && color_terminal)) |
| 107 | log_color = true; |
| 108 | } |
| 109 | |
| 110 | if (log_color) |
| 111 | { |
| 112 | const char *pg_colors_env = getenv("PG_COLORS"); |
| 113 | |
| 114 | if (pg_colors_env) |
| 115 | { |
| 116 | char *colors = strdup(pg_colors_env); |
| 117 | |
| 118 | if (colors) |
| 119 | { |
| 120 | for (char *token = strtok(colors, ":"); token; token = strtok(NULL, ":")) |
| 121 | { |
| 122 | char *e = strchr(token, '='); |
| 123 | |
| 124 | if (e) |
| 125 | { |
| 126 | char *name; |
| 127 | char *value; |
| 128 | |
| 129 | *e = '\0'; |
| 130 | name = token; |
| 131 | value = e + 1; |
| 132 | |
| 133 | if (strcmp(name, "error") == 0) |
| 134 | sgr_error = strdup(value); |
| 135 | if (strcmp(name, "warning") == 0) |
| 136 | sgr_warning = strdup(value); |
| 137 | if (strcmp(name, "locus") == 0) |