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 config.h for output configuration.
5 *****************************************************************************
6 * Copyright (C) 1998-2002 VideoLAN
7 * $Id: messages.c,v 1.34 2003/06/24 13:33:49 sam Exp $
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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 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 General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <stdio.h> /* required */
31 #include <stdarg.h> /* va_list for BSD */
32 #include <stdlib.h> /* malloc() */
33 #include <string.h> /* strerror() */
38 # include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
42 # include <errno.h> /* errno */
46 # include <unistd.h> /* close(), write() */
49 #include "vlc_interface.h"
51 /*****************************************************************************
53 *****************************************************************************/
54 #if defined(HAVE_VA_COPY)
55 # define vlc_va_copy(dest,src) va_copy(dest,src)
56 #elif defined(HAVE___VA_COPY)
57 # define vlc_va_copy(dest,src) __va_copy(dest,src)
59 # define vlc_va_copy(dest,src) (dest)=(src)
62 /*****************************************************************************
64 *****************************************************************************/
65 static void QueueMsg ( vlc_object_t
*, int , const char *,
66 const char *, va_list );
67 static void FlushMsg ( msg_bank_t
* );
68 static void PrintMsg ( vlc_object_t
*, msg_item_t
* );
70 /*****************************************************************************
71 * msg_Create: initialize messages interface
72 *****************************************************************************
73 * This functions has to be called before any call to other msg_* functions.
74 * It set up the locks and the message queue if it is used.
75 *****************************************************************************/
76 void __msg_Create( vlc_object_t
*p_this
)
78 /* Message queue initialization */
79 vlc_mutex_init( p_this
, &p_this
->p_libvlc
->msg_bank
.lock
);
81 p_this
->p_libvlc
->msg_bank
.b_configured
= VLC_FALSE
;
82 p_this
->p_libvlc
->msg_bank
.b_overflow
= VLC_FALSE
;
84 p_this
->p_libvlc
->msg_bank
.i_start
= 0;
85 p_this
->p_libvlc
->msg_bank
.i_stop
= 0;
87 p_this
->p_libvlc
->msg_bank
.i_sub
= 0;
88 p_this
->p_libvlc
->msg_bank
.pp_sub
= NULL
;
91 p_this
->p_libvlc
->msg_bank
.logfile
=
92 CreateFile( L
"vlc-log.txt", GENERIC_WRITE
,
93 FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
94 CREATE_ALWAYS
, 0, NULL
);
95 SetFilePointer( p_this
->p_libvlc
->msg_bank
.logfile
, 0, NULL
, FILE_END
);
99 /*****************************************************************************
100 * msg_Flush: flush the message queue
101 *****************************************************************************/
102 void __msg_Flush( vlc_object_t
*p_this
)
106 vlc_mutex_lock( &p_this
->p_libvlc
->msg_bank
.lock
);
108 p_this
->p_libvlc
->msg_bank
.b_configured
= VLC_TRUE
;
110 for( i_index
= p_this
->p_libvlc
->msg_bank
.i_start
;
111 i_index
!= p_this
->p_libvlc
->msg_bank
.i_stop
;
112 i_index
= (i_index
+1) % VLC_MSG_QSIZE
)
114 PrintMsg( p_this
, &p_this
->p_libvlc
->msg_bank
.msg
[i_index
] );
117 FlushMsg( &p_this
->p_libvlc
->msg_bank
);
119 vlc_mutex_unlock( &p_this
->p_libvlc
->msg_bank
.lock
);
122 /*****************************************************************************
123 * msg_Destroy: free resources allocated by msg_Create
124 *****************************************************************************
125 * This functions prints all messages remaining in queue, then free all the
126 * resources allocated by msg_Create.
127 * No other messages interface functions should be called after this one.
128 *****************************************************************************/
129 void __msg_Destroy( vlc_object_t
*p_this
)
131 if( p_this
->p_libvlc
->msg_bank
.i_sub
)
133 msg_Err( p_this
, "stale interface subscribers" );
136 /* Flush the queue */
137 if( !p_this
->p_libvlc
->msg_bank
.b_configured
)
143 FlushMsg( &p_this
->p_libvlc
->msg_bank
);
147 CloseHandle( p_this
->p_libvlc
->msg_bank
.logfile
);
151 vlc_mutex_destroy( &p_this
->p_libvlc
->msg_bank
.lock
);
154 /*****************************************************************************
155 * msg_Subscribe: subscribe to the message queue.
156 *****************************************************************************/
157 msg_subscription_t
*__msg_Subscribe( vlc_object_t
*p_this
)
159 msg_bank_t
*p_bank
= &p_this
->p_libvlc
->msg_bank
;
160 msg_subscription_t
*p_sub
= malloc( sizeof( msg_subscription_t
) );
162 vlc_mutex_lock( &p_bank
->lock
);
164 /* Add subscription to the list */
165 INSERT_ELEM( p_bank
->pp_sub
, p_bank
->i_sub
, p_bank
->i_sub
, p_sub
);
167 p_sub
->i_start
= p_bank
->i_start
;
168 p_sub
->pi_stop
= &p_bank
->i_stop
;
170 p_sub
->p_msg
= p_bank
->msg
;
171 p_sub
->p_lock
= &p_bank
->lock
;
173 vlc_mutex_unlock( &p_bank
->lock
);
178 /*****************************************************************************
179 * msg_Unsubscribe: unsubscribe from the message queue.
180 *****************************************************************************/
181 void __msg_Unsubscribe( vlc_object_t
*p_this
, msg_subscription_t
*p_sub
)
183 msg_bank_t
*p_bank
= &p_this
->p_libvlc
->msg_bank
;
186 vlc_mutex_lock( &p_bank
->lock
);
191 msg_Err( p_this
, "no subscriber in the list" );
195 /* Look for the appropriate subscription */
196 for( i_index
= 0; i_index
< p_bank
->i_sub
; i_index
++ )
198 if( p_bank
->pp_sub
[ i_index
] == p_sub
)
204 if( p_bank
->pp_sub
[ i_index
] != p_sub
)
206 msg_Err( p_this
, "subscriber not found" );
207 vlc_mutex_unlock( &p_bank
->lock
);
211 /* Remove this subscription */
212 REMOVE_ELEM( p_bank
->pp_sub
, p_bank
->i_sub
, i_index
);
214 vlc_mutex_unlock( &p_bank
->lock
);
217 /*****************************************************************************
218 * __msg_*: print a message
219 *****************************************************************************
220 * These functions queue a message for later printing.
221 *****************************************************************************/
222 void __msg_Generic( vlc_object_t
*p_this
, int i_type
, const char *psz_module
,
223 const char *psz_format
, ... )
227 va_start( args
, psz_format
);
228 QueueMsg( p_this
, i_type
, psz_module
, psz_format
, args
);
232 /* Generic functions used when variadic macros are not available. */
233 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
234 void FN_NAME( void *p_this, const char *psz_format, ... ) \
237 va_start( args, psz_format ); \
238 QueueMsg( (vlc_object_t *)p_this, FN_TYPE, "unknown", \
239 psz_format, args ); \
244 DECLARE_MSG_FN( __msg_Info
, VLC_MSG_INFO
);
245 DECLARE_MSG_FN( __msg_Err
, VLC_MSG_ERR
);
246 DECLARE_MSG_FN( __msg_Warn
, VLC_MSG_WARN
);
247 DECLARE_MSG_FN( __msg_Dbg
, VLC_MSG_DBG
);
249 /*****************************************************************************
250 * QueueMsg: add a message to a queue
251 *****************************************************************************
252 * This function provides basic functionnalities to other msg_* functions.
253 * It adds a message to a queue (after having printed all stored messages if it
254 * is full). If the message can't be converted to string in memory, it issues
256 *****************************************************************************/
257 static void QueueMsg( vlc_object_t
*p_this
, int i_type
, const char *psz_module
,
258 const char *psz_format
, va_list _args
)
260 msg_bank_t
* p_bank
= &p_this
->p_libvlc
->msg_bank
; /* message bank */
261 char * psz_str
= NULL
; /* formatted message string */
263 msg_item_t
* p_item
= NULL
; /* pointer to message */
264 msg_item_t item
; /* message in case of a full queue */
266 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
267 int i_size
= strlen(psz_format
) + INTF_MAX_MSG_SIZE
;
271 * Convert message to string
273 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined( SYS_BEOS )
274 vlc_va_copy( args
, _args
);
275 vasprintf( &psz_str
, psz_format
, args
);
278 psz_str
= (char*) malloc( i_size
* sizeof(char) );
281 if( psz_str
== NULL
)
284 fprintf( stderr
, "main warning: can't store message (%s): ",
287 fprintf( stderr
, "main warning: can't store message: " );
289 vlc_va_copy( args
, _args
);
290 vfprintf( stderr
, psz_format
, args
);
292 fprintf( stderr
, "\n" );
296 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
297 vlc_va_copy( args
, _args
);
298 vsnprintf( psz_str
, i_size
, psz_format
, args
);
300 psz_str
[ i_size
- 1 ] = 0; /* Just in case */
303 /* Put message in queue */
304 vlc_mutex_lock( &p_bank
->lock
);
306 /* Check there is room in the queue for our message */
307 if( p_bank
->b_overflow
)
311 if( ((p_bank
->i_stop
- p_bank
->i_start
+ 1) % VLC_MSG_QSIZE
) == 0 )
313 /* Still in overflow mode, print from a dummy item */
318 /* Pheeew, at last, there is room in the queue! */
319 p_bank
->b_overflow
= VLC_FALSE
;
322 else if( ((p_bank
->i_stop
- p_bank
->i_start
+ 2) % VLC_MSG_QSIZE
) == 0 )
326 if( ((p_bank
->i_stop
- p_bank
->i_start
+ 2) % VLC_MSG_QSIZE
) == 0 )
328 p_bank
->b_overflow
= VLC_TRUE
;
330 /* Put the overflow message in the queue */
331 p_item
= p_bank
->msg
+ p_bank
->i_stop
;
332 p_bank
->i_stop
= (p_bank
->i_stop
+ 1) % VLC_MSG_QSIZE
;
334 p_item
->i_type
= VLC_MSG_WARN
;
335 p_item
->i_object_id
= p_this
->i_object_id
;
336 p_item
->i_object_type
= p_this
->i_object_type
;
337 p_item
->psz_module
= strdup( "message" );
338 p_item
->psz_msg
= strdup( "message queue overflowed" );
340 PrintMsg( p_this
, p_item
);
342 /* We print from a dummy item */
347 if( !p_bank
->b_overflow
)
349 /* Put the message in the queue */
350 p_item
= p_bank
->msg
+ p_bank
->i_stop
;
351 p_bank
->i_stop
= (p_bank
->i_stop
+ 1) % VLC_MSG_QSIZE
;
354 /* Fill message information fields */
355 p_item
->i_type
= i_type
;
356 p_item
->i_object_id
= p_this
->i_object_id
;
357 p_item
->i_object_type
= p_this
->i_object_type
;
358 p_item
->psz_module
= strdup( psz_module
);
359 p_item
->psz_msg
= psz_str
;
361 PrintMsg( p_this
, p_item
);
363 if( p_bank
->b_overflow
)
365 free( p_item
->psz_module
);
366 free( p_item
->psz_msg
);
369 vlc_mutex_unlock( &p_bank
->lock
);
372 /* following functions are local */
374 /*****************************************************************************
376 *****************************************************************************
377 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
378 * this function does not check the lock.
379 *****************************************************************************/
380 static void FlushMsg ( msg_bank_t
*p_bank
)
382 int i_index
, i_start
, i_stop
;
384 /* Only flush the queue if it has been properly configured */
385 if( !p_bank
->b_configured
)
390 /* Get the maximum message index that can be freed */
391 i_stop
= p_bank
->i_stop
;
393 /* Check until which value we can free messages */
394 for( i_index
= 0; i_index
< p_bank
->i_sub
; i_index
++ )
396 i_start
= p_bank
->pp_sub
[ i_index
]->i_start
;
398 /* If this subscriber is late, we don't free messages before
399 * his i_start value, otherwise he'll miss messages */
400 if( ( i_start
< i_stop
401 && (p_bank
->i_stop
<= i_start
|| i_stop
<= p_bank
->i_stop
) )
402 || ( i_stop
< i_start
403 && (i_stop
<= p_bank
->i_stop
&& p_bank
->i_stop
<= i_start
) ) )
409 /* Free message data */
410 for( i_index
= p_bank
->i_start
;
412 i_index
= (i_index
+1) % VLC_MSG_QSIZE
)
414 free( p_bank
->msg
[i_index
].psz_msg
);
415 free( p_bank
->msg
[i_index
].psz_module
);
418 /* Update the new start value */
419 p_bank
->i_start
= i_index
;
422 /*****************************************************************************
423 * PrintMsg: output a message item to stderr
424 *****************************************************************************
425 * Print a message to stderr, with colour formatting if needed.
426 *****************************************************************************/
427 static void PrintMsg ( vlc_object_t
* p_this
, msg_item_t
* p_item
)
429 # define COL(x) "\033[" #x ";1m"
431 # define GREEN COL(32)
432 # define YELLOW COL(33)
433 # define WHITE COL(37)
434 # define GRAY "\033[0m"
439 static const char * ppsz_type
[4] = { "", " error", " warning", " debug" };
440 static const char *ppsz_color
[4] = { WHITE
, RED
, YELLOW
, GRAY
};
441 char *psz_object
= "private";
442 int i_type
= p_item
->i_type
;
447 if( p_this
->p_libvlc
->i_verbose
< 0 ) return;
450 if( p_this
->p_libvlc
->i_verbose
< 0 ) return;
453 if( p_this
->p_libvlc
->i_verbose
< 1 ) return;
456 if( p_this
->p_libvlc
->i_verbose
< 2 ) return;
460 switch( p_item
->i_object_type
)
462 case VLC_OBJECT_ROOT
: psz_object
= "root"; break;
463 case VLC_OBJECT_VLC
: psz_object
= "vlc"; break;
464 case VLC_OBJECT_MODULE
: psz_object
= "module"; break;
465 case VLC_OBJECT_INTF
: psz_object
= "interface"; break;
466 case VLC_OBJECT_PLAYLIST
: psz_object
= "playlist"; break;
467 case VLC_OBJECT_ITEM
: psz_object
= "item"; break;
468 case VLC_OBJECT_INPUT
: psz_object
= "input"; break;
469 case VLC_OBJECT_DECODER
: psz_object
= "decoder"; break;
470 case VLC_OBJECT_VOUT
: psz_object
= "video output"; break;
471 case VLC_OBJECT_AOUT
: psz_object
= "audio output"; break;
472 case VLC_OBJECT_SOUT
: psz_object
= "stream output"; break;
476 # define CE_WRITE(str) WriteFile( p_this->p_libvlc->msg_bank.logfile, \
477 str, strlen(str), &i_dummy, NULL );
478 CE_WRITE( p_item
->psz_module
);
480 CE_WRITE( psz_object
);
481 CE_WRITE( ppsz_type
[i_type
] );
483 CE_WRITE( p_item
->psz_msg
);
485 FlushFileBuffers( p_this
->p_libvlc
->msg_bank
.logfile
);
488 /* Send the message to stderr */
489 if( p_this
->p_libvlc
->b_color
)
491 fprintf( stderr
, "[" GREEN
"%.8i" GRAY
"] %s %s%s: %s%s" GRAY
"\n",
492 p_item
->i_object_id
, p_item
->psz_module
, psz_object
,
493 ppsz_type
[i_type
], ppsz_color
[i_type
],
498 fprintf( stderr
, "[%.8i] %s %s%s: %s\n", p_item
->i_object_id
,
499 p_item
->psz_module
, psz_object
, ppsz_type
[i_type
],