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