Libav
log.c
Go to the documentation of this file.
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 
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 #if HAVE_VALGRIND_VALGRIND_H
44 #include <valgrind/valgrind.h>
45 /* this is the log level at which valgrind will output a full backtrace */
46 #define BACKTRACE_LOGLEVEL AV_LOG_ERROR
47 #endif
48 
50 static int flags;
51 
52 #define NB_LEVELS 8
53 #if HAVE_SETCONSOLETEXTATTRIBUTE
54 #include <windows.h>
55 static const uint8_t color[NB_LEVELS] = { 12, 12, 12, 14, 7, 10, 11, 8};
56 static int16_t background, attr_orig;
57 static HANDLE con;
58 #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
59 #define reset_color() SetConsoleTextAttribute(con, attr_orig)
60 #define print_256color(x)
61 #else
62 static const uint8_t color[NB_LEVELS] = {
63  0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06, 0x07
64 };
65 #define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
66 #define print_256color(x) fprintf(stderr, "\033[38;5;%dm", x)
67 #define reset_color() fprintf(stderr, "\033[0m")
68 #endif
69 static int use_color = -1;
70 
71 static void check_color_terminal(void)
72 {
73 #if HAVE_SETCONSOLETEXTATTRIBUTE
74  CONSOLE_SCREEN_BUFFER_INFO con_info;
75  con = GetStdHandle(STD_ERROR_HANDLE);
76  use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
77  !getenv("AV_LOG_FORCE_NOCOLOR");
78  if (use_color) {
79  GetConsoleScreenBufferInfo(con, &con_info);
80  attr_orig = con_info.wAttributes;
81  background = attr_orig & 0xF0;
82  }
83 #elif HAVE_ISATTY
84  char *term = getenv("TERM");
85  use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
86  (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
87  if (use_color)
88  use_color += term && strstr(term, "256color");
89 #else
90  use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
91  !getenv("AV_LOG_FORCE_NOCOLOR");
92 #endif
93 }
94 
95 static void colored_fputs(int level, int tint, const char *str)
96 {
97  if (use_color < 0)
99 
100  switch (use_color) {
101  case 1:
102  set_color(level);
103  break;
104  case 2:
105  set_color(level);
106  if (tint)
107  print_256color(tint);
108  break;
109  default:
110  break;
111  }
112  fputs(str, stderr);
113  if (use_color) {
114  reset_color();
115  }
116 }
117 
118 const char *av_default_item_name(void *ptr)
119 {
120  return (*(AVClass **) ptr)->class_name;
121 }
122 
123 void av_log_default_callback(void *avcl, int level, const char *fmt, va_list vl)
124 {
125  static int print_prefix = 1;
126  static int count;
127  static char prev[1024];
128  char line[1024];
129  static int is_atty;
130  AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
131  unsigned tint = level & 0xff00;
132 
133  level &= 0xff;
134 
135  if (level > av_log_level)
136  return;
137  line[0] = 0;
138  if (print_prefix && avc) {
139  if (avc->parent_log_context_offset) {
140  AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
142  if (parent && *parent) {
143  snprintf(line, sizeof(line), "[%s @ %p] ",
144  (*parent)->item_name(parent), parent);
145  }
146  }
147  snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
148  avc->item_name(avcl), avcl);
149  }
150 
151  vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
152 
153  print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
154 
155 #if HAVE_ISATTY
156  if (!is_atty)
157  is_atty = isatty(2) ? 1 : -1;
158 #endif
159 
160  if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
161  !strncmp(line, prev, sizeof line)) {
162  count++;
163  if (is_atty == 1)
164  fprintf(stderr, " Last message repeated %d times\r", count);
165  return;
166  }
167  if (count > 0) {
168  fprintf(stderr, " Last message repeated %d times\n", count);
169  count = 0;
170  }
171  colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, line);
172  av_strlcpy(prev, line, sizeof line);
173 
174 #if CONFIG_VALGRIND_BACKTRACE
175  if (level <= BACKTRACE_LOGLEVEL)
176  VALGRIND_PRINTF_BACKTRACE("");
177 #endif
178 }
179 
180 static void (*av_log_callback)(void*, int, const char*, va_list) =
182 
183 void av_log(void* avcl, int level, const char *fmt, ...)
184 {
185  AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
186  va_list vl;
187  va_start(vl, fmt);
188  if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
189  avc->log_level_offset_offset && level >= AV_LOG_FATAL)
190  level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
191  av_vlog(avcl, level, fmt, vl);
192  va_end(vl);
193 }
194 
195 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
196 {
197  av_log_callback(avcl, level, fmt, vl);
198 }
199 
201 {
202  return av_log_level;
203 }
204 
206 {
208 }
209 
210 void av_log_set_flags(int arg)
211 {
212  flags = arg;
213 }
214 
215 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
216 {
217  av_log_callback = callback;
218 }
219 
220 static void missing_feature_sample(int sample, void *avc, const char *msg,
221  va_list argument_list)
222 {
223  av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
224  av_log(avc, AV_LOG_WARNING, " is not implemented. Update your Libav "
225  "version to the newest one from Git. If the problem still "
226  "occurs, it means that your file has a feature which has not "
227  "been implemented.\n");
228  if (sample)
229  av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
230  "of this file to ftp://upload.libav.org/incoming/ "
231  "and contact the libav-devel mailing list.\n");
232 }
233 
234 void avpriv_request_sample(void *avc, const char *msg, ...)
235 {
236  va_list argument_list;
237 
238  va_start(argument_list, msg);
239  missing_feature_sample(1, avc, msg, argument_list);
240  va_end(argument_list);
241 }
242 
243 void avpriv_report_missing_feature(void *avc, const char *msg, ...)
244 {
245  va_list argument_list;
246 
247  va_start(argument_list, msg);
248  missing_feature_sample(0, avc, msg, argument_list);
249  va_end(argument_list);
250 }
#define set_color(x)
Definition: log.c:65
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
void av_log_set_level(int level)
Set the log level.
Definition: log.c:205
#define reset_color()
Definition: log.c:67
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:118
external API header
#define sample
static void missing_feature_sample(int sample, void *avc, const char *msg, va_list argument_list)
Definition: log.c:220
uint8_t
const char *(* item_name)(void *ctx)
A pointer to a function which returns the name of a context instance ctx associated with the class...
Definition: log.h:45
int log_level_offset_offset
Offset in the structure where log_level_offset is stored.
Definition: log.h:66
static int flags
Definition: log.c:50
#define print_256color(x)
Definition: log.c:66
#define isatty(fd)
Definition: checkasm.c:53
Definition: graph2dot.c:48
#define AV_LOG_SKIP_REPEATED
Skip repeated messages, this requires the user app to use av_log() instead of (f)printf as the 2 woul...
Definition: log.h:274
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:183
int av_log_get_level(void)
Get the current log level.
Definition: log.c:200
static int av_log_level
Definition: log.c:49
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
void av_log_default_callback(void *avcl, int level, const char *fmt, va_list vl)
Default logging callback.
Definition: log.c:123
common internal API header
static void check_color_terminal(void)
Definition: log.c:71
void avpriv_report_missing_feature(void *avc, const char *msg,...)
Definition: log.c:243
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition: log.c:215
static void(* av_log_callback)(void *, int, const char *, va_list)
Definition: log.c:180
#define NB_LEVELS
Definition: log.c:52
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
Describe the class of an AVClass context structure.
Definition: log.h:34
static const uint8_t color[NB_LEVELS]
Definition: log.c:62
void av_vlog(void *avcl, int level, const char *fmt, va_list vl)
Send the specified message to the log if the level is less than or equal to the current av_log_level...
Definition: log.c:195
uint8_t level
Definition: svq3.c:204
int version
LIBAVUTIL_VERSION with which this structure was created.
Definition: log.h:60
common internal and external API header
int parent_log_context_offset
Offset in the structure where a pointer to the parent context for logging is stored.
Definition: log.h:75
static int use_color
Definition: log.c:69
void avpriv_request_sample(void *avc, const char *msg,...)
Definition: log.c:234
void av_log_set_flags(int arg)
Definition: log.c:210
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:118
static void colored_fputs(int level, int tint, const char *str)
Definition: log.c:95