Hostinfo day 2: Implement the daemon.
[virt-hostinfo.git] / hostinfod / error.c
diff --git a/hostinfod/error.c b/hostinfod/error.c
new file mode 100644 (file)
index 0000000..6fc6298
--- /dev/null
@@ -0,0 +1,202 @@
+/* virt-hostinfo
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <syslog.h>
+
+#include <apr_general.h>
+#include <apr_errno.h>
+
+#include "hostinfod.h"
+
+void
+init_syslog (void)
+{
+  openlog ("hostinfo", 0, LOG_DAEMON);
+}
+
+static void
+print (int level, const char *msg)
+{
+  const char *errwarn;
+  switch (level) {
+  case LOG_WARNING: errwarn = "warning"; break;
+  case LOG_ERR:     errwarn = "error"; break;
+  default:          errwarn = "info";
+  }
+
+  if (messages_to_stderr)
+    fprintf (stderr, "hostinfo: %s: %s\n", errwarn, msg);
+  syslog (LOG_DAEMON|level, "%s: %s", errwarn, msg);
+}
+
+void
+debug (const char *fs, ...)
+{
+  va_list args;
+  char *msg;
+
+  if (verbose) {
+    va_start (args, fs);
+    int err = vasprintf (&msg, fs, args);
+    va_end (args);
+
+    if (err < 0) return;
+
+    print (LOG_INFO, msg);
+    free (msg);
+  }
+}
+
+void
+message (const char *fs, ...)
+{
+  va_list args;
+  char *msg;
+
+  va_start (args, fs);
+  int err = vasprintf (&msg, fs, args);
+  va_end (args);
+
+  if (err < 0) return;
+
+  print (LOG_INFO, msg);
+  free (msg);
+}
+
+void
+error (const char *fs, ...)
+{
+  va_list args;
+  char *msg;
+
+  va_start (args, fs);
+  int err = vasprintf (&msg, fs, args);
+  va_end (args);
+
+  if (err < 0) return;
+
+  print (LOG_ERR, msg);
+  free (msg);
+}
+
+void
+warning (const char *fs, ...)
+{
+  va_list args;
+  char *msg;
+
+  va_start (args, fs);
+  int err = vasprintf (&msg, fs, args);
+  va_end (args);
+
+  if (err < 0) return;
+
+  print (LOG_WARNING, msg);
+  free (msg);
+}
+
+void
+perrorf (const char *fs, ...)
+{
+  va_list args;
+  char *msg;
+  int errnum = errno;
+
+  va_start (args, fs);
+  int err = vasprintf (&msg, fs, args);
+  va_end (args);
+
+  if (err < 0) return;
+
+#ifndef _GNU_SOURCE
+  char buf[256];
+  strerror_r (errnum, buf, sizeof buf);
+#else
+  char _buf[256];
+  char *buf;
+  buf = strerror_r (errnum, _buf, sizeof _buf);
+#endif
+
+  msg = realloc (msg, strlen (msg) + 2 + strlen (buf) + 1);
+  strcat (msg, ": ");
+  strcat (msg, buf);
+
+  print (LOG_ERR, msg);
+  free (msg);
+}
+
+void
+pwarningf (const char *fs, ...)
+{
+  va_list args;
+  char *msg;
+  int errnum = errno;
+
+  va_start (args, fs);
+  int err = vasprintf (&msg, fs, args);
+  va_end (args);
+
+  if (err < 0) return;
+
+#ifndef _GNU_SOURCE
+  char buf[256];
+  strerror_r (errnum, buf, sizeof buf);
+#else
+  char _buf[256];
+  char *buf;
+  buf = strerror_r (errnum, _buf, sizeof _buf);
+#endif
+
+  msg = realloc (msg, strlen (msg) + 2 + strlen (buf) + 1);
+  strcat (msg, ": ");
+  strcat (msg, buf);
+
+  print (LOG_WARNING, msg);
+  free (msg);
+}
+
+void
+paprerror (apr_status_t r, const char *fs, ...)
+{
+  va_list args;
+  char *msg;
+  char buf[256];
+
+  va_start (args, fs);
+  int err = vasprintf (&msg, fs, args);
+  va_end (args);
+
+  if (err < 0) return;
+
+  apr_strerror (r, buf, sizeof buf);
+
+  msg = realloc (msg, strlen (msg) + 2 + strlen (buf) + 1);
+  strcat (msg, ": ");
+  strcat (msg, buf);
+
+  print (LOG_ERR, msg);
+  free (msg);
+}