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: