log.c 4.77 KB
Newer Older
1 2 3 4
/*
 * log functions
 * Copyright (c) 2003 Michel Bardiaux
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * 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
18
 * License along with Libav; if not, write to the Free Software
19 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24
 * logging functions
25 26
 */

27
#include <unistd.h>
28
#include <stdlib.h>
29
#include "avutil.h"
30
#include "log.h"
31

32 33 34
#if LIBAVUTIL_VERSION_MAJOR > 50
static
#endif
35
int av_log_level = AV_LOG_INFO;
36
static int flags;
37

38
#if defined(_WIN32) && !defined(__MINGW32CE__)
Ramiro Polla's avatar
Ramiro Polla committed
39 40 41 42
#include <windows.h>
static const uint8_t color[] = {12,12,12,14,7,7,7};
static int16_t background, attr_orig;
static HANDLE con;
43
#define set_color(x)  SetConsoleTextAttribute(con, background | color[x])
Ramiro Polla's avatar
Ramiro Polla committed
44 45
#define reset_color() SetConsoleTextAttribute(con, attr_orig)
#else
46
static const uint8_t color[]={0x41,0x41,0x11,0x03,9,9,9};
Ramiro Polla's avatar
Ramiro Polla committed
47 48 49
#define set_color(x)  fprintf(stderr, "\033[%d;3%dm", color[x]>>4, color[x]&15)
#define reset_color() fprintf(stderr, "\033[0m")
#endif
50
static int use_color=-1;
51 52

#undef fprintf
53
static void colored_fputs(int level, const char *str){
54
    if(use_color<0){
55
#if defined(_WIN32) && !defined(__MINGW32CE__)
Ramiro Polla's avatar
Ramiro Polla committed
56 57
        CONSOLE_SCREEN_BUFFER_INFO con_info;
        con = GetStdHandle(STD_ERROR_HANDLE);
58
        use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") && !getenv("FFMPEG_FORCE_NOCOLOR");
Ramiro Polla's avatar
Ramiro Polla committed
59 60 61 62 63 64
        if (use_color) {
            GetConsoleScreenBufferInfo(con, &con_info);
            attr_orig  = con_info.wAttributes;
            background = attr_orig & 0xF0;
        }
#elif HAVE_ISATTY
65 66
        use_color= !getenv("NO_COLOR") && !getenv("FFMPEG_FORCE_NOCOLOR") &&
            (getenv("TERM") && isatty(2) || getenv("FFMPEG_FORCE_COLOR"));
67
#else
68
        use_color= getenv("FFMPEG_FORCE_COLOR") && !getenv("NO_COLOR") && !getenv("FFMPEG_FORCE_NOCOLOR");
69 70 71
#endif
    }

72
    if(use_color){
Ramiro Polla's avatar
Ramiro Polla committed
73
        set_color(level);
74 75
    }
    fputs(str, stderr);
76
    if(use_color){
Ramiro Polla's avatar
Ramiro Polla committed
77
        reset_color();
78 79 80
    }
}

81 82 83 84
const char* av_default_item_name(void* ptr){
    return (*(AVClass**)ptr)->class_name;
}

85
void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
86 87
{
    static int print_prefix=1;
88 89
    static int count;
    static char line[1024], prev[1024];
90
    static int is_atty;
91 92 93
    AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
    if(level>av_log_level)
        return;
94
    line[0]=0;
95 96
#undef fprintf
    if(print_prefix && avc) {
97 98 99
        if(avc->version >= (50<<16 | 15<<8 | 3) && avc->parent_log_context_offset){
            AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
            if(parent && *parent){
100
                snprintf(line, sizeof(line), "[%s @ %p] ", (*parent)->item_name(parent), parent);
101 102
            }
        }
103
        snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
104
    }
105

106
    vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
107

108
    print_prefix= line[strlen(line)-1] == '\n';
109 110

#if HAVE_ISATTY
111
    if(!is_atty) is_atty= isatty(2) ? 1 : -1;
112 113
#endif

114
    if(print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
115
        count++;
116
        if(is_atty==1)
Michael Niedermayer's avatar
indent  
Michael Niedermayer committed
117
            fprintf(stderr, "    Last message repeated %d times\r", count);
118 119 120 121 122 123
        return;
    }
    if(count>0){
        fprintf(stderr, "    Last message repeated %d times\n", count);
        count=0;
    }
124
    colored_fputs(av_clip(level>>3, 0, 6), line);
125
    strcpy(prev, line);
126 127 128 129 130 131
}

static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;

void av_log(void* avcl, int level, const char *fmt, ...)
{
132
    AVClass* avc= avcl ? *(AVClass**)avcl : NULL;
133 134
    va_list vl;
    va_start(vl, fmt);
135 136
    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);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    av_vlog(avcl, level, fmt, vl);
    va_end(vl);
}

void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
{
    av_log_callback(avcl, level, fmt, vl);
}

int av_log_get_level(void)
{
    return av_log_level;
}

void av_log_set_level(int level)
{
    av_log_level = level;
}

156 157 158 159 160
void av_log_set_flags(int arg)
{
    flags= arg;
}

161 162 163 164
void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
{
    av_log_callback = callback;
}