synced with 1.246
[mplayer/greg.git] / mp_msg.c
blob7151b6de67f03a3e57f4f6d6240f86e3437b8d2c
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 ,mp_msg_charset,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 static const char *mod_text[]= {
136 "GLOBAL",
137 "CPLAYER",
138 "GPLAYER",
139 "VIDEOOUT",
140 "AUDIOOUT",
141 "DEMUXER",
142 "DS",
143 "DEMUX",
144 "HEADER",
145 "AVSYNC",
146 "AUTOQ",
147 "CFGPARSER",
148 "DECAUDIO",
149 "DECVIDEO",
150 "SEEK",
151 "WIN32",
152 "OPEN",
153 "DVD",
154 "PARSEES",
155 "LIRC",
156 "STREAM",
157 "CACHE",
158 "MENCODER",
159 "XACODEC",
160 "TV",
161 "OSDEP",
162 "SPUDEC",
163 "PLAYTREE",
164 "INPUT",
165 "VFILTER",
166 "OSD",
167 "NETWORK",
168 "CPUDETECT",
169 "CODECCFG",
170 "SWS",
171 "VOBSUB",
172 "SUBREADER",
173 "AFILTER",
174 "NETST",
175 "MUXER"};
177 int c=v_colors[lev];
178 int c2=(mod+1)%15+1;
179 static int header=1;
180 FILE *stream= (lev) <= MSGL_WARN ? stderr : stdout;
181 if(header){
182 fprintf(stream, "\033[%d;3%dm%9s\033[0;37m: ",c2>>3,c2&7, mod_text[mod]);
184 fprintf(stream, "\033[%d;3%dm",c>>3,c&7);
185 header= tmp[strlen(tmp)-1] == '\n'
186 ||tmp[strlen(tmp)-1] == '\r';
188 #endif
189 if (lev <= MSGL_WARN){
190 fprintf(stderr, "%s", tmp);fflush(stderr);
191 } else {
192 printf("%s", tmp);fflush(stdout);