Remove trailing whitespace from most files
[mplayer/glamo.git] / mp_msg.c
bloba0f641957e02ea217035b1c845a0a86e52311307
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 char* get_term_charset(void);
12 #endif
14 #if defined(FOR_MENCODER)
15 #undef CONFIG_GUI
16 int use_gui;
17 #endif
19 #ifdef CONFIG_GUI
20 #include "gui/interface.h"
21 #endif
22 #include "mp_msg.h"
24 /* maximum message length of mp_msg */
25 #define MSGSIZE_MAX 3072
27 int mp_msg_levels[MSGT_MAX]; // verbose level of this module. initialized to -2
28 int mp_msg_level_all = MSGL_STATUS;
29 int verbose = 0;
30 int mp_msg_color = 0;
31 int mp_msg_module = 0;
32 #ifdef CONFIG_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(CONFIG_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 CONFIG_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 static void set_msg_color(FILE* stream, int lev)
90 static const unsigned char v_colors[10] = {9, 1, 3, 15, 7, 2, 2, 8, 8, 8};
91 int c = v_colors[lev];
92 #ifdef MP_ANNOY_ME
93 /* that's only a silly color test */
95 int c;
96 static int flag = 1;
97 if (flag)
98 for(c = 0; c < 24; c++)
99 printf("\033[%d;3%dm*** COLOR TEST %d ***\n", c>7, c&7, c);
100 flag = 0;
102 #endif
103 if (mp_msg_color)
104 fprintf(stream, "\033[%d;3%dm", c >> 3, c & 7);
107 static void print_msg_module(FILE* stream, int mod)
109 static const char *module_text[MSGT_MAX] = {
110 "GLOBAL",
111 "CPLAYER",
112 "GPLAYER",
113 "VIDEOOUT",
114 "AUDIOOUT",
115 "DEMUXER",
116 "DS",
117 "DEMUX",
118 "HEADER",
119 "AVSYNC",
120 "AUTOQ",
121 "CFGPARSER",
122 "DECAUDIO",
123 "DECVIDEO",
124 "SEEK",
125 "WIN32",
126 "OPEN",
127 "DVD",
128 "PARSEES",
129 "LIRC",
130 "STREAM",
131 "CACHE",
132 "MENCODER",
133 "XACODEC",
134 "TV",
135 "OSDEP",
136 "SPUDEC",
137 "PLAYTREE",
138 "INPUT",
139 "VFILTER",
140 "OSD",
141 "NETWORK",
142 "CPUDETECT",
143 "CODECCFG",
144 "SWS",
145 "VOBSUB",
146 "SUBREADER",
147 "AFILTER",
148 "NETST",
149 "MUXER",
150 "OSDMENU",
151 "IDENTIFY",
152 "RADIO",
153 "ASS",
154 "LOADER",
155 "STATUSLINE",
157 int c2 = (mod + 1) % 15 + 1;
159 if (!mp_msg_module)
160 return;
161 if (mp_msg_color)
162 fprintf(stream, "\033[%d;3%dm", c2 >> 3, c2 & 7);
163 fprintf(stream, "%9s", module_text[mod]);
164 if (mp_msg_color)
165 fprintf(stream, "\033[0;37m");
166 fprintf(stream, ": ");
169 void mp_msg(int mod, int lev, const char *format, ... ){
170 va_list va;
171 char tmp[MSGSIZE_MAX];
172 FILE *stream = lev <= MSGL_WARN ? stderr : stdout;
173 static int header = 1;
175 if (!mp_msg_test(mod, lev)) return; // do not display
176 va_start(va, format);
177 vsnprintf(tmp, MSGSIZE_MAX, format, va);
178 va_end(va);
179 tmp[MSGSIZE_MAX-2] = '\n';
180 tmp[MSGSIZE_MAX-1] = 0;
182 #ifdef CONFIG_GUI
183 if(use_gui)
184 guiMessageBox(lev, tmp);
185 #endif
187 #if defined(CONFIG_ICONV) && defined(MSG_CHARSET)
188 if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv")) {
189 char tmp2[MSGSIZE_MAX];
190 size_t inlen = strlen(tmp), outlen = MSGSIZE_MAX;
191 char *in = tmp, *out = tmp2;
192 if (!old_charset || strcmp(old_charset, mp_msg_charset)) {
193 if (old_charset) {
194 free(old_charset);
195 iconv_close(msgiconv);
197 msgiconv = iconv_open(mp_msg_charset, MSG_CHARSET);
198 old_charset = strdup(mp_msg_charset);
200 if (msgiconv == (iconv_t)(-1)) {
201 fprintf(stderr,"iconv: conversion from %s to %s unsupported\n"
202 ,MSG_CHARSET,mp_msg_charset);
203 }else{
204 memset(tmp2, 0, MSGSIZE_MAX);
205 while (iconv(msgiconv, &in, &inlen, &out, &outlen) == -1) {
206 if (!inlen || !outlen)
207 break;
208 *out++ = *in++;
209 outlen--; inlen--;
211 strncpy(tmp, tmp2, MSGSIZE_MAX);
212 tmp[MSGSIZE_MAX-1] = 0;
213 tmp[MSGSIZE_MAX-2] = '\n';
216 #endif
218 if (header)
219 print_msg_module(stream, mod);
220 set_msg_color(stream, lev);
221 header = tmp[strlen(tmp)-1] == '\n' || tmp[strlen(tmp)-1] == '\r';
223 fprintf(stream, "%s", tmp);
224 fflush(stream);
227 char *mp_gtext(const char *string)
229 return string;