Integrate libav into logging framework

pull/65/head
Andri Yngvason 2022-07-09 17:15:35 +00:00
parent 2f4f1a2caf
commit 362918a8cf
4 changed files with 117 additions and 1 deletions

19
include/logging.h 100644
View File

@ -0,0 +1,19 @@
#pragma once
/*
* Copyright (c) 2022 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
void nvnc__log_init(void);

View File

@ -115,6 +115,10 @@ if gbm.found()
config.set('HAVE_GBM', true) config.set('HAVE_GBM', true)
endif endif
if libavutil.found()
config.set('HAVE_LIBAVUTIL', true)
endif
if gbm.found() and libdrm.found() and libavcodec.found() and libavfilter.found() and libavutil.found() if gbm.found() and libdrm.found() and libavcodec.found() and libavfilter.found() and libavutil.found()
sources += [ 'src/h264-encoder.c', 'src/open-h264.c' ] sources += [ 'src/h264-encoder.c', 'src/open-h264.c' ]
dependencies += [libdrm, libavcodec, libavfilter, libavutil] dependencies += [libdrm, libavcodec, libavfilter, libavutil]

View File

@ -16,10 +16,19 @@
#include "neatvnc.h" #include "neatvnc.h"
#include "common.h" #include "common.h"
#include "logging.h"
#include "config.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_LIBAVUTIL
#include <libavutil/avutil.h>
#endif
#define EXPORT __attribute__((visibility("default"))) #define EXPORT __attribute__((visibility("default")))
@ -34,6 +43,28 @@ static enum nvnc_log_level log_level = NVNC_LOG_DEBUG;
static enum nvnc_log_level log_level = NVNC_LOG_WARNING; static enum nvnc_log_level log_level = NVNC_LOG_WARNING;
#endif #endif
static bool is_initialised = false;
static char* trim_left(char* str)
{
while (isspace(*str))
++str;
return str;
}
static char* trim_right(char* str)
{
char* end = str + strlen(str) - 1;
while (str < end && isspace(*end))
*end-- = '\0';
return str;
}
static inline char* trim(char* str)
{
return trim_right(trim_left(str));
}
static const char* log_level_to_string(enum nvnc_log_level level) static const char* log_level_to_string(enum nvnc_log_level level)
{ {
switch (level) { switch (level) {
@ -69,7 +100,7 @@ static void nvnc__vlog(const struct nvnc_log_data* meta, const char* fmt,
if (meta->level <= log_level) { if (meta->level <= log_level) {
vsnprintf(message, sizeof(message), fmt, args); vsnprintf(message, sizeof(message), fmt, args);
log_fn(meta, message); log_fn(meta, trim(message));
} }
if (meta->level == NVNC_LOG_PANIC) if (meta->level == NVNC_LOG_PANIC)
@ -91,10 +122,57 @@ static void default_logger(const struct nvnc_log_data* meta,
fflush(stream); fflush(stream);
} }
#ifdef HAVE_LIBAVUTIL
static enum nvnc_log_level nvnc__log_level_from_av(int level)
{
switch (level) {
case AV_LOG_PANIC: return NVNC_LOG_PANIC;
case AV_LOG_FATAL: return NVNC_LOG_ERROR;
case AV_LOG_ERROR: return NVNC_LOG_ERROR;
case AV_LOG_WARNING: return NVNC_LOG_WARNING;
case AV_LOG_INFO: return NVNC_LOG_INFO;
case AV_LOG_VERBOSE: return NVNC_LOG_INFO;
case AV_LOG_DEBUG: return NVNC_LOG_DEBUG;
case AV_LOG_TRACE: return NVNC_LOG_TRACE;
}
return NVNC_LOG_TRACE;
}
static int nvnc__log_level_to_av(enum nvnc_log_level level)
{
switch (level) {
case NVNC_LOG_PANIC: return AV_LOG_PANIC;
case NVNC_LOG_ERROR: return AV_LOG_ERROR;
case NVNC_LOG_WARNING: return AV_LOG_WARNING;
case NVNC_LOG_INFO: return AV_LOG_INFO;
case NVNC_LOG_DEBUG: return AV_LOG_DEBUG;
case NVNC_LOG_TRACE: return AV_LOG_TRACE;
}
return AV_LOG_TRACE;
}
static void nvnc__av_log_callback(void* ptr, int level, const char* fmt,
va_list va)
{
struct nvnc_log_data meta = {
.level = nvnc__log_level_from_av(level),
.file = "libav",
.line = 0,
};
nvnc__vlog(&meta, fmt, va);
}
#endif
EXPORT EXPORT
void nvnc_set_log_level(enum nvnc_log_level level) void nvnc_set_log_level(enum nvnc_log_level level)
{ {
log_level = level; log_level = level;
#ifdef HAVE_LIBAVUTIL
av_log_set_level(nvnc__log_level_to_av(level));
#endif
} }
EXPORT EXPORT
@ -112,3 +190,15 @@ void nvnc__log(const struct nvnc_log_data* meta,
nvnc__vlog(meta, fmt, ap); nvnc__vlog(meta, fmt, ap);
va_end(ap); va_end(ap);
} }
void nvnc__log_init(void)
{
if (is_initialised)
return;
#ifdef HAVE_LIBAVUTIL
av_log_set_callback(nvnc__av_log_callback);
#endif
is_initialised = true;
}

View File

@ -29,6 +29,7 @@
#include "tight.h" #include "tight.h"
#include "enc-util.h" #include "enc-util.h"
#include "cursor.h" #include "cursor.h"
#include "logging.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -1234,6 +1235,8 @@ static int bind_address(const char* name, uint16_t port, enum addrtype type)
static struct nvnc* open_common(const char* address, uint16_t port, enum addrtype type) static struct nvnc* open_common(const char* address, uint16_t port, enum addrtype type)
{ {
nvnc__log_init();
aml_require_workers(aml_get_default(), -1); aml_require_workers(aml_get_default(), -1);
struct nvnc* self = calloc(1, sizeof(*self)); struct nvnc* self = calloc(1, sizeof(*self));