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 #if HAVE_VALGRIND_VALGRIND_H
44 #include <valgrind/valgrind.h>
45 /* this is the log level at which valgrind will output a full backtrace */
46 #define BACKTRACE_LOGLEVEL AV_LOG_ERROR
49 static int av_log_level = AV_LOG_INFO;
53 #if HAVE_SETCONSOLETEXTATTRIBUTE
55 static const uint8_t color[NB_LEVELS] = { 12, 12, 12, 14, 7, 10, 11, 8};
56 static int16_t background, attr_orig;
58 #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
59 #define reset_color() SetConsoleTextAttribute(con, attr_orig)
60 #define print_256color(x)
62 static const uint8_t color[NB_LEVELS] = {
63 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06, 0x07
65 #define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
66 #define print_256color(x) fprintf(stderr, "\033[38;5;%dm", x)
67 #define reset_color() fprintf(stderr, "\033[0m")
69 static int use_color = -1;
71 static void check_color_terminal(void)
73 #if HAVE_SETCONSOLETEXTATTRIBUTE
74 CONSOLE_SCREEN_BUFFER_INFO con_info;
75 con = GetStdHandle(STD_ERROR_HANDLE);
76 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
77 !getenv("AV_LOG_FORCE_NOCOLOR");
79 GetConsoleScreenBufferInfo(con, &con_info);
80 attr_orig = con_info.wAttributes;
81 background = attr_orig & 0xF0;
84 char *term = getenv("TERM");
85 use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
86 (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
88 use_color += term && strstr(term, "256color");
90 use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
91 !getenv("AV_LOG_FORCE_NOCOLOR");
95 static void colored_fputs(int level, int tint, const char *str)
98 check_color_terminal();
107 print_256color(tint);
118 const char *av_default_item_name(void *ptr)
120 return (*(AVClass **) ptr)->class_name;
123 void av_log_default_callback(void *avcl, int level, const char *fmt, va_list vl)
125 static int print_prefix = 1;
127 static char prev[1024];
130 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
131 unsigned tint = level & 0xff00;
135 if (level > av_log_level)
138 if (print_prefix && avc) {
139 if (avc->parent_log_context_offset) {
140 AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
141 avc->parent_log_context_offset);
142 if (parent && *parent) {
143 snprintf(line, sizeof(line), "[%s @ %p] ",
144 (*parent)->item_name(parent), parent);
147 snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
148 avc->item_name(avcl), avcl);
151 vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
153 print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
157 is_atty = isatty(2) ? 1 : -1;
160 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
161 !strncmp(line, prev, sizeof line)) {
164 fprintf(stderr, " Last message repeated %d times\r", count);
168 fprintf(stderr, " Last message repeated %d times\n", count);
171 colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, line);
172 av_strlcpy(prev, line, sizeof line);
174 #if CONFIG_VALGRIND_BACKTRACE
175 if (level <= BACKTRACE_LOGLEVEL)
176 VALGRIND_PRINTF_BACKTRACE("");
180 static void (*av_log_callback)(void*, int, const char*, va_list) =
181 av_log_default_callback;
183 void av_log(void* avcl, int level, const char *fmt, ...)
185 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
188 if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
189 avc->log_level_offset_offset && level >= AV_LOG_FATAL)
190 level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
191 av_vlog(avcl, level, fmt, vl);
195 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
197 av_log_callback(avcl, level, fmt, vl);
200 int av_log_get_level(void)
205 void av_log_set_level(int level)
207 av_log_level = level;
210 void av_log_set_flags(int arg)
215 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
217 av_log_callback = callback;
220 static void missing_feature_sample(int sample, void *avc, const char *msg,
221 va_list argument_list)
223 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
224 av_log(avc, AV_LOG_WARNING, " is not implemented. Update your Libav "
225 "version to the newest one from Git. If the problem still "
226 "occurs, it means that your file has a feature which has not "
227 "been implemented.\n");
229 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
230 "of this file to ftp://upload.libav.org/incoming/ "
231 "and contact the libav-devel mailing list.\n");
234 void avpriv_request_sample(void *avc, const char *msg, ...)
236 va_list argument_list;
238 va_start(argument_list, msg);
239 missing_feature_sample(1, avc, msg, argument_list);
240 va_end(argument_list);
243 void avpriv_report_missing_feature(void *avc, const char *msg, ...)
245 va_list argument_list;
247 va_start(argument_list, msg);
248 missing_feature_sample(0, avc, msg, argument_list);
249 va_end(argument_list);