Skip to content

Commit

Permalink
Add a new command line option: -u to send logs to STDERR.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebrady committed Feb 16, 2019
1 parent 9f91bd2 commit fce6b51
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 65 deletions.
99 changes: 38 additions & 61 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@ void set_alsa_out_dev(char *);

static volatile int requested_connection_state_to_output = 1;

// this stuff is to direct logging to syslog via libdaemon or directly
// alternatively you can direct it to stderr using a command line option

#ifdef CONFIG_LIBDAEMON
static void (*sps_log)(int prio, const char *t, ...) = daemon_log;
#else
static void (*sps_log)(int prio, const char *t, ...) = syslog;
#endif

void do_sps_log(__attribute__((unused)) int prio, const char *t, ...) {
char s[1024];
va_list args;
va_start(args, t);
vsnprintf(s, sizeof(s), t, args);
va_end(args);
fprintf(stderr,"%s\n",s);
}

void log_to_stderr() {
sps_log = do_sps_log;
}

shairport_cfg config;

volatile int debuglev = 0;
Expand Down Expand Up @@ -146,24 +168,13 @@ void die(const char *format, ...) {
va_end(args);

if ((debuglev) && (config.debugger_show_elapsed_time) && (config.debugger_show_relative_time))

#ifdef CONFIG_LIBDAEMON
daemon_log(LOG_ERR, "|% 20.9f|% 20.9f|*fatal error: %s", tss, tsl, s);
else if ((debuglev) && (config.debugger_show_relative_time))
daemon_log(LOG_ERR, "% 20.9f|*fatal error: %s", tsl, s);
else if ((debuglev) && (config.debugger_show_elapsed_time))
daemon_log(LOG_ERR, "% 20.9f|*fatal error: %s", tss, s);
else
daemon_log(LOG_ERR, "fatal error: %s", s);
#else
syslog(LOG_ERR, "|% 20.9f|% 20.9f|*fatal error: %s", tss, tsl, s);
sps_log(LOG_ERR, "|% 20.9f|% 20.9f|*fatal error: %s", tss, tsl, s);
else if ((debuglev) && (config.debugger_show_relative_time))
syslog(LOG_ERR, "% 20.9f|*fatal error: %s", tsl, s);
sps_log(LOG_ERR, "% 20.9f|*fatal error: %s", tsl, s);
else if ((debuglev) && (config.debugger_show_elapsed_time))
syslog(LOG_ERR, "% 20.9f|*fatal error: %s", tss, s);
sps_log(LOG_ERR, "% 20.9f|*fatal error: %s", tss, s);
else
syslog(LOG_ERR, "fatal error: %s", s);
#endif
sps_log(LOG_ERR, "fatal error: %s", s);
pthread_setcancelstate(oldState, NULL);
exit(1);
}
Expand All @@ -184,25 +195,14 @@ void warn(const char *format, ...) {
va_start(args, format);
vsnprintf(s, sizeof(s), format, args);
va_end(args);
#ifdef CONFIG_LIBDAEMON
if ((debuglev) && (config.debugger_show_elapsed_time) && (config.debugger_show_relative_time))
daemon_log(LOG_WARNING, "|% 20.9f|% 20.9f|*warning: %s", tss, tsl, s);
sps_log(LOG_WARNING, "|% 20.9f|% 20.9f|*warning: %s", tss, tsl, s);
else if ((debuglev) && (config.debugger_show_relative_time))
daemon_log(LOG_WARNING, "% 20.9f|*warning: %s", tsl, s);
sps_log(LOG_WARNING, "% 20.9f|*warning: %s", tsl, s);
else if ((debuglev) && (config.debugger_show_elapsed_time))
daemon_log(LOG_WARNING, "% 20.9f|*warning: %s", tss, s);
sps_log(LOG_WARNING, "% 20.9f|*warning: %s", tss, s);
else
daemon_log(LOG_WARNING, "%s", s);
#else
if ((debuglev) && (config.debugger_show_elapsed_time) && (config.debugger_show_relative_time))
syslog(LOG_WARNING, "|% 20.9f|% 20.9f|*warning: %s", tss, tsl, s);
else if ((debuglev) && (config.debugger_show_relative_time))
syslog(LOG_WARNING, "% 20.9f|*warning: %s", tsl, s);
else if ((debuglev) && (config.debugger_show_elapsed_time))
syslog(LOG_WARNING, "% 20.9f|*warning: %s", tss, s);
else
syslog(LOG_WARNING, "%s", s);
#endif
sps_log(LOG_WARNING, "%s", s);
pthread_setcancelstate(oldState, NULL);
}

