synced with r23100
[mplayer/greg.git] / mp_msg.c
blobabd468e881299dd4994e8092de0b80c9c1def532
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 extern int use_gui;
24 #endif
25 #include "mp_msg.h"
27 /* maximum message length of mp_msg */
28 #define MSGSIZE_MAX 3072
30 int mp_msg_levels[MSGT_MAX]; // verbose level of this module. inited to -2
31 int mp_msg_level_all = MSGL_STATUS;
32 int verbose = 0;
33 #ifdef USE_ICONV
34 char *mp_msg_charset = NULL;
35 static char *old_charset = NULL;
36 static iconv_t msgiconv;
37 #endif
39 const char* filename_recode(const char* filename)
41 #if !defined(USE_ICONV) || !defined(MSG_CHARSET)
42 return filename;
43 #else
44 static iconv_t inv_msgiconv = (iconv_t)(-1);
45 static char recoded_filename[MSGSIZE_MAX];
46 size_t filename_len, max_path;
47 char* precoded;
48 if (!mp_msg_charset ||
49 !strcasecmp(mp_msg_charset, MSG_CHARSET) ||
50 !strcasecmp(mp_msg_charset, "noconv"))
51 return filename;
52 if (inv_msgiconv == (iconv_t)(-1)) {
53 inv_msgiconv = iconv_open(MSG_CHARSET, mp_msg_charset);
54 if (inv_msgiconv == (iconv_t)(-1))
55 return filename;
57 filename_len = strlen(filename);
58 max_path = MSGSIZE_MAX - 4;
59 precoded = recoded_filename;
60 if (iconv(inv_msgiconv, &filename, &filename_len,
61 &precoded, &max_path) == (size_t)(-1) && errno == E2BIG) {
62 precoded[0] = precoded[1] = precoded[2] = '.';
63 precoded += 3;
65 *precoded = '\0';
66 return recoded_filename;
67 #endif
70 void mp_msg_init(void){
71 int i;
72 char *env = getenv("MPLAYER_VERBOSE");
73 if (env)
74 verbose = atoi(env);
75 for(i=0;i<MSGT_MAX;i++) mp_msg_levels[i] = -2;
76 mp_msg_levels[MSGT_IDENTIFY] = -1; // no -identify output by default
77 #ifdef USE_ICONV
78 mp_msg_charset = getenv("MPLAYER_CHARSET");
79 if (!mp_msg_charset)
80 mp_msg_charset = get_term_charset();
81 #endif
84 int mp_msg_test(int mod, int lev)
86 return lev <= (mp_msg_levels[mod] == -2 ? mp_msg_level_all + verbose : mp_msg_levels[mod]);
89 void mp_msg(int mod, int lev, const char *format, ... ){
90 va_list va;
91 char tmp[MSGSIZE_MAX];
93 if (!mp_msg_test(mod, lev)) return; // do not display
94 va_start(va, format);
95 vsnprintf(tmp, MSGSIZE_MAX, format, va);
96 va_end(va);
97 tmp[MSGSIZE_MAX-2] = '\n';
98 tmp[MSGSIZE_MAX-1] = 0;
100 #ifdef HAVE_NEW_GUI
101 if(use_gui)
102 guiMessageBox(lev, tmp);
103 #endif
105 #if defined(USE_ICONV) && defined(MSG_CHARSET)
106 if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv")) {
107 char tmp2[MSGSIZE_MAX];
108 size_t inlen = strlen(tmp), outlen = MSGSIZE_MAX;
109 char *in = tmp, *out = tmp2;
110 if (!old_charset || strcmp(old_charset, mp_msg_charset)) {
111 if (old_charset) {
112 free(old_charset);
113 iconv_close(msgiconv);
115 msgiconv = iconv_open(mp_msg_charset, MSG_CHARSET);
116 old_charset = strdup(mp_msg_charset);
118 if (msgiconv == (iconv_t)(-1)) {
119 fprintf(stderr,"iconv: conversion from %s to %s unsupported\n"
120 ,MSG_CHARSET,mp_msg_charset);
121 }else{
122 memset(tmp2, 0, MSGSIZE_MAX);
123 while (iconv(msgiconv, &in, &inlen, &out, &outlen) == -1) {
124 if (!inlen || !outlen)
125 break;
126 *out++ = *in++;
127 outlen--; inlen--;
129 strncpy(tmp, tmp2, MSGSIZE_MAX);
130 tmp[MSGSIZE_MAX-1] = 0;
131 tmp[MSGSIZE_MAX-2] = '\n';
134 #endif
136 #ifdef MSG_USE_COLORS
137 /* that's only a silly color test */
138 #ifdef MP_ANNOY_ME
139 { int c;
140 static int flag=1;
141 if(flag)
142 for(c=0;c<24;c++)
143 printf("\033[%d;3%dm*** COLOR TEST %d ***\n",(c>7),c&7,c);
144 flag=0;
146 #endif
147 { unsigned char v_colors[10]={9,1,3,15,7,2,2,8,8,8};
148 static const char *mod_text[MSGT_MAX]= {
149 "GLOBAL",
150 "CPLAYER",
151 "GPLAYER",
152 "VIDEOOUT",
153 "AUDIOOUT",
154 "DEMUXER",
155 "DS",
156 "DEMUX",
157 "HEADER",
158 "AVSYNC",
159 "AUTOQ",
160 "CFGPARSER",
161 "DECAUDIO",
162 "DECVIDEO",
163 "SEEK",
164 "WIN32",
165 "OPEN",
166 "DVD",
167 "PARSEES",
168 "LIRC",
169 "STREAM",
170 "CACHE",
171 "MENCODER",
172 "XACODEC",
173 "TV",
174 "OSDEP",
175 "SPUDEC",
176 "PLAYTREE",
177 "INPUT",
178 "VFILTER",
179 "OSD",
180 "NETWORK",
181 "CPUDETECT",
182 "CODECCFG",
183 "SWS",
184 "VOBSUB",
185 "SUBREADER",
186 "AFILTER",
187 "NETST",
188 "MUXER",
189 "OSDMENU",
190 "IDENTIFY",
191 "RADIO",
192 "ASS",
193 "LOADER",
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);