Browse Source

add log func

master
Alexandros Theodotou 8 months ago
parent
commit
1b6d579a5b
Signed by: alex GPG Key ID: 022EAE42313D70F3
  1. 12
      inc/ad_plugin.h
  2. 41
      inc/audec/audec.h
  3. 2
      src/ad_ffmpeg.c
  4. 4
      src/ad_minimp3.c
  5. 58
      src/ad_plugin.c
  6. 4
      src/ad_soundfile.c
  7. 2
      src/main.c
  8. 24
      tests/test.c

12
inc/ad_plugin.h

@ -42,7 +42,7 @@
/** Prints a debug message. */
#define dbg(_level, _fmt, ...) \
ad_debug_printf (__func__, _level, _fmt, ##__VA_ARGS__)
audec_log (__func__, _level, _fmt, ##__VA_ARGS__)
#define MIN(A,B) ((A) < (B) ? (A) : (B))
#define MAX(A,B) ((A) > (B) ? (A) : (B))
@ -62,13 +62,13 @@
# define PRIi64 __PRI64_PREFIX "i"
#endif
extern int ad_debug_level;
extern int audec_log_level;
void
ad_debug_printf (
const char * func,
AudecDebugLevel level,
const char * format,
audec_log (
const char * func,
AudecLogLevel level,
const char * format,
...);
typedef struct ad_plugin

41
inc/audec/audec.h

@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2019-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of libaudec
*
@ -70,14 +70,22 @@ typedef struct AudecInfo
AudecHandle * handle;
} AudecInfo;
typedef enum AudecDebugLevel
typedef enum AudecLogLevel
{
AUDEC_DEBUG_LEVEL_SILENT = -1,
AUDEC_DEBUG_LEVEL_ERROR,
AUDEC_DEBUG_LEVEL_INFO,
AUDEC_DEBUG_LEVEL_DEBUG,
AUDEC_DEBUG_LEVEL_TRACE,
} AudecDebugLevel;
AUDEC_LOG_LEVEL_SILENT = -1,
AUDEC_LOG_LEVEL_ERROR,
AUDEC_LOG_LEVEL_INFO,
AUDEC_LOG_LEVEL_DEBUG,
AUDEC_LOG_LEVEL_TRACE,
} AudecLogLevel;
/**
* Logging function prototype.
*/
typedef void (*audec_log_fn_t)(
AudecLogLevel level,
const char * fmt,
va_list args);
/**
* Global init function - register codecs.
@ -215,9 +223,20 @@ audec_read_mono_dbl (
*/
void
audec_dump_info (
AudecDebugLevel dbglvl,
AudecLogLevel dbglvl,
AudecInfo * nfo);
/**
* Specify a log callback for audec to write
* log entries to.
*
* If this is not set, libaudec will write
* warnings and errors to stderr.
*/
void
audec_set_log_func (
audec_log_fn_t log_fn);
/**
* Set audio-decoder debug level.
*
@ -226,8 +245,8 @@ audec_dump_info (
* @param lvl debug-level threshold.
*/
void
audec_set_debug_level (
AudecDebugLevel lvl);
audec_set_log_level (
AudecLogLevel lvl);
#ifdef __cplusplus
} /* extern "C" */

2
src/ad_ffmpeg.c

