History

log: Support for 256color terminals

And provide extended coloring capabilities for debugging. The default colors do not change in 256 more to keep supporting people using Black on White, White on Black and Solarized terminals.

Luca Barbato committed 13 years ago

Commit 77631181 parent 006c2534 files · +50 −20Browse files

Unified · Split

4 files
  1. doc/APIchanges +3 −0
  2. libavutil/log.c +38 −19
  3. libavutil/log.h +8 −0
  4. libavutil/version.h +1 −1
doc/APIchanges+3−0
@@ -13,6 +13,9 @@ libavutil: 2013-12-xx · 12 unmodified lines
13 13
14API changes, most recent first:14API changes, most recent first:
15 15
162014-04-xx - xxxxxxx - lavu 53.09.0 - log.h
17 Add AV_LOG(c) macro to have 256 color debug messages.
18
162014-02-xx - xxxxxxx - lavu 53.08.0 - frame.h192014-02-xx - xxxxxxx - lavu 53.08.0 - frame.h
17 Add av_frame_remove_side_data() for removing a single side data20 Add av_frame_remove_side_data() for removing a single side data
18 instance from a frame.21 instance from a frame.
libavutil/log.c+38−19
@@ -53,35 +53,50 @@ static HANDLE con; · 52 unmodified lines
53#else53#else
54static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };54static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };
55#define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)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#define reset_color() fprintf(stderr, "\033[0m")57#define reset_color() fprintf(stderr, "\033[0m")
57#endif58#endif
58static int use_color = -1;59static int use_color = -1;
59 60
60static void colored_fputs(int level, const char *str)61static void check_color_terminal(void)
61{62{
62 if (use_color < 0) {
63#if HAVE_SETCONSOLETEXTATTRIBUTE63#if HAVE_SETCONSOLETEXTATTRIBUTE
64 CONSOLE_SCREEN_BUFFER_INFO con_info;64 CONSOLE_SCREEN_BUFFER_INFO con_info;
65 con = GetStdHandle(STD_ERROR_HANDLE);65 con = GetStdHandle(STD_ERROR_HANDLE);
66 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&66 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
67 !getenv("AV_LOG_FORCE_NOCOLOR");67 !getenv("AV_LOG_FORCE_NOCOLOR");
68 if (use_color) {68 if (use_color) {
69 GetConsoleScreenBufferInfo(con, &con_info);69 GetConsoleScreenBufferInfo(con, &con_info);
70 attr_orig = con_info.wAttributes;70 attr_orig = con_info.wAttributes;
71 background = attr_orig & 0xF0;71 background = attr_orig & 0xF0;
72 }72 }
73#elif HAVE_ISATTY73#elif HAVE_ISATTY
74 use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&74 char *term = getenv("TERM");
75 (getenv("TERM") && isatty(2) ||75 use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
76 getenv("AV_LOG_FORCE_COLOR"));76 (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
77 use_color += !!strstr(term, "256color") ;
77#else78#else
78 use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&79 use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
79 !getenv("AV_LOG_FORCE_NOCOLOR");80 !getenv("AV_LOG_FORCE_NOCOLOR");
80#endif81#endif
81 }82}
82 83
83 if (use_color) {84static 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 set_color(level);94 set_color(level);
95 if (tint)
96 print_256color(tint);
97 break;
98 default:
99 break;
85 }100 }
86 fputs(str, stderr);101 fputs(str, stderr);
87 if (use_color) {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 char line[1024];117 char line[1024];
103 static int is_atty;118 static int is_atty;
104 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;119 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
120 int tint = av_clip(level >> 8, 0, 256);
121
122 level &= 0xff;
123
105 if (level > av_log_level)124 if (level > av_log_level)
106 return;125 return;
107 line[0] = 0;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 fprintf(stderr, " Last message repeated %d times\n", count);157 fprintf(stderr, " Last message repeated %d times\n", count);
139 count = 0;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 av_strlcpy(prev, line, sizeof line);161 av_strlcpy(prev, line, sizeof line);
143}162}
144 163
libavutil/log.h+8−0
@@ -143,6 +143,14 @@ typedef struct AVClass { · 142 unmodified lines
143 */143 */
144#define AV_LOG_DEBUG 48144#define AV_LOG_DEBUG 48
145 145
146/**
147 * Sets additional colors for extended debugging sessions.
148 * Requires 256color terminal support. Use outside debugging is not
149 * recommended.
150 */
151
152#define AV_LOG_C(x) (x << 8)
153
146/**154/**
147 * @}155 * @}
148 */156 */
libavutil/version.h+1−1
@@ -54,7 +54,7 @@ · 53 unmodified lines
54 */54 */
55 55
56#define LIBAVUTIL_VERSION_MAJOR 5356#define LIBAVUTIL_VERSION_MAJOR 53
57#define LIBAVUTIL_VERSION_MINOR 857#define LIBAVUTIL_VERSION_MINOR 9
58#define LIBAVUTIL_VERSION_MICRO 058#define LIBAVUTIL_VERSION_MICRO 0
59 59
60#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \60#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t