Add logger

Add logger interface
Make it possible to pass extra interfaces to the element at init time,
such as a logger.
This commit is contained in:
Wim Taymans 2016-10-05 17:43:11 +02:00
parent e90c53e48d
commit 49dae17dfb
26 changed files with 251 additions and 47 deletions

150
spa/include/spa/log.h Normal file
View file

@ -0,0 +1,150 @@
/* Simple Plugin API
* Copyright (C) 2016 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __SPA_LOG_H__
#define __SPA_LOG_H__
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _SpaLog SpaLog;
#include <stdarg.h>
#include <spa/defs.h>
#include <spa/plugin.h>
#define SPA_INTERFACE_ID_LOG 3
#define SPA_INTERFACE_ID_LOG_NAME "Log interface"
#define SPA_INTERFACE_ID_LOG_DESCRIPTION "Log interface"
typedef enum
{
SPA_LOG_LEVEL_NONE = 0,
SPA_LOG_LEVEL_ERROR,
SPA_LOG_LEVEL_WARN,
SPA_LOG_LEVEL_INFO,
SPA_LOG_LEVEL_DEBUG,
SPA_LOG_LEVEL_TRACE,
} SpaLogLevel;
/**
* SpaLog:
*
* The Log interface
*/
struct _SpaLog {
/* pointer to the handle owning this interface */
SpaHandle *handle;
/* the total size of this log. This can be used to expand this
* structure in the future */
size_t size;
/**
* SpaLog::info
*
* Extra information about the log
*/
const SpaDict *info;
/**
* SpaLog::level
*
* Logging level, everything above this level is not logged
*/
SpaLogLevel level;
/**
* SpaLog::log
* @log: a #SpaLog
* @level: a #SpaLogLevel
* @file: the file name
* @line: the line number
* @func: the function name
* @fmt: printf style format
* @...: format arguments
*
* Log a message with the given log level.
*/
void (*log) (SpaLog *log,
SpaLogLevel level,
const char *file,
int line,
const char *func,
const char *fmt, ...);
/**
* SpaLog::logv
* @log: a #SpaLog
* @level: a #SpaLogLevel
* @file: the file name
* @line: the line number
* @func: the function name
* @fmt: printf style format
* @args: format arguments
*
* Log a message with the given log level.
*/
void (*logv) (SpaLog *log,
SpaLogLevel level,
const char *file,
int line,
const char *func,
const char *fmt,
va_list args);
};
#if __STDC_VERSION__ >= 199901L
#define spa_log_log(l,lev,...) \
if ((l) && (l)->level > lev) \
(l)->log((l),lev,__VA_ARGS__)
#define spa_log_error(l,...) spa_log_log(l,SPA_LOG_LEVEL_ERROR,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define spa_log_warn(l,...) spa_log_log(l,SPA_LOG_LEVEL_WARN,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define spa_log_info(l,...) spa_log_log(l,SPA_LOG_LEVEL_INFO,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define spa_log_debug(l,...) spa_log_log(l,SPA_LOG_LEVEL_DEBUG,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define spa_log_trace(l,...) spa_log_log(l,SPA_LOG_LEVEL_TRACE,__FILE__,__LINE__,__func__,__VA_ARGS__)
#else
#define SPA_LOG_FUNC(name,lev) \
static inline void spa_log_##name (SpaLog *l, const char *format, ...) \
{ \
if ((l) && (l)->level > lev) { \
va_list varargs; \
va_start (varargs, format); \
(l)->logv((l),lev,__FILE__,__LINE__,__func__,format,varargs); \
va_end (varargs); \
} \
}
SPA_LOG_FUNC(error, SPA_LOG_LEVEL_ERROR)
SPA_LOG_FUNC(warn, SPA_LOG_LEVEL_WARN)
SPA_LOG_FUNC(info, SPA_LOG_LEVEL_INFO)
SPA_LOG_FUNC(debug, SPA_LOG_LEVEL_DEBUG)
SPA_LOG_FUNC(trace, SPA_LOG_LEVEL_TRACE)
#endif
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __SPA_LOG_H__ */

View file

