synced with r21537
[mplayer/glamo.git] / mp_msg.c
blobc3d8db22a59f52cfef13ecc4591ba5a990ed8172
2 //#define MSG_USE_COLORS
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <string.h>
9 #include "config.h"
11 #ifdef USE_LANGINFO
12 #include <locale.h>
13 #include <langinfo.h>
14 #endif
15 #ifdef USE_ICONV
16 #include <iconv.h>
17 #endif
19 #if defined(FOR_MENCODER) || defined(CODECS2HTML)
20 #undef HAVE_NEW_GUI
21 #endif
23 #ifdef HAVE_NEW_GUI
24 #include "Gui/interface.h"
25 extern int use_gui;
26 #endif
27 #include "mp_msg.h"
29 /* maximum message length of mp_msg */
30 #define MSGSIZE_MAX 3072
32 int mp_msg_levels[MSGT_MAX]; // verbose level of this module. inited to -2
33 int mp_msg_level_all = MSGL_STATUS;
34 int verbose = 0;
35 #ifdef USE_ICONV
36 char *mp_msg_charset = NULL;
37 static char *old_charset = NULL;
38 static iconv_t msgiconv;
39 #endif
41 void mp_msg_init(void){
42 int i;
43 char *env = getenv("MPLAYER_VERBOSE");
44 if (env)
45 verbose = atoi(env);
46 for(i=0;i<MSGT_MAX;i++) mp_msg_levels[i] = -2;
47 mp_msg_levels[MSGT_IDENTIFY] = -1; // no -identify output by default
48 #ifdef USE_ICONV
49 mp_msg_charset = getenv("MPLAYER_CHARSET");
50 #ifdef USE_LANGINFO
51 if (!mp_msg_charset) {
52 setlocale(LC_CTYPE, "");
53 mp_msg_charset = nl_langinfo(CODESET);
54 setlocale(LC_CTYPE, "C");
56 #endif
57 #endif
60 int mp_msg_test(int mod, int lev)
62 return lev <= (mp_msg_levels[mod] == -2 ? mp_msg_level_all + verbose : mp_msg_levels[mod]);
65 void mp_msg(int mod, int lev, const char *format, ... ){
66 va_list va;
67 char tmp[MSGSIZE_MAX];
69 if (!mp_msg_test(mod, lev)) return; // do not display
70 va_start(va, format);
71 vsnprintf(tmp, MSGSIZE_MAX, format, va);
72 va_end(va);
73 tmp[MSGSIZE_MAX-2] = '\n';
74 tmp[MSGSIZE_MAX-1] = 0;
76 #ifdef HAVE_NEW_GUI
77 if(use_gui)
78 guiMessageBox(lev, tmp);
79 #endif
81 #if defined(USE_ICONV) && defined(MSG_CHARSET)
82 if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv")) {
83 char tmp2[MSGSIZE_MAX];
84 size_t inlen = strlen(tmp), outlen = MSGSIZE_MAX;
85 char *in = tmp, *out = tmp2;
86 if (!old_charset || strcmp(old_charset, mp_msg_charset)) {
87 if (old_charset) {
88 free(old_charset);
89 iconv_close(msgiconv);
91 msgiconv = iconv_open(mp_msg_charset, MSG_CHARSET);
92 old_charset = strdup(mp_msg_charset);
94 if (msgiconv == (iconv_t)(-1)) {
95 fprintf(stderr,"iconv: conversion from %s to %s unsupported\n"
96 ,MSG_CHARSET,mp_msg_charset);
97 }else{
98 memset(tmp2, 0, MSGSIZE_MAX);
99 while (iconv(msgiconv, &in, &inlen, &out, &outlen) == -1) {
100 if (!inlen || !outlen)
101 break;
102 *out++ = *in++;
103 outlen--; inlen--;
105 strncpy(tmp, tmp2, MSGSIZE_MAX);
106 tmp[MSGSIZE_MAX-1] = 0;
107 tmp[MSGSIZE_MAX-2] = '\n';
110 #endif
112 #ifdef MSG_USE_COLORS
113 /* that's only a silly color test */
114 #ifdef MP_ANNOY_ME
115 { int c;
116 static int flag=1;
117 if(flag)
118 for(c=0;c<24;c++)
119 printf("\033[%d;3%dm*** COLOR TEST %d ***\n",(c>7),c&7,c);
120 flag=0;
122 #endif
123 { unsigned char v_colors[10]={9,1,3,15,7,2,2,8,8,8};
124 static const char *lev_text[]= {
125 "FATAL",
126 "ERROR",
127 "WARN",
128 "HINT",
129 "INFO",
130 "STATUS",
131 "V",
132 "DGB2",
133 "DGB3",
134 "DGB4",
135 "DBG5",
137 static const char *mod_text[MSGT_MAX]= {
138 "GLOBAL",
139 "CPLAYER",
140 "GPLAYER",
141 "VIDEOOUT",
142 "AUDIOOUT",
143 "DEMUXER",
144 "DS",
145 "DEMUX",
146 "HEADER",
147 "AVSYNC",
148 "AUTOQ",
149 "CFGPARSER",
150 "DECAUDIO",
151 "DECVIDEO",
152 "SEEK",
153 "WIN32",
154 "OPEN",
155 "DVD",
156 "PARSEES",
157 "LIRC",
158 "STREAM",
159 "CACHE",
160 "MENCODER",
161 "XACODEC",
162 "TV",
163 "OSDEP",
164 "SPUDEC",
165 "PLAYTREE",
166 "INPUT",
167 "VFILTER",
168 "OSD",
169 "NETWORK",
170 "CPUDETECT",
171 "CODECCFG",
172 "SWS",
173 "VOBSUB",
174 "SUBREADER",
175 "AFILTER",
176 "NETST",
177 "MUXER",
178 "OSDMENU",
179 "IDENTIFY",
180 "RADIO",
181 "ASS",
182 "LOADER",
185 int c=v_colors[lev];
186 int c2=(mod+1)%15+1;
187 static int header=1;
188 FILE *stream= (lev) <= MSGL_WARN ? stderr : stdout;
189 if(header){
190 fprintf(stream, "\033[%d;3%dm%9s\033[0;37m: ",c2>>3,c2&7, mod_text[mod]);
192 fprintf(stream, "\033[%d;3%dm",c>>3,c&7);
193 header= tmp[strlen(tmp)-1] == '\n'
194 ||tmp[strlen(tmp)-1] == '\r';
196 #endif
197 if (lev <= MSGL_WARN){
198 fprintf(stderr, "%s", tmp);fflush(stderr);
199 } else {
200 printf("%s", tmp);fflush(stdout);