libavutil/log.c
4b45de0e
 /*
  * log functions
  * Copyright (c) 2003 Michel Bardiaux
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
4b45de0e
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
4b45de0e
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
4b45de0e
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
4b45de0e
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
7d685b48
  * logging functions
4b45de0e
  */
 
539df611
 #include "config.h"
 
145a8096
 #if HAVE_UNISTD_H
51e026d1
 #include <unistd.h>
539df611
 #endif
b70abd5b
 #if HAVE_IO_H
 #include <io.h>
 #endif
f099d3d1
 #include <stdarg.h>
62044024
 #include <stdlib.h>
4b45de0e
 #include "avutil.h"
148310ca
 #include "bprint.h"
1d9c2dc8
 #include "common.h"
f099d3d1
 #include "internal.h"
77652a6a
 #include "log.h"
4b45de0e
 
423ae313
 #if HAVE_PTHREADS
 #include <pthread.h>
 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 #endif
 
258dfff8
 #define LINE_SZ 1024
 
eaa2d123
 #if HAVE_VALGRIND_VALGRIND_H
 #include <valgrind/valgrind.h>
 /* this is the log level at which valgrind will output a full backtrace */
 #define BACKTRACE_LOGLEVEL AV_LOG_ERROR
 #endif
 
490a022d
 static int av_log_level = AV_LOG_INFO;
1c1c80f0
 static int flags;
4b45de0e
 
c253340a
 #define NB_LEVELS 8
f87ad3a0
 #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE
4855f867
 #include <windows.h>
2191aef9
 static const uint8_t color[16 + AV_CLASS_CATEGORY_NB] = {
     [AV_LOG_PANIC  /8] = 12,
     [AV_LOG_FATAL  /8] = 12,
     [AV_LOG_ERROR  /8] = 12,
     [AV_LOG_WARNING/8] = 14,
     [AV_LOG_INFO   /8] =  7,
     [AV_LOG_VERBOSE/8] = 10,
eb7efc12
     [AV_LOG_DEBUG  /8] = 10,
8e8219e0
     [AV_LOG_TRACE  /8] = 8,
eb7efc12
     [16+AV_CLASS_CATEGORY_NA              ] =  7,
bc210bfc
     [16+AV_CLASS_CATEGORY_INPUT           ] = 13,
     [16+AV_CLASS_CATEGORY_OUTPUT          ] =  5,
     [16+AV_CLASS_CATEGORY_MUXER           ] = 13,
     [16+AV_CLASS_CATEGORY_DEMUXER         ] =  5,
     [16+AV_CLASS_CATEGORY_ENCODER         ] = 11,
     [16+AV_CLASS_CATEGORY_DECODER         ] =  3,
ee7214c5
     [16+AV_CLASS_CATEGORY_FILTER          ] = 10,
eb7efc12
     [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] =  9,
1842b275
     [16+AV_CLASS_CATEGORY_SWSCALER        ] =  7,
     [16+AV_CLASS_CATEGORY_SWRESAMPLER     ] =  7,
19b3a250
     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT ] = 13,
     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT  ] = 5,
     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT ] = 13,
     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT  ] = 5,
     [16+AV_CLASS_CATEGORY_DEVICE_OUTPUT       ] = 13,
     [16+AV_CLASS_CATEGORY_DEVICE_INPUT        ] = 5,
2191aef9
 };
 
4855f867
 static int16_t background, attr_orig;
 static HANDLE con;
 #else
2191aef9
 
