1 /*****************************************************************************
2 * log.c: libvlc new API log functions
3 *****************************************************************************
4 * Copyright (C) 2005 VLC authors and VideoLAN
8 * Authors: Damien Fouilleul <damienf@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
30 #include <vlc/libvlc.h>
31 #include "libvlc_internal.h"
32 #include <vlc_common.h>
33 #include <vlc_interface.h>
35 /*** Logging core dispatcher ***/
37 static vlc_rwlock_t log_lock
= VLC_STATIC_RWLOCK
;
38 static libvlc_log_subscriber_t
*log_first
= NULL
;
39 static msg_subscription_t sub
;
42 static void libvlc_log (int level
, const char *fmt
, ...)
44 libvlc_log_subscriber_t
*sub
;
49 case VLC_MSG_INFO
: level
= LIBVLC_NOTICE
; break;
50 case VLC_MSG_ERR
: level
= LIBVLC_ERROR
; break;
51 case VLC_MSG_WARN
: level
= LIBVLC_WARNING
; break;
52 case VLC_MSG_DBG
: level
= LIBVLC_DEBUG
; break;
56 vlc_rwlock_rdlock (&log_lock
);
57 for (sub
= log_first
; sub
!= NULL
; sub
= sub
->next
)
58 sub
->func (sub
->opaque
, level
, fmt
, ap
);
59 vlc_rwlock_unlock (&log_lock
);
63 static void libvlc_logf (void *dummy
, int level
, const msg_item_t
*item
,
64 const char *fmt
, va_list ap
)
68 if (unlikely(vasprintf (&msg
, fmt
, ap
) == -1))
70 if (item
->psz_header
!= NULL
)
71 libvlc_log (level
, "[%p] [%s]: %s %s %s", (void *)item
->i_object_id
,
72 item
->psz_header
, item
->psz_module
, item
->psz_object_type
,
73 msg
? msg
: "Not enough memory");
75 libvlc_log (level
, "[%p]: %s %s %s", (void *)item
->i_object_id
,
76 item
->psz_module
, item
->psz_object_type
,
77 msg
? msg
: "Not enough memory");
82 void libvlc_log_init (void)
84 vlc_Subscribe (&sub
, libvlc_logf
, NULL
);
87 void libvlc_log_deinit (void)
89 vlc_Unsubscribe (&sub
);
92 void libvlc_log_subscribe (libvlc_log_subscriber_t
*sub
,
93 libvlc_log_cb cb
, void *data
)
98 vlc_rwlock_wrlock (&log_lock
);
99 sub
->next
= log_first
;
101 vlc_rwlock_unlock (&log_lock
);
104 void libvlc_log_unsubscribe( libvlc_log_subscriber_t
*sub
)
106 vlc_rwlock_wrlock (&log_lock
);
107 if (sub
->next
!= NULL
)
108 sub
->next
->prev
= sub
->prev
;
109 if (sub
->prev
!= NULL
)
110 sub
->prev
->next
= sub
->next
;
112 log_first
= sub
->next
;
113 vlc_rwlock_unlock (&log_lock
);
116 /*** Helpers for logging to files ***/
117 static void libvlc_log_file (void *data
, int level
, const char *fmt
,
123 vfprintf (stream
, fmt
, ap
);
124 fputc ('\n', stream
);
125 funlockfile (stream
);
129 void libvlc_log_subscribe_file (libvlc_log_subscriber_t
*sub
, FILE *stream
)
131 libvlc_log_subscribe (sub
, libvlc_log_file
, stream
);
134 /*** Stubs for the old interface ***/
135 unsigned libvlc_get_log_verbosity( const libvlc_instance_t
*p_instance
)
141 void libvlc_set_log_verbosity( libvlc_instance_t
*p_instance
, unsigned level
)
147 libvlc_log_t
*libvlc_log_open( libvlc_instance_t
*p_instance
)
153 void libvlc_log_close( libvlc_log_t
*p_log
)
158 unsigned libvlc_log_count( const libvlc_log_t
*p_log
)
164 void libvlc_log_clear( libvlc_log_t
*p_log
)
169 libvlc_log_iterator_t
*libvlc_log_get_iterator( const libvlc_log_t
*p_log
)
171 return (p_log
!= NULL
) ? malloc(1) : NULL
;
174 void libvlc_log_iterator_free( libvlc_log_iterator_t
*p_iter
)
179 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t
*p_iter
)
185 libvlc_log_message_t
*libvlc_log_iterator_next( libvlc_log_iterator_t
*p_iter
,
186 libvlc_log_message_t
*buffer
)
188 (void) p_iter
; (void) buffer
;