2 //#define MSG_USE_COLORS
14 extern char* get_term_charset(void);
17 #if defined(FOR_MENCODER)
22 #include "gui/interface.h"
27 /* maximum message length of mp_msg */
28 #define MSGSIZE_MAX 3072
30 int mp_msg_levels
[MSGT_MAX
]; // verbose level of this module. inited to -2
31 int mp_msg_level_all
= MSGL_STATUS
;
34 char *mp_msg_charset
= NULL
;
35 static char *old_charset
= NULL
;
36 static iconv_t msgiconv
;
39 const char* filename_recode(const char* filename
)
41 #if !defined(USE_ICONV) || !defined(MSG_CHARSET)
44 static iconv_t inv_msgiconv
= (iconv_t
)(-1);
45 static char recoded_filename
[MSGSIZE_MAX
];
46 size_t filename_len
, max_path
;
48 if (!mp_msg_charset
||
49 !strcasecmp(mp_msg_charset
, MSG_CHARSET
) ||
50 !strcasecmp(mp_msg_charset
, "noconv"))
52 if (inv_msgiconv
== (iconv_t
)(-1)) {
53 inv_msgiconv
= iconv_open(MSG_CHARSET
, mp_msg_charset
);
54 if (inv_msgiconv
== (iconv_t
)(-1))
57 filename_len
= strlen(filename
);
58 max_path
= MSGSIZE_MAX
- 4;
59 precoded
= recoded_filename
;
60 if (iconv(inv_msgiconv
, &filename
, &filename_len
,
61 &precoded
, &max_path
) == (size_t)(-1) && errno
== E2BIG
) {
62 precoded
[0] = precoded
[1] = precoded
[2] = '.';
66 return recoded_filename
;
70 void mp_msg_init(void){
72 char *env
= getenv("MPLAYER_VERBOSE");
75 for(i
=0;i
<MSGT_MAX
;i
++) mp_msg_levels
[i
] = -2;
76 mp_msg_levels
[MSGT_IDENTIFY
] = -1; // no -identify output by default
78 mp_msg_charset
= getenv("MPLAYER_CHARSET");
80 mp_msg_charset
= get_term_charset();
84 int mp_msg_test(int mod
, int lev
)
86 return lev
<= (mp_msg_levels
[mod
] == -2 ? mp_msg_level_all
+ verbose
: mp_msg_levels
[mod
]);
89 void mp_msg(int mod
, int lev
, const char *format
, ... ){
91 char tmp
[MSGSIZE_MAX
];
93 if (!mp_msg_test(mod
, lev
)) return; // do not display
95 vsnprintf(tmp
, MSGSIZE_MAX
, format
, va
);
97 tmp
[MSGSIZE_MAX
-2] = '\n';
98 tmp
[MSGSIZE_MAX
-1] = 0;
102 guiMessageBox(lev
, tmp
);
105 #if defined(USE_ICONV) && defined(MSG_CHARSET)
106 if (mp_msg_charset
&& strcasecmp(mp_msg_charset
, "noconv")) {
107 char tmp2
[MSGSIZE_MAX
];
108 size_t inlen
= strlen(tmp
), outlen
= MSGSIZE_MAX
;
109 char *in
= tmp
, *out
= tmp2
;
110 if (!old_charset
|| strcmp(old_charset
, mp_msg_charset
)) {
113 iconv_close(msgiconv
);
115 msgiconv
= iconv_open(mp_msg_charset
, MSG_CHARSET
);
116 old_charset
= strdup(mp_msg_charset
);
118 if (msgiconv
== (iconv_t
)(-1)) {
119 fprintf(stderr
,"iconv: conversion from %s to %s unsupported\n"
120 ,MSG_CHARSET
,mp_msg_charset
);
122 memset(tmp2
, 0, MSGSIZE_MAX
);
123 while (iconv(msgiconv
, &in
, &inlen
, &out
, &outlen
) == -1) {
124 if (!inlen
|| !outlen
)
129 strncpy(tmp
, tmp2
, MSGSIZE_MAX
);
130 tmp
[MSGSIZE_MAX
-1] = 0;
131 tmp
[MSGSIZE_MAX
-2] = '\n';
136 #ifdef MSG_USE_COLORS
137 /* that's only a silly color test */
143 printf("\033[%d;3%dm*** COLOR TEST %d ***\n",(c
>7),c
&7,c
);
147 { unsigned char v_colors
[10]={9,1,3,15,7,2,2,8,8,8};
148 static const char *mod_text
[MSGT_MAX
]= {
200 FILE *stream
= (lev
) <= MSGL_WARN
? stderr
: stdout
;
202 fprintf(stream
, "\033[%d;3%dm%9s\033[0;37m: ",c2
>>3,c2
&7, mod_text
[mod
]);
204 fprintf(stream
, "\033[%d;3%dm",c
>>3,c
&7);
205 header
= tmp
[strlen(tmp
)-1] == '\n'
206 ||tmp
[strlen(tmp
)-1] == '\r';
209 if (lev
<= MSGL_WARN
){
210 fprintf(stderr
, "%s", tmp
);fflush(stderr
);
212 printf("%s", tmp
);fflush(stdout
);