1842b275
 static const uint32_t color[16 + AV_CLASS_CATEGORY_NB] = {
     [AV_LOG_PANIC  /8] =  52 << 16 | 196 << 8 | 0x41,
     [AV_LOG_FATAL  /8] = 208 <<  8 | 0x41,
     [AV_LOG_ERROR  /8] = 196 <<  8 | 0x11,
     [AV_LOG_WARNING/8] = 226 <<  8 | 0x03,
     [AV_LOG_INFO   /8] = 253 <<  8 | 0x09,
     [AV_LOG_VERBOSE/8] =  40 <<  8 | 0x02,
     [AV_LOG_DEBUG  /8] =  34 <<  8 | 0x02,
8e8219e0
     [AV_LOG_TRACE  /8] =  34 <<  8 | 0x07,
1842b275
     [16+AV_CLASS_CATEGORY_NA              ] = 250 << 8 | 0x09,
     [16+AV_CLASS_CATEGORY_INPUT           ] = 219 << 8 | 0x15,
     [16+AV_CLASS_CATEGORY_OUTPUT          ] = 201 << 8 | 0x05,
     [16+AV_CLASS_CATEGORY_MUXER           ] = 213 << 8 | 0x15,
     [16+AV_CLASS_CATEGORY_DEMUXER         ] = 207 << 8 | 0x05,
     [16+AV_CLASS_CATEGORY_ENCODER         ] =  51 << 8 | 0x16,
     [16+AV_CLASS_CATEGORY_DECODER         ] =  39 << 8 | 0x06,
     [16+AV_CLASS_CATEGORY_FILTER          ] = 155 << 8 | 0x12,
     [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 192 << 8 | 0x14,
     [16+AV_CLASS_CATEGORY_SWSCALER        ] = 153 << 8 | 0x14,
     [16+AV_CLASS_CATEGORY_SWRESAMPLER     ] = 147 << 8 | 0x14,
19b3a250
     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT ] = 213 << 8 | 0x15,
     [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT  ] = 207 << 8 | 0x05,
     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT ] = 213 << 8 | 0x15,
     [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT  ] = 207 << 8 | 0x05,
     [16+AV_CLASS_CATEGORY_DEVICE_OUTPUT       ] = 213 << 8 | 0x15,
     [16+AV_CLASS_CATEGORY_DEVICE_INPUT        ] = 207 << 8 | 0x05,
2191aef9
 };
 
4855f867
 #endif
425b45d4
 static int use_color = -1;
51e026d1
 
061e340c
 static void check_color_terminal(void)
