]> git.sesse.net Git - ffmpeg/blob - libavutil/log.c
Merge commit '88d1fb4e3f156d8fd5bb6cb3ba3a9ef1e6d6fb08'
[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 #define NB_LEVELS 8
54 #if defined(_WIN32) && !defined(__MINGW32CE__) && HAVE_SETCONSOLETEXTATTRIBUTE
55 #include <windows.h>
56 static const uint8_t color[16 + AV_CLASS_CATEGORY_NB] = {
57     [AV_LOG_PANIC  /8] = 12,
58     [AV_LOG_FATAL  /8] = 12,
59     [AV_LOG_ERROR  /8] = 12,
60     [AV_LOG_WARNING/8] = 14,
61     [AV_LOG_INFO   /8] =  7,
62     [AV_LOG_VERBOSE/8] = 10,
63     [AV_LOG_DEBUG  /8] = 10,
64     [AV_LOG_TRACE  /8] = 8,
65     [16+AV_CLASS_CATEGORY_NA              ] =  7,
66     [16+AV_CLASS_CATEGORY_INPUT           ] = 13,
67     [16+AV_CLASS_CATEGORY_OUTPUT          ] =  5,
68     [16+AV_CLASS_CATEGORY_MUXER           ] = 13,
69     [16+AV_CLASS_CATEGORY_DEMUXER         ] =  5,
70     [16+AV_CLASS_CATEGORY_ENCODER         ] = 11,
71     [16+AV_CLASS_CATEGORY_DECODER         ] =  3,
72     [16+AV_CLASS_CATEGORY_FILTER          ] = 10,
73     [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] =  9,
74     [16+AV_CLASS_CATEGORY_SWSCALER        ] =  7,
75     [16+AV_CLASS_CATEGORY_SWRESAMPLER     ] =  7,
76     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT ] = 13,
77     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT  ] = 5,
78     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT ] = 13,
79     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT  ] = 5,
80     [16+AV_CLASS_CATEGORY_DEVICE_OUTPUT       ] = 13,
81     [16+AV_CLASS_CATEGORY_DEVICE_INPUT        ] = 5,
82 };
83
84 static int16_t background, attr_orig;
85 static HANDLE con;
86 #else
87
88 static const uint32_t color[16 + AV_CLASS_CATEGORY_NB] = {
89     [AV_LOG_PANIC  /8] =  52 << 16 | 196 << 8 | 0x41,
90     [AV_LOG_FATAL  /8] = 208 <<  8 | 0x41,
91     [AV_LOG_ERROR  /8] = 196 <<  8 | 0x11,
92     [AV_LOG_WARNING/8] = 226 <<  8 | 0x03,
93     [AV_LOG_INFO   /8] = 253 <<  8 | 0x09,
94     [AV_LOG_VERBOSE/8] =  40 <<  8 | 0x02,
95     [AV_LOG_DEBUG  /8] =  34 <<  8 | 0x02,
96     [AV_LOG_TRACE  /8] =  34 <<  8 | 0x07,
97     [16+AV_CLASS_CATEGORY_NA              ] = 250 << 8 | 0x09,
98     [16+AV_CLASS_CATEGORY_INPUT           ] = 219 << 8 | 0x15,
99     [16+AV_CLASS_CATEGORY_OUTPUT          ] = 201 << 8 | 0x05,
100     [16+AV_CLASS_CATEGORY_MUXER           ] = 213 << 8 | 0x15,
101     [16+AV_CLASS_CATEGORY_DEMUXER         ] = 207 << 8 | 0x05,
102     [16+AV_CLASS_CATEGORY_ENCODER         ] =  51 << 8 | 0x16,
103     [16+AV_CLASS_CATEGORY_DECODER         ] =  39 << 8 | 0x06,
104     [16+AV_CLASS_CATEGORY_FILTER          ] = 155 << 8 | 0x12,
105     [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 192 << 8 | 0x14,
106     [16+AV_CLASS_CATEGORY_SWSCALER        ] = 153 << 8 | 0x14,
107     [16+AV_CLASS_CATEGORY_SWRESAMPLER     ] = 147 << 8 | 0x14,
108     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT ] = 213 << 8 | 0x15,
109     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT  ] = 207 << 8 | 0x05,
110     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT ] = 213 << 8 | 0x15,
111     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT  ] = 207 << 8 | 0x05,
112     [16+AV_CLASS_CATEGORY_DEVICE_OUTPUT       ] = 213 << 8 | 0x15,
113     [16+AV_CLASS_CATEGORY_DEVICE_INPUT        ] = 207 << 8 | 0x05,
114 };
115
116 #endif
117 static int use_color = -1;
118
119 static void check_color_terminal(void)
120 {
121 #if defined(_WIN32) && !defined(__MINGW32CE__) && HAVE_SETCONSOLETEXTATTRIBUTE
122     CONSOLE_SCREEN_BUFFER_INFO con_info;
123     con = GetStdHandle(STD_ERROR_HANDLE);
124     use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
125                 !getenv("AV_LOG_FORCE_NOCOLOR");
126     if (use_color) {
127         GetConsoleScreenBufferInfo(con, &con_info);
128         attr_orig  = con_info.wAttributes;
129         background = attr_orig & 0xF0;
130     }
131 #elif HAVE_ISATTY
132     char *term = getenv("TERM");
133     use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
134                 (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
135     if (   getenv("AV_LOG_FORCE_256COLOR")
136         || (term && strstr(term, "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 static void colored_fputs(int level, int tint, const char *str)
145 {
146     int local_use_color;
147     if (!*str)
148         return;
149
150     if (use_color < 0)
151         check_color_terminal();
152
153     if (level == AV_LOG_INFO/8) local_use_color = 0;
154     else                        local_use_color = use_color;
155
156 #if defined(_WIN32) && !defined(__MINGW32CE__) && HAVE_SETCONSOLETEXTATTRIBUTE
157     if (local_use_color)
158         SetConsoleTextAttribute(con, background | color[level]);
159     fputs(str, stderr);
160     if (local_use_color)
161         SetConsoleTextAttribute(con, attr_orig);
162 #else
163     if (local_use_color == 1) {
164         fprintf(stderr,
165                 "\033[%d;3%dm%s\033[0m",
166                 (color[level] >> 4) & 15,
167                 color[level] & 15,
168                 str);
169     } else if (tint && use_color == 256) {
170         fprintf(stderr,
171                 "\033[48;5;%dm\033[38;5;%dm%s\033[0m",
172                 (color[level] >> 16) & 0xff,
173                 tint,
174                 str);
175     } else if (local_use_color == 256) {
176         fprintf(stderr,
177                 "\033[48;5;%dm\033[38;5;%dm%s\033[0m",
178                 (color[level] >> 16) & 0xff,
179                 (color[level] >> 8) & 0xff,
180                 str);
181     } else
182         fputs(str, stderr);
183 #endif
184
185 }
186
187 const char *av_default_item_name(void *ptr)
188 {
189     return (*(AVClass **) ptr)->class_name;
190 }
191
192 AVClassCategory av_default_get_category(void *ptr)
193 {
194     return (*(AVClass **) ptr)->category;
195 }
196
197 static void sanitize(uint8_t *line){
198     while(*line){
199         if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
200             *line='?';
201         line++;
202     }
203 }
204
205 static int get_category(void *ptr){
206     AVClass *avc = *(AVClass **) ptr;
207     if(    !avc
208         || (avc->version&0xFF)<100
209         ||  avc->version < (51 << 16 | 59 << 8)
210         ||  avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
211
212     if(avc->get_category)
213         return avc->get_category(ptr) + 16;
214
215     return avc->category + 16;
216 }
217
218 static const char *get_level_str(int level)
219 {
220     switch (level) {
221     case AV_LOG_QUIET:
222         return "quiet";
223     case AV_LOG_DEBUG:
224         return "debug";
225     case AV_LOG_VERBOSE:
226         return "verbose";
227     case AV_LOG_INFO:
228         return "info";
229     case AV_LOG_WARNING:
230         return "warning";
231     case AV_LOG_ERROR:
232         return "error";
233     case AV_LOG_FATAL:
234         return "fatal";
235     case AV_LOG_PANIC:
236         return "panic";
237     default:
238         return "";
239     }
240 }
241
242 static void format_line(void *avcl, int level, const char *fmt, va_list vl,
243                         AVBPrint part[4], int *print_prefix, int type[2])
244 {
245     AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
246     av_bprint_init(part+0, 0, 1);
247     av_bprint_init(part+1, 0, 1);
248     av_bprint_init(part+2, 0, 1);
249     av_bprint_init(part+3, 0, 65536);
250
251     if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
252     if (*print_prefix && avc) {
253         if (avc->parent_log_context_offset) {
254             AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
255                                    avc->parent_log_context_offset);
256             if (parent && *parent) {
257                 av_bprintf(part+0, "[%s @ %p] ",
258                          (*parent)->item_name(parent), parent);
259                 if(type) type[0] = get_category(parent);
260             }
261         }
262         av_bprintf(part+1, "[%s @ %p] ",
263                  avc->item_name(avcl), avcl);
264         if(type) type[1] = get_category(avcl);
265
266         if (flags & AV_LOG_PRINT_LEVEL)
267             av_bprintf(part+2, "[%s] ", get_level_str(level));
268     }
269
270     av_vbprintf(part+3, fmt, vl);
271
272     if(*part[0].str || *part[1].str || *part[2].str || *part[3].str) {
273         char lastc = part[3].len && part[3].len <= part[3].size ? part[3].str[part[3].len - 1] : 0;
274         *print_prefix = lastc == '\n' || lastc == '\r';
275     }
276 }
277
278 void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
279                         char *line, int line_size, int *print_prefix)
280 {
281     AVBPrint part[4];
282     format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
283     snprintf(line, line_size, "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
284     av_bprint_finalize(part+3, NULL);
285 }
286
287 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
288 {
289     static int print_prefix = 1;
290     static int count;
291     static char prev[LINE_SZ];
292     AVBPrint part[4];
293     char line[LINE_SZ];
294     static int is_atty;
295     int type[2];
296     unsigned tint = 0;
297
298     if (level >= 0) {
299         tint = level & 0xff00;
300         level &= 0xff;
301     }
302
303     if (level > av_log_level)
304         return;
305 #if HAVE_PTHREADS
306     pthread_mutex_lock(&mutex);
307 #endif
308
309     format_line(ptr, level, fmt, vl, part, &print_prefix, type);
310     snprintf(line, sizeof(line), "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
311
312 #if HAVE_ISATTY
313     if (!is_atty)
314         is_atty = isatty(2) ? 1 : -1;
315 #endif
316
317     if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
318         *line && line[strlen(line) - 1] != '\r'){
319         count++;
320         if (is_atty == 1)
321             fprintf(stderr, "    Last message repeated %d times\r", count);
322         goto end;
323     }
324     if (count > 0) {
325         fprintf(stderr, "    Last message repeated %d times\n", count);
326         count = 0;
327     }
328     strcpy(prev, line);
329     sanitize(part[0].str);
330     colored_fputs(type[0], 0, part[0].str);
331     sanitize(part[1].str);
332     colored_fputs(type[1], 0, part[1].str);
333     sanitize(part[2].str);
334     colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[2].str);
335     sanitize(part[3].str);
336     colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[3].str);
337 end:
338     av_bprint_finalize(part+3, NULL);
339 #if HAVE_PTHREADS
340     pthread_mutex_unlock(&mutex);
341 #endif
342 }
343
344 static void (*av_log_callback)(void*, int, const char*, va_list) =
345     av_log_default_callback;
346
347 void av_log(void* avcl, int level, const char *fmt, ...)
348 {
349     AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
350     va_list vl;
351     va_start(vl, fmt);
352     if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
353         avc->log_level_offset_offset && level >= AV_LOG_FATAL)
354         level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
355     av_vlog(avcl, level, fmt, vl);
356     va_end(vl);
357 }
358
359 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
360 {
361     void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
362     if (log_callback)
363         log_callback(avcl, level, fmt, vl);
364 }
365
366 int av_log_get_level(void)
367 {
368     return av_log_level;
369 }
370
371 void av_log_set_level(int level)
372 {
373     av_log_level = level;
374 }
375
376 void av_log_set_flags(int arg)
377 {
378     flags = arg;
379 }
380
381 int av_log_get_flags(void)
382 {
383     return flags;
384 }
385
386 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
387 {
388     av_log_callback = callback;
389 }
390
391 static void missing_feature_sample(int sample, void *avc, const char *msg,
392                                    va_list argument_list)
393 {
394     av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
395     av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg "
396            "version to the newest one from Git. If the problem still "
397            "occurs, it means that your file has a feature which has not "
398            "been implemented.\n");
399     if (sample)
400         av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
401                "of this file to ftp://upload.ffmpeg.org/incoming/ "
402                "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
403 }
404
405 void avpriv_request_sample(void *avc, const char *msg, ...)
406 {
407     va_list argument_list;
408
409     va_start(argument_list, msg);
410     missing_feature_sample(1, avc, msg, argument_list);
411     va_end(argument_list);
412 }
413
414 void avpriv_report_missing_feature(void *avc, const char *msg, ...)
415 {
416     va_list argument_list;
417
418     va_start(argument_list, msg);
419     missing_feature_sample(0, avc, msg, argument_list);
420     va_end(argument_list);
421 }
422
423 #ifdef TEST
424 // LCOV_EXCL_START
425 #include <string.h>
426
427 int main(int argc, char **argv)
428 {
429     int i;
430     av_log_set_level(AV_LOG_DEBUG);
431     for (use_color=0; use_color<=256; use_color = 255*use_color+1) {
432         av_log(NULL, AV_LOG_FATAL, "use_color: %d\n", use_color);
433         for (i = AV_LOG_DEBUG; i>=AV_LOG_QUIET; i-=8) {
434             av_log(NULL, i, " %d", i);
435             av_log(NULL, AV_LOG_INFO, "e ");
436             av_log(NULL, i + 256*123, "C%d", i);
437             av_log(NULL, AV_LOG_INFO, "e");
438         }
439         av_log(NULL, AV_LOG_PANIC, "\n");
440     }
441     return 0;
442 }
443 // LCOV_EXCL_STOP
444 #endif