+ /* Calculate a local version of erroutput_type, based on the
+ * context if the process has not set erroutput_type explicitly */
+ if (!(erroutput_type & ERR_FROM_CONTEXT)) {
+ local_erroutput = erroutput_type;
+ } else {
+ switch (context) {
+ case CONTEXT_SCRIPTUTIL:
+ local_erroutput = ERR_INTERACTIVE;
+ break;
+
+ case CONTEXT_DAEMON:
+ local_erroutput = ERR_INTERACTIVE
+ | ERR_AMANDALOG
+ | ERR_SYSLOG;
+ break;
+
+ case CONTEXT_CMDLINE:
+ case CONTEXT_DEFAULT:
+ default:
+ local_erroutput = ERR_INTERACTIVE;
+ break;
+ }
+ }
+
+ if (local_erroutput & ERR_AMANDALOG && logerror_fn != NULL)
+ (*logerror_fn)((char *)message); /* discard 'const' */
+
+ if (local_erroutput & ERR_SYSLOG) {
+#ifdef LOG_AUTH
+ openlog(get_pname(), LOG_PID, LOG_AUTH);
+#else
+ openlog(get_pname(), LOG_PID, 0);
+#endif
+ syslog(LOG_NOTICE, "%s", message);
+ closelog();
+ }
+
+ if (local_erroutput & ERR_INTERACTIVE) {
+ g_fprintf(stderr, "%s: %s\n", get_pname(), message);
+ fflush(stderr);
+ }
+
+#ifdef HAVE_GLIBC_BACKTRACE
+ /* try logging a traceback to the debug log */
+ if (db_fd != -1) {
+ void *stack[32];
+ int naddrs;
+ naddrs = backtrace(stack, sizeof(stack)/sizeof(*stack));
+ backtrace_symbols_fd(stack, naddrs, db_fd);
+ }
+#endif
+
+ /* we're done */
+ if (log_level & G_LOG_LEVEL_CRITICAL)
+ exit(error_exit_status);
+ else
+ abort();
+ g_assert_not_reached();
+ }
+}
+
+/* Install our handler into the glib log handling system.
+ */
+static void
+debug_setup_logging(void)
+{
+ /* g_error and g_critical should be fatal, although the log handler
+ * takes care of this anyway */
+ g_log_set_always_fatal(G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+
+ /* set up handler (g_log_set_default_handler is new in glib-2.6, and
+ * hence not useable here) */
+ g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
+ debug_logging_handler, NULL);
+}
+
+/* Set the global dbgdir according to 'config' and 'subdir', and clean
+ * old debug files out of that directory
+ *
+ * The global open_time is set to the current time, and used to delete
+ * old files.
+ *
+ * @param config: configuration or NULL
+ * @param subdir: subdirectory (server, client, etc.) or NULL
+ */