Line data Source code
1 : /*
2 : * log functions
3 : * Copyright (c) 2003 Michel Bardiaux
4 : *
5 : * This file is part of Libav.
6 : *
7 : * Libav 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 : * Libav 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 Libav; 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_UNISTD_H
30 : #include <unistd.h>
31 : #endif
32 : #if HAVE_IO_H
33 : #include <io.h>
34 : #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 : static int16_t background, attr_orig;
50 : static HANDLE con;
51 : #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
52 : #define reset_color() SetConsoleTextAttribute(con, attr_orig)
53 : #define print_256color(x)
54 : #else
55 : static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };
56 : #define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
57 : #define print_256color(x) fprintf(stderr, "\033[38;5;%dm", x)
58 : #define reset_color() fprintf(stderr, "\033[0m")
59 : #endif
60 : static int use_color = -1;
61 :
62 0 : static void check_color_terminal(void)
63 : {
64 : #if HAVE_SETCONSOLETEXTATTRIBUTE
65 : CONSOLE_SCREEN_BUFFER_INFO con_info;
66 : con = GetStdHandle(STD_ERROR_HANDLE);
67 : use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
68 : !getenv("AV_LOG_FORCE_NOCOLOR");
69 : if (use_color) {
70 : GetConsoleScreenBufferInfo(con, &con_info);
71 : attr_orig = con_info.wAttributes;
72 : background = attr_orig & 0xF0;
73 : }
74 : #elif HAVE_ISATTY
75 0 : char *term = getenv("TERM");
76 0 : use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
77 0 : (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
78 0 : if (use_color)
79 0 : use_color += term && strstr(term, "256color");
80 : #else
81 : use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
82 : !getenv("AV_LOG_FORCE_NOCOLOR");
83 : #endif
84 0 : }
85 :
86 0 : static void colored_fputs(int level, int tint, const char *str)
87 : {
88 0 : if (use_color < 0)
89 0 : check_color_terminal();
90 :
91 0 : switch (use_color) {
92 : case 1:
93 0 : set_color(level);
94 0 : break;
95 : case 2:
96 0 : set_color(level);
97 0 : if (tint)
98 0 : print_256color(tint);
99 0 : break;
100 : default:
101 0 : break;
102 : }
103 0 : fputs(str, stderr);
104 0 : if (use_color) {
105 0 : reset_color();
106 : }
107 0 : }
108 :
109 0 : const char *av_default_item_name(void *ptr)
110 : {
111 0 : return (*(AVClass **) ptr)->class_name;
112 : }
113 :
114 0 : void av_log_default_callback(void *avcl, int level, const char *fmt, va_list vl)
115 : {
116 : static int print_prefix = 1;
117 : static int count;
118 : static char prev[1024];
119 : char line[1024];
120 : static int is_atty;
121 0 : AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
122 0 : unsigned tint = level & 0xff00;
123 :
124 0 : level &= 0xff;
125 :
126 0 : if (level > av_log_level)
127 0 : return;
128 0 : line[0] = 0;
129 0 : if (print_prefix && avc) {
130 0 : if (avc->parent_log_context_offset) {
131 0 : AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
132 0 : avc->parent_log_context_offset);
133 0 : if (parent && *parent) {
134 0 : snprintf(line, sizeof(line), "[%s @ %p] ",
135 0 : (*parent)->item_name(parent), parent);
136 : }
137 : }
138 0 : snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
139 0 : avc->item_name(avcl), avcl);
140 : }
141 :
142 0 : vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
143 :
144 0 : print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
145 :
146 : #if HAVE_ISATTY
147 0 : if (!is_atty)
148 0 : is_atty = isatty(2) ? 1 : -1;
149 : #endif
150 :
151 0 : if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
152 0 : !strncmp(line, prev, sizeof line)) {
153 0 : count++;
154 0 : if (is_atty == 1)
155 0 : fprintf(stderr, " Last message repeated %d times\r", count);
156 0 : return;
157 : }
158 0 : if (count > 0) {
159 0 : fprintf(stderr, " Last message repeated %d times\n", count);
160 0 : count = 0;
161 : }
162 0 : colored_fputs(av_clip(level >> 3, 0, 6), tint >> 8, line);
163 0 : av_strlcpy(prev, line, sizeof line);
164 : }
165 :
166 : static void (*av_log_callback)(void*, int, const char*, va_list) =
167 : av_log_default_callback;
168 :
169 0 : void av_log(void* avcl, int level, const char *fmt, ...)
170 : {
171 0 : AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
172 : va_list vl;
173 0 : va_start(vl, fmt);
174 0 : if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
175 0 : avc->log_level_offset_offset && level >= AV_LOG_FATAL)
176 0 : level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
177 0 : av_vlog(avcl, level, fmt, vl);
178 0 : va_end(vl);
179 0 : }
180 :
181 0 : void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
182 : {
183 0 : av_log_callback(avcl, level, fmt, vl);
184 0 : }
185 :
186 0 : int av_log_get_level(void)
187 : {
188 0 : return av_log_level;
189 : }
190 :
191 0 : void av_log_set_level(int level)
192 : {
193 0 : av_log_level = level;
194 0 : }
195 :
196 0 : void av_log_set_flags(int arg)
197 : {
198 0 : flags = arg;
199 0 : }
200 :
201 0 : void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
202 : {
203 0 : av_log_callback = callback;
204 0 : }
205 :
206 0 : static void missing_feature_sample(int sample, void *avc, const char *msg,
207 : va_list argument_list)
208 : {
209 0 : av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
210 0 : av_log(avc, AV_LOG_WARNING, " is not implemented. Update your Libav "
211 : "version to the newest one from Git. If the problem still "
212 : "occurs, it means that your file has a feature which has not "
213 : "been implemented.\n");
214 0 : if (sample)
215 0 : av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
216 : "of this file to ftp://upload.libav.org/incoming/ "
217 : "and contact the libav-devel mailing list.\n");
218 0 : }
219 :
220 0 : void avpriv_request_sample(void *avc, const char *msg, ...)
221 : {
222 : va_list argument_list;
223 :
224 0 : va_start(argument_list, msg);
225 0 : missing_feature_sample(1, avc, msg, argument_list);
226 0 : va_end(argument_list);
227 0 : }
228 :
229 0 : void avpriv_report_missing_feature(void *avc, const char *msg, ...)
230 : {
231 : va_list argument_list;
232 :
233 0 : va_start(argument_list, msg);
234 0 : missing_feature_sample(0, avc, msg, argument_list);
235 0 : va_end(argument_list);
236 0 : }
|