// Custom includes
#include <boost/utility.hpp>
-#include "../Logger/SenfLog.hh"
+#include <senf/Utils/Logger/SenfLog.hh>
//#include "Daemon.mpp"
-///////////////////////////////hh.p////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
namespace senf {
/** \brief %Daemon process
- The %Daemon class provides simple management for daemon processes. Specifically, it implements
+ The %Daemon class provides simple management for daemon processes. Specifically, it
+ implements
\li <i>Safe startup.</i> If the startup fails, the foreground process which launches the
daemon will terminate with an appropriate error exit code.
\li <i>Straight forward application initialization.</i> The daemon process is forked before
- even initializing the application. The initialization procedure must not cater for a
- later fork().
- \li <i>Automatic pid file management.</i> The daemon will not be started, if a valid pid file is
- found. Stale pid files are automatically removed.
+ even initializing the application. The initialization procedure doesn't need to cater
+ for a later fork().
+ \li <i>Automatic pid file management.</i> The daemon will not be started, if a valid pid
+ file is found. Stale pid files are automatically removed.
\li <i>Console log management.</i> It is possible, to redirect standard output and error to
one or two log files. Messages pertaining to application initialization will be written
to both the console and the log file whereas later messages will be directed to the log
\li <i>Optional foreground execution.</i> The daemon may be started in the foreground for
debugging purposes. In this case, the console log file(s) is/are automatically
suppressed.
-
+
Starting the daemon process proceeds along the following steps:
\li The daemon is started by calling the daemon class instances start() member. This
normally happens from the \c main() function generated by \ref SENF_DAEMON_MAIN().
created. The parent (foreground) process keeps running overseeing the daemon process.
\li main() is called. This virtual member may optionally be overridden in the derived
class. Here we assume, main() is not overridden so the default implementation is used.
- \li main() calls init().
+ \li main() calls init().
\li after init() returns, main() calls detach().
\li detach() signals successful startup to the parent process. The parent process terminates
leaving the daemon process running in the background.
arguments. It just checks, if any of above arguments are present and precesses them. Other
arguments are completely ignored. The command line parameters should be completely processed
within init().
-
+
*/
class Daemon : boost::noncopyable
{
public:
SENF_LOG_CLASS_AREA();
- ///////////////////////////////////////////////////////////////////////////
+ //-/////////////////////////////////////////////////////////////////////////
// Types
-
+
/// Select standard stream to redirect
- enum StdStream {
+ enum StdStream {
StdOut /** Standard output stream */
, StdErr /** Standard error stream */
, Both /** Both, standard output and error stream */
- };
+ };
- ///////////////////////////////////////////////////////////////////////////
+ //-/////////////////////////////////////////////////////////////////////////
///\name Structors and default members
- ///\{
+ //\{
virtual ~Daemon();
- ///\}
+ //\}
///\name Parameters
- ///\{
+ //\{
void daemonize(bool); ///< Configure whether to run in fore- or background
bool daemon(); ///< \c true, if running as daemon
- void consoleLog(std::string const &, StdStream which = Both);
+ void consoleLog(std::string const &, StdStream which = Both);
///< Configure console log file
/**< May be called multiple times to set the log file
for stdout and stderr seperately. Any standard stream
not assigned to a log file will be redirected to
- <tt>/dev/null</tt>.
+ <tt>/dev/null</tt>.
When running in the foreground, the log files will be
ignored. */
daemon startup. If another running instance of the
daemon is detected, starting the daemon will fail. */
- ///\}
+ //\}
///\name Auxiliary helpers
- ///\{
+ //\{
void detach(); ///< Detach into background now
/**< This is \e not the same as forking. The process will
/**< This is used when rotating the logs. By default,
SIGHUP calls logReopen. */
- ///\}
-
+ //\}
+
int start(int argc, char const ** argv); ///< Called from main() to launch daemon.
/**< Normally not called directly but from the
\ref SENF_DAEMON_MAIN macro. */
}
-///////////////////////////////hh.e////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
#include "Daemon.cci"
//#include "Daemon.ct"
//#include "Daemon.cti"