log: pass the correct parameters to missing_feature_sample
[libav.git] / libavutil / log.c
CommitLineData
4b45de0e
LA
1/*
2 * log functions
3 * Copyright (c) 2003 Michel Bardiaux
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
4b45de0e
LA
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
4b45de0e 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
4b45de0e
LA
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
4b45de0e
LA
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
7d685b48 24 * logging functions
4b45de0e
LA
25 */
26
539df611
MR
27#include "config.h"
28
145a8096 29#if HAVE_UNISTD_H
51e026d1 30#include <unistd.h>
539df611 31#endif
b70abd5b
MS
32#if HAVE_IO_H
33#include <io.h>
34#endif
f099d3d1 35#include <stdarg.h>
62044024 36#include <stdlib.h>
1a5e4fd8 37#include "avstring.h"
4b45de0e 38#include "avutil.h"
1d9c2dc8 39#include "common.h"
f099d3d1 40#include "internal.h"
77652a6a 41#include "log.h"
4b45de0e 42
490a022d 43static int av_log_level = AV_LOG_INFO;
1c1c80f0 44static int flags;
4b45de0e 45
7e6a11bc 46#if HAVE_SETCONSOLETEXTATTRIBUTE
4855f867 47#include <windows.h>
780ff97f 48static const uint8_t color[] = { 12, 12, 12, 14, 7, 10, 11 };
4855f867
RP
49static int16_t background, attr_orig;
50static HANDLE con;
db16e3ca 51#define set_color(x) SetConsoleTextAttribute(con, background | color[x])
4855f867
RP
52#define reset_color() SetConsoleTextAttribute(con, attr_orig)
53#else
780ff97f 54static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };
425b45d4 55#define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
4855f867
RP
56#define reset_color() fprintf(stderr, "\033[0m")
57#endif
425b45d4 58static int use_color = -1;
51e026d1 59
425b45d4
YM
60static void colored_fputs(int level, const char *str)
61{
62 if (use_color < 0) {
7e6a11bc 63#if HAVE_SETCONSOLETEXTATTRIBUTE
4855f867
RP
64 CONSOLE_SCREEN_BUFFER_INFO con_info;
65 con = GetStdHandle(STD_ERROR_HANDLE);
425b45d4
YM
66 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
67 !getenv("AV_LOG_FORCE_NOCOLOR");
4855f867
RP
68 if (use_color) {
69 GetConsoleScreenBufferInfo(con, &con_info);
70 attr_orig = con_info.wAttributes;
71 background = attr_orig & 0xF0;
72 }
73#elif HAVE_ISATTY
425b45d4
YM
74 use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
75 (getenv("TERM") && isatty(2) ||
76 getenv("AV_LOG_FORCE_COLOR"));
62044024 77#else
425b45d4
YM
78 use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
79 !getenv("AV_LOG_FORCE_NOCOLOR");
62044024
MN
80#endif
81 }
82
425b45d4 83 if (use_color) {
4855f867 84 set_color(level);
51e026d1
MN
85 }
86 fputs(str, stderr);
425b45d4 87 if (use_color) {
4855f867 88 reset_color();
51e026d1
MN
89 }
90}
91
425b45d4
YM
92const char *av_default_item_name(void *ptr)
93{
94 return (*(AVClass **) ptr)->class_name;
8d2a5139
MN
95}
96
411983c1 97void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
4b45de0e 98{
425b45d4 99 static int print_prefix = 1;
b9c353ff 100 static int count;
0247bdee
RT
101 static char prev[1024];
102 char line[1024];
8a190533 103 static int is_atty;
425b45d4
YM
104 AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
105 if (level > av_log_level)
4b45de0e 106 return;
425b45d4 107 line[0] = 0;
425b45d4 108 if (print_prefix && avc) {
63de9e7d 109 if (avc->parent_log_context_offset) {
425b45d4
YM
110 AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
111 avc->parent_log_context_offset);
112 if (parent && *parent) {
113 snprintf(line, sizeof(line), "[%s @ %p] ",
114 (*parent)->item_name(parent), parent);
4880cfd9
MN
115 }
116 }
425b45d4
YM
117 snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
118 avc->item_name(ptr), ptr);
4880cfd9 119 }
4b45de0e 120
b9c353ff 121 vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
4b45de0e 122
425b45d4 123 print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
c157fe63
MN
124
125#if HAVE_ISATTY
425b45d4
YM
126 if (!is_atty)
127 is_atty = isatty(2) ? 1 : -1;
c157fe63
MN
128#endif
129
425b45d4
YM
130 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
131 !strncmp(line, prev, sizeof line)) {
b9c353ff 132 count++;
425b45d4 133 if (is_atty == 1)
d7cd001f 134 fprintf(stderr, " Last message repeated %d times\r", count);
b9c353ff
MN
135 return;
136 }
425b45d4 137 if (count > 0) {
b9c353ff 138 fprintf(stderr, " Last message repeated %d times\n", count);
425b45d4 139 count = 0;
b9c353ff 140 }
425b45d4 141 colored_fputs(av_clip(level >> 3, 0, 6), line);
1a5e4fd8 142 av_strlcpy(prev, line, sizeof line);
4b45de0e
LA
143}
144
425b45d4
YM
145static void (*av_log_callback)(void*, int, const char*, va_list) =
146 av_log_default_callback;
4b45de0e
LA
147
148void av_log(void* avcl, int level, const char *fmt, ...)
149{
425b45d4 150 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
4b45de0e
LA
151 va_list vl;
152 va_start(vl, fmt);
425b45d4
YM
153 if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
154 avc->log_level_offset_offset && level >= AV_LOG_FATAL)
155 level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
4b45de0e
LA
156 av_vlog(avcl, level, fmt, vl);
157 va_end(vl);
158}
159
160void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
161{
162 av_log_callback(avcl, level, fmt, vl);
163}
164
165int av_log_get_level(void)
166{
167 return av_log_level;
168}
169
170void av_log_set_level(int level)
171{
172 av_log_level = level;
173}
174
1c1c80f0
MN
175void av_log_set_flags(int arg)
176{
425b45d4 177 flags = arg;
1c1c80f0
MN
178}
179
4b45de0e
LA
180void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
181{
182 av_log_callback = callback;
183}
f099d3d1 184
0420c810
LB
185static void missing_feature_sample(int sample, void *avc, const char *msg,
186 va_list argument_list)
f099d3d1 187{
f099d3d1
DB
188 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
189 av_log(avc, AV_LOG_WARNING, " is not implemented. Update your Libav "
190 "version to the newest one from Git. If the problem still "
191 "occurs, it means that your file has a feature which has not "
192 "been implemented.\n");
193 if (sample)
194 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
195 "of this file to ftp://upload.libav.org/incoming/ "
196 "and contact the libav-devel mailing list.\n");
f099d3d1
DB
197}
198
199void avpriv_request_sample(void *avc, const char *msg, ...)
200{
201 va_list argument_list;
202
203 va_start(argument_list, msg);
204 missing_feature_sample(1, avc, msg, argument_list);
205 va_end(argument_list);
206}
207
208void avpriv_report_missing_feature(void *avc, const char *msg, ...)
209{
210 va_list argument_list;
211
212 va_start(argument_list, msg);
213 missing_feature_sample(0, avc, msg, argument_list);
214 va_end(argument_list);
215}