Fix type and usage of avs_get_audio function
[mplayer/glamo.git] / mp_msg.c
blob99c587fb77f83c43dd7b4da4e0963fe5f5594391
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_LANGINFO
12 #include <locale.h>
13 #include <langinfo.h>
14 #endif
15 #ifdef USE_ICONV
16 #include <iconv.h>
17 #include <errno.h>
18 #endif
20 #if defined(FOR_MENCODER) || defined(CODECS2HTML)
21 #undef HAVE_NEW_GUI
22 #endif
24 #ifdef HAVE_NEW_GUI
25 #include "Gui/interface.h"
26 extern int use_gui;
27 #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. inited to -2
34 int mp_msg_level_all = MSGL_STATUS;
35 int verbose = 0;
36 #ifdef USE_ICONV
37 char *mp_msg_charset = NULL;
38 static char *old_charset = NULL;
39 static iconv_t msgiconv;
40 #endif
42 const char* filename_recode(const char* filename)
44 #if !defined(USE_ICONV) || !defined(MSG_CHARSET)
45 return filename;
46 #else
47 static iconv_t inv_msgiconv = (iconv_t)(-1);
48 static char recoded_filename[MSGSIZE_MAX];
49 size_t filename_len, max_path;
50 char* precoded;
51 if (!mp_msg_charset ||
52 !strcasecmp(mp_msg_charset, MSG_CHARSET) ||
53 !strcasecmp(mp_msg_charset, "noconv"))
54 return filename;
55 if (inv_msgiconv == (iconv_t)(-1)) {
56 inv_msgiconv = iconv_open(MSG_CHARSET, mp_msg_charset);
57 if (inv_msgiconv == (iconv_t)(-1))
58 return filename;
60 filename_len = strlen(filename);
61 max_path = MSGSIZE_MAX - 4;
62 precoded = recoded_filename;
63 if (iconv(inv_msgiconv, &filename, &filename_len,
64 &precoded, &max_path) == (size_t)(-1) && errno == E2BIG) {
65 precoded[0] = precoded[1] = precoded[2] = '.';
66 precoded += 3;
68 *precoded = '\0';
69 return recoded_filename;
70 #endif
73 void mp_msg_init(void){
74 int i;
75 char *env = getenv("MPLAYER_VERBOSE");
76 if (env)
77 verbose = atoi(env);
78 for(i=0;i<MSGT_MAX;i++) mp_msg_levels[i] = -2;
79 mp_msg_levels[MSGT_IDENTIFY] = -1; // no -identify output by default
80 #ifdef USE_ICONV
81 mp_msg_charset = getenv("MPLAYER_CHARSET");
82 #ifdef USE_LANGINFO
83 if (!mp_msg_charset) {
84 setlocale(LC_CTYPE, "");
85 mp_msg_charset = nl_langinfo(CODESET);
86 setlocale(LC_CTYPE, "C");
88 #endif
89 #endif
92 int mp_msg_test(int mod, int lev)
94 return lev <= (mp_msg_levels[mod] == -2 ? mp_msg_level_all + verbose : mp_msg_levels[mod]);
97 void mp_msg(int mod, int lev, const char *format, ... ){
98 va_list va;
99 char tmp[MSGSIZE_MAX];
101 if (!mp_msg_test(mod, lev)) return; // do not display
102 va_start(va, format);
103 vsnprintf(tmp, MSGSIZE_MAX, format, va);
104 va_end(va);
105 tmp[MSGSIZE_MAX-2] = '\n';
106 tmp[MSGSIZE_MAX-1] = 0;
108 #ifdef HAVE_NEW_GUI
109 if(use_gui)
110 guiMessageBox(lev, tmp);
111 #endif
113 #if defined(USE_ICONV) && defined(MSG_CHARSET)
114 if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv")) {
115 char tmp2[MSGSIZE_MAX];
116 size_t inlen = strlen(tmp), outlen = MSGSIZE_MAX;
117 char *in = tmp, *out = tmp2;
118 if (!old_charset || strcmp(old_charset, mp_msg_charset)) {
119 if (old_charset) {
120 free(old_charset);
121 iconv_close(msgiconv);
123 msgiconv = iconv_open(mp_msg_charset, MSG_CHARSET);
124 old_charset = strdup(mp_msg_charset);
126 if (msgiconv == (iconv_t)(-1)) {
127 fprintf(stderr,"iconv: conversion from %s to %s unsupported\n"
128 ,MSG_CHARSET,mp_msg_charset);
129 }else{
130 memset(tmp2, 0, MSGSIZE_MAX);
131 while (iconv(msgiconv, &in, &inlen, &out, &outlen) == -1) {
132 if (!inlen || !outlen)
133 break;
134 *out++ = *in++;
135 outlen--; inlen--;
137 strncpy(tmp, tmp2, MSGSIZE_MAX);
138 tmp[MSGSIZE_MAX-1] = 0;
139 tmp[MSGSIZE_MAX-2] = '\n';
142 #endif
144 #ifdef MSG_USE_COLORS
145 /* that's only a silly color test */
146 #ifdef MP_ANNOY_ME
147 { int c;
148 static int flag=1;
149 if(flag)
150 for(c=0;c<24;c++)
151 printf("\033[%d;3%dm*** COLOR TEST %d ***\n",(c>7),c&7,c);
152 flag=0;
154 #endif
155 { unsigned char v_colors[10]={9,1,3,15,7,2,2,8,8,8};
156 static const char *lev_text[]= {
157 "FATAL",
158 "ERROR",
159 "WARN",
160 "HINT",
161 "INFO",
162 "STATUS",
163 "V",
164 "DGB2",
165 "DGB3",
166 "DGB4",
167 "DBG5",
169 static const char *mod_text[MSGT_MAX]= {
170 "GLOBAL",
171 "CPLAYER",
172 "GPLAYER",
173 "VIDEOOUT",
174 "AUDIOOUT",
175 "DEMUXER",
176 "DS",
177 "DEMUX",
178 "HEADER",
179 "AVSYNC",
180 "AUTOQ",
181 "CFGPARSER",
182 "DECAUDIO",
183 "DECVIDEO",
184 "SEEK",
185 "WIN32",
186 "OPEN",
187 "DVD",
188 "PARSEES",
189 "LIRC",
190 "STREAM",
191 "CACHE",
192 "MENCODER",
193 "XACODEC",
194 "TV",
195 "OSDEP",
196 "SPUDEC",
197 "PLAYTREE",
198 "INPUT",
199 "VFILTER",
200 "OSD",
201 "NETWORK",
202 "CPUDETECT",
203 "CODECCFG",
204 "SWS",
205 "VOBSUB",
206 "SUBREADER",
207 "AFILTER",
208 "NETST",
209 "MUXER",
210 "OSDMENU",
211 "IDENTIFY",
212 "RADIO",
213 "ASS",
214 "LOADER",
217 int c=v_colors[lev];
218 int c2=(mod+1)%15+1;
219 static int header=1;
220 FILE *stream= (lev) <= MSGL_WARN ? stderr : stdout;
221 if(header){
222 fprintf(stream, "\033[%d;3%dm%9s\033[0;37m: ",c2>>3,c2&7, mod_text[mod]);
224 fprintf(stream, "\033[%d;3%dm",c>>3,c&7);
225 header= tmp[strlen(tmp)-1] == '\n'
226 ||tmp[strlen(tmp)-1] == '\r';
228 #endif
229 if (lev <= MSGL_WARN){
230 fprintf(stderr, "%s", tmp);fflush(stderr);
231 } else {
232 printf("%s", tmp);fflush(stdout);