@ -377,7 +377,7 @@ const ad_plugin * adp_get_ffmpeg() {
#endif
av_register_all();
avcodec_register_all();
if(ad_debug_level <= 1)
if(audec_log_level <= 1)
av_log_set_level(AV_LOG_QUIET);
else
av_log_set_level(AV_LOG_VERBOSE);

4
src/ad_minimp3.c

@ -124,13 +124,13 @@ ad_open_minimp3 (
if (res)
{
dbg (
AUDEC_DEBUG_LEVEL_ERROR,
AUDEC_LOG_LEVEL_ERROR,
"unable to open file '%s'.", filename);
char err_str[600];
err_to_string (res, err_str);
puts (err_str);
dbg (
AUDEC_DEBUG_LEVEL_ERROR, "error=%i", res);
AUDEC_LOG_LEVEL_ERROR, "error=%i", res);
free (priv);
return NULL;
}

58
src/ad_plugin.c

@ -49,8 +49,10 @@
#include "ad_plugin.h"
AudecDebugLevel ad_debug_level =
AUDEC_DEBUG_LEVEL_ERROR;
AudecLogLevel audec_log_level =
AUDEC_LOG_LEVEL_ERROR;
audec_log_fn_t log_fn = NULL;
#define UNUSED(x) (void)(x)
int ad_eval_null(const char *f) { UNUSED(f); return -1; }
@ -67,7 +69,10 @@ typedef struct adecoder
ad_plugin const * plugin;
/** Backend data, such as SF file. */
void * data;
void * data;
/* Log function. */
audec_log_fn_t log_fn;
} adecoder;
/* samplecat api */
@ -121,7 +126,7 @@ audec_open (
if (!decoder->plugin)
{
dbg (
AUDEC_DEBUG_LEVEL_ERROR,
AUDEC_LOG_LEVEL_ERROR,
"fatal: no decoder backend available");
free(decoder);
return NULL;
@ -196,7 +201,7 @@ get_buf_size_for_sample_rate (
if (src_is_valid_ratio (resample_ratio) == 0)
{
dbg (
AUDEC_DEBUG_LEVEL_ERROR,
AUDEC_LOG_LEVEL_ERROR,
"Sample rate change out of valid "
"range.");
return -1;
@ -238,7 +243,7 @@ audec_read (
if (*out != NULL)
{
dbg (
AUDEC_DEBUG_LEVEL_ERROR,
AUDEC_LOG_LEVEL_ERROR,
"Please set 'out' to NULL before calling "
"audec_read()");
return -1;
@ -260,7 +265,7 @@ audec_read (
if (ret != (ssize_t) in_len)
{
dbg (
AUDEC_DEBUG_LEVEL_DEBUG,
AUDEC_LOG_LEVEL_DEBUG,
"Number of read in frames %zu not equal to "
"given buf size %zd",
ret, in_len);
@ -269,7 +274,7 @@ audec_read (
if (ret > (ssize_t) in_len)
{
dbg (
AUDEC_DEBUG_LEVEL_ERROR,
AUDEC_LOG_LEVEL_ERROR,
"Number of read in frames %zu greater than "
"given buf size %zd",
ret, in_len);
@ -304,7 +309,7 @@ audec_read (
if (!state)
{
dbg (
AUDEC_DEBUG_LEVEL_ERROR,
AUDEC_LOG_LEVEL_ERROR,
"Failed to create a src callback: "
"%s", src_strerror (err));
free (in);
@ -343,7 +348,7 @@ audec_read (
if (err_ret)
{
dbg (
AUDEC_DEBUG_LEVEL_ERROR,
AUDEC_LOG_LEVEL_ERROR,
"An error occurred during "
"resampling: %s",
src_strerror (err_ret));
@ -366,7 +371,7 @@ audec_read (
if (total_read != num_out_frames)
{
dbg (
AUDEC_DEBUG_LEVEL_INFO,
AUDEC_LOG_LEVEL_INFO,
"Total frames read (%zu) and out "
"frames expected (%zu) do not match",
total_read, num_out_frames);
@ -374,7 +379,7 @@ audec_read (
if (frames_read == -1)
{
dbg (
AUDEC_DEBUG_LEVEL_ERROR,
AUDEC_LOG_LEVEL_ERROR,
"An error has occurred in "
"resampling: frames read == -1");
free (*out);
@ -384,7 +389,7 @@ audec_read (
ret = total_read;
dbg (
AUDEC_DEBUG_LEVEL_INFO,
AUDEC_LOG_LEVEL_INFO,
"%zu frames read after resampling "
"(out buffer size %zu)",
total_read, out_buf_size);
@ -396,7 +401,7 @@ audec_read (
ret = nfo.frames;
dbg (
AUDEC_DEBUG_LEVEL_INFO,
AUDEC_LOG_LEVEL_INFO,
"No resampling done, returning %" PRIi64 " frames "
"(out buffer size %zu)",
nfo.frames, ret);
@ -461,7 +466,7 @@ audec_free_nfo (
void
audec_dump_info (
AudecDebugLevel dbglvl,
AudecLogLevel dbglvl,
AudecInfo * nfo)
{
dbg(dbglvl, "sample_rate: %u", nfo->sample_rate);
@ -475,16 +480,20 @@ audec_dump_info (
}
void
ad_debug_printf (
audec_log (
const char * func,
AudecDebugLevel level,
AudecLogLevel level,
const char * format,
...)
{
va_list args;
va_start (args, format);
if (level <= ad_debug_level)
if (log_fn)
{
log_fn (level, format, args);
}
else if (level <= audec_log_level)
{
fprintf (stderr, "%s(): ", func);
vfprintf (stderr, format, args);
@ -494,8 +503,15 @@ ad_debug_printf (
}
void
audec_set_debug_level (
AudecDebugLevel lvl)
audec_set_log_func (
audec_log_fn_t _log_fn)
{
log_fn = _log_fn;
}
void
audec_set_log_level (
AudecLogLevel lvl)
{
ad_debug_level = lvl;
audec_log_level = lvl;
}

4
src/ad_soundfile.c

@ -92,12 +92,12 @@ static void * ad_open_sndfile (
if (!(priv->sffile))
{
dbg (
AUDEC_DEBUG_LEVEL_ERROR,
AUDEC_LOG_LEVEL_ERROR,
"unable to open file '%s'.", filename);
puts (sf_strerror (NULL));
int e = sf_error (NULL);
dbg (
AUDEC_DEBUG_LEVEL_ERROR, "error=%i", e);
AUDEC_LOG_LEVEL_ERROR, "error=%i", e);
free (priv);
return NULL;
}

2
src/main.c

@ -55,7 +55,7 @@ main (int argc,
AudecInfo nfo;
AudecHandle * handle =
audec_open (arg2, &nfo);
audec_dump_info (AUDEC_DEBUG_LEVEL_ERROR, &nfo);
audec_dump_info (AUDEC_LOG_LEVEL_ERROR, &nfo);
int ret = audec_close (handle);
if (ret)
{

24
tests/test.c

@ -21,6 +21,23 @@
#include <audec/audec.h>
void
audec_log (
const char * func,
AudecLogLevel level,
const char * format,
...);
static void
log_fn (
AudecLogLevel level,
const char * fmt,
va_list args)
{
fprintf (stderr, "using log fn\n");
vfprintf (stderr, fmt, args);
}
int main (
int argc, const char* argv[])
{
@ -85,6 +102,13 @@ int main (
out_frames[(size_t) samples_read * nfo.channels - 1];
(void) frame;
/* test log function */
audec_set_log_func (log_fn);
audec_log (
__func__, AUDEC_LOG_LEVEL_DEBUG,
"abc %s %s\n", "abc2", "abc3");
audec_close (handle);
free (out_frames);

Loading…
Cancel
Save