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 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
33 #include <stdarg.h> /* va_list for BSD */
36 # include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
39 #include <errno.h> /* errno */
42 # include <unistd.h> /* close(), write() */
47 #include <vlc_charset.h>
49 /*****************************************************************************
51 *****************************************************************************/
52 #if defined(HAVE_VA_COPY)
53 # define vlc_va_copy(dest,src) va_copy(dest,src)
54 #elif defined(HAVE___VA_COPY)
55 # define vlc_va_copy(dest,src) __va_copy(dest,src)
57 # define vlc_va_copy(dest,src) (dest)=(src)
60 #define QUEUE(i) p_this->p_libvlc->msg_bank.queues[i]
61 #define LOCK_BANK vlc_mutex_lock( &p_this->p_libvlc->msg_bank.lock );
62 #define UNLOCK_BANK vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.lock );
64 /*****************************************************************************
66 *****************************************************************************/
67 static void QueueMsg ( vlc_object_t
*, int, int , const char *,
68 const char *, va_list );
69 static void FlushMsg ( msg_queue_t
* );
70 static void PrintMsg ( vlc_object_t
*, msg_item_t
* );
73 * Initialize messages queues
74 * This function initializes all message queues
76 void __msg_Create( vlc_object_t
*p_this
)
79 vlc_mutex_init( p_this
, &(p_this
->p_libvlc
->msg_bank
.lock
) );
81 for( i
= 0; i
< 2; i
++ )
83 vlc_mutex_init( p_this
, &QUEUE(i
).lock
);
84 QUEUE(i
).b_overflow
= VLC_FALSE
;
93 QUEUE(MSG_QUEUE_NORMAL
).logfile
=
94 CreateFile( L
"vlc-log.txt", GENERIC_WRITE
,
95 FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
96 CREATE_ALWAYS
, 0, NULL
);
97 SetFilePointer( QUEUE(MSG_QUEUE_NORMAL
).logfile
, 0, NULL
, FILE_END
);
102 * Flush all message queues
104 void __msg_Flush( vlc_object_t
*p_this
)
107 for( i
= 0 ; i
< NB_QUEUES
; i
++ )
109 vlc_mutex_lock( &QUEUE(i
).lock
);
110 FlushMsg( &QUEUE(i
) );
111 vlc_mutex_unlock( &QUEUE(i
).lock
);
116 * Destroy the message queues
118 * This functions prints all messages remaining in the queues,
119 * then frees all the allocated ressources
120 * No other messages interface functions should be called after this one.
122 void __msg_Destroy( vlc_object_t
*p_this
)
125 for( i
= NB_QUEUES
-1 ; i
>= 0; i
-- )
128 msg_Err( p_this
, "stale interface subscribers" );
130 FlushMsg( &QUEUE(i
) );
133 if( i
== MSG_QUEUE_NORMAL
)
134 CloseHandle( QUEUE(MSG_QUEUE_NORMAL
).logfile
);
137 vlc_mutex_destroy( &QUEUE(i
).lock
);
139 vlc_mutex_destroy( &(p_this
->p_libvlc
->msg_bank
.lock
) );
143 * Subscribe to a message queue.
145 msg_subscription_t
*__msg_Subscribe( vlc_object_t
*p_this
, int i
)
147 msg_subscription_t
*p_sub
= malloc( sizeof( msg_subscription_t
) );
149 assert( i
< NB_QUEUES
);
152 vlc_mutex_lock( &QUEUE(i
).lock
);
154 TAB_APPEND( QUEUE(i
).i_sub
, QUEUE(i
).pp_sub
, p_sub
);
156 p_sub
->i_start
= QUEUE(i
).i_start
;
157 p_sub
->pi_stop
= &QUEUE(i
).i_stop
;
158 p_sub
->p_msg
= QUEUE(i
).msg
;
159 p_sub
->p_lock
= &QUEUE(i
).lock
;
161 vlc_mutex_unlock( &QUEUE(i
).lock
);
168 * Unsubscribe from a message queue.
170 void __msg_Unsubscribe( vlc_object_t
*p_this
, msg_subscription_t
*p_sub
)
175 for( i
= 0 ; i
< NB_QUEUES
; i
++ )
177 vlc_mutex_lock( &QUEUE(i
).lock
);
178 for( j
= 0 ; j
< QUEUE(i
).i_sub
; j
++ )
180 if( QUEUE(i
).pp_sub
[j
] == p_sub
)
182 REMOVE_ELEM( QUEUE(i
).pp_sub
, QUEUE(i
).i_sub
, j
);
183 if( p_sub
) free( p_sub
);
186 vlc_mutex_unlock( & QUEUE(i
).lock
);
191 const char *msg_GetObjectTypeName(int i_object_type
)
193 switch( i_object_type
)
195 case VLC_OBJECT_GLOBAL
: return "global";
196 case VLC_OBJECT_LIBVLC
: return "libvlc";
197 case VLC_OBJECT_MODULE
: return "module";
198 case VLC_OBJECT_INTF
: return "interface";
199 case VLC_OBJECT_PLAYLIST
: return "playlist";
200 case VLC_OBJECT_ITEM
: return "item";
201 case VLC_OBJECT_INPUT
: return "input";
202 case VLC_OBJECT_DECODER
: return "decoder";
203 case VLC_OBJECT_PACKETIZER
: return "packetizer";
204 case VLC_OBJECT_ENCODER
: return "encoder";
205 case VLC_OBJECT_VOUT
: return "video output";
206 case VLC_OBJECT_AOUT
: return "audio output";
207 case VLC_OBJECT_SOUT
: return "stream output";
208 case VLC_OBJECT_HTTPD
: return "http server";
209 case VLC_OBJECT_HTTPD_HOST
: return "http server";
210 case VLC_OBJECT_DIALOGS
: return "dialogs provider";
211 case VLC_OBJECT_VLM
: return "vlm";
212 case VLC_OBJECT_ANNOUNCE
: return "announce handler";
213 case VLC_OBJECT_DEMUX
: return "demuxer";
214 case VLC_OBJECT_ACCESS
: return "access";
215 case VLC_OBJECT_META_ENGINE
: return "meta engine";
216 default: return "private";
220 /*****************************************************************************
221 * __msg_*: print a message
222 *****************************************************************************
223 * These functions queue a message for later printing.
224 *****************************************************************************/
225 void __msg_Generic( vlc_object_t
*p_this
, int i_queue
, int i_type
,
226 const char *psz_module
,
227 const char *psz_format
, ... )
231 va_start( args
, psz_format
);
232 QueueMsg( p_this
, i_queue
, i_type
, psz_module
, psz_format
, args
);
236 void __msg_GenericVa( vlc_object_t
*p_this
, int i_queue
,
237 int i_type
, const char *psz_module
,
238 const char *psz_format
, va_list args
)
240 QueueMsg( p_this
, i_queue
, i_type
, psz_module
, psz_format
, args
);
243 /* Generic functions used when variadic macros are not available. */
244 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
245 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
248 va_start( args, psz_format ); \
249 QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
250 psz_format, args ); \
255 * Output an informational message.
256 * \note Do not use this for debug messages
259 DECLARE_MSG_FN( __msg_Info
, VLC_MSG_INFO
);
261 * Output an error message.
263 DECLARE_MSG_FN( __msg_Err
, VLC_MSG_ERR
);
265 * Output a waring message
267 DECLARE_MSG_FN( __msg_Warn
, VLC_MSG_WARN
);
269 * Output a debug message
271 DECLARE_MSG_FN( __msg_Dbg
, VLC_MSG_DBG
);
274 * Add a message to a queue
276 * This function provides basic functionnalities to other msg_* functions.
277 * It adds a message to a queue (after having printed all stored messages if it
278 * is full). If the message can't be converted to string in memory, it issues
281 static void QueueMsg( vlc_object_t
*p_this
, int i_queue
, int i_type
,
282 const char *psz_module
,
283 const char *psz_format
, va_list _args
)
285 int i_header_size
; /* Size of the additionnal header */
287 char * psz_str
= NULL
; /* formatted message string */
288 char * psz_header
= NULL
;
290 msg_item_t
* p_item
= NULL
; /* pointer to message */
291 msg_item_t item
; /* message in case of a full queue */
292 msg_queue_t
*p_queue
;
294 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
295 int i_size
= strlen(psz_format
) + INTF_MAX_MSG_SIZE
;
298 if( p_this
== NULL
|| p_this
->i_flags
& OBJECT_FLAGS_QUIET
||
299 (p_this
->i_flags
& OBJECT_FLAGS_NODBG
&& i_type
== VLC_MSG_DBG
) )
305 /* Expand %m to strerror(errno) - only once */
306 char buf
[strlen( psz_format
) + 2001], *ptr
;
307 strcpy( buf
, psz_format
);
308 ptr
= psz_format
= buf
;
312 ptr
= strchr( ptr
, '%' );
322 strerror_r( errno
, errbuf
, 1001 );
324 int sockerr
= WSAGetLastError( );
327 strncpy( errbuf
, net_strerror( sockerr
), 1001 );
328 WSASetLastError( sockerr
);
331 || (strcmp ("Unknown network stack error", errbuf
) == 0))
332 strncpy( errbuf
, strerror( errno
), 1001 );
336 /* Escape '%' from the error string */
337 for( char *percent
= strchr( errbuf
, '%' );
339 percent
= strchr( percent
+ 2, '%' ) )
341 memmove( percent
+ 1, percent
, strlen( percent
) + 1 );
344 errlen
= strlen( errbuf
);
345 memmove( ptr
+ errlen
, ptr
+ 2, strlen( ptr
+ 2 ) + 1 );
346 memcpy( ptr
, errbuf
, errlen
);
347 break; /* Only once, so we don't overflow */
350 /* Looks for conversion specifier... */
353 while( *ptr
&& ( strchr( "diouxXeEfFgGaAcspn%", *ptr
) == NULL
) );
355 ptr
++; /* ...and skip it */
359 /* Convert message to string */
360 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
361 vlc_va_copy( args
, _args
);
362 if( vasprintf( &psz_str
, psz_format
, args
) == -1 )
366 psz_str
= (char*) malloc( i_size
);
369 if( psz_str
== NULL
)
372 fprintf( stderr
, "main warning: can't store message (%m): " );
376 /* we're not using GLIBC, so we are sure that the error description
377 * will be stored in the buffer we provide to strerror_r() */
378 strerror_r( errno
, psz_err
, 1001 );
380 strncpy( psz_err
, strerror( errno
), 1001 );
382 psz_err
[1000] = '\0';
383 fprintf( stderr
, "main warning: can't store message (%s): ", psz_err
);
385 vlc_va_copy( args
, _args
);
386 /* We should use utf8_vfprintf - but it calls malloc()... */
387 vfprintf( stderr
, psz_format
, args
);
389 fputs( "\n", stderr
);
395 while( p_obj
!= NULL
)
397 char *psz_old
= NULL
;
398 if( p_obj
->psz_header
)
400 i_header_size
+= strlen( p_obj
->psz_header
) + 4;
403 psz_old
= strdup( psz_header
);
404 psz_header
= (char*)realloc( psz_header
, i_header_size
);
405 snprintf( psz_header
, i_header_size
, "[%s] %s",
406 p_obj
->psz_header
, psz_old
);
410 psz_header
= (char *)malloc( i_header_size
);
411 snprintf( psz_header
, i_header_size
, "[%s]",
415 if( psz_old
) free( psz_old
);
416 p_obj
= p_obj
->p_parent
;
419 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
420 vlc_va_copy( args
, _args
);
421 vsnprintf( psz_str
, i_size
, psz_format
, args
);
423 psz_str
[ i_size
- 1 ] = 0; /* Just in case */
426 assert( i_queue
< NB_QUEUES
);
428 p_queue
= &QUEUE(i_queue
) ;
429 vlc_mutex_lock( &p_queue
->lock
);
431 /* Check there is room in the queue for our message */
432 if( p_queue
->b_overflow
)
436 if( ((p_queue
->i_stop
- p_queue
->i_start
+ 1) % VLC_MSG_QSIZE
) == 0 )
438 /* Still in overflow mode, print from a dummy item */
443 /* Pheeew, at last, there is room in the queue! */
444 p_queue
->b_overflow
= VLC_FALSE
;
447 else if( ((p_queue
->i_stop
- p_queue
->i_start
+ 2) % VLC_MSG_QSIZE
) == 0 )
451 if( ((p_queue
->i_stop
- p_queue
->i_start
+ 2) % VLC_MSG_QSIZE
) == 0 )
453 p_queue
->b_overflow
= VLC_TRUE
;
455 if( p_queue
->i_id
== MSG_QUEUE_NORMAL
)
457 /* Put the overflow message in the queue */
458 p_item
= p_queue
->msg
+ p_queue
->i_stop
;
459 p_queue
->i_stop
= (p_queue
->i_stop
+ 1) % VLC_MSG_QSIZE
;
461 p_item
->i_type
= VLC_MSG_WARN
;
462 p_item
->i_object_id
= p_this
->i_object_id
;
463 p_item
->i_object_type
= p_this
->i_object_type
;
464 p_item
->psz_module
= strdup( "message" );
465 p_item
->psz_msg
= strdup( "message queue overflowed" );
466 p_item
->psz_header
= NULL
;
468 PrintMsg( p_this
, p_item
);
469 /* We print from a dummy item */
475 if( !p_queue
->b_overflow
)
477 /* Put the message in the queue */
478 p_item
= p_queue
->msg
+ p_queue
->i_stop
;
479 p_queue
->i_stop
= (p_queue
->i_stop
+ 1) % VLC_MSG_QSIZE
;
482 /* Fill message information fields */
483 p_item
->i_type
= i_type
;
484 p_item
->i_object_id
= p_this
->i_object_id
;
485 p_item
->i_object_type
= p_this
->i_object_type
;
486 p_item
->psz_module
= strdup( psz_module
);
487 p_item
->psz_msg
= psz_str
;
488 p_item
->psz_header
= psz_header
;
490 if( p_queue
->i_id
== MSG_QUEUE_NORMAL
)
491 PrintMsg( p_this
, p_item
);
493 if( p_queue
->b_overflow
)
495 if( p_item
->psz_module
)
496 free( p_item
->psz_module
);
497 if( p_item
->psz_msg
)
498 free( p_item
->psz_msg
);
499 if( p_item
->psz_header
)
500 free( p_item
->psz_header
);
503 vlc_mutex_unlock ( &p_queue
->lock
);
507 /* following functions are local */
509 /*****************************************************************************
511 *****************************************************************************
512 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
513 * this function does not check the lock.
514 *****************************************************************************/
515 static void FlushMsg ( msg_queue_t
*p_queue
)
517 int i_index
, i_start
, i_stop
;
519 /* Get the maximum message index that can be freed */
520 i_stop
= p_queue
->i_stop
;
522 /* Check until which value we can free messages */
523 for( i_index
= 0; i_index
< p_queue
->i_sub
; i_index
++ )
525 i_start
= p_queue
->pp_sub
[ i_index
]->i_start
;
527 /* If this subscriber is late, we don't free messages before
528 * his i_start value, otherwise he'll miss messages */
529 if( ( i_start
< i_stop
530 && (p_queue
->i_stop
<= i_start
|| i_stop
<= p_queue
->i_stop
) )
531 || ( i_stop
< i_start
532 && (i_stop
<= p_queue
->i_stop
&& p_queue
->i_stop
<= i_start
) ) )
538 /* Free message data */
539 for( i_index
= p_queue
->i_start
;
541 i_index
= (i_index
+1) % VLC_MSG_QSIZE
)
543 if( p_queue
->msg
[i_index
].psz_msg
)
544 free( p_queue
->msg
[i_index
].psz_msg
);
545 if( p_queue
->msg
[i_index
].psz_module
)
546 free( p_queue
->msg
[i_index
].psz_module
);
547 if( p_queue
->msg
[i_index
].psz_header
)
548 free( p_queue
->msg
[i_index
].psz_header
);
551 /* Update the new start value */
552 p_queue
->i_start
= i_index
;
555 /*****************************************************************************
556 * PrintMsg: output a standard message item to stderr
557 *****************************************************************************
558 * Print a message to stderr, with colour formatting if needed.
559 *****************************************************************************/
560 static void PrintMsg ( vlc_object_t
* p_this
, msg_item_t
* p_item
)
562 # define COL(x) "\033[" #x ";1m"
564 # define GREEN COL(32)
565 # define YELLOW COL(33)
566 # define WHITE COL(0)
567 # define GRAY "\033[0m"
572 static const char * ppsz_type
[4] = { "", " error", " warning", " debug" };
573 static const char *ppsz_color
[4] = { WHITE
, RED
, YELLOW
, GRAY
};
574 const char *psz_object
;
575 int i_type
= p_item
->i_type
;
580 if( p_this
->p_libvlc
->i_verbose
< 0 ) return;
583 if( p_this
->p_libvlc
->i_verbose
< 0 ) return;
586 if( p_this
->p_libvlc
->i_verbose
< 1 ) return;
589 if( p_this
->p_libvlc
->i_verbose
< 2 ) return;
593 psz_object
= msg_GetObjectTypeName(p_item
->i_object_type
);
596 # define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL).logfile, \
597 str, strlen(str), &i_dummy, NULL );
598 CE_WRITE( p_item
->psz_module
);
600 CE_WRITE( psz_object
);
601 CE_WRITE( ppsz_type
[i_type
] );
603 CE_WRITE( p_item
->psz_msg
);
605 FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL
).logfile
);
608 /* Send the message to stderr */
609 if( p_this
->p_libvlc
->b_color
)
611 if( p_item
->psz_header
)
613 utf8_fprintf( stderr
, "[" GREEN
"%.8i" GRAY
"] %s %s %s%s: %s%s" GRAY
615 p_item
->i_object_id
, p_item
->psz_header
,
616 p_item
->psz_module
, psz_object
,
617 ppsz_type
[i_type
], ppsz_color
[i_type
],
622 utf8_fprintf( stderr
, "[" GREEN
"%.8i" GRAY
"] %s %s%s: %s%s" GRAY
"\n",
623 p_item
->i_object_id
, p_item
->psz_module
, psz_object
,
624 ppsz_type
[i_type
], ppsz_color
[i_type
],
630 if( p_item
->psz_header
)
632 utf8_fprintf( stderr
, "[%.8i] %s %s %s%s: %s\n", p_item
->i_object_id
,
633 p_item
->psz_header
, p_item
->psz_module
,
634 psz_object
, ppsz_type
[i_type
], p_item
->psz_msg
);
638 utf8_fprintf( stderr
, "[%.8i] %s %s%s: %s\n", p_item
->i_object_id
,
639 p_item
->psz_module
, psz_object
, ppsz_type
[i_type
],
650 static msg_context_t
* GetContext(void)
652 msg_context_t
*p_ctx
= vlc_threadvar_get( &msg_context_global_key
);
655 MALLOC_NULL( p_ctx
, msg_context_t
);
656 p_ctx
->psz_message
= NULL
;
657 vlc_threadvar_set( &msg_context_global_key
, p_ctx
);
662 void msg_StackSet( int i_code
, const char *psz_message
, ... )
665 msg_context_t
*p_ctx
= GetContext();
668 va_start( ap
, psz_message
);
669 free( p_ctx
->psz_message
);
671 if( vasprintf( &p_ctx
->psz_message
, psz_message
, ap
) == -1 )
672 p_ctx
->psz_message
= NULL
;
675 p_ctx
->i_code
= i_code
;
678 void msg_StackAdd( const char *psz_message
, ... )
682 msg_context_t
*p_ctx
= GetContext();
685 va_start( ap
, psz_message
);
686 if( vasprintf( &psz_tmp
, psz_message
, ap
) == -1 )
690 if( !p_ctx
->psz_message
)
691 p_ctx
->psz_message
= psz_tmp
;
694 char *psz_old
= malloc( strlen( p_ctx
->psz_message
) + 1 );
695 memcpy( psz_old
, p_ctx
->psz_message
, strlen( p_ctx
->psz_message
) + 1 );
696 p_ctx
->psz_message
= realloc( p_ctx
->psz_message
,
697 strlen( p_ctx
->psz_message
) +
699 strlen( psz_tmp
) + 3 );
700 sprintf( p_ctx
->psz_message
, "%s: %s", psz_tmp
, psz_old
);
701 free( psz_tmp
); free( psz_old
);
705 const char* msg_StackMsg( void )
707 msg_context_t
*p_ctx
= GetContext();
709 return p_ctx
->psz_message
;