1 /*****************************************************************************
2 * intf_msg.c: messages interface
3 * This library provides basic functions for threads to interact with user
4 * interface, such as message output. See config.h for output configuration.
5 *****************************************************************************
6 * Copyright (C) 1998, 1999, 2000 VideoLAN
7 * $Id: intf_msg.c,v 1.35 2001/05/31 01:37:08 sam Exp $
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <errno.h> /* errno */
32 #include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
33 #include <stdio.h> /* required */
34 #include <stdarg.h> /* va_list for BSD */
35 #include <stdlib.h> /* malloc() */
36 #include <string.h> /* strerror() */
39 #include <unistd.h> /* close(), write() */
48 #include "interface.h"
53 #define snprintf _snprintf /* snprintf not defined in mingw32 (bug?) */
56 /*****************************************************************************
58 *****************************************************************************
59 * Store a single message. Messages have a maximal size of INTF_MSG_MSGSIZE.
60 * If TRACE is defined, messages have a date field and debug messages are
61 * printed with a date to allow more precise profiling.
62 *****************************************************************************/
65 int i_type; /* message type, see below */
66 char * psz_msg; /* the message itself */
69 /* Debugging informations - in TRACE mode, debug messages have calling
70 * location informations printed */
71 mtime_t date; /* date of the message */
72 char * psz_file; /* file in which the function was called */
73 char * psz_function; /* function from which the function was called */
74 int i_line; /* line at which the function was called */
79 #define INTF_MSG_STD 0 /* standard message */
80 #define INTF_MSG_ERR 1 /* error message */
81 #define INTF_MSG_DBG 3 /* debug message */
82 #define INTF_MSG_WARN 4 /* warning message*/
85 /*****************************************************************************
87 *****************************************************************************
88 * Store all data requiered by messages interfaces. It has a single reference
90 *****************************************************************************/
91 typedef struct intf_msg_s
95 vlc_mutex_t lock; /* message queue lock */
96 int i_count; /* number of messages stored */
97 intf_msg_item_t msg[INTF_MSG_QSIZE]; /* message queue */
102 FILE * p_log_file; /* log file */
105 #if !defined(INTF_MSG_QUEUE) && !defined(TRACE_LOG)
106 /* If neither messages queue, neither log file is used, then the structure
107 * is empty. However, empty structures are not allowed in C. Therefore, a
108 * dummy integer is used to fill it. */
109 int i_dummy; /* unused filler */
113 /*****************************************************************************
115 *****************************************************************************/
117 static void QueueMsg ( intf_msg_t *p_msg, int i_type,
118 char *psz_format, va_list ap );
119 static void PrintMsg ( intf_msg_item_t *p_msg );
121 static void QueueDbgMsg ( intf_msg_t *p_msg, char *psz_file,
122 char *psz_function, int i_line,
123 char *psz_format, va_list ap );
125 #ifdef INTF_MSG_QUEUE
126 static void FlushLockedMsg ( intf_msg_t *p_msg );
130 /*****************************************************************************
131 * intf_MsgCreate: initialize messages interface (ok ?)
132 *****************************************************************************
133 * This functions has to be called before any call to other intf_*Msg functions.
134 * It set up the locks and the message queue if it is used.
135 *****************************************************************************/
136 p_intf_msg_t intf_MsgCreate( void )
140 /* Allocate structure */
141 p_msg = malloc( sizeof( intf_msg_t ) );
148 #ifdef INTF_MSG_QUEUE
149 /* Message queue initialization */
150 vlc_mutex_init( &p_msg->lock ); /* intialize lock */
151 p_msg->i_count = 0; /* queue is empty */
156 /* Log file initialization - on failure, file pointer will be null,
157 * and no log will be issued, but this is not considered as an
159 p_msg->p_log_file = fopen( TRACE_LOG, "w" );
165 /*****************************************************************************
166 * intf_MsgDestroy: free resources allocated by intf_InitMsg (ok ?)
167 *****************************************************************************
168 * This functions prints all messages remaining in queue, then free all the
169 * resources allocated by intf_InitMsg.
170 * No other messages interface functions should be called after this one.
171 *****************************************************************************/
172 void intf_MsgDestroy( void )
174 intf_FlushMsg(); /* print all remaining messages */
177 /* Close log file if any */
178 if( p_main->p_msg->p_log_file != NULL )
180 fclose( p_main->p_msg->p_log_file );
184 #ifdef INTF_MSG_QUEUE
186 vlc_mutex_destroy( &p_main->p_msg->lock );
190 free( p_main->p_msg );
193 /*****************************************************************************
194 * intf_Msg: print a message (ok ?)
195 *****************************************************************************
196 * This function queue a message for later printing, or print it immediately
197 * if the queue isn't used.
198 *****************************************************************************/
199 void intf_Msg( char *psz_format, ... )
203 va_start( ap, psz_format );
204 QueueMsg( p_main->p_msg, INTF_MSG_STD, psz_format, ap );
208 /*****************************************************************************
209 * intf_ErrMsg : print an error message (ok ?)
210 *****************************************************************************
211 * This function is the same as intf_Msg, except that it prints its messages
213 *****************************************************************************/
214 void intf_ErrMsg( char *psz_format, ... )
218 va_start( ap, psz_format );
219 QueueMsg( p_main->p_msg, INTF_MSG_ERR, psz_format, ap );
223 /*****************************************************************************
224 * intf_WarnMsg : print a warning message
225 *****************************************************************************
226 * This function is the same as intf_Msg, except that it concerns warning
227 * messages for testing purpose.
228 *****************************************************************************/
229 void intf_WarnMsg( int i_level, char *psz_format, ... )
233 if( i_level <= p_main->i_warning_level )
235 va_start( ap, psz_format );
236 QueueMsg( p_main->p_msg, INTF_MSG_WARN, psz_format, ap );
242 /*****************************************************************************
243 * _intf_DbgMsg: print a debugging message (ok ?)
244 *****************************************************************************
245 * This function prints a debugging message. Compared to other intf_*Msg
246 * functions, it is only defined if TRACE is defined and require a file name,
247 * a function name and a line number as additionnal debugging informations. It
248 * also prints a debugging header for each received line.
249 *****************************************************************************/
251 void _intf_DbgMsg( char *psz_file, char *psz_function, int i_line,
252 char *psz_format, ...)
256 va_start( ap, psz_format );
257 QueueDbgMsg( p_main->p_msg, psz_file, psz_function, i_line,
263 /*****************************************************************************
264 * intf_MsgImm: print a message (ok ?)
265 *****************************************************************************
266 * This function prints a message immediately. If the queue is used, all
267 * waiting messages are also printed.
268 *****************************************************************************/
269 void intf_MsgImm( char *psz_format, ... )
273 va_start( ap, psz_format );
274 QueueMsg( p_main->p_msg, INTF_MSG_STD, psz_format, ap );
279 /*****************************************************************************
280 * intf_ErrMsgImm: print an error message immediately (ok ?)
281 *****************************************************************************
282 * This function is the same as intf_MsgImm, except that it prints its message
284 *****************************************************************************/
285 void intf_ErrMsgImm(char *psz_format, ...)
289 va_start( ap, psz_format );
290 QueueMsg( p_main->p_msg, INTF_MSG_ERR, psz_format, ap );
295 /*****************************************************************************
296 * intf_WarnMsgImm : print a warning message
297 *****************************************************************************
298 * This function is the same as intf_MsgImm, except that it concerns warning
299 * messages for testing purpose.
300 *****************************************************************************/
301 void intf_WarnMsgImm( int i_level, char *psz_format, ... )
305 if( i_level <= p_main->i_warning_level )
307 va_start( ap, psz_format );
308 QueueMsg( p_main->p_msg, INTF_MSG_WARN, psz_format, ap );
316 /*****************************************************************************
317 * _intf_DbgMsgImm: print a debugging message immediately (ok ?)
318 *****************************************************************************
319 * This function is the same as intf_DbgMsgImm, except that it prints its
320 * message immediately. It should only be called through the macro
322 *****************************************************************************/
324 void _intf_DbgMsgImm( char *psz_file, char *psz_function, int i_line,
325 char *psz_format, ...)
329 va_start( ap, psz_format );
330 QueueDbgMsg( p_main->p_msg, psz_file, psz_function, i_line,
337 /*****************************************************************************
338 * intf_WarnHexDump : print a hexadecimal dump of a memory area
339 *****************************************************************************
340 * This is convenient for debugging purposes.
341 *****************************************************************************/
342 void intf_WarnHexDump( int i_level, void *p_data, int i_size )
347 u8 *p_area = (u8 *)p_data;
349 intf_WarnMsg( i_level, "hexdump: dumping %i bytes at address %p",
352 while( i_index < i_size )
356 while( ( i_subindex < 24 ) && ( i_index + i_subindex < i_size ) )
358 sprintf( p_string + 3 * i_subindex, "%.2x ",
359 p_area[ i_index + i_subindex ] );
364 /* -1 here is safe because we know we printed at least one */
365 p_string[ 3 * i_subindex - 1 ] = '\0';
366 intf_WarnMsg( i_level, "0x%.4x: %s", i_index, p_string );
371 intf_WarnMsg( i_level, "hexdump: %i bytes dumped", i_size );
374 /*****************************************************************************
375 * intf_FlushMsg (ok ?)
376 *****************************************************************************
377 * Print all messages remaining in queue: get lock and call FlushLockedMsg.
378 * This function does nothing if the message queue isn't used.
379 * This function is only implemented if message queue is used. If not, it is
381 *****************************************************************************/
382 #ifdef INTF_MSG_QUEUE
383 void intf_FlushMsg( void )
385 vlc_mutex_lock( &p_main->p_msg->lock ); /* get lock */
386 FlushLockedMsg( p_main->p_msg ); /* flush messages */
387 vlc_mutex_unlock( &p_main->p_msg->lock ); /* give lock back */
391 /* following functions are local */
393 /*****************************************************************************
394 * QueueMsg: add a message to a queue
395 *****************************************************************************
396 * This function provide basic functionnalities to other intf_*Msg functions.
397 * It add a message to a queue (after having printed all stored messages if it
398 * is full. If the message can't be converted to string in memory, it exit the
399 * program. If the queue is not used, it prints the message immediately.
400 *****************************************************************************/
401 static void QueueMsg( intf_msg_t *p_msg, int i_type, char *psz_format, va_list ap )
403 char * psz_str; /* formatted message string */
404 intf_msg_item_t * p_msg_item; /* pointer to message */
406 #ifndef INTF_MSG_QUEUE /*................................... instant mode ...*/
407 intf_msg_item_t msg_item; /* message */
408 p_msg_item = &msg_item;
409 #endif /*....................................................................*/
412 * Convert message to string
414 #ifdef HAVE_VASPRINTF
415 vasprintf( &psz_str, psz_format, ap );
417 psz_str = (char*) malloc( strlen(psz_format) + INTF_MAX_MSG_SIZE );
419 if( psz_str == NULL )
421 fprintf(stderr, "warning: can't store following message (%s): ",
423 vfprintf(stderr, psz_format, ap );
424 fprintf(stderr, "\n" );
427 #ifdef HAVE_VASPRINTF
429 vsprintf( psz_str, psz_format, ap );
432 #ifdef INTF_MSG_QUEUE /*...................................... queue mode ...*/
433 vlc_mutex_lock( &p_msg->lock ); /* get lock */
434 if( p_msg->i_count == INTF_MSG_QSIZE ) /* flush queue if needed */
436 #ifdef DEBUG /* in debug mode, queue overflow causes a warning */
437 fprintf(stderr, "warning: message queue overflow\n" );
439 FlushLockedMsg( p_msg );
441 p_msg_item = p_msg->msg + p_msg->i_count++; /* select message */
442 #endif /*.............................................. end of queue mode ...*/
445 * Fill message information fields
447 p_msg_item->i_type = i_type;
448 p_msg_item->psz_msg = psz_str;
450 p_msg_item->date = mdate();
453 #ifdef INTF_MSG_QUEUE /*......................................... queue mode */
454 vlc_mutex_unlock( &p_msg->lock ); /* give lock back */
455 #else /*....................................................... instant mode */
456 PrintMsg( p_msg_item ); /* print message */
457 free( psz_str ); /* free message data */
458 #endif /*....................................................................*/
461 /*****************************************************************************
462 * QueueDbgMsg: add a message to a queue with debugging informations
463 *****************************************************************************
464 * This function is the same as QueueMsg, except that it is only defined when
465 * TRACE is define, and require additionnal debugging informations.
466 *****************************************************************************/
468 static void QueueDbgMsg(intf_msg_t *p_msg, char *psz_file, char *psz_function,
469 int i_line, char *psz_format, va_list ap)
471 char * psz_str; /* formatted message string */
472 intf_msg_item_t * p_msg_item; /* pointer to message */
474 #ifndef INTF_MSG_QUEUE /*................................... instant mode ...*/
475 intf_msg_item_t msg_item; /* message */
476 p_msg_item = &msg_item;
477 #endif /*....................................................................*/
480 * Convert message to string
482 #ifdef HAVE_VASPRINTF
483 vasprintf( &psz_str, psz_format, ap );
485 psz_str = (char*) malloc( INTF_MAX_MSG_SIZE );
486 vsprintf( psz_str, psz_format, ap );
488 if( psz_str == NULL )
490 fprintf(stderr, "warning: can't store following message (%s): ",
492 fprintf(stderr, INTF_MSG_DBG_FORMAT, psz_file, psz_function, i_line );
493 vfprintf(stderr, psz_format, ap );
494 fprintf(stderr, "\n" );
498 #ifdef INTF_MSG_QUEUE /*...................................... queue mode ...*/
499 vlc_mutex_lock( &p_msg->lock ); /* get lock */
500 if( p_msg->i_count == INTF_MSG_QSIZE ) /* flush queue if needed */
502 fprintf(stderr, "warning: message queue overflow\n" );
503 FlushLockedMsg( p_msg );
505 p_msg_item = p_msg->msg + p_msg->i_count++; /* select message */
506 #endif /*.............................................. end of queue mode ...*/
509 * Fill message information fields
511 p_msg_item->i_type = INTF_MSG_DBG;
512 p_msg_item->psz_msg = psz_str;
513 p_msg_item->psz_file = psz_file;
514 p_msg_item->psz_function = psz_function;
515 p_msg_item->i_line = i_line;
516 p_msg_item->date = mdate();
518 #ifdef INTF_MSG_QUEUE /*......................................... queue mode */
519 vlc_mutex_unlock( &p_msg->lock ); /* give lock back */
520 #else /*....................................................... instant mode */
521 PrintMsg( p_msg_item ); /* print message */
522 free( psz_str ); /* free message data */
523 #endif /*....................................................................*/
527 /*****************************************************************************
528 * FlushLockedMsg (ok ?)
529 *****************************************************************************
530 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
531 * this function does not check the lock. This function is only defined if
532 * INTF_MSG_QUEUE is defined.
533 *****************************************************************************/
534 #ifdef INTF_MSG_QUEUE
535 static void FlushLockedMsg ( intf_msg_t *p_msg )
539 for( i_index = 0; i_index < p_msg->i_count; i_index++ )
541 /* Print message and free message data */
542 PrintMsg( &p_msg->msg[i_index] );
543 free( p_msg->msg[i_index].psz_msg );
550 /*****************************************************************************
551 * PrintMsg: print a message (ok ?)
552 *****************************************************************************
553 * Print a single message. The message data is not freed. This function exists
554 * in two version. The TRACE version prints a date with each message, and is
555 * able to log messages (if TRACE_LOG is defined).
556 * The normal one just prints messages to the screen.
557 *****************************************************************************/
560 static void PrintMsg( intf_msg_item_t *p_msg )
562 char psz_date[MSTRTIME_MAX_SIZE]; /* formatted time buffer */
563 int i_msg_len = MSTRTIME_MAX_SIZE + strlen(p_msg->psz_msg) + 200;
564 char *psz_msg; /* message buffer */
566 psz_msg = malloc( sizeof( char ) * i_msg_len );
568 /* Check if allocation succeeded */
569 if( psz_msg == NULL )
571 fprintf( stderr, "error: not enough memory for message %s\n",
576 /* Format message - the message is formatted here because in case the log
577 * file is used, it avoids another format string parsing */
578 switch( p_msg->i_type )
580 case INTF_MSG_STD: /* regular messages */
582 snprintf( psz_msg, i_msg_len, "%s", p_msg->psz_msg );
585 case INTF_MSG_WARN: /* Warning message */
586 mstrtime( psz_date, p_msg->date );
587 snprintf( psz_msg, i_msg_len, "(%s) %s",
588 psz_date, p_msg->psz_msg );
592 case INTF_MSG_DBG: /* debug messages */
593 mstrtime( psz_date, p_msg->date );
594 snprintf( psz_msg, i_msg_len, "(%s) " INTF_MSG_DBG_FORMAT "%s",
595 psz_date, p_msg->psz_file, p_msg->psz_function, p_msg->i_line,
603 switch( p_msg->i_type )
605 case INTF_MSG_STD: /* standard messages */
606 fprintf( stdout, "%s\n", psz_msg );
608 case INTF_MSG_ERR: /* error messages */
610 #ifndef TRACE_LOG_ONLY
611 case INTF_MSG_DBG: /* debugging messages */
613 fprintf( stderr, "%s\n", psz_msg );
618 /* Append all messages to log file */
619 if( p_main->p_msg->p_log_file != NULL )
621 fwrite( psz_msg, strlen( psz_msg ), 1, p_main->p_msg->p_log_file );
622 fwrite( "\n", 1, 1, p_main->p_msg->p_log_file );
626 /* Free the message */
632 static void PrintMsg( intf_msg_item_t *p_msg )
635 * Print messages on screen
637 switch( p_msg->i_type )
639 case INTF_MSG_STD: /* standard messages */
640 case INTF_MSG_DBG: /* debug messages */
641 fprintf( stdout, "%s\n", p_msg->psz_msg );
643 case INTF_MSG_ERR: /* error messages */
645 fprintf( stderr, "%s\n", p_msg->psz_msg ); /* warning message */