425b45d4
 {
f87ad3a0
 #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE
061e340c
     CONSOLE_SCREEN_BUFFER_INFO con_info;
     con = GetStdHandle(STD_ERROR_HANDLE);
     use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
                 !getenv("AV_LOG_FORCE_NOCOLOR");
     if (use_color) {
         GetConsoleScreenBufferInfo(con, &con_info);
         attr_orig  = con_info.wAttributes;
         background = attr_orig & 0xF0;
     }
4855f867
 #elif HAVE_ISATTY
7763118c
     char *term = getenv("TERM");
061e340c
     use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
                 (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
c16e0061
     if (   getenv("AV_LOG_FORCE_256COLOR")
609ef897
         || (term && strstr(term, "256color")))
061e340c
         use_color *= 256;
62044024
 #else
061e340c
     use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
                !getenv("AV_LOG_FORCE_NOCOLOR");
62044024
 #endif
061e340c
 }
 
7763118c
 static void colored_fputs(int level, int tint, const char *str)
061e340c
 {
7ac2f7e4
     int local_use_color;
061e340c
     if (!*str)
         return;
 
     if (use_color < 0)
         check_color_terminal();
62044024
 
7ac2f7e4
     if (level == AV_LOG_INFO/8) local_use_color = 0;
     else                        local_use_color = use_color;
 
f87ad3a0
 #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE
7ac2f7e4
     if (local_use_color)
2c21e2ee
         SetConsoleTextAttribute(con, background | color[level]);
51e026d1
     fputs(str, stderr);
7ac2f7e4
     if (local_use_color)
2c21e2ee
         SetConsoleTextAttribute(con, attr_orig);
 #else
7ac2f7e4
     if (local_use_color == 1) {
2c21e2ee
         fprintf(stderr,
54904525
                 "\033[%"PRIu32";3%"PRIu32"m%s\033[0m",
2c21e2ee
                 (color[level] >> 4) & 15,
                 color[level] & 15,
                 str);
c16e0061
     } else if (tint && use_color == 256) {
         fprintf(stderr,
54904525
                 "\033[48;5;%"PRIu32"m\033[38;5;%dm%s\033[0m",
c16e0061
                 (color[level] >> 16) & 0xff,
                 tint,
                 str);
7ac2f7e4
     } else if (local_use_color == 256) {
2c21e2ee
         fprintf(stderr,
54904525
                 "\033[48;5;%"PRIu32"m\033[38;5;%"PRIu32"m%s\033[0m",
2c21e2ee
                 (color[level] >> 16) & 0xff,
                 (color[level] >> 8) & 0xff,
                 str);
     } else
         fputs(str, stderr);
 #endif
 
51e026d1
 }
 
425b45d4
 const char *av_default_item_name(void *ptr)
 {
     return (*(AVClass **) ptr)->class_name;
8d2a5139
 }
 
e69263cd
 AVClassCategory av_default_get_category(void *ptr)
 {
     return (*(AVClass **) ptr)->category;
 }
 
0a51362b
 static void sanitize(uint8_t *line){
     while(*line){
         if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
             *line='?';
         line++;
     }
 }
 
e69263cd
 static int get_category(void *ptr){
     AVClass *avc = *(AVClass **) ptr;
eb7efc12
     if(    !avc
         || (avc->version&0xFF)<100
e69263cd
         ||  avc->version < (51 << 16 | 59 << 8)
eb7efc12
         ||  avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
 
e69263cd
     if(avc->get_category)
         return avc->get_category(ptr) + 16;
 
eb7efc12
     return avc->category + 16;
 }
 
669a09fb
 static const char *get_level_str(int level)
 {
     switch (level) {
     case AV_LOG_QUIET:
         return "quiet";
     case AV_LOG_DEBUG:
         return "debug";
     case AV_LOG_VERBOSE:
         return "verbose";
     case AV_LOG_INFO:
         return "info";
     case AV_LOG_WARNING:
         return "warning";
     case AV_LOG_ERROR:
         return "error";
     case AV_LOG_FATAL:
         return "fatal";
     case AV_LOG_PANIC:
         return "panic";
     default:
         return "";
     }
 }
 
74744790
 static void format_line(void *avcl, int level, const char *fmt, va_list vl,
669a09fb
                         AVBPrint part[4], int *print_prefix, int type[2])
4b45de0e
 {
25a1ba81
     AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
148310ca
     av_bprint_init(part+0, 0, 1);
     av_bprint_init(part+1, 0, 1);
669a09fb
     av_bprint_init(part+2, 0, 1);
     av_bprint_init(part+3, 0, 65536);
148310ca
 
eb7efc12
     if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
26c6fec9
     if (*print_prefix && avc) {
63de9e7d
         if (avc->parent_log_context_offset) {
25a1ba81
             AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
425b45d4
                                    avc->parent_log_context_offset);
             if (parent && *parent) {
148310ca
                 av_bprintf(part+0, "[%s @ %p] ",
425b45d4
                          (*parent)->item_name(parent), parent);
7edb984d
                 if(type) type[0] = get_category(parent);
4880cfd9
             }
         }
148310ca
         av_bprintf(part+1, "[%s @ %p] ",
25a1ba81
                  avc->item_name(avcl), avcl);
74744790
         if(type) type[1] = get_category(avcl);
669a09fb
 
         if (flags & AV_LOG_PRINT_LEVEL)
             av_bprintf(part+2, "[%s] ", get_level_str(level));
4880cfd9
     }
4b45de0e
 
669a09fb
     av_vbprintf(part+3, fmt, vl);
26c6fec9
 
669a09fb
     if(*part[0].str || *part[1].str || *part[2].str || *part[3].str) {
         char lastc = part[3].len && part[3].len <= part[3].size ? part[3].str[part[3].len - 1] : 0;
c25d1ba5
         *print_prefix = lastc == '\n' || lastc == '\r';
     }
eb7efc12
 }
 
 void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
                         char *line, int line_size, int *print_prefix)
 {
fb918712
     av_log_format_line2(ptr, level, fmt, vl, line, line_size, print_prefix);
 }
 
 int av_log_format_line2(void *ptr, int level, const char *fmt, va_list vl,
                         char *line, int line_size, int *print_prefix)
 {
669a09fb
     AVBPrint part[4];
fb918712
     int ret;
 
148310ca
     format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
fb918712
     ret = snprintf(line, line_size, "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
669a09fb
     av_bprint_finalize(part+3, NULL);
fb918712
     return ret;
26c6fec9
 }
 
 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
 {
     static int print_prefix = 1;
     static int count;
258dfff8
     static char prev[LINE_SZ];
669a09fb
     AVBPrint part[4];
258dfff8
     char line[LINE_SZ];
26c6fec9
     static int is_atty;
eb7efc12
     int type[2];
49e040e8
     unsigned tint = 0;
c16e0061
 
49e040e8
     if (level >= 0) {
         tint = level & 0xff00;
         level &= 0xff;
     }
4b45de0e
 
26c6fec9
     if (level > av_log_level)
         return;
423ae313
 #if HAVE_PTHREADS
     pthread_mutex_lock(&mutex);
 #endif
 
148310ca
     format_line(ptr, level, fmt, vl, part, &print_prefix, type);
669a09fb
     snprintf(line, sizeof(line), "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
c157fe63
 
 #if HAVE_ISATTY
425b45d4
     if (!is_atty)
         is_atty = isatty(2) ? 1 : -1;
c157fe63
 #endif
 
39bb26f9
     if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
         *line && line[strlen(line) - 1] != '\r'){
b9c353ff
         count++;
425b45d4
         if (is_atty == 1)
d7cd001f
             fprintf(stderr, "    Last message repeated %d times\r", count);
423ae313
         goto end;
b9c353ff
     }
425b45d4
     if (count > 0) {
b9c353ff
         fprintf(stderr, "    Last message repeated %d times\n", count);
425b45d4
         count = 0;
b9c353ff
     }
     strcpy(prev, line);
148310ca
     sanitize(part[0].str);
c16e0061
     colored_fputs(type[0], 0, part[0].str);
148310ca
     sanitize(part[1].str);
c16e0061
     colored_fputs(type[1], 0, part[1].str);
148310ca
     sanitize(part[2].str);
8e8219e0
     colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[2].str);
669a09fb
     sanitize(part[3].str);
8e8219e0
     colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[3].str);
