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);
23 /* maximum message length of mp_msg */
24 #define MSGSIZE_MAX 3072
26 int mp_msg_levels
[MSGT_MAX
]; // verbose level of this module. initialized to -2
27 int mp_msg_level_all
= MSGL_STATUS
;
30 int mp_msg_module
= 0;
32 char *mp_msg_charset
= NULL
;
33 static char *old_charset
= NULL
;
34 static iconv_t msgiconv
;
37 const char* filename_recode(const char* filename
)
39 #if !defined(CONFIG_ICONV) || !defined(MSG_CHARSET)
42 static iconv_t inv_msgiconv
= (iconv_t
)(-1);
43 static char recoded_filename
[MSGSIZE_MAX
];
44 size_t filename_len
, max_path
;
46 if (!mp_msg_charset
||
47 !strcasecmp(mp_msg_charset
, MSG_CHARSET
) ||
48 !strcasecmp(mp_msg_charset
, "noconv"))
50 if (inv_msgiconv
== (iconv_t
)(-1)) {
51 inv_msgiconv
= iconv_open(MSG_CHARSET
, mp_msg_charset
);
52 if (inv_msgiconv
== (iconv_t
)(-1))
55 filename_len
= strlen(filename
);
56 max_path
= MSGSIZE_MAX
- 4;
57 precoded
= recoded_filename
;
58 if (iconv(inv_msgiconv
, &filename
, &filename_len
,
59 &precoded
, &max_path
) == (size_t)(-1) && errno
== E2BIG
) {
60 precoded
[0] = precoded
[1] = precoded
[2] = '.';
64 return recoded_filename
;
68 void mp_msg_init(void){
70 char *env
= getenv("MPLAYER_VERBOSE");
73 for(i
=0;i
<MSGT_MAX
;i
++) mp_msg_levels
[i
] = -2;
74 mp_msg_levels
[MSGT_IDENTIFY
] = -1; // no -identify output by default
76 mp_msg_charset
= getenv("MPLAYER_CHARSET");
78 mp_msg_charset
= get_term_charset();
82 int mp_msg_test(int mod
, int lev
)
84 return lev
<= (mp_msg_levels
[mod
] == -2 ? mp_msg_level_all
+ verbose
: mp_msg_levels
[mod
]);
87 static void set_msg_color(FILE* stream
, int lev
)
89 static const unsigned char v_colors
[10] = {9, 1, 3, 15, 7, 2, 2, 8, 8, 8};
90 int c
= v_colors
[lev
];
92 /* that's only a silly color test */
97 for(c
= 0; c
< 24; c
++)
98 printf("\033[%d;3%dm*** COLOR TEST %d ***\n", c
>7, c
&7, c
);
103 fprintf(stream
, "\033[%d;3%dm", c
>> 3, c
& 7);
106 static void print_msg_module(FILE* stream
, int mod
)
108 static const char *module_text
[MSGT_MAX
] = {
156 int c2
= (mod
+ 1) % 15 + 1;
161 fprintf(stream
, "\033[%d;3%dm", c2
>> 3, c2
& 7);
162 fprintf(stream
, "%9s", module_text
[mod
]);
164 fprintf(stream
, "\033[0;37m");
165 fprintf(stream
, ": ");
168 void mp_msg(int mod
, int lev
, const char *format
, ... ){
170 char tmp
[MSGSIZE_MAX
];
171 FILE *stream
= lev
<= MSGL_WARN
? stderr
: stdout
;
172 static int header
= 1;
174 if (!mp_msg_test(mod
, lev
)) return; // do not display
175 va_start(va
, format
);
176 vsnprintf(tmp
, MSGSIZE_MAX
, format
, va
);
178 tmp
[MSGSIZE_MAX
-2] = '\n';
179 tmp
[MSGSIZE_MAX
-1] = 0;
181 #if defined(CONFIG_ICONV) && defined(MSG_CHARSET)
182 if (mp_msg_charset
&& strcasecmp(mp_msg_charset
, "noconv")) {
183 char tmp2
[MSGSIZE_MAX
];
184 size_t inlen
= strlen(tmp
), outlen
= MSGSIZE_MAX
;
185 char *in
= tmp
, *out
= tmp2
;
186 if (!old_charset
|| strcmp(old_charset
, mp_msg_charset
)) {
189 iconv_close(msgiconv
);
191 msgiconv
= iconv_open(mp_msg_charset
, MSG_CHARSET
);
192 old_charset
= strdup(mp_msg_charset
);
194 if (msgiconv
== (iconv_t
)(-1)) {
195 fprintf(stderr
,"iconv: conversion from %s to %s unsupported\n"
196 ,MSG_CHARSET
,mp_msg_charset
);
198 memset(tmp2
, 0, MSGSIZE_MAX
);
199 while (iconv(msgiconv
, &in
, &inlen
, &out
, &outlen
) == -1) {
200 if (!inlen
|| !outlen
)
205 strncpy(tmp
, tmp2
, MSGSIZE_MAX
);
206 tmp
[MSGSIZE_MAX
-1] = 0;
207 tmp
[MSGSIZE_MAX
-2] = '\n';
213 print_msg_module(stream
, mod
);
214 set_msg_color(stream
, lev
);
215 header
= tmp
[strlen(tmp
)-1] == '\n' || tmp
[strlen(tmp
)-1] == '\r';
217 fprintf(stream
, "%s", tmp
);
221 char *mp_gtext(const char *string
)