+/* Call this to suppress tracebacks on error() or g_critical(). This is used
+ * when a critical error is indicated in perl, and the traceback will not be
+ * useful. */
+void
+suppress_error_traceback(void)
+{
+ do_suppress_error_traceback = 1;
+}
+
+/* A GLogFunc to handle g_log calls. This function assumes that user_data
+ * is either NULL or a pointer to one of the debug_* configuration variables
+ * in conffile.c, indicating whether logging for this log domain is enabled.
+ *
+ * @param log_domain: the log domain, or NULL for general logging
+ * @param log_level: level, fatality, and recursion flags
+ * @param message: the message to log
+ * @param user_pointer: unused
+ */
+static void
+debug_logging_handler(const gchar *log_domain G_GNUC_UNUSED,
+ GLogLevelFlags log_level,
+ const gchar *message,
+ gpointer user_data G_GNUC_UNUSED)
+{
+ GLogLevelFlags maxlevel;
+ char *levprefix = NULL;
+ pcontext_t context = get_pcontext();
+
+ /* glib allows a message to have multiple levels, so calculate the "worst"
+ * level */
+ if (log_level & G_LOG_LEVEL_ERROR) {
+ maxlevel = G_LOG_LEVEL_ERROR;
+ levprefix = _("error (fatal): ");
+ } else if (log_level & G_LOG_LEVEL_CRITICAL) {
+ maxlevel = G_LOG_LEVEL_CRITICAL;
+ levprefix = _("critical (fatal): ");
+ } else if (log_level & G_LOG_LEVEL_WARNING) {
+ maxlevel = G_LOG_LEVEL_WARNING;
+ levprefix = _("warning: ");
+ } else if (log_level & G_LOG_LEVEL_MESSAGE) {
+ maxlevel = G_LOG_LEVEL_MESSAGE;
+ levprefix = _("message: ");
+ } else if (log_level & G_LOG_LEVEL_INFO) {
+ maxlevel = G_LOG_LEVEL_INFO;
+ levprefix = _("info: ");
+ } else {
+ maxlevel = G_LOG_LEVEL_DEBUG;
+ levprefix = ""; /* no level displayed for debugging */
+ }
+
+ /* scriptutil context doesn't do any logging except for critical
+ * and error levels */
+ if (context != CONTEXT_SCRIPTUTIL) {
+ /* convert the highest level to a string and dbprintf it */
+ debug_printf("%s%s\n", levprefix, message);
+ }
+
+ if (amanda_log_handlers) {
+ GSList *iter = amanda_log_handlers;
+ while (iter) {
+ amanda_log_handler_t *hdlr = (amanda_log_handler_t *)iter->data;
+ hdlr(maxlevel, message);
+ iter = g_slist_next(iter);
+ }
+ } else {
+ /* call the appropriate handlers, based on the context */
+ amanda_log_stderr(maxlevel, message);
+ if (context == CONTEXT_DAEMON)
+ amanda_log_syslog(maxlevel, message);
+ }
+
+ /* error and critical levels have special handling */
+ if (log_level & (G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL)) {
+#ifdef HAVE_GLIBC_BACKTRACE
+ /* try logging a traceback to the debug log */
+ if (!do_suppress_error_traceback && 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);
+}
+
+void
+add_amanda_log_handler(amanda_log_handler_t *hdlr)
+{
+ amanda_log_handlers = g_slist_append(amanda_log_handlers, (gpointer)hdlr);
+}