eaa2d123
 
 #if CONFIG_VALGRIND_BACKTRACE
     if (level <= BACKTRACE_LOGLEVEL)
63cdb6e4
         VALGRIND_PRINTF_BACKTRACE("%s", "");
eaa2d123
 #endif
423ae313
 end:
7c710764
     av_bprint_finalize(part+3, NULL);
423ae313
 #if HAVE_PTHREADS
     pthread_mutex_unlock(&mutex);
 #endif
4b45de0e
 }
 
425b45d4
 static void (*av_log_callback)(void*, int, const char*, va_list) =
     av_log_default_callback;
4b45de0e
 
 void av_log(void* avcl, int level, const char *fmt, ...)
 {
425b45d4
     AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
4b45de0e
     va_list vl;
     va_start(vl, fmt);
425b45d4
     if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
         avc->log_level_offset_offset && level >= AV_LOG_FATAL)
         level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
4b45de0e
     av_vlog(avcl, level, fmt, vl);
     va_end(vl);
 }
 
 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
 {
3ed65d98
     void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
     if (log_callback)
         log_callback(avcl, level, fmt, vl);
4b45de0e
 }
 
 int av_log_get_level(void)
 {
     return av_log_level;
 }
 
 void av_log_set_level(int level)
 {
     av_log_level = level;
 }
 
1c1c80f0
 void av_log_set_flags(int arg)
 {
425b45d4
     flags = arg;
1c1c80f0
 }
 
4f698be8
 int av_log_get_flags(void)
 {
     return flags;
 }
 
4b45de0e
 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
 {
     av_log_callback = callback;
 }
f099d3d1
 
0420c810
 static void missing_feature_sample(int sample, void *avc, const char *msg,
                                    va_list argument_list)
f099d3d1
 {
     av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
905e7eb4
     av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg "
f099d3d1
            "version to the newest one from Git. If the problem still "
            "occurs, it means that your file has a feature which has not "
            "been implemented.\n");
     if (sample)
         av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2422a7ff
                "of this file to ftp://upload.ffmpeg.org/incoming/ "
3e7b4b9f
                "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
f099d3d1
 }
 
 void avpriv_request_sample(void *avc, const char *msg, ...)
 {
     va_list argument_list;
 
     va_start(argument_list, msg);
     missing_feature_sample(1, avc, msg, argument_list);
     va_end(argument_list);
 }
 
 void avpriv_report_missing_feature(void *avc, const char *msg, ...)
 {
     va_list argument_list;
 
     va_start(argument_list, msg);
     missing_feature_sample(0, avc, msg, argument_list);
     va_end(argument_list);
 }