Commit a708ad10 authored by Jean-Paul Saman's avatar Jean-Paul Saman

dvbinfo: fix logging

Logging functions nested use of va_arg(), which was wrong. The solutions
is to simplify the logging function nesting.
parent a24fd472
...@@ -120,50 +120,39 @@ static void usage(void) ...@@ -120,50 +120,39 @@ static void usage(void)
/* Logging */ /* Logging */
static int log_level[] = { LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG }; static int log_level[] = { LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG };
static void log_remote(const int level, const char *format, ...)
{
va_list ap;
char *msg = NULL;
va_start(ap, format);
int err = vasprintf(&msg, format, ap);
va_end(ap);
if (err != 1)
{
syslog(log_level[level], "%s", msg);
free(msg);
}
}
static const char *psz_level[] = { "ERROR", "WARNING", "INFO", "DEBUG" }; static const char *psz_level[] = { "ERROR", "WARNING", "INFO", "DEBUG" };
static void log_local(const int level, const char *format, ...)
static void libdvbpsi_log(void *data, const int level, const char *format, ...)
{ {
va_list ap;
int err = 0; int err = 0;
char *msg = NULL; char *msg = NULL;
va_list ap;
/* Get arguments and construct final message */
va_start(ap, format); va_start(ap, format);
#if defined(_GNU_SOURCE) #if defined(_GNU_SOURCE)
err = vasprintf(&msg, format, ap); err = vasprintf(&msg, format, ap);
#else #else
msg = calloc(1, 1024); msg = calloc(1, strlen(format) + 1024);
if (msg) if (msg)
err = vsnprintf(msg, 1024, format, ap); err = vsnprintf(msg, 1024, format, ap);
#endif #endif
va_end(ap); va_end(ap);
if (err != 1) if (err < 0)
return;
/* Print message */
params_t *param = (params_t *)data;
if (!param)
{ {
fprintf(stderr, "%s: %s", psz_level[level], msg);
free(msg); free(msg);
return;
} }
} if (param->b_monitor)
syslog(log_level[level], "%s", msg);
static void libdvbpsi_log(void *data, const int level, const char *format, ...) else
{ fprintf(stderr, "%s: %s", psz_level[level], msg);
va_list ap; free(msg);
va_start(ap, format);
params_t *param = (params_t *)data;
if (param)
param->pf_log(level, format, ap);
va_end(ap);
} }
/* Parameters */ /* Parameters */
...@@ -178,6 +167,9 @@ static params_t *params_init(void) ...@@ -178,6 +167,9 @@ static params_t *params_init(void)
param->input = NULL; param->input = NULL;
param->output = NULL; param->output = NULL;
param->b_verbose = false;
param->b_monitor = false;
/* statistics */ /* statistics */
param->b_summary = false; param->b_summary = false;
param->summary.mode = SUM_BANDWIDTH; param->summary.mode = SUM_BANDWIDTH;
...@@ -188,7 +180,6 @@ static params_t *params_init(void) ...@@ -188,7 +180,6 @@ static params_t *params_init(void)
/* functions */ /* functions */
param->pf_read = NULL; param->pf_read = NULL;
param->pf_write = NULL; param->pf_write = NULL;
param->pf_log = log_local;
return param; return param;
} }
...@@ -309,7 +300,7 @@ static int dvbinfo_process(dvbinfo_capture_t *capture) ...@@ -309,7 +300,7 @@ static int dvbinfo_process(dvbinfo_capture_t *capture)
{ {
if (asprintf(&psz_temp, "%s.part", param->summary.file) < 0) if (asprintf(&psz_temp, "%s.part", param->summary.file) < 0)
{ {
param->pf_log(DVBINFO_LOG_ERROR, "Could not create temporary summary file %s\n", libdvbpsi_log(param, DVBINFO_LOG_ERROR, "Could not create temporary summary file %s\n",
param->summary.file); param->summary.file);
return err; return err;
} }
...@@ -341,12 +332,14 @@ static int dvbinfo_process(dvbinfo_capture_t *capture) ...@@ -341,12 +332,14 @@ static int dvbinfo_process(dvbinfo_capture_t *capture)
size_t size = param->pf_write(param->fd_out, buffer->p_data, buffer->i_size); size_t size = param->pf_write(param->fd_out, buffer->p_data, buffer->i_size);
if (size < 0) /* error writing */ if (size < 0) /* error writing */
{ {
param->pf_log(DVBINFO_LOG_ERROR, "error (%d) writting to %s", errno, param->output); libdvbpsi_log(param, DVBINFO_LOG_ERROR,
"error (%d) writting to %s", errno, param->output);
break; break;
} }
else if (size < buffer->i_size) /* short writting disk full? */ else if (size < buffer->i_size) /* short writting disk full? */
{ {
param->pf_log(DVBINFO_LOG_ERROR, "error writting to %s (disk full?)", param->output); libdvbpsi_log(param, DVBINFO_LOG_ERROR,
"error writting to %s (disk full?)", param->output);
break; break;
} }
} }
...@@ -370,7 +363,8 @@ static int dvbinfo_process(dvbinfo_capture_t *capture) ...@@ -370,7 +363,8 @@ static int dvbinfo_process(dvbinfo_capture_t *capture)
} }
else else
{ {
param->pf_log(DVBINFO_LOG_ERROR, "failed opening summary file (disabling summary logging)\n"); libdvbpsi_log(param, DVBINFO_LOG_ERROR,
"failed opening summary file (disabling summary logging)\n");
param->b_summary = false; param->b_summary = false;
} }
deadline = mdate() + param->summary.period; deadline = mdate() + param->summary.period;
...@@ -387,7 +381,7 @@ static int dvbinfo_process(dvbinfo_capture_t *capture) ...@@ -387,7 +381,7 @@ static int dvbinfo_process(dvbinfo_capture_t *capture)
out: out:
if (b_error) if (b_error)
param->pf_log(DVBINFO_LOG_ERROR, "error while processing\n" ); libdvbpsi_log(param, DVBINFO_LOG_ERROR, "error while processing\n" );
if (buffer) buffer_free(buffer); if (buffer) buffer_free(buffer);
free(psz_temp); free(psz_temp);
...@@ -401,7 +395,6 @@ int main(int argc, char **pp_argv) ...@@ -401,7 +395,6 @@ int main(int argc, char **pp_argv)
{ {
dvbinfo_capture_t capture; dvbinfo_capture_t capture;
params_t *param = NULL; params_t *param = NULL;
bool b_monitor = false;
char c; char c;
printf("dvbinfo: Copyright (C) 2011-2012 M2X BV\n"); printf("dvbinfo: Copyright (C) 2011-2012 M2X BV\n");
...@@ -491,8 +484,7 @@ int main(int argc, char **pp_argv) ...@@ -491,8 +484,7 @@ int main(int argc, char **pp_argv)
break; break;
case 'm': case 'm':
b_monitor = true; param->b_monitor = true;
param->pf_log = log_remote;
break; break;
case 'o': case 'o':
...@@ -583,27 +575,27 @@ int main(int argc, char **pp_argv) ...@@ -583,27 +575,27 @@ int main(int argc, char **pp_argv)
}; };
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
if (b_monitor) if (param->b_monitor)
{ {
openlog("dvbinfo", LOG_PID, LOG_DAEMON); openlog("dvbinfo", LOG_PID, LOG_DAEMON);
if (daemon(1,0) < 0) if (daemon(1,0) < 0)
{ {
param->pf_log(DVBINFO_LOG_ERROR, "Failed to start in background\n"); libdvbpsi_log(param, DVBINFO_LOG_ERROR, "Failed to start in background\n");
params_free(param); params_free(param);
closelog(); closelog();
usage(); /* exits application */ usage(); /* exits application */
} }
param->pf_log(DVBINFO_LOG_INFO, "dvbinfo: Copyright (C) 2011-2012 M2X BV\n"); libdvbpsi_log(param, DVBINFO_LOG_INFO, "dvbinfo: Copyright (C) 2011-2012 M2X BV\n");
param->pf_log(DVBINFO_LOG_ERROR, "License: LGPL v2.1\n"); libdvbpsi_log(param, DVBINFO_LOG_ERROR, "License: LGPL v2.1\n");
} }
#endif #endif
if (param->input == NULL) if (param->input == NULL)
{ {
param->pf_log(DVBINFO_LOG_ERROR, "No source given\n"); libdvbpsi_log(param, DVBINFO_LOG_ERROR, "No source given\n");
params_free(param); params_free(param);
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
if (b_monitor) if (param->b_monitor)
closelog(); closelog();
#endif #endif
params_free(param); params_free(param);
...@@ -614,13 +606,15 @@ int main(int argc, char **pp_argv) ...@@ -614,13 +606,15 @@ int main(int argc, char **pp_argv)
if (param->b_udp || param->b_tcp) if (param->b_udp || param->b_tcp)
{ {
capture.size = 7*188; capture.size = 7*188;
param->pf_log(DVBINFO_LOG_INFO, "Listen: host=%s port=%d\n", param->input, param->port); libdvbpsi_log(param, DVBINFO_LOG_INFO, "Listen: host=%s port=%d\n",
param->input, param->port);
} }
else else
#endif #endif
{ {
capture.size = 188; capture.size = 188;
param->pf_log(DVBINFO_LOG_INFO, "Examining: %s\n", param->input); libdvbpsi_log(param, DVBINFO_LOG_INFO, "Examining: %s\n",
param->input);
} }
/* Capture thread */ /* Capture thread */
...@@ -629,10 +623,10 @@ int main(int argc, char **pp_argv) ...@@ -629,10 +623,10 @@ int main(int argc, char **pp_argv)
capture.b_alive = true; capture.b_alive = true;
if (pthread_create(&handle, NULL, dvbinfo_capture, (void *)&capture) < 0) if (pthread_create(&handle, NULL, dvbinfo_capture, (void *)&capture) < 0)
{ {
param->pf_log(DVBINFO_LOG_ERROR, "failed creating thread\n"); libdvbpsi_log(param, DVBINFO_LOG_ERROR, "failed creating thread\n");
dvbinfo_close(param); dvbinfo_close(param);
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
if (b_monitor) if (param->b_monitor)
closelog(); closelog();
#endif #endif
params_free(param); params_free(param);
...@@ -641,7 +635,7 @@ int main(int argc, char **pp_argv) ...@@ -641,7 +635,7 @@ int main(int argc, char **pp_argv)
int err = dvbinfo_process(&capture); int err = dvbinfo_process(&capture);
capture.b_alive = false; /* stop thread */ capture.b_alive = false; /* stop thread */
if (pthread_join(handle, NULL) < 0) if (pthread_join(handle, NULL) < 0)
param->pf_log(DVBINFO_LOG_ERROR, "error joining capture thread\n"); libdvbpsi_log(param, DVBINFO_LOG_ERROR, "error joining capture thread\n");
dvbinfo_close(param); dvbinfo_close(param);
/* cleanup */ /* cleanup */
...@@ -654,7 +648,7 @@ int main(int argc, char **pp_argv) ...@@ -654,7 +648,7 @@ int main(int argc, char **pp_argv)
fifo_free((&capture)->empty); fifo_free((&capture)->empty);
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
if (b_monitor) if (param->b_monitor)
closelog(); closelog();
#endif #endif
if (err < 0) if (err < 0)
......
...@@ -47,6 +47,7 @@ typedef struct params_s ...@@ -47,6 +47,7 @@ typedef struct params_s
int debug; int debug;
bool b_verbose; bool b_verbose;
bool b_monitor; /* run in daemon mode */
/* statistics */ /* statistics */
bool b_summary; /* write summary */ bool b_summary; /* write summary */
...@@ -60,9 +61,6 @@ typedef struct params_s ...@@ -60,9 +61,6 @@ typedef struct params_s
/* read data from file of socket */ /* read data from file of socket */
ssize_t (*pf_read)(int fd, void *buf, size_t count); ssize_t (*pf_read)(int fd, void *buf, size_t count);
ssize_t (*pf_write)(int fd, const void *buf, size_t count); ssize_t (*pf_write)(int fd, const void *buf, size_t count);
/* logging function */
void (*pf_log)(const int level, const char *format, ...);
} params_t; } params_t;
#endif #endif
...@@ -1489,8 +1489,8 @@ bool libdvbpsi_process(ts_stream_t *stream, uint8_t *buf, ssize_t length, mtime_ ...@@ -1489,8 +1489,8 @@ bool libdvbpsi_process(ts_stream_t *stream, uint8_t *buf, ssize_t length, mtime_
stream->pid[i_pid].i_received = date; stream->pid[i_pid].i_received = date;
if (stream->level < DVBPSI_MSG_DEBUG) if (stream->level < DVBPSI_MSG_DEBUG)
stream->pf_log(stream->cb_data, 0, stream->pf_log(stream->cb_data, 2,
"dvbinfo: %"PRId64" packet %"PRId64" pid %d (0x%x) cc %d\n", "dvbinfo: %"PRId64" packet %"PRId64" pid %u (0x%x) cc %d\n",
date, stream->i_packets, i_pid, i_pid, i_cc); date, stream->i_packets, i_pid, i_pid, i_cc);
if (i_pid == 0x0) /* PAT */ if (i_pid == 0x0) /* PAT */
...@@ -1664,7 +1664,7 @@ bool libdvbpsi_process(ts_stream_t *stream, uint8_t *buf, ssize_t length, mtime_ ...@@ -1664,7 +1664,7 @@ bool libdvbpsi_process(ts_stream_t *stream, uint8_t *buf, ssize_t length, mtime_
if (b_discontinuity_seen) if (b_discontinuity_seen)
{ {
stream->pf_log(stream->cb_data, 2, stream->pf_log(stream->cb_data, 2,
"dvbinfo: Continuity counter discontinuity (pid %d 0x%x found %d expected %d)\n", "dvbinfo: Continuity counter discontinuity (pid %u 0x%x found %d expected %d)\n",
i_pid, i_pid, stream->pid[i_pid].i_cc, i_old_cc+1); i_pid, i_pid, stream->pid[i_pid].i_cc, i_old_cc+1);
/* Discontinuity has been handled */ /* Discontinuity has been handled */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment