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 <stdlib.h>
36 #include "avstring.h"
37 #include "avutil.h"
38 #include "common.h"
39 #include "log.h"
40 
42 static int flags;
43 
44 #if HAVE_SETCONSOLETEXTATTRIBUTE
45 #include <windows.h>
46 static const uint8_t color[] = { 12, 12, 12, 14, 7, 10, 11 };
47 static int16_t background, attr_orig;
48 static HANDLE con;
49 #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
50 #define reset_color() SetConsoleTextAttribute(con, attr_orig)
51 #else
52 static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };
53 #define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
54 #define reset_color() fprintf(stderr, "\033[0m")
55 #endif
56 static int use_color = -1;
57 
58 static void colored_fputs(int level, const char *str)
59 {
60  if (use_color < 0) {
61 #if HAVE_SETCONSOLETEXTATTRIBUTE
62  CONSOLE_SCREEN_BUFFER_INFO con_info;
63  con = GetStdHandle(STD_ERROR_HANDLE);
64  use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
65  !getenv("AV_LOG_FORCE_NOCOLOR");
66  if (use_color) {
67  GetConsoleScreenBufferInfo(con, &con_info);
68  attr_orig = con_info.wAttributes;
69  background = attr_orig & 0xF0;
70  }
71 #elif HAVE_ISATTY
72  use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
73  (getenv("TERM") && isatty(2) ||
74  getenv("AV_LOG_FORCE_COLOR"));
75 #else
76  use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
77  !getenv("AV_LOG_FORCE_NOCOLOR");
78 #endif
79  }
80 
81  if (use_color) {
82  set_color(level);
83  }
84  fputs(str, stderr);
85  if (use_color) {
86  reset_color();
87  }
88 }
89 
90 const char *av_default_item_name(void *ptr)
91 {
92  return (*(AVClass **) ptr)->class_name;
93 }
94 
95 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
96 {
97  static int print_prefix = 1;
98  static int count;
99  static char prev[1024];
100  char line[1024];
101  static int is_atty;
102  AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
103  if (level > av_log_level)
104  return;
105  line[0] = 0;
106  if (print_prefix && avc) {
107  if (avc->parent_log_context_offset) {
108  AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
110  if (parent && *parent) {
111  snprintf(line, sizeof(line), "[%s @ %p] ",
112  (*parent)->item_name(parent), parent);
113  }
114  }
115  snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
116  avc->item_name(ptr), ptr);
117  }
118 
119  vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
120 
121  print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
122 
123 #if HAVE_ISATTY
124  if (!is_atty)
125  is_atty = isatty(2) ? 1 : -1;
126 #endif
127 
128  if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
129  !strncmp(line, prev, sizeof line)) {
130  count++;
131  if (is_atty == 1)
132  fprintf(stderr, " Last message repeated %d times\r", count);
133  return;
134  }
135  if (count > 0) {
136  fprintf(stderr, " Last message repeated %d times\n", count);
137  count = 0;
138  }
139  colored_fputs(av_clip(level >> 3, 0, 6), line);
140  av_strlcpy(prev, line, sizeof line);
141 }
142 
143 static void (*av_log_callback)(void*, int, const char*, va_list) =
145 
146 void av_log(void* avcl, int level, const char *fmt, ...)
147 {
148  AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
149  va_list vl;
150  va_start(vl, fmt);
151  if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
152  avc->log_level_offset_offset && level >= AV_LOG_FATAL)
153  level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
154  av_vlog(avcl, level, fmt, vl);
155  va_end(vl);
156 }
157 
158 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
159 {
160  av_log_callback(avcl, level, fmt, vl);
161 }
162 
164 {
165  return av_log_level;
166 }
167 
169 {
171 }
172 
173 void av_log_set_flags(int arg)
174 {
175  flags = arg;
176 }
177 
178 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
179 {
180  av_log_callback = callback;
181 }