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