1 /*****************************************************************************
2 * messages.c: messages interface
3 * This library provides an interface to the message queue to be used by other
4 * modules, especially intf modules. See vlc_config.h for output configuration.
5 *****************************************************************************
6 * Copyright (C) 1998-2005 VLC authors and VideoLAN
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Samuel Hocevar <sam@zoy.org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
36 #include <stdarg.h> /* va_list for BSD */
39 #include <vlc_common.h>
40 #include <vlc_interface.h>
41 #include <vlc_charset.h>
42 #include "../libvlc.h"
46 * \param obj VLC object emitting the message or NULL
47 * \param type VLC_MSG_* message type (info, error, warning or debug)
48 * \param module name of module from which the message come
49 * (normally MODULE_STRING)
50 * \param format printf-like message format
52 void vlc_Log (vlc_object_t *obj, int type, const char *module,
53 const char *format, ... )
57 va_start (args, format);
58 vlc_vaLog (obj, type, module, format, args);
63 static void Win32DebugOutputMsg (void *, int , const vlc_log_t *,
64 const char *, va_list);
68 * Emit a log message. This function is the variable argument list equivalent
71 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
72 const char *format, va_list args)
74 if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
77 /* Get basename from the module filename */
78 char *p = strrchr(module, '/');
81 p = strchr(module, '.');
83 size_t modlen = (p != NULL) ? (p - module) : 1;
84 char modulebuf[modlen + 1];
87 memcpy(modulebuf, module, modlen);
88 modulebuf[modlen] = '\0';
92 /* Fill message information fields */
95 msg.i_object_id = (uintptr_t)obj;
96 msg.psz_object_type = (obj != NULL) ? obj->psz_object_type : "generic";
97 msg.psz_module = module;
98 msg.psz_header = NULL;
100 for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
101 if (o->psz_header != NULL)
103 msg.psz_header = o->psz_header;
107 /* Pass message to the callback */
108 libvlc_priv_t *priv = obj ? libvlc_priv (obj->p_libvlc) : NULL;
114 Win32DebugOutputMsg (priv ? &priv->log.verbose : NULL, type, &msg, format, ap);
119 vlc_rwlock_rdlock (&priv->log.lock);
120 priv->log.cb (priv->log.opaque, type, &msg, format, args);
121 vlc_rwlock_unlock (&priv->log.lock);
125 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
126 #define COL(x,y) "\033[" #x ";" #y "m"
127 #define RED COL(31,1)
128 #define GREEN COL(32,1)
129 #define YELLOW COL(0,33)
130 #define WHITE COL(0,1)
131 #define GRAY "\033[0m"
132 static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY };
134 /* Display size of a pointer */
135 static const int ptr_width = 2 * /* hex digits */ sizeof(uintptr_t);
137 static void PrintColorMsg (void *d, int type, const vlc_log_t *p_item,
138 const char *format, va_list ap)
140 FILE *stream = stderr;
141 int verbose = (intptr_t)d;
143 if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
146 int canc = vlc_savecancel ();
149 utf8_fprintf (stream, "["GREEN"%0*"PRIxPTR""GRAY"] ", ptr_width, p_item->i_object_id);
150 if (p_item->psz_header != NULL)
151 utf8_fprintf (stream, "[%s] ", p_item->psz_header);
152 utf8_fprintf (stream, "%s %s%s: %s", p_item->psz_module,
153 p_item->psz_object_type, msg_type[type], msg_color[type]);
154 utf8_vfprintf (stream, format, ap);
155 fputs (GRAY"\n", stream);
156 #if defined (_WIN32) || defined (__OS2__)
159 funlockfile (stream);
160 vlc_restorecancel (canc);
163 static void PrintMsg (void *d, int type, const vlc_log_t *p_item,
164 const char *format, va_list ap)
166 FILE *stream = stderr;
167 int verbose = (intptr_t)d;
169 if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
172 int canc = vlc_savecancel ();
175 utf8_fprintf (stream, "[%0*"PRIxPTR"] ", ptr_width, p_item->i_object_id);
176 if (p_item->psz_header != NULL)
177 utf8_fprintf (stream, "[%s] ", p_item->psz_header);
178 utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module,
179 p_item->psz_object_type, msg_type[type]);
180 utf8_vfprintf (stream, format, ap);
181 putc_unlocked ('\n', stream);
182 #if defined (_WIN32) || defined (__OS2__)
185 funlockfile (stream);
186 vlc_restorecancel (canc);
190 static void Win32DebugOutputMsg (void* d, int type, const vlc_log_t *p_item,
191 const char *format, va_list dol)
195 const signed char *pverbose = d;
196 if (pverbose && (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)))
201 int msg_len = vsnprintf(NULL, 0, format, dol2);
207 char *msg = malloc(msg_len + 1 + 1);
211 msg_len = vsnprintf(msg, msg_len+1, format, dol);
213 if(msg[msg_len-1] != '\n'){
215 msg[msg_len + 1] = '\0';
217 char* psz_msg = NULL;
218 if(asprintf(&psz_msg, "%s %s%s: %s", p_item->psz_module,
219 p_item->psz_object_type, msg_type[type], msg) > 0) {
220 wchar_t* wmsg = ToWide(psz_msg);
221 OutputDebugStringW(wmsg);
231 * Sets the message logging callback.
232 * \param cb message callback, or NULL to reset
233 * \param data data pointer for the message callback
235 void vlc_LogSet (libvlc_int_t *vlc, vlc_log_cb cb, void *opaque)
237 libvlc_priv_t *priv = libvlc_priv (vlc);
241 #if defined (HAVE_ISATTY) && !defined (_WIN32)
242 if (isatty (STDERR_FILENO) && var_InheritBool (vlc, "color"))
247 opaque = (void *)(intptr_t)priv->log.verbose;
250 vlc_rwlock_wrlock (&priv->log.lock);
252 priv->log.opaque = opaque;
253 vlc_rwlock_unlock (&priv->log.lock);
255 /* Announce who we are */
256 msg_Dbg (vlc, "VLC media player - %s", VERSION_MESSAGE);
257 msg_Dbg (vlc, "%s", COPYRIGHT_MESSAGE);
258 msg_Dbg (vlc, "revision %s", psz_vlc_changeset);
259 msg_Dbg (vlc, "configured with %s", CONFIGURE_LINE);
262 void vlc_LogInit (libvlc_int_t *vlc)
264 libvlc_priv_t *priv = libvlc_priv (vlc);
267 if (var_InheritBool (vlc, "quiet"))
268 priv->log.verbose = -1;
270 if ((str = getenv ("VLC_VERBOSE")) != NULL)
271 priv->log.verbose = atoi (str);
273 priv->log.verbose = var_InheritInteger (vlc, "verbose");
275 vlc_rwlock_init (&priv->log.lock);
276 vlc_LogSet (vlc, NULL, NULL);
279 void vlc_LogDeinit (libvlc_int_t *vlc)
281 libvlc_priv_t *priv = libvlc_priv (vlc);
283 vlc_rwlock_destroy (&priv->log.lock);