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