sync with en/mplayer.1 rev. 30611
[mplayer/greg.git] / mp_msg.c
blob93f793d7c8c680c0857e30cbab6d9b3c34c4ca1a
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <string.h>
24 #include "config.h"
26 #ifdef CONFIG_ICONV
27 #include <iconv.h>
28 #include <errno.h>
29 /**
30 * \brief gets the name of the system's terminal character set
31 * \return a malloced string indicating the system charset
33 * Be warned that this function on many systems is in no way thread-safe
34 * since it modifies global data
36 char* get_term_charset(void);
37 #endif
39 #if defined(FOR_MENCODER)
40 #undef CONFIG_GUI
41 int use_gui;
42 #else
43 #include "gui/interface.h"
44 #endif
46 #include "mp_msg.h"
48 /* maximum message length of mp_msg */
49 #define MSGSIZE_MAX 3072
51 int mp_msg_levels[MSGT_MAX]; // verbose level of this module. initialized to -2
52 int mp_msg_level_all = MSGL_STATUS;
53 int verbose = 0;
54 int mp_msg_color = 0;
55 int mp_msg_module = 0;
56 #ifdef CONFIG_ICONV
57 char *mp_msg_charset = NULL;
58 static char *old_charset = NULL;
59 static iconv_t msgiconv;
60 #endif
62 const char* filename_recode(const char* filename)
64 #if !defined(CONFIG_ICONV) || !defined(MSG_CHARSET)
65 return filename;
66 #else
67 static iconv_t inv_msgiconv = (iconv_t)(-1);
68 static char recoded_filename[MSGSIZE_MAX];
69 size_t filename_len, max_path;
70 char* precoded;
71 if (!mp_msg_charset ||
72 !strcasecmp(mp_msg_charset, MSG_CHARSET) ||
73 !strcasecmp(mp_msg_charset, "noconv"))
74 return filename;
75 if (inv_msgiconv == (iconv_t)(-1)) {
76 inv_msgiconv = iconv_open(MSG_CHARSET, mp_msg_charset);
77 if (inv_msgiconv == (iconv_t)(-1))
78 return filename;
80 filename_len = strlen(filename);
81 max_path = MSGSIZE_MAX - 4;
82 precoded = recoded_filename;
83 if (iconv(inv_msgiconv, &filename, &filename_len,
84 &precoded, &max_path) == (size_t)(-1) && errno == E2BIG) {
85 precoded[0] = precoded[1] = precoded[2] = '.';
86 precoded += 3;
88 *precoded = '\0';
89 return recoded_filename;
90 #endif
93 void mp_msg_init(void){
94 int i;
95 char *env = getenv("MPLAYER_VERBOSE");
96 if (env)
97 verbose = atoi(env);
98 for(i=0;i<MSGT_MAX;i++) mp_msg_levels[i] = -2;
99 mp_msg_levels[MSGT_IDENTIFY] = -1; // no -identify output by default
100 #ifdef CONFIG_ICONV
101 mp_msg_charset = getenv("MPLAYER_CHARSET");
102 if (!mp_msg_charset)
103 mp_msg_charset = get_term_charset();
104 #endif
107 int mp_msg_test(int mod, int lev)
109 return lev <= (mp_msg_levels[mod] == -2 ? mp_msg_level_all + verbose : mp_msg_levels[mod]);
112 static void set_msg_color(FILE* stream, int lev)
114 static const unsigned char v_colors[10] = {9, 1, 3, 15, 7, 2, 2, 8, 8, 8};
115 int c = v_colors[lev];
116 #ifdef MP_ANNOY_ME
117 /* that's only a silly color test */
119 int c;
120 static int flag = 1;
121 if (flag)
122 for(c = 0; c < 24; c++)
123 printf("\033[%d;3%dm*** COLOR TEST %d ***\n", c>7, c&7, c);
124 flag = 0;
126 #endif
127 if (mp_msg_color)
128 fprintf(stream, "\033[%d;3%dm", c >> 3, c & 7);
131 static void print_msg_module(FILE* stream, int mod)
133 static const char *module_text[MSGT_MAX] = {
134 "GLOBAL",
135 "CPLAYER",
136 "GPLAYER",
137 "VIDEOOUT",
138 "AUDIOOUT",
139 "DEMUXER",
140 "DS",
141 "DEMUX",
142 "HEADER",
143 "AVSYNC",
144 "AUTOQ",
145 "CFGPARSER",
146 "DECAUDIO",
147 "DECVIDEO",
148 "SEEK",
149 "WIN32",
150 "OPEN",
151 "DVD",
152 "PARSEES",
153 "LIRC",
154 "STREAM",
155 "CACHE",
156 "MENCODER",
157 "XACODEC",
158 "TV",
159 "OSDEP",
160 "SPUDEC",
161 "PLAYTREE",
162 "INPUT",
163 "VFILTER",
164 "OSD",
165 "NETWORK",
166 "CPUDETECT",
167 "CODECCFG",
168 "SWS",
169 "VOBSUB",
170 "SUBREADER",
171 "AFILTER",
172 "NETST",
173 "MUXER",
174 "OSDMENU",
175 "IDENTIFY",
176 "RADIO",
177 "ASS",
178 "LOADER",
179 "STATUSLINE",
181 int c2 = (mod + 1) % 15 + 1;
183 if (!mp_msg_module)
184 return;
185 if (mp_msg_color)
186 fprintf(stream, "\033[%d;3%dm", c2 >> 3, c2 & 7);
187 fprintf(stream, "%9s", module_text[mod]);
188 if (mp_msg_color)
189 fprintf(stream, "\033[0;37m");
190 fprintf(stream, ": ");
193 void mp_msg(int mod, int lev, const char *format, ... ){
194 va_list va;
195 char tmp[MSGSIZE_MAX];
196 FILE *stream = lev <= MSGL_WARN ? stderr : stdout;
197 static int header = 1;
199 if (!mp_msg_test(mod, lev)) return; // do not display
200 va_start(va, format);
201 vsnprintf(tmp, MSGSIZE_MAX, format, va);
202 va_end(va);
203 tmp[MSGSIZE_MAX-2] = '\n';
204 tmp[MSGSIZE_MAX-1] = 0;
206 #ifdef CONFIG_GUI
207 if(use_gui)
208 guiMessageBox(lev, tmp);
209 #endif
211 #if defined(CONFIG_ICONV) && defined(MSG_CHARSET)
212 if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv")) {
213 char tmp2[MSGSIZE_MAX];
214 size_t inlen = strlen(tmp), outlen = MSGSIZE_MAX;
215 char *in = tmp, *out = tmp2;
216 if (!old_charset || strcmp(old_charset, mp_msg_charset)) {
217 if (old_charset) {
218 free(old_charset);
219 iconv_close(msgiconv);
221 msgiconv = iconv_open(mp_msg_charset, MSG_CHARSET);
222 old_charset = strdup(mp_msg_charset);
224 if (msgiconv == (iconv_t)(-1)) {
225 fprintf(stderr,"iconv: conversion from %s to %s unsupported\n"
226 ,MSG_CHARSET,mp_msg_charset);
227 }else{
228 memset(tmp2, 0, MSGSIZE_MAX);
229 while (iconv(msgiconv, &in, &inlen, &out, &outlen) == -1) {
230 if (!inlen || !outlen)
231 break;
232 *out++ = *in++;
233 outlen--; inlen--;
235 strncpy(tmp, tmp2, MSGSIZE_MAX);
236 tmp[MSGSIZE_MAX-1] = 0;
237 tmp[MSGSIZE_MAX-2] = '\n';
240 #endif
242 if (header)
243 print_msg_module(stream, mod);
244 set_msg_color(stream, lev);
245 header = tmp[strlen(tmp)-1] == '\n' || tmp[strlen(tmp)-1] == '\r';
247 fprintf(stream, "%s", tmp);
248 fflush(stream);