Expand All @@ -224,25 +224,14 @@ void debug(int level, const char *format, ...) {
va_start(args, format);
vsnprintf(s, sizeof(s), format, args);
va_end(args);
#ifdef CONFIG_LIBDAEMON
if ((config.debugger_show_elapsed_time) && (config.debugger_show_relative_time))
daemon_log(LOG_DEBUG, "|% 20.9f|% 20.9f|%s", tss, tsl, s);
else if (config.debugger_show_relative_time)
daemon_log(LOG_DEBUG, "% 20.9f|%s", tsl, s);
else if (config.debugger_show_elapsed_time)
daemon_log(LOG_DEBUG, "% 20.9f|%s", tss, s);
else
daemon_log(LOG_DEBUG, "%s", s);
#else
if ((config.debugger_show_elapsed_time) && (config.debugger_show_relative_time))
syslog(LOG_DEBUG, "|% 20.9f|% 20.9f|%s", tss, tsl, s);
sps_log(LOG_DEBUG, "|% 20.9f|% 20.9f|%s", tss, tsl, s);
else if (config.debugger_show_relative_time)
syslog(LOG_DEBUG, "% 20.9f|%s", tsl, s);
sps_log(LOG_DEBUG, "% 20.9f|%s", tsl, s);
else if (config.debugger_show_elapsed_time)
syslog(LOG_DEBUG, "% 20.9f|%s", tss, s);
sps_log(LOG_DEBUG, "% 20.9f|%s", tss, s);
else
syslog(LOG_DEBUG, "%s", s);
#endif
sps_log(LOG_DEBUG, "%s", s);
pthread_setcancelstate(oldState, NULL);
}

Expand All @@ -262,26 +251,14 @@ void inform(const char *format, ...) {
va_start(args, format);
vsnprintf(s, sizeof(s), format, args);
va_end(args);
#ifdef CONFIG_LIBDAEMON
if ((debuglev) && (config.debugger_show_elapsed_time) && (config.debugger_show_relative_time))
daemon_log(LOG_INFO, "|% 20.9f|% 20.9f|%s", tss, tsl, s);
sps_log(LOG_INFO, "|% 20.9f|% 20.9f|%s", tss, tsl, s);
else if ((debuglev) && (config.debugger_show_relative_time))
daemon_log(LOG_INFO, "% 20.9f|%s", tsl, s);
sps_log(LOG_INFO, "% 20.9f|%s", tsl, s);
else if ((debuglev) && (config.debugger_show_elapsed_time))
daemon_log(LOG_INFO, "% 20.9f|%s", tss, s);
sps_log(LOG_INFO, "% 20.9f|%s", tss, s);
else
daemon_log(LOG_INFO, "%s", s);
#else
if ((debuglev) && (config.debugger_show_elapsed_time) && (config.debugger_show_relative_time))
syslog(LOG_INFO, "|% 20.9f|% 20.9f|%s", tss, tsl, s);
else if ((debuglev) && (config.debugger_show_relative_time))
syslog(LOG_INFO, "% 20.9f|%s", tsl, s);
else if ((debuglev) && (config.debugger_show_elapsed_time))
syslog(LOG_INFO, "% 20.9f|%s", tss, s);
else
syslog(LOG_INFO, "%s", s);

#endif
sps_log(LOG_INFO, "%s", s);
pthread_setcancelstate(oldState, NULL);
}

Expand Down
2 changes: 2 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ uint16_t nctohs(const uint8_t *p); // read 2 characters from *p and do ntohs on

void memory_barrier();

void log_to_stderr(); // call this to director logging to stderr;

