]> git.sesse.net Git - ffmpeg/blob - libavutil/log.c
Merge commit 'f1c4a54f6ecbf77a29aaaee09ca70c5468d0c509'
[ffmpeg] / libavutil / log.c
1 /*
2  * log functions
3  * Copyright (c) 2003 Michel Bardiaux
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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.
11  *
12  * FFmpeg 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * logging functions
25  */
26
27 #include "config.h"
28
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #if HAVE_IO_H
33 #include <io.h>
34 #endif
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include "avutil.h"
38 #include "bprint.h"
39 #include "common.h"
40 #include "internal.h"
41 #include "log.h"
42
43 #if HAVE_PTHREADS
44 #include <pthread.h>
45 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
46 #endif
47
48 #define LINE_SZ 1024
49
50 static int av_log_level = AV_LOG_INFO;
51 static int flags;
52
53 #if HAVE_SETCONSOLETEXTATTRIBUTE
54 #include <windows.h>
55 static const uint8_t color[16 + AV_CLASS_CATEGORY_NB] = {
56     [AV_LOG_PANIC  /8] = 12,
57     [AV_LOG_FATAL  /8] = 12,
58     [AV_LOG_ERROR  /8] = 12,
59     [AV_LOG_WARNING/8] = 14,
60     [AV_LOG_INFO   /8] =  7,
61     [AV_LOG_VERBOSE/8] = 10,
62     [AV_LOG_DEBUG  /8] = 10,
63     [16+AV_CLASS_CATEGORY_NA              ] =  7,
64     [16+AV_CLASS_CATEGORY_INPUT           ] = 13,
65     [16+AV_CLASS_CATEGORY_OUTPUT          ] =  5,
66     [16+AV_CLASS_CATEGORY_MUXER           ] = 13,
67     [16+AV_CLASS_CATEGORY_DEMUXER         ] =  5,
68     [16+AV_CLASS_CATEGORY_ENCODER         ] = 11,
69     [16+AV_CLASS_CATEGORY_DECODER         ] =  3,
70     [16+AV_CLASS_CATEGORY_FILTER          ] = 10,
71     [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] =  9,
72     [16+AV_CLASS_CATEGORY_SWSCALER        ] =  7,
73     [16+AV_CLASS_CATEGORY_SWRESAMPLER     ] =  7,
74     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT ] = 13,
75     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT  ] = 5,
76     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT ] = 13,
77     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT  ] = 5,
78     [16+AV_CLASS_CATEGORY_DEVICE_OUTPUT       ] = 13,
79     [16+AV_CLASS_CATEGORY_DEVICE_INPUT        ] = 5,
80 };
81
82 static int16_t background, attr_orig;
83 static HANDLE con;
84 #else
85
86 static const uint32_t color[16 + AV_CLASS_CATEGORY_NB] = {
87     [AV_LOG_PANIC  /8] =  52 << 16 | 196 << 8 | 0x41,
88     [AV_LOG_FATAL  /8] = 208 <<  8 | 0x41,
89     [AV_LOG_ERROR  /8] = 196 <<  8 | 0x11,
90     [AV_LOG_WARNING/8] = 226 <<  8 | 0x03,
91     [AV_LOG_INFO   /8] = 253 <<  8 | 0x09,
92     [AV_LOG_VERBOSE/8] =  40 <<  8 | 0x02,
93     [AV_LOG_DEBUG  /8] =  34 <<  8 | 0x02,
94     [16+AV_CLASS_CATEGORY_NA              ] = 250 << 8 | 0x09,
95     [16+AV_CLASS_CATEGORY_INPUT           ] = 219 << 8 | 0x15,
96     [16+AV_CLASS_CATEGORY_OUTPUT          ] = 201 << 8 | 0x05,
97     [16+AV_CLASS_CATEGORY_MUXER           ] = 213 << 8 | 0x15,
98     [16+AV_CLASS_CATEGORY_DEMUXER         ] = 207 << 8 | 0x05,
99     [16+AV_CLASS_CATEGORY_ENCODER         ] =  51 << 8 | 0x16,
100     [16+AV_CLASS_CATEGORY_DECODER         ] =  39 << 8 | 0x06,
101     [16+AV_CLASS_CATEGORY_FILTER          ] = 155 << 8 | 0x12,
102     [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 192 << 8 | 0x14,
103     [16+AV_CLASS_CATEGORY_SWSCALER        ] = 153 << 8 | 0x14,
104     [16+AV_CLASS_CATEGORY_SWRESAMPLER     ] = 147 << 8 | 0x14,
105     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT ] = 213 << 8 | 0x15,
106     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT  ] = 207 << 8 | 0x05,
107     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT ] = 213 << 8 | 0x15,
108     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT  ] = 207 << 8 | 0x05,
109     [16+AV_CLASS_CATEGORY_DEVICE_OUTPUT       ] = 213 << 8 | 0x15,
110     [16+AV_CLASS_CATEGORY_DEVICE_INPUT        ] = 207 << 8 | 0x05,
111 };
112
113 #endif
114 static int use_color = -1;
115
116 static void colored_fputs(int level, const char *str)
117 {
118     if (!*str)
119         return;
120
121     if (use_color < 0) {
122 #if HAVE_SETCONSOLETEXTATTRIBUTE
123         CONSOLE_SCREEN_BUFFER_INFO con_info;
124         con = GetStdHandle(STD_ERROR_HANDLE);
125         use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
126                     !getenv("AV_LOG_FORCE_NOCOLOR");
127         if (use_color) {
128             GetConsoleScreenBufferInfo(con, &con_info);
129             attr_orig  = con_info.wAttributes;
130             background = attr_orig & 0xF0;
131         }
132 #elif HAVE_ISATTY
133         use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
134                     (getenv("TERM") && isatty(2) ||
135                      getenv("AV_LOG_FORCE_COLOR"));
136         if (getenv("AV_LOG_FORCE_256COLOR"))
137             use_color *= 256;
138 #else
139         use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
140                    !getenv("AV_LOG_FORCE_NOCOLOR");
141 #endif
142     }
143
144 #if HAVE_SETCONSOLETEXTATTRIBUTE
145     if (use_color && level != AV_LOG_INFO/8)
146         SetConsoleTextAttribute(con, background | color[level]);
147     fputs(str, stderr);
148     if (use_color && level != AV_LOG_INFO/8)
149         SetConsoleTextAttribute(con, attr_orig);
150 #else
151     if (use_color == 1 && level != AV_LOG_INFO/8) {
152         fprintf(stderr,
153                 "\033[%d;3%dm%s\033[0m",
154                 (color[level] >> 4) & 15,
155                 color[level] & 15,
156                 str);
157     } else if (use_color == 256 && level != AV_LOG_INFO/8) {
158         fprintf(stderr,
159                 "\033[48;5;%dm\033[38;5;%dm%s\033[0m",
160                 (color[level] >> 16) & 0xff,
161                 (color[level] >> 8) & 0xff,
162                 str);
163     } else
164         fputs(str, stderr);
165 #endif
166
167 }
168
169 const char *av_default_item_name(void *ptr)
170 {
171     return (*(AVClass **) ptr)->class_name;
172 }
173
174 AVClassCategory av_default_get_category(void *ptr)
175 {
176     return (*(AVClass **) ptr)->category;
177 }
178
179 static void sanitize(uint8_t *line){
180     while(*line){
181         if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
182             *line='?';
183         line++;
184     }
185 }
186
187 static int get_category(void *ptr){
188     AVClass *avc = *(AVClass **) ptr;
189     if(    !avc
190         || (avc->version&0xFF)<100
191         ||  avc->version < (51 << 16 | 59 << 8)
192         ||  avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
193
194     if(avc->get_category)
195         return avc->get_category(ptr) + 16;
196
197     return avc->category + 16;
198 }
199
200 static void format_line(void *avcl, int level, const char *fmt, va_list vl,
201                         AVBPrint part[3], int *print_prefix, int type[2])
202 {
203     AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
204     av_bprint_init(part+0, 0, 1);
205     av_bprint_init(part+1, 0, 1);
206     av_bprint_init(part+2, 0, 65536);
207
208     if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
209     if (*print_prefix && avc) {
210         if (avc->parent_log_context_offset) {
211             AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
212                                    avc->parent_log_context_offset);
213             if (parent && *parent) {
214                 av_bprintf(part+0, "[%s @ %p] ",
215                          (*parent)->item_name(parent), parent);
216                 if(type) type[0] = get_category(parent);
217             }
218         }
219         av_bprintf(part+1, "[%s @ %p] ",
220                  avc->item_name(avcl), avcl);
221         if(type) type[1] = get_category(avcl);
222     }
223
224     av_vbprintf(part+2, fmt, vl);
225
226     if(*part[0].str || *part[1].str || *part[2].str) {
227         char lastc = part[2].len && part[2].len <= part[2].size ? part[2].str[part[2].len - 1] : 0;
228         *print_prefix = lastc == '\n' || lastc == '\r';
229     }
230 }
231
232 void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
233                         char *line, int line_size, int *print_prefix)
234 {
235     AVBPrint part[3];
236     format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
237     snprintf(line, line_size, "%s%s%s", part[0].str, part[1].str, part[2].str);
238     av_bprint_finalize(part+2, NULL);
239 }
240
241 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
242 {
243     static int print_prefix = 1;
244     static int count;
245     static char prev[LINE_SZ];
246     AVBPrint part[3];
247     char line[LINE_SZ];
248     static int is_atty;
249     int type[2];
250
251     if (level > av_log_level)
252         return;
253 #if HAVE_PTHREADS
254     pthread_mutex_lock(&mutex);
255 #endif
256
257     format_line(ptr, level, fmt, vl, part, &print_prefix, type);
258     snprintf(line, sizeof(line), "%s%s%s", part[0].str, part[1].str, part[2].str);
259
260 #if HAVE_ISATTY
261     if (!is_atty)
262         is_atty = isatty(2) ? 1 : -1;
263 #endif
264
265     if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
266         *line && line[strlen(line) - 1] != '\r'){
267         count++;
268         if (is_atty == 1)
269             fprintf(stderr, "    Last message repeated %d times\r", count);
270         goto end;
271     }
272     if (count > 0) {
273         fprintf(stderr, "    Last message repeated %d times\n", count);
274         count = 0;
275     }
276     strcpy(prev, line);
277     sanitize(part[0].str);
278     colored_fputs(type[0], part[0].str);
279     sanitize(part[1].str);
280     colored_fputs(type[1], part[1].str);
281     sanitize(part[2].str);
282     colored_fputs(av_clip(level >> 3, 0, 6), part[2].str);
283 end:
284     av_bprint_finalize(part+2, NULL);
285 #if HAVE_PTHREADS
286     pthread_mutex_unlock(&mutex);
287 #endif
288 }
289
290 static void (*av_log_callback)(void*, int, const char*, va_list) =
291     av_log_default_callback;
292
293 void av_log(void* avcl, int level, const char *fmt, ...)
294 {
295     AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
296     va_list vl;
297     va_start(vl, fmt);
298     if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
299         avc->log_level_offset_offset && level >= AV_LOG_FATAL)
300         level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
301     av_vlog(avcl, level, fmt, vl);
302     va_end(vl);
303 }
304
305 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
306 {
307     void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
308     if (log_callback)
309         log_callback(avcl, level, fmt, vl);
310 }
311
312 int av_log_get_level(void)
313 {
314     return av_log_level;
315 }
316
317 void av_log_set_level(int level)
318 {
319     av_log_level = level;
320 }
321
322 void av_log_set_flags(int arg)
323 {
324     flags = arg;
325 }
326
327 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
328 {
329     av_log_callback = callback;
330 }
331
332 static void missing_feature_sample(int sample, void *avc, const char *msg,
333                                    va_list argument_list)
334 {
335     av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
336     av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg "
337            "version to the newest one from Git. If the problem still "
338            "occurs, it means that your file has a feature which has not "
339            "been implemented.\n");
340     if (sample)
341         av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
342                "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
343                "and contact the ffmpeg-devel mailing list.\n");
344 }
345
346 void avpriv_request_sample(void *avc, const char *msg, ...)
347 {
348     va_list argument_list;
349
350     va_start(argument_list, msg);
351     missing_feature_sample(1, avc, msg, argument_list);
352     va_end(argument_list);
353 }
354
355 void avpriv_report_missing_feature(void *avc, const char *msg, ...)
356 {
357     va_list argument_list;
358
359     va_start(argument_list, msg);
360     missing_feature_sample(0, avc, msg, argument_list);
361     va_end(argument_list);
362 }