/* This file is part of GNU Pies. Copyright (C) 2009-2013 Sergey Poznyakoff GNU Pies is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. GNU Pies 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 General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Pies. If not, see . */ #include "pies.h" unsigned debug_level; int source_info_option; int diag_output = DIAG_TO_STDERR; void diag_setup (int flags) { if (flags) diag_output = flags; if (diag_output & DIAG_TO_SYSLOG) openlog (log_tag, LOG_PID, log_facility); } void syslog_printer (int prio, const char *fmt, va_list ap) { #if HAVE_VSYSLOG vsyslog (prio, fmt, ap); #else char buf[128]; vsnprintf (buf, sizeof buf, fmt, ap); syslog (prio, "%s", buf); #endif } static FILE * stderr_open () { if (!init_process) return stderr; else { int fd = console_open (O_WRONLY|O_NOCTTY|O_NDELAY); if (fd == -1) return NULL; return fdopen (fd, "w"); } } static void stderr_close (FILE *fp) { if (init_process) fclose (fp); } void vlogmsg (int prio, const char *fmt, va_list ap) { if (DIAG_OUTPUT (DIAG_TO_STDERR)) { va_list aq; FILE *fp = stderr_open (); if (!fp) return; fprintf (fp, "%s: ", program_name); va_copy (aq, ap); vfprintf (fp, fmt, aq); va_end (aq); fprintf (fp, "\n"); stderr_close (fp); } if (DIAG_OUTPUT (DIAG_TO_SYSLOG)) syslog_printer (prio, fmt, ap); } void logmsg (int prio, const char *fmt, ...) { va_list ap; va_start (ap, fmt); vlogmsg (prio, fmt, ap); va_end (ap); } void debug_msg (const char *fmt, ...) { va_list ap; va_start (ap, fmt); vlogmsg (LOG_DEBUG, fmt, ap); va_end (ap); } void logmsg_vprintf (int prio, const char *fmt, va_list ap) { char *p, *t, *str; static char *buf = NULL; static size_t len = 0; static struct grecs_txtacc *log_acc; if (grecs_vasprintf (&buf, &len, fmt, ap)) { logmsg (LOG_CRIT, _("out of memory trying to log a message")); return; } if (!log_acc) log_acc = grecs_txtacc_create (); str = buf; while (str) { p = strchr (str, '\n'); if (p) { *p++ = 0; if (!*p) p = NULL; grecs_txtacc_grow_string (log_acc, str); grecs_txtacc_grow_char (log_acc, 0); t = grecs_txtacc_finish (log_acc, 0); logmsg (prio, "%s", t); grecs_txtacc_free_string (log_acc, t); } else grecs_txtacc_grow_string (log_acc, str); str = p; } } void logmsg_printf (int prio, const char *fmt, ...) { va_list ap; va_start (ap, fmt); logmsg_vprintf (prio, fmt, ap); va_end (ap); } void grecs_print_diag (grecs_locus_t *locus, int err, int errcode, const char *msg) { char *locstr = NULL; if (locus) { size_t size = 0; if (locus->beg.col == 0) grecs_asprintf (&locstr, &size, "%s:%u", locus->beg.file, locus->beg.line); else if (strcmp (locus->beg.file, locus->end.file)) grecs_asprintf (&locstr, &size, "%s:%u.%u-%s:%u.%u", locus->beg.file, locus->beg.line, locus->beg.col, locus->end.file, locus->end.line, locus->end.col); else if (locus->beg.line != locus->end.line) grecs_asprintf (&locstr, &size, "%s:%u.%u-%u.%u", locus->beg.file, locus->beg.line, locus->beg.col, locus->end.line, locus->end.col); else grecs_asprintf (&locstr, &size, "%s:%u.%u-%u", locus->beg.file, locus->beg.line, locus->beg.col, locus->end.col); } if (locstr) { if (errcode) logmsg (err ? LOG_ERR : LOG_WARNING, "%s: %s: %s", locstr, msg, strerror (errcode)); else logmsg (err ? LOG_ERR : LOG_WARNING, "%s: %s", locstr, msg); free (locstr); } else { if (errcode) logmsg (err ? LOG_ERR : LOG_WARNING, "%s: %s", msg, strerror (errcode)); else logmsg (err ? LOG_ERR : LOG_WARNING, "%s", msg); } }