// true if Shairport Sync is supposed to be sending output to the output device, false otherwise

int get_requested_connection_state_to_output();
Expand Down
14 changes: 10 additions & 4 deletions shairport.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ void usage(char *progname) {
#ifdef CONFIG_LIBDAEMON
printf(" -d, --daemon daemonise.\n");
printf(" -j, --justDaemoniseNoPIDFile daemonise without a PID file.\n");
printf(" -k, --kill kill the existing shairport daemon.\n");
#endif
printf(" -V, --version show version information.\n");
printf(" -k, --kill kill the existing shairport daemon.\n");
printf(" -c, --configfile=FILE read configuration settings from FILE. Default is "
"/etc/shairport-sync.conf.\n");

Expand All @@ -171,7 +171,6 @@ void usage(char *progname) {
printf(" -L, --latency=FRAMES [Deprecated] Set the latency for audio sent from an unknown "
"device.\n");
printf(" The default is to set it automatically.\n");
printf(" The default is to set it automatically.\n");
printf(" -S, --stuffing=MODE set how to adjust current latency to match desired latency, "
"where \n");
printf(" \"basic\" (default) inserts or deletes audio frames from "
Expand Down Expand Up @@ -213,6 +212,7 @@ void usage(char *progname) {
printf(" The default is /tmp/shairport-sync-metadata.\n");
printf(" --get-coverart send cover art through the metadata pipe.\n");
#endif
printf(" -u, --use-stderr log messages through STDERR rather than syslog.\n");
printf("\n");
mdns_ls_backends();
printf("\n");
Expand All @@ -229,15 +229,17 @@ int parse_options(int argc, char **argv) {
int fResyncthreshold = (int)(config.resyncthreshold * 44100);
int fTolerance = (int)(config.tolerance * 44100);
poptContext optCon; /* context for parsing command-line options */
#if CONFIG_LIBDAEMON
int daemonisewith = 0;
int daemonisewithout = 0;
#endif
struct poptOption optionsTable[] = {
{"verbose", 'v', POPT_ARG_NONE, NULL, 'v', NULL, NULL},
{"disconnectFromOutput", 'D', POPT_ARG_NONE, NULL, 0, NULL, NULL},
{"reconnectToOutput", 'R', POPT_ARG_NONE, NULL, 0, NULL, NULL},
#if CONFIG_LIBDAEMON
{"kill", 'k', POPT_ARG_NONE, NULL, 0, NULL, NULL},
{"daemon", 'd', POPT_ARG_NONE, &daemonisewith, 0, NULL, NULL},
{"justDaemoniseNoPIDFile", 'j', POPT_ARG_NONE, &daemonisewithout, 0, NULL, NULL},
#endif
{"configfile", 'c', POPT_ARG_STRING, &config.configfile, 0, NULL, NULL},
{"statistics", 0, POPT_ARG_NONE, &config.statistics_requested, 0, NULL, NULL},
{"logOutputLevel", 0, POPT_ARG_NONE, &config.logOutputLevel, 0, NULL, NULL},
Expand All @@ -255,6 +257,7 @@ int parse_options(int argc, char **argv) {
{"timeout", 't', POPT_ARG_INT, &config.timeout, 't', NULL, NULL},
{"password", 0, POPT_ARG_STRING, &config.password, 0, NULL, NULL},
{"tolerance", 'z', POPT_ARG_INT, &fTolerance, 0, NULL, NULL},
{"use-stderr", 'u', POPT_ARG_NONE, NULL, 'u', NULL, NULL},
#ifdef CONFIG_METADATA
{"metadata-pipename", 'M', POPT_ARG_STRING, &config.metadata_pipename, 'M', NULL, NULL},
{"get-coverart", 'g', POPT_ARG_NONE, &config.get_coverart, 'g', NULL, NULL},
Expand All @@ -281,6 +284,9 @@ int parse_options(int argc, char **argv) {
case 'v':
debuglev++;
break;
case 'u':
log_to_stderr();
break;
case 'D':
inform("Warning: the option -D or --disconnectFromOutput is deprecated.");
break;
Expand Down

0 comments on commit fce6b51

Please sign in to comment.