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);
21 #if defined(FOR_MENCODER)
27 #include "gui/interface.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
;
38 int mp_msg_module
= 0;
40 char *mp_msg_charset
= NULL
;
41 static char *old_charset
= NULL
;
42 static iconv_t msgiconv
;
45 const char* filename_recode(const char* filename
)
47 #if !defined(CONFIG_ICONV) || !defined(MSG_CHARSET)
50 static iconv_t inv_msgiconv
= (iconv_t
)(-1);
51 static char recoded_filename
[MSGSIZE_MAX
];
52 size_t filename_len
, max_path
;
54 if (!mp_msg_charset
||
55 !strcasecmp(mp_msg_charset
, MSG_CHARSET
) ||
56 !strcasecmp(mp_msg_charset
, "noconv"))
58 if (inv_msgiconv
== (iconv_t
)(-1)) {
59 inv_msgiconv
= iconv_open(MSG_CHARSET
, mp_msg_charset
);
60 if (inv_msgiconv
== (iconv_t
)(-1))
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] = '.';
72 return recoded_filename
;
76 void mp_msg_init(void){
78 char *env
= getenv("MPLAYER_VERBOSE");
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
84 mp_msg_charset
= getenv("MPLAYER_CHARSET");
86 mp_msg_charset
= get_term_charset();
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
];
100 /* that's only a silly color test */
105 for(c
= 0; c
< 24; c
++)
106 printf("\033[%d;3%dm*** COLOR TEST %d ***\n", c
>7, c
&7, c
);
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
] = {
164 int c2
= (mod
+ 1) % 15 + 1;
169 fprintf(stream
, "\033[%d;3%dm", c2
>> 3, c2
& 7);
170 fprintf(stream
, "%9s", module_text
[mod
]);
172 fprintf(stream
, "\033[0;37m");
173 fprintf(stream
, ": ");
176 void mp_msg(int mod
, int lev
, const char *format
, ... ){
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
);
186 tmp
[MSGSIZE_MAX
-2] = '\n';
187 tmp
[MSGSIZE_MAX
-1] = 0;
191 guiMessageBox(lev
, tmp
);
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
)) {
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
);
211 memset(tmp2
, 0, MSGSIZE_MAX
);
212 while (iconv(msgiconv
, &in
, &inlen
, &out
, &outlen
) == -1) {
213 if (!inlen
|| !outlen
)
218 strncpy(tmp
, tmp2
, MSGSIZE_MAX
);
219 tmp
[MSGSIZE_MAX
-1] = 0;
220 tmp
[MSGSIZE_MAX
-2] = '\n';
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
);