+#define MASKOF(x) (1 << (x))
+
+#if !defined(DEFAULT_LOGLEVEL)
+# define DEFAULT_LOGLEVEL Log_Level_Warning
+#endif
+
+#if !defined(DEFAULT_LOGMASK)
+# define DEFAULT_LOGMASK (MASKOF((DEFAULT_LOGLEVEL) + 1) - 1)
+#endif
+
+#if !defined(MINIMAL_LOGLEVEL)
+# define MINIMAL_LOGLEVEL Log_Level_Error
+#endif
+
+#if !defined(MINIMAL_LOGMASK)
+# define MINIMAL_LOGMASK (MASKOF((MINIMAL_LOGLEVEL) + 1) - 1)
+#endif
+
+static const char *names[] = {
+ "emergency",
+ "alert",
+ "critical",
+ "error",
+ "warning",
+ "notice",
+ "info",
+ "debug"
+};
+
+static const char asort[] = { 1, 2, 7, 0, 3, 6, 5, 4 };
+
+int logmask = DEFAULT_LOGMASK | MINIMAL_LOGMASK;
+
+void (*verbose_observer)(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args);
+
+#define CROP_LOGLEVEL(x) \
+ ((x) < Log_Level_Emergency ? Log_Level_Emergency \
+ : (x) > Log_Level_Debug ? Log_Level_Debug : (x))
+
+#if defined(VERBOSE_WITH_SYSLOG)
+
+#include <syslog.h>
+
+static void _vverbose_(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args)
+{
+ char *p;
+
+ if (file == NULL || vasprintf(&p, fmt, args) < 0)
+ vsyslog(loglevel, fmt, args);
+ else {
+ syslog(CROP_LOGLEVEL(loglevel), "%s [%s:%d, %s]", p, file, line, function?:"?");
+ free(p);
+ }
+}
+
+void verbose_set_name(const char *name, int authority)
+{
+ openlog(name, LOG_PERROR, authority ? LOG_AUTH : LOG_USER);
+}
+
+#elif defined(VERBOSE_WITH_SYSTEMD)
+
+#define SD_JOURNAL_SUPPRESS_LOCATION
+
+#include <systemd/sd-journal.h>
+
+static const char *appname;
+
+static int appauthority;
+
+static void _vverbose_(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args)
+{
+ char lino[20];
+
+ if (file == NULL) {
+ sd_journal_printv(loglevel, fmt, args);
+ } else {
+ sprintf(lino, "%d", line);
+ sd_journal_printv_with_location(loglevel, file, lino, function, fmt, args);
+ }
+}
+
+void verbose_set_name(const char *name, int authority)
+{
+ appname = name;
+ appauthority = authority;
+}
+