Added SENF_NO_DEBUG symbol and removed dependency on NDEBUG
[senf.git] / Utils / Daemon / Daemon.cc
index 34f476a..88b79a1 100644 (file)
@@ -1,8 +1,8 @@
 // $Id$
 //
 // Copyright (C) 2007 
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer NETwork research (NET)
+// Fraunhofer Institute for Open Communication Systems (FOKUS) 
+// Competence Center NETwork research (NET), St. Augustin, GERMANY 
 //     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
@@ -110,7 +110,7 @@ namespace {
 
 prefix_ void senf::Daemon::detach()
 {
-    if (daemonize_) {
+    if (daemonize_ && ! detached_) {
         // Wow .. ouch .. 
         // To ensure all data is written to the console log file in the correct order, we suspend
         // execution here until the parent process tells us to continue via SIGUSR1: We block
@@ -144,20 +144,30 @@ prefix_ void senf::Daemon::detach()
 
         LIBC_CALL( ::sigaction, (SIGUSR1, &oldact, 0) );
         LIBC_CALL( ::sigprocmask, (SIG_SETMASK, &oldsig, 0) );
+
+        detached_ = true;
     }
 }
 
+namespace {
+    /* Purposely *not* derived from std::exception */
+    struct DaemonExitException {
+        DaemonExitException(unsigned c) : code(c) {}
+        unsigned code;
+    };
+}
+
+prefix_ void senf::Daemon::exit(unsigned code)
+{
+    throw DaemonExitException(code);
+}
+
 prefix_ int senf::Daemon::start(int argc, char const ** argv)
 {
     argc_ = argc;
     argv_ = argv;
 
-#   ifdef NDEBUG
-
     try {
-
-#   endif
-
         configure();
 
         if (daemonize_) {
@@ -171,10 +181,13 @@ prefix_ int senf::Daemon::start(int argc, char const ** argv)
         }
 
         main();
+    }
+    catch (DaemonExitException & e) {
+        return e.code;
+    }
 
-#   ifdef NDEBUG
+#ifdef SENF_NO_DEBUG
 
-    }
     catch (std::exception & e) {
         std::cerr << "\n*** Fatal exception: " << e.what() << std::endl;
         return 1;
@@ -210,7 +223,8 @@ prefix_ void senf::Daemon::configure()
             std::string::size_type komma (arg.find(','));
             if (komma == std::string::npos) {
                 boost::trim(arg);
-                consoleLog(arg);
+                if (arg == std::string("none")) consoleLog("");
+                else if (!arg.empty())          consoleLog(arg);
             } else {
                 std::string arg1 (arg,0,komma);
                 std::string arg2 (arg,komma+1);