Changeset 2177

Show
Ignore:
Timestamp:
28/05/2008 14:28:16 (2 years ago)
Author:
chris
Message:

Move loading configuration into a separate method.

Add -W<level> option to set warning level explicitly.

Location:
box/trunk/lib/server
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • box/trunk/lib/server/Daemon.cpp

    r2131 r2177  
    113113                "DFkP" 
    114114        #endif 
    115                 "hqvVt:TU"; 
     115                "hqt:TUvVW:"; 
    116116} 
    117117 
     
    138138        "  -v         Run more verbosely, increase verbosity level by one, can repeat\n" 
    139139        "  -V         Run at maximum verbosity\n" 
     140        "  -W <level> Set verbosity to error/warning/notice/info/trace/everything\n" 
    140141        "  -t <tag>   Tag console output with specified marker\n" 
    141142        "  -T         Timestamp console output\n" 
     
    235236                { 
    236237                        mLogLevel = Log::EVERYTHING; 
     238                } 
     239                break; 
     240 
     241                case 'W': 
     242                { 
     243                        mLogLevel = Logging::GetNamedLevel(optarg); 
     244                        if (mLogLevel == Log::INVALID) 
     245                        { 
     246                                BOX_FATAL("Invalid logging level"); 
     247                                return 2; 
     248                        } 
    237249                } 
    238250                break; 
     
    354366// 
    355367// Function 
     368//              Name:    Daemon::Configure(const std::string& rConfigFileName) 
     369//              Purpose: Loads daemon configuration. Useful when you have 
     370//                       a local Daemon object and don't intend to fork() 
     371//                       or call Main(). 
     372//              Created: 2008/04/19 
     373// 
     374// -------------------------------------------------------------------------- 
     375 
     376bool Daemon::Configure(const std::string& rConfigFileName) 
     377{ 
     378        mConfigFileName = rConfigFileName; 
     379 
     380        // Load the configuration file. 
     381        std::string errors; 
     382        std::auto_ptr<Configuration> pconfig; 
     383 
     384        try 
     385        { 
     386                pconfig = Configuration::LoadAndVerify( 
     387                        mConfigFileName.c_str(),  
     388                        GetConfigVerify(), errors); 
     389        } 
     390        catch(BoxException &e) 
     391        { 
     392                if(e.GetType() == CommonException::ExceptionType && 
     393                        e.GetSubType() == CommonException::OSFileOpenError) 
     394                { 
     395                        BOX_ERROR("Failed to open configuration file: "  
     396                                << mConfigFileName); 
     397                        return false; 
     398                } 
     399 
     400                throw; 
     401        } 
     402 
     403        // Got errors? 
     404        if(pconfig.get() == 0 || !errors.empty()) 
     405        { 
     406                BOX_ERROR("Configuration errors: " << errors); 
     407                return false; 
     408        } 
     409         
     410        // Store configuration 
     411        mpConfiguration = pconfig.release(); 
     412        mLoadedConfigModifiedTime = GetConfigFileModifiedTime(); 
     413         
     414        // Let the derived class have a go at setting up stuff 
     415        // in the initial process 
     416        SetupInInitialProcess(); 
     417                 
     418        return true; 
     419} 
     420 
     421// -------------------------------------------------------------------------- 
     422// 
     423// Function 
    356424//              Name:    Daemon::Main(const std::string& rConfigFileName) 
    357425//              Purpose: Starts the daemon off -- equivalent of C main() function 
     
    368436        std::string pidFileName; 
    369437 
    370         mConfigFileName = rConfigFileName; 
    371          
    372         bool asDaemon   = !mSingleProcess && !mRunInForeground; 
     438        bool asDaemon = !mSingleProcess && !mRunInForeground; 
    373439 
    374440        try 
    375441        { 
    376                 // Load the configuration file. 
    377                 std::string errors; 
    378                 std::auto_ptr<Configuration> pconfig; 
    379  
    380                 try 
    381                 { 
    382                         pconfig = Configuration::LoadAndVerify( 
    383                                 mConfigFileName.c_str(),  
    384                                 GetConfigVerify(), errors); 
    385                 } 
    386                 catch(BoxException &e) 
    387                 { 
    388                         if(e.GetType() == CommonException::ExceptionType && 
    389                                 e.GetSubType() == CommonException::OSFileOpenError) 
    390                         { 
    391                                 BOX_FATAL("Failed to start: failed to open " 
    392                                         "configuration file: "  
    393                                         << mConfigFileName); 
    394                                 return 1; 
    395                         } 
    396  
    397                         throw; 
    398                 } 
    399  
    400                 // Got errors? 
    401                 if(pconfig.get() == 0 || !errors.empty()) 
    402                 { 
    403                         // Tell user about errors 
    404                         BOX_FATAL("Failed to start: errors in configuration " 
    405                                 "file: " << mConfigFileName << ": " << errors); 
    406                         // And give up 
     442                if (!Configure(rConfigFileName)) 
     443                { 
     444                        BOX_FATAL("Failed to start: failed to load " 
     445                                "configuration file: " << rConfigFileName); 
    407446                        return 1; 
    408447                } 
    409                  
    410                 // Store configuration 
    411                 mpConfiguration = pconfig.release(); 
    412                 mLoadedConfigModifiedTime = GetConfigFileModifiedTime(); 
    413                  
    414                 // Let the derived class have a go at setting up stuff in the initial process 
    415                 SetupInInitialProcess(); 
    416448                 
    417449                // Server configuration 
     
    676708#ifdef WIN32 
    677709        WSACleanup(); 
     710#else 
     711        // Should clean up here, but it breaks memory leak tests. 
     712        /* 
     713        if(asDaemon) 
     714        { 
     715                // we are running in the child by now, and should not return 
     716                delete mpConfiguration; 
     717                mpConfiguration = NULL; 
     718                exit(0); 
     719        } 
     720        */ 
    678721#endif 
    679          
     722 
    680723        return retcode; 
    681724} 
     
    841884// Function 
    842885//              Name:    Daemon::SetupInInitialProcess() 
    843 //              Purpose: A chance for the daemon to do something initial setting up in the process which 
    844 //                               initiates everything, and after the configuration file has been read and verified. 
     886//              Purpose: A chance for the daemon to do something initial 
     887//                       setting up in the process which initiates 
     888//                       everything, and after the configuration file has 
     889//                       been read and verified. 
    845890//              Created: 2003/08/20 
    846891// 
  • box/trunk/lib/server/Daemon.h

    r2127 r2177  
    5555        virtual const ConfigurationVerify *GetConfigVerify() const; 
    5656        virtual void Usage(); 
    57          
     57 
     58        virtual bool Configure(const std::string& rConfigFileName); 
     59 
    5860        bool StopRun() {return mReloadConfigWanted | mTerminateWanted;} 
    5961        bool IsReloadConfigWanted() {return mReloadConfigWanted;} 
     
    6466        void SetTerminateWanted() {mTerminateWanted = true;} 
    6567         
    66         virtual void SetupInInitialProcess(); 
    6768        virtual void EnterChild(); 
    6869         
     
    7071 
    7172protected: 
     73        virtual void SetupInInitialProcess(); 
    7274        box_time_t GetLoadedConfigModifiedTime() const; 
    7375        bool IsSingleProcess() { return mSingleProcess; }