Commit files by Steinar Gunderson, forgotten in r30866.
[mplayer.git] / mp_msg.c
blob8d93c68c6e60be40edd56447699f24bada1c3c4d
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <string.h>
24 #include "config.h"
25 #include "osdep/getch2.h"
27 #ifdef CONFIG_ICONV
28 #include <iconv.h>
29 #include <errno.h>
30 #endif
32 #if defined(FOR_MENCODER)
33 #undef CONFIG_GUI
34 int use_gui;
35 #else
36 #include "gui/interface.h"
37 #endif
39 #include "mp_msg.h"
41 /* maximum message length of mp_msg */
42 #define MSGSIZE_MAX 3072
44 int mp_msg_levels[MSGT_MAX]; // verbose level of this module. initialized to -2
45 int mp_msg_level_all = MSGL_STATUS;
46 int verbose = 0;
47 int mp_msg_color = 0;
48 int mp_msg_module = 0;
49 #ifdef CONFIG_ICONV
50 char *mp_msg_charset = NULL;
51 static char *old_charset = NULL;
52 static iconv_t msgiconv;
53 #endif
55 const char* filename_recode(const char* filename)
57 #if !defined(CONFIG_ICONV) || !defined(MSG_CHARSET)
58 return filename;
59 #else
60 static iconv_t inv_msgiconv = (iconv_t)(-1);
61 static char recoded_filename[MSGSIZE_MAX];
62 size_t filename_len, max_path;
63 char* precoded;
64 if (!mp_msg_charset ||
65 !strcasecmp(mp_msg_charset, MSG_CHARSET) ||
66 !strcasecmp(mp_msg_charset, "noconv"))
67 return filename;
68 if (inv_msgiconv == (iconv_t)(-1)) {
69 inv_msgiconv = iconv_open(MSG_CHARSET, mp_msg_charset);
70 if (inv_msgiconv == (iconv_t)(-1))
71 return filename;
73 filename_len = strlen(filename);
74 max_path = MSGSIZE_MAX - 4;
75 precoded = recoded_filename;
76 if (iconv(inv_msgiconv, &filename, &filename_len,
77 &precoded, &max_path) == (size_t)(-1) && errno == E2BIG) {
78 precoded[0] = precoded[1] = precoded[2] = '.';
79 precoded += 3;
81 *precoded = '\0';
82 return recoded_filename;
83 #endif
86 void mp_msg_init(void){
87 int i;
88 char *env = getenv("MPLAYER_VERBOSE");
89 if (env)
90 verbose = atoi(env);
91 for(i=0;i<MSGT_MAX;i++) mp_msg_levels[i] = -2;
92 mp_msg_levels[MSGT_IDENTIFY] = -1; // no -identify output by default
93 #ifdef CONFIG_ICONV
94 mp_msg_charset = getenv("MPLAYER_CHARSET");
95 if (!mp_msg_charset)
96 mp_msg_charset = get_term_charset();
97 #endif
100 int mp_msg_test(int mod, int lev)
102 return lev <= (mp_msg_levels[mod] == -2 ? mp_msg_level_all + verbose : mp_msg_levels[mod]);
105 static void set_msg_color(FILE* stream, int lev)
107 static const unsigned char v_colors[10] = {9, 1, 3, 15, 7, 2, 2, 8, 8, 8};
108 int c = v_colors[lev];
109 #ifdef MP_ANNOY_ME
110 /* that's only a silly color test */
112 int c;
113 static int flag = 1;
114 if (flag)
115 for(c = 0; c < 24; c++)
116 printf("\033[%d;3%dm*** COLOR TEST %d ***\n", c>7, c&7, c);
117 flag = 0;
119 #endif
120 if (mp_msg_color)
121 fprintf(stream, "\033[%d;3%dm", c >> 3, c & 7);
124 static void print_msg_module(FILE* stream, int mod)
126 static const char *module_text[MSGT_MAX] = {
127 "GLOBAL",
128 "CPLAYER",
129 "GPLAYER",
130 "VIDEOOUT",
131 "AUDIOOUT",
132 "DEMUXER",
133 "DS",
134 "DEMUX",
135 "HEADER",
136 "AVSYNC",
137 "AUTOQ",
138 "CFGPARSER",
139 "DECAUDIO",
140 "DECVIDEO",
141 "SEEK",
142 "WIN32",
143 "OPEN",
144 "DVD",
145 "PARSEES",
146 "LIRC",
147 "STREAM",
148 "CACHE",
149 "MENCODER",
150 "XACODEC",
151 "TV",
152 "OSDEP",
153 "SPUDEC",
154 "PLAYTREE",
155 "INPUT",
156 "VFILTER",
157 "OSD",
158 "NETWORK",
159 "CPUDETECT",
160 "CODECCFG",
161 "SWS",
162 "VOBSUB",
163 "SUBREADER",
164 "AFILTER",
165 "NETST",
166 "MUXER",
167 "OSDMENU",
168 "IDENTIFY",
169 "RADIO",
170 "ASS",
171 "LOADER",
172 "STATUSLINE",
174 int c2 = (mod + 1) % 15 + 1;
176 if (!mp_msg_module)
177 return;
178 if (mp_msg_color)
179 fprintf(stream, "\033[%d;3%dm", c2 >> 3, c2 & 7);
180 fprintf(stream, "%9s", module_text[mod]);
181 if (mp_msg_color)
182 fprintf(stream, "\033[0;37m");
183 fprintf(stream, ": ");
186 void mp_msg(int mod, int lev, const char *format, ... ){
187 va_list va;
188 char tmp[MSGSIZE_MAX];
189 FILE *stream = lev <= MSGL_WARN ? stderr : stdout;
190 static int header = 1;
192 if (!mp_msg_test(mod, lev)) return; // do not display
193 va_start(va, format);
194 vsnprintf(tmp, MSGSIZE_MAX, format, va);
195 va_end(va);
196 tmp[MSGSIZE_MAX-2] = '\n';
197 tmp[MSGSIZE_MAX-1] = 0;
199 #ifdef CONFIG_GUI
200 if(use_gui)
201 guiMessageBox(lev, tmp);
202 #endif
204 #if defined(CONFIG_ICONV) && defined(MSG_CHARSET)
205 if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv")) {
206 char tmp2[MSGSIZE_MAX];
207 size_t inlen = strlen(tmp), outlen = MSGSIZE_MAX;
208 char *in = tmp, *out = tmp2;
209 if (!old_charset || strcmp(old_charset, mp_msg_charset)) {
210 if (old_charset) {
211 free(old_charset);
212 iconv_close(msgiconv);
214 msgiconv = iconv_open(mp_msg_charset, MSG_CHARSET);
215 old_charset = strdup(mp_msg_charset);
217 if (msgiconv == (iconv_t)(-1)) {
218 fprintf(stderr,"iconv: conversion from %s to %s unsupported\n"
219 ,MSG_CHARSET,mp_msg_charset);
220 }else{
221 memset(tmp2, 0, MSGSIZE_MAX);
222 while (iconv(msgiconv, &in, &inlen, &out, &outlen) == -1) {
223 if (!inlen || !outlen)
224 break;
225 *out++ = *in++;
226 outlen--; inlen--;
228 strncpy(tmp, tmp2, MSGSIZE_MAX);
229 tmp[MSGSIZE_MAX-1] = 0;
230 tmp[MSGSIZE_MAX-2] = '\n';
233 #endif
235 if (header)
236 print_msg_module(stream, mod);
237 set_msg_color(stream, lev);
238 header = tmp[strlen(tmp)-1] == '\n' || tmp[strlen(tmp)-1] == '\r';
240 fprintf(stream, "%s", tmp);
241 if (mp_msg_color)
242 fprintf(stream, "\033[0m");
243 fflush(stream);