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
1313
1414 API changes, most recent first:
1515
16+2014-04-xx - xxxxxxx - lavu 53.09.0 - log.h
17+ Add AV_LOG(c) macro to have 256 color debug messages.
18+
1619 2014-02-xx - xxxxxxx - lavu 53.08.0 - frame.h
1720 Add av_frame_remove_side_data() for removing a single side data
1821 instance from a frame.
libavutil/log.c+38−19
@@ -53,35 +53,50 @@ static HANDLE con; · 52 unmodified lines
5353 #else
5454 static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };
5555 #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)
5657 #define reset_color() fprintf(stderr, "\033[0m")
5758 #endif
5859 static int use_color = -1;
5960
60−static void colored_fputs(int level, const char *str)
61+static void check_color_terminal(void)
6162 {
62− if (use_color < 0) {
6363 #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+ }
7373 #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") ;
7778 #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");
8081 #endif
81− }
82+}
8283
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:
8494 set_color(level);
95+ if (tint)
96+ print_256color(tint);
97+ break;
98+ default:
99+ break;
85100 }
86101 fputs(str, stderr);
87102 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
102117 char line[1024];
103118 static int is_atty;
104119 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
120+ int tint = av_clip(level >> 8, 0, 256);
121+
122+ level &= 0xff;
123+
105124 if (level > av_log_level)
106125 return;
107126 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
138157 fprintf(stderr, " Last message repeated %d times\n", count);
139158 count = 0;
140159 }
141− colored_fputs(av_clip(level >> 3, 0, 6), line);
160+ colored_fputs(av_clip(level >> 3, 0, 6), tint, line);
142161 av_strlcpy(prev, line, sizeof line);
143162 }
144163
libavutil/log.h+8−0
@@ -143,6 +143,14 @@ typedef struct AVClass { · 142 unmodified lines
143143 */
144144 #define AV_LOG_DEBUG 48
145145
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+
146154 /**
147155 * @}
148156 */
libavutil/version.h+1−1
@@ -54,7 +54,7 @@ · 53 unmodified lines
5454 */
5555
5656 #define LIBAVUTIL_VERSION_MAJOR 53
57−#define LIBAVUTIL_VERSION_MINOR 8
57+#define LIBAVUTIL_VERSION_MINOR 9
5858 #define LIBAVUTIL_VERSION_MICRO 0
5959
6060 #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