Fix printing of ID_LENGTH
[mplayer/kovensky.git] / mp_msg.c
blob6742cf0ddeaf02df943b2a9e5d02b1a6173cd861
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <string.h>
6 #include "config.h"
8 #ifdef CONFIG_ICONV
9 #include <iconv.h>
10 #include <errno.h>
11 /**
12 * \brief gets the name of the system's terminal character set
13 * \return a malloced string indicating the system charset
15 * Be warned that this function on many systems is in no way thread-safe
16 * since it modifies global data
18 char* get_term_charset(void);
19 #endif
21 #include "mp_msg.h"
23 #ifdef _WIN32
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
26 #include <io.h>
27 short stdoutAttrs = 0;
28 HANDLE hConOut = INVALID_HANDLE_VALUE;
29 static const unsigned char ansi2win32[10]=
32 FOREGROUND_RED,
33 FOREGROUND_GREEN,
34 FOREGROUND_GREEN | FOREGROUND_RED,
35 FOREGROUND_BLUE,
36 FOREGROUND_BLUE | FOREGROUND_RED,
37 FOREGROUND_BLUE | FOREGROUND_GREEN,
38 FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED,
39 FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED,
40 FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
42 #endif
44 /* maximum message length of mp_msg */
45 #define MSGSIZE_MAX 3072
47 int mp_msg_levels[MSGT_MAX]; // verbose level of this module. initialized to -2
48 int mp_msg_level_all = MSGL_STATUS;
49 int verbose = 0;
50 int mp_msg_color = 0;
51 int mp_msg_module = 0;
52 #ifdef CONFIG_ICONV
53 char *mp_msg_charset = NULL;
54 static char *old_charset = NULL;
55 static iconv_t msgiconv;
56 #endif
58 const char* filename_recode(const char* filename)
60 #if !defined(CONFIG_ICONV) || !defined(MSG_CHARSET)
61 return filename;
62 #else
63 static iconv_t inv_msgiconv = (iconv_t)(-1);
64 static char recoded_filename[MSGSIZE_MAX];
65 size_t filename_len, max_path;
66 char* precoded;
67 if (!mp_msg_charset ||
68 !strcasecmp(mp_msg_charset, MSG_CHARSET) ||
69 !strcasecmp(mp_msg_charset, "noconv"))
70 return filename;
71 if (inv_msgiconv == (iconv_t)(-1)) {
72 inv_msgiconv = iconv_open(MSG_CHARSET, mp_msg_charset);
73 if (inv_msgiconv == (iconv_t)(-1))
74 return filename;
76 filename_len = strlen(filename);
77 max_path = MSGSIZE_MAX - 4;
78 precoded = recoded_filename;
79 if (iconv(inv_msgiconv, &filename, &filename_len,
80 &precoded, &max_path) == (size_t)(-1) && errno == E2BIG) {
81 precoded[0] = precoded[1] = precoded[2] = '.';
82 precoded += 3;
84 *precoded = '\0';
85 return recoded_filename;
86 #endif
89 void mp_msg_init(void){
90 int i;
91 char *env = getenv("MPLAYER_VERBOSE");
92 if (env)
93 verbose = atoi(env);
94 for(i=0;i<MSGT_MAX;i++) mp_msg_levels[i] = -2;
95 mp_msg_levels[MSGT_IDENTIFY] = -1; // no -identify output by default
96 #ifdef CONFIG_ICONV
97 mp_msg_charset = getenv("MPLAYER_CHARSET");
98 if (!mp_msg_charset)
99 mp_msg_charset = get_term_charset();
100 #endif
101 #ifdef _WIN32
103 CONSOLE_SCREEN_BUFFER_INFO cinfo;
104 long cmode = 0;
106 hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
107 if ((hConOut == NULL) || (hConOut == INVALID_HANDLE_VALUE))
109 hConOut = NULL;
110 fprintf(stderr, "Cannot get Console handle of stdout\n");
111 return;
114 GetConsoleMode(hConOut, &cmode);
115 cmode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
116 SetConsoleMode(hConOut, cmode);
118 GetConsoleScreenBufferInfo(hConOut, &cinfo);
119 stdoutAttrs = cinfo.wAttributes;
121 #endif
124 int mp_msg_test(int mod, int lev)
126 return lev <= (mp_msg_levels[mod] == -2 ? mp_msg_level_all + verbose : mp_msg_levels[mod]);
129 static void set_msg_color(FILE* stream, int lev)
131 static const unsigned char v_colors[10] = {9, 1, 3, 15, 7, 2, 2, 8, 8, 8};
132 int c = v_colors[lev];
133 #ifdef MP_ANNOY_ME
134 /* that's only a silly color test */
136 int c;
137 static int flag = 1;
138 if (flag)
139 for(c = 0; c < 24; c++)
140 printf("\033[%d;3%dm*** COLOR TEST %d ***\n", c>7, c&7, c);
141 flag = 0;
143 #endif
145 #ifdef _WIN32
146 if (mp_msg_color)
147 SetConsoleTextAttribute(hConOut, ansi2win32[c] | FOREGROUND_INTENSITY);
148 #else
149 if (mp_msg_color)
150 fprintf(stream, "\033[%d;3%dm", c >> 3, c & 7);
151 #endif
154 static void print_msg_module(FILE* stream, int mod)
156 static const char *module_text[MSGT_MAX] = {
157 "GLOBAL",
158 "CPLAYER",
159 "GPLAYER",
160 "VIDEOOUT",
161 "AUDIOOUT",
162 "DEMUXER",
163 "DS",
164 "DEMUX",
165 "HEADER",
166 "AVSYNC",
167 "AUTOQ",
168 "CFGPARSER",
169 "DECAUDIO",
170 "DECVIDEO",
171 "SEEK",
172 "WIN32",
173 "OPEN",
174 "DVD",
175 "PARSEES",
176 "LIRC",
177 "STREAM",
178 "CACHE",
179 "MENCODER",
180 "XACODEC",
181 "TV",
182 "OSDEP",
183 "SPUDEC",
184 "PLAYTREE",
185 "INPUT",
186 "VFILTER",
187 "OSD",
188 "NETWORK",
189 "CPUDETECT",
190 "CODECCFG",
191 "SWS",
192 "VOBSUB",
193 "SUBREADER",
194 "AFILTER",
195 "NETST",
196 "MUXER",
197 "OSDMENU",
198 "IDENTIFY",
199 "RADIO",
200 "ASS",
201 "LOADER",
202 "STATUSLINE",
204 int c2 = (mod + 1) % 15 + 1;
205 if (!mp_msg_module)
206 return;
207 #ifdef _WIN32
208 if (mp_msg_color)
209 SetConsoleTextAttribute(hConOut, ansi2win32[c2&7] | FOREGROUND_INTENSITY);
210 fprintf(stream, "%9s", module_text[mod]);
211 if (mp_msg_color)
212 SetConsoleTextAttribute(hConOut, stdoutAttrs);
213 #else
214 if (mp_msg_color)
215 fprintf(stream, "\033[%d;3%dm", c2 >> 3, c2 & 7);
216 fprintf(stream, "%9s", module_text[mod]);
217 if (mp_msg_color)
218 fprintf(stream, "\033[0;37m");
219 #endif
220 fprintf(stream, ": ");
223 void mp_msg(int mod, int lev, const char *format, ... ){
224 va_list va;
225 char tmp[MSGSIZE_MAX];
226 FILE *stream = lev <= MSGL_WARN ? stderr : stdout;
227 static int header = 1;
229 if (!mp_msg_test(mod, lev)) return; // do not display
230 va_start(va, format);
231 vsnprintf(tmp, MSGSIZE_MAX, format, va);
232 va_end(va);
233 tmp[MSGSIZE_MAX-2] = '\n';
234 tmp[MSGSIZE_MAX-1] = 0;
236 #if defined(CONFIG_ICONV) && defined(MSG_CHARSET)
237 if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv")) {
238 char tmp2[MSGSIZE_MAX];
239 size_t inlen = strlen(tmp), outlen = MSGSIZE_MAX;
240 char *in = tmp, *out = tmp2;
241 if (!old_charset || strcmp(old_charset, mp_msg_charset)) {
242 if (old_charset) {
243 free(old_charset);
244 iconv_close(msgiconv);
246 msgiconv = iconv_open(mp_msg_charset, MSG_CHARSET);
247 old_charset = strdup(mp_msg_charset);
249 if (msgiconv == (iconv_t)(-1)) {
250 fprintf(stderr,"iconv: conversion from %s to %s unsupported\n"
251 ,MSG_CHARSET,mp_msg_charset);
252 }else{
253 memset(tmp2, 0, MSGSIZE_MAX);
254 while (iconv(msgiconv, &in, &inlen, &out, &outlen) == -1) {
255 if (!inlen || !outlen)
256 break;
257 *out++ = *in++;
258 outlen--; inlen--;
260 strncpy(tmp, tmp2, MSGSIZE_MAX);
261 tmp[MSGSIZE_MAX-1] = 0;
262 tmp[MSGSIZE_MAX-2] = '\n';
265 #endif
267 if (header)
268 print_msg_module(stream, mod);
269 set_msg_color(stream, lev);
270 header = tmp[strlen(tmp)-1] == '\n' || tmp[strlen(tmp)-1] == '\r';
272 fprintf(stream, "%s", tmp);
273 fflush(stream);
276 char *mp_gtext(const char *string)
278 return string;