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