| @@ -53,35 +53,50 @@ static HANDLE con; · 52 unmodified lines |
| 53 | 53 | #else |
| 54 | 54 | static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 }; |
| 55 | 55 | #define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15) |
| 56 | +#define print_256color(x) fprintf(stderr, "\033[38;5;%dm", x) |
| 56 | 57 | #define reset_color() fprintf(stderr, "\033[0m") |
| 57 | 58 | #endif |
| 58 | 59 | static int use_color = -1; |
| 59 | 60 | |
| 60 | | −static void colored_fputs(int level, const char *str) |
| 61 | +static void check_color_terminal(void) |
| 61 | 62 | { |
| 62 | | − if (use_color < 0) { |
| 63 | 63 | #if HAVE_SETCONSOLETEXTATTRIBUTE |
| 64 | | − CONSOLE_SCREEN_BUFFER_INFO con_info; |
| 65 | | − con = GetStdHandle(STD_ERROR_HANDLE); |
| 66 | | − use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") && |
| 67 | | − !getenv("AV_LOG_FORCE_NOCOLOR"); |
| 68 | | − if (use_color) { |
| 69 | | − GetConsoleScreenBufferInfo(con, &con_info); |
| 70 | | − attr_orig = con_info.wAttributes; |
| 71 | | − background = attr_orig & 0xF0; |
| 72 | | − } |
| 64 | + CONSOLE_SCREEN_BUFFER_INFO con_info; |
| 65 | + con = GetStdHandle(STD_ERROR_HANDLE); |
| 66 | + use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") && |
| 67 | + !getenv("AV_LOG_FORCE_NOCOLOR"); |
| 68 | + if (use_color) { |
| 69 | + GetConsoleScreenBufferInfo(con, &con_info); |
| 70 | + attr_orig = con_info.wAttributes; |
| 71 | + background = attr_orig & 0xF0; |
| 72 | + } |
| 73 | 73 | #elif HAVE_ISATTY |
| 74 | | − use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") && |
| 75 | | − (getenv("TERM") && isatty(2) || |
| 76 | | − getenv("AV_LOG_FORCE_COLOR")); |
| 74 | + char *term = getenv("TERM"); |
| 75 | + use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") && |
| 76 | + (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR")); |
| 77 | + use_color += !!strstr(term, "256color") ; |
| 77 | 78 | #else |
| 78 | | − use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") && |
| 79 | | − !getenv("AV_LOG_FORCE_NOCOLOR"); |
| 79 | + use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") && |
| 80 | + !getenv("AV_LOG_FORCE_NOCOLOR"); |
| 80 | 81 | #endif |
| 81 | | − } |
| 82 | +} |
| 82 | 83 | |
| 83 | | − if (use_color) { |
| 84 | +static void colored_fputs(int level, int tint, const char *str) |
| 85 | +{ |
| 86 | + if (use_color < 0) |
| 87 | + check_color_terminal(); |
| 88 | + |
| 89 | + switch (use_color) { |
| 90 | + case 1: |
| 91 | + set_color(level); |
| 92 | + break; |
| 93 | + case 2: |
| 84 | 94 | set_color(level); |
| 95 | + if (tint) |
| 96 | + print_256color(tint); |
| 97 | + break; |
| 98 | + default: |
| 99 | + break; |
| 85 | 100 | } |
| 86 | 101 | fputs(str, stderr); |
| 87 | 102 | if (use_color) { |
| @@ -102,6 +117,10 @@ void av_log_default_callback(void *avcl, int level, const char *fmt, va_list vl) · 14 unmodified lines |
| 102 | 117 | char line[1024]; |
| 103 | 118 | static int is_atty; |
| 104 | 119 | AVClass* avc = avcl ? *(AVClass **) avcl : NULL; |
| 120 | + int tint = av_clip(level >> 8, 0, 256); |
| 121 | + |
| 122 | + level &= 0xff; |
| 123 | + |
| 105 | 124 | if (level > av_log_level) |
| 106 | 125 | return; |
| 107 | 126 | line[0] = 0; |
| @@ -138,7 +157,7 @@ void av_log_default_callback(void *avcl, int level, const char *fmt, va_list vl) · 30 unmodified lines |
| 138 | 157 | fprintf(stderr, " Last message repeated %d times\n", count); |
| 139 | 158 | count = 0; |
| 140 | 159 | } |
| 141 | | − colored_fputs(av_clip(level >> 3, 0, 6), line); |
| 160 | + colored_fputs(av_clip(level >> 3, 0, 6), tint, line); |
| 142 | 161 | av_strlcpy(prev, line, sizeof line); |
| 143 | 162 | } |
| 144 | 163 | |