log.c 6.18 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 28
#include "config.h"

29
#if HAVE_UNISTD_H
30
#include <unistd.h>
31
#endif
32 33 34
#if HAVE_IO_H
#include <io.h>
#endif
35
#include <stdarg.h>
36
#include <stdlib.h>
37
#include "avstring.h"
38
#include "avutil.h"
39
#include "common.h"
40
#include "internal.h"
41
#include "log.h"
42

43
static int av_log_level = AV_LOG_INFO;
44
static int flags;
45

46
#if HAVE_SETCONSOLETEXTATTRIBUTE
47
#include <windows.h>
48
static const uint8_t color[] = { 12, 12, 12, 14, 7, 10, 11 };
49 50
static int16_t background, attr_orig;
static HANDLE con;
51
#define set_color(x)  SetConsoleTextAttribute(con, background | color[x])
52 53
#define reset_color() SetConsoleTextAttribute(con, attr_orig)
#else
54
static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };
55
#define set_color(x)  fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
56 57
#define reset_color() fprintf(stderr, "\033[0m")
#endif
58
static int use_color = -1;
59

60 61 62
static void colored_fputs(int level, const char *str)
{
    if (use_color < 0) {
63
#if HAVE_SETCONSOLETEXTATTRIBUTE
64 65
        CONSOLE_SCREEN_BUFFER_INFO con_info;
        con = GetStdHandle(STD_ERROR_HANDLE);
66 67
        use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
                    !getenv("AV_LOG_FORCE_NOCOLOR");
68 69 70 71 72 73
        if (use_color) {
            GetConsoleScreenBufferInfo(con, &con_info);
            attr_orig  = con_info.wAttributes;
            background = attr_orig & 0xF0;
        }
#elif HAVE_ISATTY
74 75 76
        use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
                    (getenv("TERM") && isatty(2) ||
                     getenv("AV_LOG_FORCE_COLOR"));
77
#else
78 79
        use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
                   !getenv("AV_LOG_FORCE_NOCOLOR");
80 81 82
#endif
    }

83
    if (use_color) {
84
        set_color(level);
85 86
    }
    fputs(str, stderr);
87
    if (use_color) {
88
        reset_color();
89 90 91
    }
}

92 93 94
const char *av_default_item_name(void *ptr)
{
    return (*(AVClass **) ptr)->class_name;
95 96
}

97
void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
98
{
99
    static int print_prefix = 1;
100
    static int count;
101 102
    static char prev[1024];
    char line[1024];
103
    static int is_atty;
104 105
    AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
    if (level > av_log_level)
106
        return;
107 108
    line[0] = 0;
    if (print_prefix && avc) {
109
        if (avc->parent_log_context_offset) {
110 111 112 113 114
            AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
                                   avc->parent_log_context_offset);
            if (parent && *parent) {
                snprintf(line, sizeof(line), "[%s @ %p] ",
                         (*parent)->item_name(parent), parent);
115 116
            }
        }
117 118
        snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
                 avc->item_name(ptr), ptr);
119
    }
120

121
    vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
122

123
    print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
124 125

#if HAVE_ISATTY
126 127
    if (!is_atty)
        is_atty = isatty(2) ? 1 : -1;
128 129
#endif

130 131
    if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
        !strncmp(line, prev, sizeof line)) {
132
        count++;
133
        if (is_atty == 1)
Michael Niedermayer's avatar
Michael Niedermayer committed
134
            fprintf(stderr, "    Last message repeated %d times\r", count);
135 136
        return;
    }
137
    if (count > 0) {
138
        fprintf(stderr, "    Last message repeated %d times\n", count);
139
        count = 0;
140
    }
141
    colored_fputs(av_clip(level >> 3, 0, 6), line);
142
    av_strlcpy(prev, line, sizeof line);
143 144
}

145 146
static void (*av_log_callback)(void*, int, const char*, va_list) =
    av_log_default_callback;
147 148 149

void av_log(void* avcl, int level, const char *fmt, ...)
{
150
    AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
151 152
    va_list vl;
    va_start(vl, fmt);
153 154 155
    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);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    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;
}

175 176
void av_log_set_flags(int arg)
{
177
    flags = arg;
178 179
}

180 181 182 183
void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
{
    av_log_callback = callback;
}
184

185 186
static void missing_feature_sample(int sample, void *avc, const char *msg,
                                   va_list argument_list)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
{
    av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
    av_log(avc, AV_LOG_WARNING, " is not implemented. Update your Libav "
           "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 "
               "of this file to ftp://upload.libav.org/incoming/ "
               "and contact the libav-devel mailing list.\n");
}

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);
}