]> git.sesse.net Git - vlc/blob - src/misc/messages.c
3e05628973599c54eb609bc2336cc239b69a7e2d
[vlc] / src / misc / messages.c
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-2005 the VideoLAN team
7  * $Id$
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdio.h>                                               /* required */
31 #include <stdarg.h>                                       /* va_list for BSD */
32 #include <stdlib.h>                                              /* malloc() */
33 #include <string.h>                                            /* strerror() */
34
35 #include <vlc/vlc.h>
36
37 #ifdef HAVE_FCNTL_H
38 #   include <fcntl.h>                  /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
39 #endif
40
41 #include <errno.h>                                                  /* errno */
42
43 #ifdef HAVE_UNISTD_H
44 #   include <unistd.h>                                   /* close(), write() */
45 #endif
46
47 #include "vlc_interface.h"
48
49 /*****************************************************************************
50  * Local macros
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)
56 #else
57 #   define vlc_va_copy(dest,src) (dest)=(src)
58 #endif
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static void QueueMsg ( vlc_object_t *, int , const char *,
64                        const char *, va_list );
65 static void FlushMsg ( msg_bank_t * );
66 static void PrintMsg ( vlc_object_t *, msg_item_t * );
67
68 /**
69  * Initialize messages interface
70  *
71  * This functions has to be called before any call to other msg_* functions.
72  * It set up the locks and the message queue if it is used.
73  */
74 void __msg_Create( vlc_object_t *p_this )
75 {
76     /* Message queue initialization */
77     vlc_mutex_init( p_this, &p_this->p_libvlc->msg_bank.lock );
78
79     p_this->p_libvlc->msg_bank.b_configured = VLC_FALSE;
80     p_this->p_libvlc->msg_bank.b_overflow = VLC_FALSE;
81
82     p_this->p_libvlc->msg_bank.i_start = 0;
83     p_this->p_libvlc->msg_bank.i_stop = 0;
84
85     p_this->p_libvlc->msg_bank.i_sub = 0;
86     p_this->p_libvlc->msg_bank.pp_sub = NULL;
87
88 #ifdef UNDER_CE
89     p_this->p_libvlc->msg_bank.logfile =
90         CreateFile( L"vlc-log.txt", GENERIC_WRITE,
91                     FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
92                     CREATE_ALWAYS, 0, NULL );
93     SetFilePointer( p_this->p_libvlc->msg_bank.logfile, 0, NULL, FILE_END );
94 #endif
95 }
96
97 /**
98  * Flush the message queue
99  */
100 void __msg_Flush( vlc_object_t *p_this )
101 {
102     int i_index;
103
104     vlc_mutex_lock( &p_this->p_libvlc->msg_bank.lock );
105
106     p_this->p_libvlc->msg_bank.b_configured = VLC_TRUE;
107
108     for( i_index = p_this->p_libvlc->msg_bank.i_start;
109          i_index != p_this->p_libvlc->msg_bank.i_stop;
110          i_index = (i_index+1) % VLC_MSG_QSIZE )
111     {
112         PrintMsg( p_this, &p_this->p_libvlc->msg_bank.msg[i_index] );
113     }
114
115     FlushMsg( &p_this->p_libvlc->msg_bank );
116
117     vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.lock );
118 }
119
120 /**
121  * Free resources allocated by msg_Create
122  *
123  * This functions prints all messages remaining in queue, then free all the
124  * resources allocated by msg_Create.
125  * No other messages interface functions should be called after this one.
126  */
127 void __msg_Destroy( vlc_object_t *p_this )
128 {
129     if( p_this->p_libvlc->msg_bank.i_sub )
130     {
131         msg_Err( p_this, "stale interface subscribers" );
132     }
133
134     /* Flush the queue */
135     if( !p_this->p_libvlc->msg_bank.b_configured )
136     {
137         msg_Flush( p_this );
138     }
139     else
140     {
141         FlushMsg( &p_this->p_libvlc->msg_bank );
142     }
143
144 #ifdef UNDER_CE
145     CloseHandle( p_this->p_libvlc->msg_bank.logfile );
146 #endif
147
148     /* Destroy lock */
149     vlc_mutex_destroy( &p_this->p_libvlc->msg_bank.lock );
150 }
151
152 /**
153  * Subscribe to the message queue.
154  */
155 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
156 {
157     msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
158     msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
159
160     vlc_mutex_lock( &p_bank->lock );
161
162     /* Add subscription to the list */
163     INSERT_ELEM( p_bank->pp_sub, p_bank->i_sub, p_bank->i_sub, p_sub );
164
165     p_sub->i_start = p_bank->i_start;
166     p_sub->pi_stop = &p_bank->i_stop;
167
168     p_sub->p_msg   = p_bank->msg;
169     p_sub->p_lock  = &p_bank->lock;
170
171     vlc_mutex_unlock( &p_bank->lock );
172
173     return p_sub;
174 }
175
176 /**
177  * Unsubscribe from the message queue.
178  */
179 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
180 {
181     msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
182     int i_index;
183
184     vlc_mutex_lock( &p_bank->lock );
185
186     /* Sanity check */
187     if( !p_bank->i_sub )
188     {
189         msg_Err( p_this, "no subscriber in the list" );
190         return;
191     }
192
193     /* Look for the appropriate subscription */
194     for( i_index = 0; i_index < p_bank->i_sub; i_index++ )
195     {
196         if( p_bank->pp_sub[ i_index ] == p_sub )
197         {
198             break;
199         }
200     }
201
202     if( p_bank->pp_sub[ i_index ] != p_sub )
203     {
204         msg_Err( p_this, "subscriber not found" );
205         vlc_mutex_unlock( &p_bank->lock );
206         return;
207     }
208
209     /* Remove this subscription */
210     REMOVE_ELEM( p_bank->pp_sub, p_bank->i_sub, i_index );
211
212     if( p_sub ) free( p_sub );
213     vlc_mutex_unlock( &p_bank->lock );
214 }
215
216 /*****************************************************************************
217  * __msg_*: print a message
218  *****************************************************************************
219  * These functions queue a message for later printing.
220  *****************************************************************************/
221 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
222                     const char *psz_format, ... )
223 {
224     va_list args;
225
226     va_start( args, psz_format );
227     QueueMsg( p_this, i_type, psz_module, psz_format, args );
228     va_end( args );
229 }
230
231 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
232                       const char *psz_format, va_list args )
233 {
234     QueueMsg( p_this, i_type, psz_module, psz_format, args );
235 }
236
237 /* Generic functions used when variadic macros are not available. */
238 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
239     void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
240     { \
241         va_list args; \
242         va_start( args, psz_format ); \
243         QueueMsg( (vlc_object_t *)p_this, FN_TYPE, "unknown", \
244                   psz_format, args ); \
245         va_end( args ); \
246     } \
247     struct _
248 /**
249  * Output an informational message.
250  * \note Do not use this for debug messages
251  * \see input_AddInfo
252  */
253 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
254 /**
255  * Output an error message.
256  */
257 DECLARE_MSG_FN( __msg_Err,  VLC_MSG_ERR );
258 /**
259  * Output a waring message
260  */
261 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
262 /**
263  * Output a debug message
264  */
265 DECLARE_MSG_FN( __msg_Dbg,  VLC_MSG_DBG );
266
267 /**
268  * Add a message to a queue
269  *
270  * This function provides basic functionnalities to other msg_* functions.
271  * It adds a message to a queue (after having printed all stored messages if it
272  * is full). If the message can't be converted to string in memory, it issues
273  * a warning.
274  */
275 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
276                       const char *psz_format, va_list _args )
277 {
278     msg_bank_t * p_bank = &p_this->p_libvlc->msg_bank;       /* message bank */
279     char *       psz_str = NULL;                 /* formatted message string */
280     va_list      args;
281     msg_item_t * p_item = NULL;                        /* pointer to message */
282     msg_item_t   item;                    /* message in case of a full queue */
283
284 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
285     int          i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
286 #endif
287
288     /*
289      * Convert message to string
290      */
291 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined( SYS_BEOS )
292     vlc_va_copy( args, _args );
293     vasprintf( &psz_str, psz_format, args );
294     va_end( args );
295 #else
296     psz_str = (char*) malloc( i_size * sizeof(char) );
297 #endif
298
299     if( psz_str == NULL )
300     {
301         fprintf( stderr, "main warning: can't store message (%s): ",
302                  strerror(errno) );
303         vlc_va_copy( args, _args );
304         vfprintf( stderr, psz_format, args );
305         va_end( args );
306         fprintf( stderr, "\n" );
307         return;
308     }
309
310 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
311     vlc_va_copy( args, _args );
312     vsnprintf( psz_str, i_size, psz_format, args );
313     va_end( args );
314     psz_str[ i_size - 1 ] = 0; /* Just in case */
315 #endif
316
317     /* Put message in queue */
318     vlc_mutex_lock( &p_bank->lock );
319
320     /* Check there is room in the queue for our message */
321     if( p_bank->b_overflow )
322     {
323         FlushMsg( p_bank );
324
325         if( ((p_bank->i_stop - p_bank->i_start + 1) % VLC_MSG_QSIZE) == 0 )
326         {
327             /* Still in overflow mode, print from a dummy item */
328             p_item = &item;
329         }
330         else
331         {
332             /* Pheeew, at last, there is room in the queue! */
333             p_bank->b_overflow = VLC_FALSE;
334         }
335     }
336     else if( ((p_bank->i_stop - p_bank->i_start + 2) % VLC_MSG_QSIZE) == 0 )
337     {
338         FlushMsg( p_bank );
339
340         if( ((p_bank->i_stop - p_bank->i_start + 2) % VLC_MSG_QSIZE) == 0 )
341         {
342             p_bank->b_overflow = VLC_TRUE;
343
344             /* Put the overflow message in the queue */
345             p_item = p_bank->msg + p_bank->i_stop;
346             p_bank->i_stop = (p_bank->i_stop + 1) % VLC_MSG_QSIZE;
347
348             p_item->i_type =        VLC_MSG_WARN;
349             p_item->i_object_id =   p_this->i_object_id;
350             p_item->i_object_type = p_this->i_object_type;
351             p_item->psz_module =    strdup( "message" );
352             p_item->psz_msg =       strdup( "message queue overflowed" );
353
354             PrintMsg( p_this, p_item );
355
356             /* We print from a dummy item */
357             p_item = &item;
358         }
359     }
360
361     if( !p_bank->b_overflow )
362     {
363         /* Put the message in the queue */
364         p_item = p_bank->msg + p_bank->i_stop;
365         p_bank->i_stop = (p_bank->i_stop + 1) % VLC_MSG_QSIZE;
366     }
367
368     /* Fill message information fields */
369     p_item->i_type =        i_type;
370     p_item->i_object_id =   p_this->i_object_id;
371     p_item->i_object_type = p_this->i_object_type;
372     p_item->psz_module =    strdup( psz_module );
373     p_item->psz_msg =       psz_str;
374
375     PrintMsg( p_this, p_item );
376
377     if( p_bank->b_overflow )
378     {
379         if( p_item->psz_module )
380             free( p_item->psz_module );
381         if( p_item->psz_msg )
382             free( p_item->psz_msg );
383     }
384
385     vlc_mutex_unlock( &p_bank->lock );
386 }
387
388 /* following functions are local */
389
390 /*****************************************************************************
391  * FlushMsg
392  *****************************************************************************
393  * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
394  * this function does not check the lock.
395  *****************************************************************************/
396 static void FlushMsg ( msg_bank_t *p_bank )
397 {
398     int i_index, i_start, i_stop;
399
400     /* Only flush the queue if it has been properly configured */
401     if( !p_bank->b_configured )
402     {
403         return;
404     }
405
406     /* Get the maximum message index that can be freed */
407     i_stop = p_bank->i_stop;
408
409     /* Check until which value we can free messages */
410     for( i_index = 0; i_index < p_bank->i_sub; i_index++ )
411     {
412         i_start = p_bank->pp_sub[ i_index ]->i_start;
413
414         /* If this subscriber is late, we don't free messages before
415          * his i_start value, otherwise he'll miss messages */
416         if(   ( i_start < i_stop
417                && (p_bank->i_stop <= i_start || i_stop <= p_bank->i_stop) )
418            || ( i_stop < i_start
419                && (i_stop <= p_bank->i_stop && p_bank->i_stop <= i_start) ) )
420         {
421             i_stop = i_start;
422         }
423     }
424
425     /* Free message data */
426     for( i_index = p_bank->i_start;
427          i_index != i_stop;
428          i_index = (i_index+1) % VLC_MSG_QSIZE )
429     {
430         if( p_bank->msg[i_index].psz_msg )
431             free( p_bank->msg[i_index].psz_msg );
432         if( p_bank->msg[i_index].psz_module )
433             free( p_bank->msg[i_index].psz_module );
434     }
435
436     /* Update the new start value */
437     p_bank->i_start = i_index;
438 }
439
440 /*****************************************************************************
441  * PrintMsg: output a message item to stderr
442  *****************************************************************************
443  * Print a message to stderr, with colour formatting if needed.
444  *****************************************************************************/
445 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
446 {
447 #   define COL(x)  "\033[" #x ";1m"
448 #   define RED     COL(31)
449 #   define GREEN   COL(32)
450 #   define YELLOW  COL(33)
451 #   define WHITE   COL(37)
452 #   define GRAY    "\033[0m"
453
454 #ifdef UNDER_CE
455     int i_dummy;
456 #endif
457     static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
458     static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
459     char *psz_object = "private";
460     int i_type = p_item->i_type;
461
462     switch( i_type )
463     {
464         case VLC_MSG_ERR:
465             if( p_this->p_libvlc->i_verbose < 0 ) return;
466             break;
467         case VLC_MSG_INFO:
468             if( p_this->p_libvlc->i_verbose < 0 ) return;
469             break;
470         case VLC_MSG_WARN:
471             if( p_this->p_libvlc->i_verbose < 1 ) return;
472             break;
473         case VLC_MSG_DBG:
474             if( p_this->p_libvlc->i_verbose < 2 ) return;
475             break;
476     }
477
478     switch( p_item->i_object_type )
479     {
480         case VLC_OBJECT_ROOT: psz_object = "root"; break;
481         case VLC_OBJECT_VLC: psz_object = "vlc"; break;
482         case VLC_OBJECT_MODULE: psz_object = "module"; break;
483         case VLC_OBJECT_INTF: psz_object = "interface"; break;
484         case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
485         case VLC_OBJECT_ITEM: psz_object = "item"; break;
486         case VLC_OBJECT_INPUT: psz_object = "input"; break;
487         case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
488         case VLC_OBJECT_PACKETIZER: psz_object = "packetizer"; break;
489         case VLC_OBJECT_ENCODER: psz_object = "encoder"; break;
490         case VLC_OBJECT_VOUT: psz_object = "video output"; break;
491         case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
492         case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
493         case VLC_OBJECT_HTTPD: psz_object = "http daemon"; break;
494         case VLC_OBJECT_DIALOGS: psz_object = "dialogs provider"; break;
495         case VLC_OBJECT_VLM: psz_object = "vlm"; break;
496         case VLC_OBJECT_ANNOUNCE: psz_object = "announce handler"; break;
497         case VLC_OBJECT_DEMUX: psz_object = "demuxer"; break;
498         case VLC_OBJECT_ACCESS: psz_object = "access"; break;
499     }
500
501 #ifdef UNDER_CE
502 #   define CE_WRITE(str) WriteFile( p_this->p_libvlc->msg_bank.logfile, \
503                                     str, strlen(str), &i_dummy, NULL );
504     CE_WRITE( p_item->psz_module );
505     CE_WRITE( " " );
506     CE_WRITE( psz_object );
507     CE_WRITE( ppsz_type[i_type] );
508     CE_WRITE( ": " );
509     CE_WRITE( p_item->psz_msg );
510     CE_WRITE( "\r\n" );
511     FlushFileBuffers( p_this->p_libvlc->msg_bank.logfile );
512
513 #else
514     /* Send the message to stderr */
515     if( p_this->p_libvlc->b_color )
516     {
517         fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
518                          p_item->i_object_id, p_item->psz_module, psz_object,
519                          ppsz_type[i_type], ppsz_color[i_type],
520                          p_item->psz_msg );
521     }
522     else
523     {
524         fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
525                          p_item->psz_module, psz_object, ppsz_type[i_type],
526                          p_item->psz_msg );
527     }
528
529 #   if defined(WIN32)
530     fflush( stderr );
531 #   endif
532 #endif
533 }