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