Revert r30100: It breaks some mov and asf samples.
[mplayer/glamo.git] / mp_msg.c
blobb20d99ee6f369a3e63c9cb88a435b44673c58a02
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 #if defined(FOR_MENCODER)
22 #undef CONFIG_GUI
23 int use_gui;
24 #else
25 #include "gui/interface.h"
26 #endif
28 #include "mp_msg.h"
30 /* maximum message length of mp_msg */
31 #define MSGSIZE_MAX 3072
33 int mp_msg_levels[MSGT_MAX]; // verbose level of this module. initialized to -2
34 int mp_msg_level_all = MSGL_STATUS;
35 int verbose = 0;
36 int mp_msg_color = 0;
37 int mp_msg_module = 0;
38 #ifdef CONFIG_ICONV
39 char *mp_msg_charset = NULL;
40 static char *old_charset = NULL;
41 static iconv_t msgiconv;
42 #endif
44 const char* filename_recode(const char* filename)
46 #if !defined(CONFIG_ICONV) || !defined(MSG_CHARSET)
47 return filename;
48 #else
49 static iconv_t inv_msgiconv = (iconv_t)(-1);
50 static char recoded_filename[MSGSIZE_MAX];
51 size_t filename_len, max_path;
52 char* precoded;
53 if (!mp_msg_charset ||
54 !strcasecmp(mp_msg_charset, MSG_CHARSET) ||
55 !strcasecmp(mp_msg_charset, "noconv"))
56 return filename;
57 if (inv_msgiconv == (iconv_t)(-1)) {
58 inv_msgiconv = iconv_open(MSG_CHARSET, mp_msg_charset);
59 if (inv_msgiconv == (iconv_t)(-1))
60 return filename;
62 filename_len = strlen(filename);
63 max_path = MSGSIZE_MAX - 4;
64 precoded = recoded_filename;
65 if (iconv(inv_msgiconv, &filename, &filename_len,
66 &precoded, &max_path) == (size_t)(-1) && errno == E2BIG) {
67 precoded[0] = precoded[1] = precoded[2] = '.';
68 precoded += 3;
70 *precoded = '\0';
71 return recoded_filename;
72 #endif
75 void mp_msg_init(void){
76 int i;
77 char *env = getenv("MPLAYER_VERBOSE");
78 if (env)
79 verbose = atoi(env);
80 for(i=0;i<MSGT_MAX;i++) mp_msg_levels[i] = -2;
81 mp_msg_levels[MSGT_IDENTIFY] = -1; // no -identify output by default
82 #ifdef CONFIG_ICONV
83 mp_msg_charset = getenv("MPLAYER_CHARSET");
84 if (!mp_msg_charset)
85 mp_msg_charset = get_term_charset();
86 #endif
89 int mp_msg_test(int mod, int lev)
91 return lev <= (mp_msg_levels[mod] == -2 ? mp_msg_level_all + verbose : mp_msg_levels[mod]);
94 static void set_msg_color(FILE* stream, int lev)
96 static const unsigned char v_colors[10] = {9, 1, 3, 15, 7, 2, 2, 8, 8, 8};
97 int c = v_colors[lev];
98 #ifdef MP_ANNOY_ME
99 /* that's only a silly color test */
101 int c;
102 static int flag = 1;
103 if (flag)
104 for(c = 0; c < 24; c++)
105 printf("\033[%d;3%dm*** COLOR TEST %d ***\n", c>7, c&7, c);
106 flag = 0;
108 #endif
109 if (mp_msg_color)
110 fprintf(stream, "\033[%d;3%dm", c >> 3, c & 7);
113 static void print_msg_module(FILE* stream, int mod)
115 static const char *module_text[MSGT_MAX] = {
116 "GLOBAL",
117 "CPLAYER",
118 "GPLAYER",
119 "VIDEOOUT",
120 "AUDIOOUT",
121 "DEMUXER",
122 "DS",
123 "DEMUX",
124 "HEADER",
125 "AVSYNC",
126 "AUTOQ",
127 "CFGPARSER",
128 "DECAUDIO",
129 "DECVIDEO",
130 "SEEK",
131 "WIN32",
132 "OPEN",
133 "DVD",
134 "PARSEES",
135 "LIRC",
136 "STREAM",
137 "CACHE",
138 "MENCODER",
139 "XACODEC",
140 "TV",
141 "OSDEP",
142 "SPUDEC",
143 "PLAYTREE",
144 "INPUT",
145 "VFILTER",
146 "OSD",
147 "NETWORK",
148 "CPUDETECT",
149 "CODECCFG",
150 "SWS",
151 "VOBSUB",
152 "SUBREADER",
153 "AFILTER",
154 "NETST",
155 "MUXER",
156 "OSDMENU",
157 "IDENTIFY",
158 "RADIO",
159 "ASS",
160 "LOADER",
161 "STATUSLINE",
163 int c2 = (mod + 1) % 15 + 1;
165 if (!mp_msg_module)
166 return;
167 if (mp_msg_color)
168 fprintf(stream, "\033[%d;3%dm", c2 >> 3, c2 & 7);
169 fprintf(stream, "%9s", module_text[mod]);
170 if (mp_msg_color)
171 fprintf(stream, "\033[0;37m");
172 fprintf(stream, ": ");
175 void mp_msg(int mod, int lev, const char *format, ... ){
176 va_list va;
177 char tmp[MSGSIZE_MAX];
178 FILE *stream = lev <= MSGL_WARN ? stderr : stdout;
179 static int header = 1;
181 if (!mp_msg_test(mod, lev)) return; // do not display
182 va_start(va, format);
183 vsnprintf(tmp, MSGSIZE_MAX, format, va);
184 va_end(va);
185 tmp[MSGSIZE_MAX-2] = '\n';
186 tmp[MSGSIZE_MAX-1] = 0;
188 #ifdef CONFIG_GUI
189 if(use_gui)
190 guiMessageBox(lev, tmp);
191 #endif
193 #if defined(CONFIG_ICONV) && defined(MSG_CHARSET)
194 if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv")) {
195 char tmp2[MSGSIZE_MAX];
196 size_t inlen = strlen(tmp), outlen = MSGSIZE_MAX;
197 char *in = tmp, *out = tmp2;
198 if (!old_charset || strcmp(old_charset, mp_msg_charset)) {
199 if (old_charset) {
200 free(old_charset);
201 iconv_close(msgiconv);
203 msgiconv = iconv_open(mp_msg_charset, MSG_CHARSET);
204 old_charset = strdup(mp_msg_charset);
206 if (msgiconv == (iconv_t)(-1)) {
207 fprintf(stderr,"iconv: conversion from %s to %s unsupported\n"
208 ,MSG_CHARSET,mp_msg_charset);
209 }else{
210 memset(tmp2, 0, MSGSIZE_MAX);
211 while (iconv(msgiconv, &in, &inlen, &out, &outlen) == -1) {
212 if (!inlen || !outlen)
213 break;
214 *out++ = *in++;
215 outlen--; inlen--;
217 strncpy(tmp, tmp2, MSGSIZE_MAX);
218 tmp[MSGSIZE_MAX-1] = 0;
219 tmp[MSGSIZE_MAX-2] = '\n';
222 #endif
224 if (header)
225 print_msg_module(stream, mod);
226 set_msg_color(stream, lev);
227 header = tmp[strlen(tmp)-1] == '\n' || tmp[strlen(tmp)-1] == '\r';
229 fprintf(stream, "%s", tmp);
230 fflush(stream);