3 * Copyright (c) 2003 Michel Bardiaux
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
43 static int av_log_level = AV_LOG_INFO;
46 #if HAVE_SETCONSOLETEXTATTRIBUTE
48 static const uint8_t color[] = { 12, 12, 12, 14, 7, 10, 11 };
49 static int16_t background, attr_orig;
51 #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
52 #define reset_color() SetConsoleTextAttribute(con, attr_orig)
53 #define print_256color(x)
55 static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };
56 #define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
57 #define print_256color(x) fprintf(stderr, "\033[38;5;%dm", x)
58 #define reset_color() fprintf(stderr, "\033[0m")
60 static int use_color = -1;
62 static void check_color_terminal(void)
64 #if HAVE_SETCONSOLETEXTATTRIBUTE
65 CONSOLE_SCREEN_BUFFER_INFO con_info;
66 con = GetStdHandle(STD_ERROR_HANDLE);
67 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
68 !getenv("AV_LOG_FORCE_NOCOLOR");
70 GetConsoleScreenBufferInfo(con, &con_info);
71 attr_orig = con_info.wAttributes;
72 background = attr_orig & 0xF0;
75 char *term = getenv("TERM");
76 use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
77 (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
78 use_color += term && strstr(term, "256color");
80 use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
81 !getenv("AV_LOG_FORCE_NOCOLOR");
85 static void colored_fputs(int level, int tint, const char *str)
88 check_color_terminal();
108 const char *av_default_item_name(void *ptr)
110 return (*(AVClass **) ptr)->class_name;
113 void av_log_default_callback(void *avcl, int level, const char *fmt, va_list vl)
115 static int print_prefix = 1;
117 static char prev[1024];
120 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
121 unsigned tint = level & 0xff00;
125 if (level > av_log_level)
128 if (print_prefix && avc) {
129 if (avc->parent_log_context_offset) {
130 AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
131 avc->parent_log_context_offset);
132 if (parent && *parent) {
133 snprintf(line, sizeof(line), "[%s @ %p] ",
134 (*parent)->item_name(parent), parent);
137 snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
138 avc->item_name(avcl), avcl);
141 vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
143 print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
147 is_atty = isatty(2) ? 1 : -1;
150 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
151 !strncmp(line, prev, sizeof line)) {
154 fprintf(stderr, " Last message repeated %d times\r", count);
158 fprintf(stderr, " Last message repeated %d times\n", count);
161 colored_fputs(av_clip(level >> 3, 0, 6), tint >> 8, line);
162 av_strlcpy(prev, line, sizeof line);
165 static void (*av_log_callback)(void*, int, const char*, va_list) =
166 av_log_default_callback;
168 void av_log(void* avcl, int level, const char *fmt, ...)
170 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
173 if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
174 avc->log_level_offset_offset && level >= AV_LOG_FATAL)
175 level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
176 av_vlog(avcl, level, fmt, vl);
180 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
182 av_log_callback(avcl, level, fmt, vl);
185 int av_log_get_level(void)
190 void av_log_set_level(int level)
192 av_log_level = level;
195 void av_log_set_flags(int arg)
200 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
202 av_log_callback = callback;
205 static void missing_feature_sample(int sample, void *avc, const char *msg,
206 va_list argument_list)
208 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
209 av_log(avc, AV_LOG_WARNING, " is not implemented. Update your Libav "
210 "version to the newest one from Git. If the problem still "
211 "occurs, it means that your file has a feature which has not "
212 "been implemented.\n");
214 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
215 "of this file to ftp://upload.libav.org/incoming/ "
216 "and contact the libav-devel mailing list.\n");
219 void avpriv_request_sample(void *avc, const char *msg, ...)
221 va_list argument_list;
223 va_start(argument_list, msg);
224 missing_feature_sample(1, avc, msg, argument_list);
225 va_end(argument_list);
228 void avpriv_report_missing_feature(void *avc, const char *msg, ...)
230 va_list argument_list;
232 va_start(argument_list, msg);
233 missing_feature_sample(0, avc, msg, argument_list);
234 va_end(argument_list);