reverted useless r24539
[mplayer/glamo.git] / mp_msg.c
blob7f4ff70e87aff162c8be1feb7f92ee25ab715f5a
2 //#define MSG_USE_COLORS
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <string.h>
9 #include "config.h"
11 #ifdef USE_ICONV
12 #include <iconv.h>
13 #include <errno.h>
14 extern char* get_term_charset(void);
15 #endif
17 #if defined(FOR_MENCODER)
18 #undef HAVE_NEW_GUI
19 #endif
21 #ifdef HAVE_NEW_GUI
22 #include "gui/interface.h"
23 #endif
24 #include "mp_msg.h"
26 /* maximum message length of mp_msg */
27 #define MSGSIZE_MAX 3072
29 int mp_msg_levels[MSGT_MAX]; // verbose level of this module. inited to -2
30 int mp_msg_level_all = MSGL_STATUS;
31 int verbose = 0;
32 #ifdef USE_ICONV
33 char *mp_msg_charset = NULL;
34 static char *old_charset = NULL;
35 static iconv_t msgiconv;
36 #endif
38 const char* filename_recode(const char* filename)
40 #if !defined(USE_ICONV) || !defined(MSG_CHARSET)
41 return filename;
42 #else
43 static iconv_t inv_msgiconv = (iconv_t)(-1);
44 static char recoded_filename[MSGSIZE_MAX];
45 size_t filename_len, max_path;
46 char* precoded;
47 if (!mp_msg_charset ||
48 !strcasecmp(mp_msg_charset, MSG_CHARSET) ||
49 !strcasecmp(mp_msg_charset, "noconv"))
50 return filename;
51 if (inv_msgiconv == (iconv_t)(-1)) {
52 inv_msgiconv = iconv_open(MSG_CHARSET, mp_msg_charset);
53 if (inv_msgiconv == (iconv_t)(-1))
54 return filename;
56 filename_len = strlen(filename);
57 max_path = MSGSIZE_MAX - 4;
58 precoded = recoded_filename;
59 if (iconv(inv_msgiconv, &filename, &filename_len,
60 &precoded, &max_path) == (size_t)(-1) && errno == E2BIG) {
61 precoded[0] = precoded[1] = precoded[2] = '.';
62 precoded += 3;
64 *precoded = '\0';
65 return recoded_filename;
66 #endif
69 void mp_msg_init(void){
70 int i;
71 char *env = getenv("MPLAYER_VERBOSE");
72 if (env)
73 verbose = atoi(env);
74 for(i=0;i<MSGT_MAX;i++) mp_msg_levels[i] = -2;
75 mp_msg_levels[MSGT_IDENTIFY] = -1; // no -identify output by default
76 #ifdef USE_ICONV
77 mp_msg_charset = getenv("MPLAYER_CHARSET");
78 if (!mp_msg_charset)
79 mp_msg_charset = get_term_charset();
80 #endif
83 int mp_msg_test(int mod, int lev)
85 return lev <= (mp_msg_levels[mod] == -2 ? mp_msg_level_all + verbose : mp_msg_levels[mod]);
88 void mp_msg(int mod, int lev, const char *format, ... ){
89 va_list va;
90 char tmp[MSGSIZE_MAX];
92 if (!mp_msg_test(mod, lev)) return; // do not display
93 va_start(va, format);
94 vsnprintf(tmp, MSGSIZE_MAX, format, va);
95 va_end(va);
96 tmp[MSGSIZE_MAX-2] = '\n';
97 tmp[MSGSIZE_MAX-1] = 0;
99 #ifdef HAVE_NEW_GUI
100 if(use_gui)
101 guiMessageBox(lev, tmp);
102 #endif
104 #if defined(USE_ICONV) && defined(MSG_CHARSET)
105 if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv")) {
106 char tmp2[MSGSIZE_MAX];
107 size_t inlen = strlen(tmp), outlen = MSGSIZE_MAX;
108 char *in = tmp, *out = tmp2;
109 if (!old_charset || strcmp(old_charset, mp_msg_charset)) {
110 if (old_charset) {
111 free(old_charset);
112 iconv_close(msgiconv);
114 msgiconv = iconv_open(mp_msg_charset, MSG_CHARSET);
115 old_charset = strdup(mp_msg_charset);
117 if (msgiconv == (iconv_t)(-1)) {
118 fprintf(stderr,"iconv: conversion from %s to %s unsupported\n"
119 ,MSG_CHARSET,mp_msg_charset);
120 }else{
121 memset(tmp2, 0, MSGSIZE_MAX);
122 while (iconv(msgiconv, &in, &inlen, &out, &outlen) == -1) {
123 if (!inlen || !outlen)
124 break;
125 *out++ = *in++;
126 outlen--; inlen--;
128 strncpy(tmp, tmp2, MSGSIZE_MAX);
129 tmp[MSGSIZE_MAX-1] = 0;
130 tmp[MSGSIZE_MAX-2] = '\n';
133 #endif
135 #ifdef MSG_USE_COLORS
136 /* that's only a silly color test */
137 #ifdef MP_ANNOY_ME
138 { int c;
139 static int flag=1;
140 if(flag)
141 for(c=0;c<24;c++)
142 printf("\033[%d;3%dm*** COLOR TEST %d ***\n",(c>7),c&7,c);
143 flag=0;
145 #endif
146 { unsigned char v_colors[10]={9,1,3,15,7,2,2,8,8,8};
147 static const char *mod_text[MSGT_MAX]= {
148 "GLOBAL",
149 "CPLAYER",
150 "GPLAYER",
151 "VIDEOOUT",
152 "AUDIOOUT",
153 "DEMUXER",
154 "DS",
155 "DEMUX",
156 "HEADER",
157 "AVSYNC",
158 "AUTOQ",
159 "CFGPARSER",
160 "DECAUDIO",
161 "DECVIDEO",
162 "SEEK",
163 "WIN32",
164 "OPEN",
165 "DVD",
166 "PARSEES",
167 "LIRC",
168 "STREAM",
169 "CACHE",
170 "MENCODER",
171 "XACODEC",
172 "TV",
173 "OSDEP",
174 "SPUDEC",
175 "PLAYTREE",
176 "INPUT",
177 "VFILTER",
178 "OSD",
179 "NETWORK",
180 "CPUDETECT",
181 "CODECCFG",
182 "SWS",
183 "VOBSUB",
184 "SUBREADER",
185 "AFILTER",
186 "NETST",
187 "MUXER",
188 "OSDMENU",
189 "IDENTIFY",
190 "RADIO",
191 "ASS",
192 "LOADER",
193 "STATUSLINE",
196 int c=v_colors[lev];
197 int c2=(mod+1)%15+1;
198 static int header=1;
199 FILE *stream= (lev) <= MSGL_WARN ? stderr : stdout;
200 if(header){
201 fprintf(stream, "\033[%d;3%dm%9s\033[0;37m: ",c2>>3,c2&7, mod_text[mod]);
203 fprintf(stream, "\033[%d;3%dm",c>>3,c&7);
204 header= tmp[strlen(tmp)-1] == '\n'
205 ||tmp[strlen(tmp)-1] == '\r';
207 #endif
208 if (lev <= MSGL_WARN){
209 fprintf(stderr, "%s", tmp);fflush(stderr);
210 } else {
211 printf("%s", tmp);fflush(stdout);