diff options
Diffstat (limited to 'lib/server/Daemon.cpp')
-rw-r--r-- | lib/server/Daemon.cpp | 159 |
1 files changed, 33 insertions, 126 deletions
diff --git a/lib/server/Daemon.cpp b/lib/server/Daemon.cpp index 7419f973..d3c8441f 100644 --- a/lib/server/Daemon.cpp +++ b/lib/server/Daemon.cpp @@ -9,23 +9,27 @@ #include "Box.h" -#ifdef HAVE_UNISTD_H - #include <unistd.h> -#endif - #include <errno.h> #include <stdio.h> #include <signal.h> #include <string.h> #include <stdarg.h> +#ifdef HAVE_PROCESS_H +# include <process.h> +#endif + +#ifdef HAVE_UNISTD_H +# include <unistd.h> +#endif + #ifdef HAVE_BSD_UNISTD_H #include <bsd/unistd.h> #endif #ifdef WIN32 + #include <Strsafe.h> #include <ws2tcpip.h> - #include <process.h> #endif #include "depot.h" @@ -36,12 +40,13 @@ # include "BoxVersion.h" #endif +#include "autogen_ConnectionException.h" +#include "autogen_ServerException.h" #include "Configuration.h" #include "Daemon.h" #include "FileModificationTime.h" #include "Guards.h" #include "Logging.h" -#include "ServerException.h" #include "UnixUser.h" #include "Utils.h" @@ -106,11 +111,11 @@ Daemon::~Daemon() // -------------------------------------------------------------------------- std::string Daemon::GetOptionString() { - return "c:" + return std::string("c:" #ifndef WIN32 "DF" #endif - "hkKo:O:PqQt:TUvVW:"; + "hkKo:O:") + Logging::OptionParser::GetOptionString(); } void Daemon::Usage() @@ -133,16 +138,7 @@ void Daemon::Usage() " -K Stop writing log messages to console while daemon is running\n" " -o <file> Log to a file, defaults to maximum verbosity\n" " -O <level> Set file log verbosity to error/warning/notice/info/trace/everything\n" - " -P Show process ID (PID) in console output\n" - " -q Run more quietly, reduce verbosity level by one, can repeat\n" - " -Q Run at minimum verbosity, log nothing to console and system\n" - " -t <tag> Tag console output with specified marker\n" - " -T Timestamp console output\n" - " -U Timestamp console output with microseconds\n" - " -v Run more verbosely, increase verbosity level by one, can repeat\n" - " -V Run at maximum verbosity, log everything to console and system\n" - " -W <level> Set verbosity to error/warning/notice/info/trace/everything\n" - ; + << Logging::OptionParser::GetUsageString(); } // -------------------------------------------------------------------------- @@ -218,94 +214,9 @@ int Daemon::ProcessOption(signed int option) } break; - case 'P': - { - Console::SetShowPID(true); - } - break; - - case 'q': - { - if(mLogLevel == Log::NOTHING) - { - BOX_FATAL("Too many '-q': " - "Cannot reduce logging " - "level any more"); - return 2; - } - mLogLevel--; - } - break; - - case 'Q': - { - mLogLevel = Log::NOTHING; - } - break; - - case 't': - { - Logging::SetProgramName(optarg); - Console::SetShowTag(true); - } - break; - - case 'T': - { - Console::SetShowTime(true); - } - break; - - case 'U': - { - Console::SetShowTime(true); - Console::SetShowTimeMicros(true); - } - break; - - case 'v': - { - if(mLogLevel == Log::EVERYTHING) - { - BOX_FATAL("Too many '-v': " - "Cannot increase logging " - "level any more"); - return 2; - } - mLogLevel++; - } - break; - - case 'V': - { - mLogLevel = Log::EVERYTHING; - } - break; - - case 'W': - { - mLogLevel = Logging::GetNamedLevel(optarg); - if (mLogLevel == Log::INVALID) - { - BOX_FATAL("Invalid logging level: " << optarg); - return 2; - } - } - break; - - case '?': - { - BOX_FATAL("Unknown option on command line: " - << "'" << (char)optopt << "'"); - return 2; - } - break; - default: { - BOX_FATAL("Unknown error in getopt: returned " - << "'" << option << "'"); - return 1; + return mLogLevel.ProcessOption(option); } } @@ -351,12 +262,6 @@ int Daemon::Main(const std::string& rDefaultConfigFile, int argc, int Daemon::ProcessOptions(int argc, const char *argv[]) { - #ifdef BOX_RELEASE_BUILD - mLogLevel = Log::NOTICE; - #else - mLogLevel = Log::INFO; - #endif - if (argc == 2 && strcmp(argv[1], "/?") == 0) { Usage(); @@ -368,7 +273,7 @@ int Daemon::ProcessOptions(int argc, const char *argv[]) // reset getopt, just in case anybody used it before. // unfortunately glibc and BSD differ on this point! // http://www.ussg.iu.edu/hypermail/linux/kernel/0305.3/0262.html - #if HAVE_DECL_OPTRESET == 1 || defined WIN32 + #if HAVE_DECL_OPTRESET == 1 || defined BOX_BSD_GETOPT optind = 1; optreset = 1; #elif defined __GLIBC__ @@ -406,13 +311,14 @@ int Daemon::ProcessOptions(int argc, const char *argv[]) return 2; } - Logging::FilterConsole((Log::Level)mLogLevel); - Logging::FilterSyslog ((Log::Level)mLogLevel); + Logging::FilterConsole(mLogLevel.GetCurrentLevel()); + Logging::FilterSyslog (mLogLevel.GetCurrentLevel()); if (mLogFileLevel != Log::INVALID) { mapLogFileLogger.reset( - new FileLogger(mLogFile, mLogFileLevel)); + new FileLogger(mLogFile, mLogFileLevel, + !mLogLevel.mTruncateLogFile)); } return 0; @@ -473,17 +379,17 @@ bool Daemon::Configure(const std::string& rConfigFileName) BOX_ERROR("Failed to load or verify configuration file"); return false; } - + if(!Configure(*apConfig)) { BOX_ERROR("Failed to verify configuration file"); - return false; + return false; } - + // Store configuration mConfigFileName = rConfigFileName; mLoadedConfigModifiedTime = GetConfigFileModifiedTime(); - + return true; } @@ -513,14 +419,14 @@ bool Daemon::Configure(const Configuration& rConfig) BOX_ERROR("Configuration errors: " << errors); return false; } - + // Store configuration mapConfiguration = apConf; - + // Let the derived class have a go at setting up stuff // in the initial process SetupInInitialProcess(); - + return true; } @@ -664,7 +570,7 @@ int Daemon::Main(const std::string &rConfigFileName) // Write PID to file char pid[32]; - int pidsize = sprintf(pid, "%d", (int)getpid()); + int pidsize = snprintf(pid, sizeof(pid), "%d", (int)getpid()); if(::write(pidFile, pid, pidsize) != pidsize) { @@ -676,9 +582,8 @@ int Daemon::Main(const std::string &rConfigFileName) // Set up memory leak reporting #ifdef BOX_MEMORY_LEAK_TESTING { - char filename[256]; - sprintf(filename, "%s.memleaks", DaemonName()); - memleakfinder_setup_exit_report(filename, DaemonName()); + memleakfinder_setup_exit_report(std::string(DaemonName()) + + ".memleaks", DaemonName()); } #endif // BOX_MEMORY_LEAK_TESTING @@ -986,7 +891,9 @@ const Configuration &Daemon::GetConfiguration() const if(mapConfiguration.get() == 0) { // Shouldn't get anywhere near this if a configuration file can't be loaded - THROW_EXCEPTION(ServerException, Internal) + THROW_EXCEPTION_MESSAGE(ServerException, Internal, + "The daemon has not been configured; no config file " + "has been loaded."); } return *mapConfiguration; |