@ -6,6 +6,7 @@ spa_headers = [
'defs.h',
'dict.h',
'format.h',
'log.h',
'monitor.h',
'node-command.h',
'node-event.h',

View file

@ -78,6 +78,19 @@ typedef struct {
const char *description;
} SpaInterfaceInfo;
/**
* SpaInterface:
* @interface_id: the id of the interface
* @interface: an interface instance
*
* An interface instance that is usually passed to the init() function of
* a factory to provide extra API such as logging.
*/
typedef struct {
uint32_t interface_id;
void *interface;
} SpaInterface;
struct _SpaHandleFactory {
/**
* SpaHandleFactory::name
@ -104,17 +117,24 @@ struct _SpaHandleFactory {
* @handle: a pointer to memory
* @info: extra handle specific information, usually obtained
* from a #SpaMonitor. This can be used to configure the handle.
* @platform: platform interfaces
* @n_platform: number of elements in @platform
*
* Initialize an instance of this factory. The caller should allocate
* memory at least SpaHandleFactory::size bytes and pass this as @handle.
*
* @platform can optionally contain extra interfaces that the plugin can
* use such as a logger.
*
* Returns: #SPA_RESULT_OK on success
* #SPA_RESULT_NOT_IMPLEMENTED when an instance can't be made
* #SPA_RESULT_INVALID_ARGUMENTS when factory or handle are %NULL
*/
SpaResult (*init) (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info);
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform);
/**
* SpaHandle::enum_interface_info:

View file

@ -379,7 +379,9 @@ alsa_monitor_clear (SpaHandle *handle)
static SpaResult
alsa_monitor_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaALSAMonitor *this;

View file

@ -598,7 +598,9 @@ alsa_sink_clear (SpaHandle *handle)
static SpaResult
alsa_sink_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaALSASink *this;

View file

@ -751,7 +751,9 @@ alsa_source_clear (SpaHandle *handle)
static SpaResult
alsa_source_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaALSASource *this;
unsigned int i;

View file

@ -779,7 +779,9 @@ spa_audiomixer_clear (SpaHandle *handle)
static SpaResult
spa_audiomixer_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaAudioMixer *this;

View file

@ -988,7 +988,9 @@ audiotestsrc_clear (SpaHandle *handle)
static SpaResult
audiotestsrc_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaAudioTestSrc *this;

View file

@ -31,7 +31,9 @@ SpaResult spa_ffmpeg_enc_init (SpaHandle *handle);
static SpaResult
ffmpeg_dec_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
if (factory == NULL || handle == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
@ -42,7 +44,9 @@ ffmpeg_dec_init (const SpaHandleFactory *factory,
static SpaResult
ffmpeg_enc_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
if (factory == NULL || handle == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;

View file

@ -1347,7 +1347,9 @@ proxy_clear (SpaHandle *handle)
static SpaResult
proxy_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaProxy *this;

View file

@ -350,7 +350,9 @@ v4l2_monitor_clear (SpaHandle *handle)
static SpaResult
v4l2_monitor_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaV4l2Monitor *this;

View file

@ -28,6 +28,7 @@
#include <spa/video/format.h>
#include <spa/debug.h>
#include <spa/queue.h>
#include <spa/log.h>
typedef struct _SpaV4l2Source SpaV4l2Source;
@ -111,6 +112,8 @@ typedef struct {
SpaAllocParamMetaEnable param_meta;
SpaPortStatus status;
SpaLog *log;
int64_t last_ticks;
int64_t last_monotonic;
} SpaV4l2State;
@ -827,7 +830,9 @@ v4l2_source_clear (SpaHandle *handle)
static SpaResult
v4l2_source_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaV4l2Source *this;
unsigned int i;
@ -849,6 +854,7 @@ v4l2_source_init (const SpaHandleFactory *factory,
SPA_QUEUE_INIT (&this->state[0].ready);
this->state[0].log = NULL;
this->state[0].info.flags = SPA_PORT_INFO_FLAG_LIVE;
this->state[0].status.flags = SPA_PORT_STATUS_FLAG_NONE;

View file

@ -32,27 +32,27 @@ spa_v4l2_open (SpaV4l2Source *this)
return 0;
if (props->props.unset_mask & 1) {
fprintf(stderr, "v4l2: Device property not set\n");
spa_log_error (state->log, "v4l2: Device property not set\n");
return -1;
}
fprintf (stderr, "v4l2: Playback device is '%s'\n", props->device);
spa_log_info (state->log, "v4l2: Playback device is '%s'\n", props->device);
if (stat (props->device, &st) < 0) {
fprintf(stderr, "v4l2: Cannot identify '%s': %d, %s\n",
spa_log_error (state->log, "v4l2: Cannot identify '%s': %d, %s\n",
props->device, errno, strerror (errno));
return -1;
}
if (!S_ISCHR (st.st_mode)) {
fprintf(stderr, "v4l2: %s is no device\n", props->device);
spa_log_error (state->log, "v4l2: %s is no device\n", props->device);
return -1;
}
state->fd = open (props->device, O_RDWR | O_NONBLOCK, 0);
if (state->fd == -1) {
fprintf (stderr, "v4l2: Cannot open '%s': %d, %s\n",
spa_log_error (state->log, "v4l2: Cannot open '%s': %d, %s\n",
props->device, errno, strerror (errno));
return -1;
}
@ -63,7 +63,7 @@ spa_v4l2_open (SpaV4l2Source *this)
}
if ((state->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
fprintf (stderr, "v4l2: %s is no video capture device\n", props->device);
spa_log_error (state->log, "v4l2: %s is no video capture device\n", props->device);
return -1;
}
state->opened = true;
@ -103,7 +103,7 @@ spa_v4l2_clear_buffers (SpaV4l2Source *this)
b = &state->buffers[i];
if (b->outstanding) {
fprintf (stderr, "v4l2: queueing outstanding buffer %p\n", b);
spa_log_info (state->log, "v4l2: queueing outstanding buffer %p\n", b);
spa_v4l2_buffer_recycle (this, i);
}
if (b->allocated) {
@ -139,7 +139,7 @@ spa_v4l2_close (SpaV4l2Source *this)
if (state->n_buffers > 0)
return 0;
fprintf (stderr, "v4l2: close\n");
spa_log_info (state->log, "v4l2: close\n");
if (close(state->fd))
perror ("close");
@ -749,7 +749,7 @@ spa_v4l2_set_format (SpaV4l2Source *this, V4l2Format *f, bool try_only)
f->format,
0);
if (info == NULL) {
fprintf (stderr, "v4l2: unknown media type %d %d %d\n", f->fmt.media_type,
spa_log_error (state->log, "v4l2: unknown media type %d %d %d\n", f->fmt.media_type,
f->fmt.media_subtype, f->format);
return -1;
}
@ -761,7 +761,7 @@ spa_v4l2_set_format (SpaV4l2Source *this, V4l2Format *f, bool try_only)
streamparm.parm.capture.timeperframe.numerator = f->framerate.denom;
streamparm.parm.capture.timeperframe.denominator = f->framerate.num;
fprintf (stderr, "v4l2: set %08x %dx%d %d/%d\n", fmt.fmt.pix.pixelformat,
spa_log_info (state->log, "v4l2: set %08x %dx%d %d/%d\n", fmt.fmt.pix.pixelformat,
fmt.fmt.pix.width, fmt.fmt.pix.height,
streamparm.parm.capture.timeperframe.numerator,
streamparm.parm.capture.timeperframe.denominator);
@ -781,7 +781,7 @@ spa_v4l2_set_format (SpaV4l2Source *this, V4l2Format *f, bool try_only)
if (xioctl (state->fd, VIDIOC_S_PARM, &streamparm) < 0)
perror ("VIDIOC_S_PARM");
fprintf (stderr, "v4l2: got %08x %dx%d %d/%d\n", fmt.fmt.pix.pixelformat,
spa_log_info (state->log, "v4l2: got %08x %dx%d %d/%d\n", fmt.fmt.pix.pixelformat,
fmt.fmt.pix.width, fmt.fmt.pix.height,
streamparm.parm.capture.timeperframe.numerator,
streamparm.parm.capture.timeperframe.denominator);
@ -882,6 +882,9 @@ v4l2_on_fd_events (SpaPollNotifyData *data)
if (data->fds[0].revents & POLLERR)
return -1;
if (!(data->fds[0].revents & POLLIN))
return 0;
if (mmap_read (this) < 0)
return 0;
@ -912,7 +915,7 @@ spa_v4l2_use_buffers (SpaV4l2Source *this, SpaBuffer **buffers, uint32_t n_buffe
state->memtype = V4L2_MEMORY_DMABUF;
break;
default:
fprintf (stderr, "v4l2: can't use buffers\n");
spa_log_error (state->log, "v4l2: can't use buffers\n");
return SPA_RESULT_ERROR;
}
}
@ -926,9 +929,9 @@ spa_v4l2_use_buffers (SpaV4l2Source *this, SpaBuffer **buffers, uint32_t n_buffe
perror ("VIDIOC_REQBUFS");
return SPA_RESULT_ERROR;
}
fprintf (stderr, "v4l2: got %d buffers\n", reqbuf.count);
spa_log_info (state->log, "v4l2: got %d buffers\n", reqbuf.count);
if (reqbuf.count < 2) {
fprintf (stderr, "v4l2: can't allocate enough buffers\n");
spa_log_error (state->log, "v4l2: can't allocate enough buffers\n");
return SPA_RESULT_ERROR;
}
@ -941,10 +944,10 @@ spa_v4l2_use_buffers (SpaV4l2Source *this, SpaBuffer **buffers, uint32_t n_buffe
b->allocated = false;
b->h = spa_buffer_find_meta (b->outbuf, SPA_META_TYPE_HEADER);
fprintf (stderr, "v4l2: import buffer %p\n", buffers[i]);
spa_log_info (state->log, "v4l2: import buffer %p\n", buffers[i]);
if (buffers[i]->n_datas < 1) {
fprintf (stderr, "v4l2: invalid memory on buffer %p\n", buffers[i]);
spa_log_error (state->log, "v4l2: invalid memory on buffer %p\n", buffers[i]);
continue;
}
d = buffers[i]->datas;
@ -957,7 +960,7 @@ spa_v4l2_use_buffers (SpaV4l2Source *this, SpaBuffer **buffers, uint32_t n_buffe
case SPA_DATA_TYPE_MEMPTR:
case SPA_DATA_TYPE_MEMFD:
if (d[0].data == NULL) {
fprintf (stderr, "v4l2: need mmaped memory\n");
spa_log_error (state->log, "v4l2: need mmaped memory\n");
continue;
}
b->v4l2_buffer.m.userptr = (unsigned long) SPA_MEMBER (d[0].data, d[0].offset, void *);
@ -999,22 +1002,22 @@ mmap_init (SpaV4l2Source *this,
return SPA_RESULT_ERROR;
}
fprintf (stderr, "v4l2: got %d buffers\n", reqbuf.count);
spa_log_info (state->log, "v4l2: got %d buffers\n", reqbuf.count);
*n_buffers = reqbuf.count;
if (reqbuf.count < 2) {
fprintf (stderr, "v4l2: can't allocate enough buffers\n");
spa_log_error (state->log, "v4l2: can't allocate enough buffers\n");
return SPA_RESULT_ERROR;
}
if (state->export_buf)
fprintf (stderr, "v4l2: using EXPBUF\n");
spa_log_info (state->log, "v4l2: using EXPBUF\n");
for (i = 0; i < reqbuf.count; i++) {
V4l2Buffer *b;
SpaData *d;
if (buffers[i]->n_datas < 1) {
fprintf (stderr, "v4l2: invalid buffer data\n");
spa_log_error (state->log, "v4l2: invalid buffer data\n");
return SPA_RESULT_ERROR;
}

View file

@ -936,7 +936,9 @@ videotestsrc_clear (SpaHandle *handle)
static SpaResult
videotestsrc_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaVideoTestSrc *this;

View file

@ -676,7 +676,9 @@ volume_clear (SpaHandle *handle)
static SpaResult
volume_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaVolume *this;

View file

@ -557,7 +557,9 @@ xv_sink_clear (SpaHandle *handle)
static SpaResult
xv_sink_init (const SpaHandleFactory *factory,
SpaHandle *handle,
const SpaDict *info)
const SpaDict *info,
const SpaInterface **platform,
unsigned int n_platform)
{
SpaXvSink *this;

View file

@ -74,7 +74,7 @@ make_node (SpaNode **node, const char *lib, const char *name)
continue;
handle = calloc (1, factory->size);
if ((res = spa_handle_factory_init (factory, handle, NULL)) < 0) {
if ((res = spa_handle_factory_init (factory, handle, NULL, NULL, 0)) < 0) {
printf ("can't make factory instance: %d\n", res);
return res;
}

View file

@ -94,7 +94,7 @@ make_node (SpaNode **node, const char *lib, const char *name)
continue;
handle = calloc (1, factory->size);
if ((res = spa_handle_factory_init (factory, handle, NULL)) < 0) {
if ((res = spa_handle_factory_init (factory, handle, NULL, NULL, 0)) < 0) {
printf ("can't make factory instance: %d\n", res);
return res;
}

View file

@ -101,7 +101,7 @@ inspect_factory (const SpaHandleFactory *factory)
printf (" none\n");
handle = calloc (1, factory->size);
if ((res = spa_handle_factory_init (factory, handle, NULL)) < 0) {
if ((res = spa_handle_factory_init (factory, handle, NULL, NULL, 0)) < 0) {
printf ("can't make factory instance: %d\n", res);
return;
}

View file

@ -211,7 +211,7 @@ main (int argc, char *argv[])
void *interface;
handle = calloc (1, factory->size);
if ((res = spa_handle_factory_init (factory, handle, NULL)) < 0) {
if ((res = spa_handle_factory_init (factory, handle, NULL, NULL, 0)) < 0) {
printf ("can't make factory instance: %d\n", res);
continue;
}