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 
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 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  char *term = getenv("TERM");
76  use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
77  (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
78  use_color += term && strstr(term, "256color");
79 #else
80  use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
81  !getenv("AV_LOG_FORCE_NOCOLOR");
82 #endif
83 }
84 
85 static void colored_fputs(int level, int tint, const char *str)
86 {
87  if (use_color < 0)
89 
90  switch (use_color) {
91  case 1:
92  set_color(level);
93  break;
94  case 2:
95  set_color(level);
96  if (tint)
97  print_256color(tint);
98  break;
99  default:
100  break;
101  }
102  fputs(str, stderr);
103  if (use_color) {
104  reset_color();
105  }
106 }
107 
108 const char *av_default_item_name(void *ptr)
109 {
110  return (*(AVClass **) ptr)->class_name;
111 }
112 
113 void av_log_default_callback(void *avcl, int level, const char *fmt, va_list vl)
114 {
115  static int print_prefix = 1;
116  static int count;
117  static char prev[1024];
118  char line[1024];
119  static int is_atty;
120  AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
121  unsigned tint = level & 0xff00;
122 
123  level &= 0xff;
124 
125  if (level > av_log_level)
126  return;
127  line[0] = 0;
128  if (print_prefix && avc) {
129  if (avc->parent_log_context_offset) {
130  AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
132  if (parent && *parent) {
133  snprintf(line, sizeof(line), "[%s @ %p] ",
134  (*parent)->item_name(parent), parent);
135  }
136  }
137  snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
138  avc->item_name(avcl), avcl);
139  }
140 
141  vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
142 
143  print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
144 
145 #if HAVE_ISATTY
146  if (!is_atty)
147  is_atty = isatty(2) ? 1 : -1;
148 #endif
149 
150  if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
151  !strncmp(line, prev, sizeof line)) {
152  count++;
153  if (is_atty == 1)
154  fprintf(stderr, " Last message repeated %d times\r", count);
155  return;
156  }
157  if (count > 0) {
158  fprintf(stderr, " Last message repeated %d times\n", count);
159  count = 0;
160  }
161  colored_fputs(av_clip(level >> 3, 0, 6), tint >> 8, line);
162  av_strlcpy(prev, line, sizeof line);
163 }
164 
165 static void (*av_log_callback)(void*, int, const char*, va_list) =
167 
168 void av_log(void* avcl, int level, const char *fmt, ...)
169 {
170  AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
171  va_list vl;
172  va_start(vl, fmt);
173  if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
174  avc->log_level_offset_offset && level >= AV_LOG_FATAL)
175  level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
176  av_vlog(avcl, level, fmt, vl);
177  va_end(vl);
178 }
179 
180 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
181 {
182  av_log_callback(avcl, level, fmt, vl);
183 }
184 
186 {
187  return av_log_level;
188 }
189 
191 {
193 }
194 
195 void av_log_set_flags(int arg)
196 {
197  flags = arg;
198 }
199 
200 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
201 {
202  av_log_callback = callback;
203 }
204 
205 static void missing_feature_sample(int sample, void *avc, const char *msg,
206  va_list argument_list)
207 {
208  av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
209  av_log(avc, AV_LOG_WARNING, " is not implemented. Update your Libav "
210  "version to the newest one from Git. If the problem still "
211  "occurs, it means that your file has a feature which has not "
212  "been implemented.\n");
213  if (sample)
214  av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
215  "of this file to ftp://upload.libav.org/incoming/ "
216  "and contact the libav-devel mailing list.\n");
217 }
218 
219 void avpriv_request_sample(void *avc, const char *msg, ...)
220 {
221  va_list argument_list;
222 
223  va_start(argument_list, msg);
224  missing_feature_sample(1, avc, msg, argument_list);
225  va_end(argument_list);
226 }
227 
228 void avpriv_report_missing_feature(void *avc, const char *msg, ...)
229 {
230  va_list argument_list;
231 
232  va_start(argument_list, msg);
233  missing_feature_sample(0, avc, msg, argument_list);
234  va_end(argument_list);
235 }
#define set_color(x)
Definition: log.c:56
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
void av_log_set_level(int level)
Set the log level.
Definition: log.c:190
#define reset_color()
Definition: log.c:58
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:108
external API header
#define sample
static void missing_feature_sample(int sample, void *avc, const char *msg, va_list argument_list)
Definition: log.c:205
uint8_t
int log_level_offset_offset
Offset in the structure where log_level_offset is stored.
Definition: log.h:65
static int flags
Definition: log.c:44
#define print_256color(x)
Definition: log.c:57
Definition: graph2dot.c:49
#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:265
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
int av_log_get_level(void)
Get the current log level.
Definition: log.c:185
static int av_log_level
Definition: log.c:43
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:113
common internal API header
static void check_color_terminal(void)
Definition: log.c:62
void avpriv_report_missing_feature(void *avc, const char *msg,...)
Definition: log.c:228
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition: log.c:200
static void(* av_log_callback)(void *, int, const char *, va_list)
Definition: log.c:165
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:44
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
Describe the class of an AVClass context structure.
Definition: log.h:33
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:180
uint8_t level
Definition: svq3.c:147
int version
LIBAVUTIL_VERSION with which this structure was created.
Definition: log.h:59
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:74
static const uint8_t color[]
Definition: log.c:55
static int use_color
Definition: log.c:60
void avpriv_request_sample(void *avc, const char *msg,...)
Definition: log.c:219
void av_log_set_flags(int arg)
Definition: log.c:195
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:117
static void colored_fputs(int level, int tint, const char *str)
Definition: log.c:85