Changeset 5924 for branches/newapi

Show
Ignore:
Timestamp:
12/03/07 06:17:48 (12 months ago)
Author:
erijo
Message:
  • Replaced static.cpp with a Globals class. This approach has the same advantage as static.cpp with regard to unit-tests. Globals also gives a well defined construct and destruct order of global objects.
  • Dropped the {get,set}ThreadLog? idea. It wasn't so great after all. Replaced it with Globals::getDaemonLog() to avoid having to pass around the log to all classes within the daemon.
  • Removed PluginManager::setPluginLogSink() since it was no longer needed. The Globals class makes it possible to pass the sink in the constructor.
  • Replaced PluginManager::unloadAll() with Globals::cleanup().
  • Minor refactoring of main().
  • Made some interface dtors protected.
Location:
branches/newapi/licq
Files:
2 added
1 removed
15 modified

Legend:

Unmodified
Added
Removed
  • branches/newapi/licq/licq/log.h

    r5912 r5924  
    3333{ 
    3434public: 
    35   /** 
    36    * Get the calling thread's log. Can only be called from the 
    37    * daemon's or plugin's main thread (i.e. the thread executing 
    38    * Plugin::run()). 
    39    * 
    40    * @returns The calling thread's log instance. 
    41    */ 
    42   static Log* getThreadLog(); 
    43  
    4435  enum Level 
    4536  { 
  • branches/newapi/licq/src/CMakeLists.txt

    r5909 r5924  
    11set(daemon_SRCS 
    22  daemon.cpp 
     3  globals.cpp 
    34  main.cpp 
    4   static.cpp 
    55 
    66#   backgroundcodes.c 
  • branches/newapi/licq/src/daemon.cpp

    r5921 r5924  
    1919 
    2020#include "daemon.h" 
     21#include "globals.h" 
    2122#include "licq/exception/invalidargumentexception.h" 
    2223#include "licq/exception/runtimeexception.h" 
     
    3839#include <sys/types.h> 
    3940 
     41Licq::Daemon* Licq::Daemon::getInstance() 
     42{ 
     43  return LicqDaemon::Globals::getDaemon(); 
     44} 
     45 
    4046static inline void printLicqVersion(std::ostream& os) 
    4147{ 
     
    7884 
    7985LicqDaemon::Daemon::Daemon() 
    80   : myLog(*Licq::Log::getThreadLog()) 
     86  : myLog(Globals::getDaemonLog()) 
    8187{ 
    8288  // Empty 
     
    165171  if (vm.count("plugin")) 
    166172  { 
    167     PluginManager* pluginManager = PluginManager::getInstance(); 
     173    PluginManager* pluginManager = Globals::getPluginManager(); 
    168174 
    169175    std::vector<std::string> 
     
    182188int LicqDaemon::Daemon::main() 
    183189{ 
    184   PluginManager::getInstance()->unloadAll(); 
    185190  return 1; 
    186191} 
     
    189194  std::vector< boost::shared_ptr<const Licq::Plugin::Information> >* info) 
    190195{ 
    191   PluginManager::getInstance()->getPluginInformation(info); 
     196  Globals::getPluginManager()->getPluginInformation(info); 
    192197} 
    193198 
     
    226231void LicqDaemon::Daemon::setupDefaultPluginPaths(bool reload) 
    227232{ 
    228   PluginCache* cache = PluginManager::getInstance()->getPluginCache(); 
     233  PluginCache* cache = Globals::getPluginManager()->getPluginCache(); 
    229234  cache->addSearchPath(myBaseDir + ".plugins/"); 
    230235 
     
    237242  setupDefaultPluginPaths(false); 
    238243 
    239   PluginCache* cache = PluginManager::getInstance()->getPluginCache(); 
     244  PluginCache* cache = Globals::getPluginManager()->getPluginCache(); 
    240245  for (std::vector<std::string>::const_iterator it = paths.begin(); 
    241246       it != paths.end(); ++it) 
     
    249254void LicqDaemon::Daemon::printAvailablePlugins(std::ostream& os) const 
    250255{ 
    251   PluginCache* cache = PluginManager::getInstance()->getPluginCache(); 
     256  PluginCache* cache = Globals::getPluginManager()->getPluginCache(); 
    252257 
    253258  const size_t size = cache->getSize(); 
  • branches/newapi/licq/src/main.cpp

    r5918 r5924  
    88 
    99#include "daemon.h" 
     10#include "globals.h" 
    1011#include "licq/exception/exception.h" 
    1112#include "licq/exception/invalidargumentexception.h" 
     13#include "licq/exception/runtimeexception.h" 
     14#include "licq/log.h" 
    1215#include "plugin/pluginmanager.h" 
    13 #include "util/log.h" 
    1416#include "util/logdistributor.h" 
    1517#include "util/tr.h" 
     
    2022 
    2123/* 
    22 #ifdef HAVE_CONFIG_H 
    23 #include "config.h" 
    24 #endif 
    25  
    26 #include <signal.h> 
    27 #include <cstdlib> 
    28 #include <cstring> 
    29  
    30 #include <locale> 
    31  
    3224#include "licq.h" 
    3325#include "licq_sighandler.h" 
     
    4032} 
    4133#endif 
    42  
    43 // Localization 
    44 #include "gettext.h" 
    4534*/ 
    46  
    47 #include "licq_constants.h" 
    48  
    49 // define a global variable for the base directory containing the data 
    50 // and config subdirectories 
    51 char BASE_DIR[MAX_FILENAME_LEN]; 
    52 char SHARE_DIR[MAX_FILENAME_LEN]; 
    53 char LIB_DIR[MAX_FILENAME_LEN]; 
    54 unsigned short DEBUG_LEVEL; 
    5535 
    5636int main(int argc, char** argv) 
    5737{ 
    58   // Set up logging 
    59   LicqDaemon::LogDistributor logDistributor; 
    60   LicqDaemon::Log::setThreadLog(new LicqDaemon::Log("licq", &logDistributor)); 
    61   Licq::Log& log = *Licq::Log::getThreadLog(); 
     38  int retval = EXIT_SUCCESS; 
     39 
     40  LicqDaemon::Globals globals; 
     41  Licq::Log& log = globals.getDaemonLog(); 
    6242 
    6343  // Enable logging to stderr 
    6444  LicqDaemon::StreamLogSink streamLogSink(std::cerr); 
    65   logDistributor.registerSink(&streamLogSink); 
     45  globals.getLogDistributor()->registerSink(&streamLogSink); 
    6646  if (!isatty(STDERR_FILENO)) 
    6747    streamLogSink.setUseColors(false); 
    6848 
    69   // Configure PluginManager 
    70   LicqDaemon::PluginManager::getInstance()->setPluginLogSink(&logDistributor); 
    71  
    72   // Fail if argv[0] isn't set 
    73   if (argc < 1) 
    74   { 
    75     log.fatal() << tr("Licq requires argv[0] to be set"); 
    76     exit(EXIT_FAILURE); 
    77   } 
    78  
    79   int retval = 0; 
    8049  try 
    8150  { 
    8251    LicqDaemon::setupLocalization(); 
    8352 
    84     LicqDaemon::Daemon* daemon = LicqDaemon::Daemon::getInstance(); 
     53    if (argc < 1) 
     54      throw Licq::RuntimeException(tr("Licq requires argv[0] to be set")); 
     55 
     56    LicqDaemon::Daemon* daemon = globals.getDaemon(); 
    8557    daemon->installSignalHandlers(); 
    8658 
     59    bool continueAfterInit = false; 
    8760    try 
    8861    { 
    89       if (!daemon->init(argc, argv, streamLogSink)) 
    90         exit(EXIT_SUCCESS); 
     62      continueAfterInit = daemon->init(argc, argv, streamLogSink); 
     63      if (!continueAfterInit) 
     64        retval = EXIT_SUCCESS; 
    9165    } 
    9266    catch (const Licq::InvalidArgumentException& ex) 
     
    9468      std::cerr << ex << "\n"; 
    9569      std::cerr << tr("Try `%1% --help' for more information.") % argv[0] << "\n"; 
    96       exit(EXIT_FAILURE); 
     70      retval = EXIT_FAILURE; 
    9771    } 
    9872 
    99     retval = daemon->main(); 
     73    if (continueAfterInit) 
     74      retval = daemon->main(); 
    10075  } 
    10176  catch (const Licq::Exception& ex) 
    10277  { 
    10378    log.fatal() << ex; 
    104     exit(EXIT_FAILURE); 
     79    retval = EXIT_FAILURE; 
    10580  } 
    10681  catch (const std::exception& ex) 
    10782  { 
    10883    log.fatal() << tr("Caught std::exception: %1%") % ex.what(); 
    109     exit(EXIT_FAILURE); 
     84    retval = EXIT_FAILURE; 
    11085  } 
    11186  catch (...) 
    11287  { 
    11388    log.fatal() << tr("Caught unknown exception"); 
    114     exit(EXIT_FAILURE); 
     89    throw; 
    11590  } 
    11691 
    117   if (retval != 0) 
     92  globals.cleanup(); 
     93 
     94  if (retval != EXIT_SUCCESS) 
    11895    log.error(tr("Licq finished with exit code %1%") % retval); 
    11996 
    120   logDistributor.unregisterSink(&streamLogSink); 
     97  globals.getLogDistributor()->unregisterSink(&streamLogSink); 
    12198  return retval; 
    12299 
    123100  /* 
    124 #if ENABLE_NLS 
    125   // prepare daemon localization 
    126   setlocale(LC_MESSAGES,""); 
    127   setlocale(LC_CTYPE,""); 
    128   bindtextdomain(PACKAGE, LOCALEDIR); 
    129   textdomain(PACKAGE); 
    130 #endif 
    131  
    132 // Make sure argv[0] is defined otherwise licq will crash if it is NULL 
    133   if (argv[0] == NULL) 
    134     argv[0] = strdup("licq"); 
    135101#ifdef USE_SOCKS5 
    136102   SOCKSinit(argv[0]); 
  • branches/newapi/licq/src/plugin/plugincacheimpl.cpp

    r5913 r5924  
    1818 */ 
    1919 
     20#include "globals.h" 
    2021#include "licq/exception/invalidargumentexception.h" 
    2122#include "licq/exception/runtimeexception.h" 
     
    117118LicqDaemon::PluginCacheImpl::PluginCacheImpl(PluginLoader& loader, 
    118119                                             bool testMode) 
    119   : myLog(*Licq::Log::getThreadLog()), 
     120  : myLog(Globals::getDaemonLog()), 
    120121    myLoader(loader), 
    121122    myInTestMode(testMode) 
  • branches/newapi/licq/src/plugin/plugininstanceimpl.cpp

    r5921 r5924  
    5757{ 
    5858  delete myThread; 
     59 
    5960  myLibrary->destroyPlugin(myPlugin); 
     61  myPlugin = NULL; 
    6062 
    6163  delete static_cast<Log*>(mySetup.log); 
  • branches/newapi/licq/src/plugin/pluginlibrary.h

    r5921 r5924  
    3333{ 
    3434public: 
    35   virtual ~PluginLibrary() {} 
    36  
    3735  virtual unsigned int getPluginAbiVersion() = 0; 
    3836  virtual void getPluginInformation(Licq::Plugin::Information* info) = 0; 
    3937  virtual Licq::Plugin* createPlugin(const Licq::Plugin::Setup& setup) = 0; 
    4038  virtual void destroyPlugin(Licq::Plugin* plugin) = 0; 
     39 
     40protected: 
     41  virtual ~PluginLibrary() { /* Empty */ } 
    4142}; 
    4243 
  • branches/newapi/licq/src/plugin/pluginloader.h

    r5362 r5924  
    3535{ 
    3636public: 
    37   virtual ~PluginLoader() {} 
     37  virtual boost::shared_ptr<PluginLibrary> load(const std::string& filename) = 0; 
    3838 
    39   virtual boost::shared_ptr<PluginLibrary> load(const std::string& filename) = 0; 
     39protected: 
     40  virtual ~PluginLoader() { /* Empty */ } 
    4041}; 
    4142 
  • branches/newapi/licq/src/plugin/pluginmanager.h

    r5921 r5924  
    2727#include <vector> 
    2828 
    29 namespace Licq 
    30 { 
    31 class LogSink; 
    32 } 
    33  
    3429namespace LicqDaemon 
    3530{ 
     
    4439{ 
    4540public: 
    46   /** 
    47    * @returns The PluginManager singleton. 
    48    */ 
    49   static PluginManager* getInstance(); 
    50  
    51   virtual void unloadAll() = 0; 
    52  
    53   virtual void setPluginLogSink(Licq::LogSink* sink) = 0; 
    54  
    5541  virtual void getPluginInformation( 
    5642    std::vector< boost::shared_ptr<const Licq::Plugin::Information> >* info) = 0; 
  • branches/newapi/licq/src/plugin/pluginmanagerimpl.cpp

    r5921 r5924  
    2222#include "plugin/pluginmanagerimpl.h" 
    2323 
    24 LicqDaemon::PluginManagerImpl::PluginManagerImpl(PluginCache& cache) 
     24LicqDaemon::PluginManagerImpl::PluginManagerImpl(PluginCache& cache, 
     25                                                 Licq::LogSink& sink) 
    2526  : myCache(cache), 
    26     myPluginLogSink(NULL) 
     27    myPluginLogSink(sink) 
    2728{ 
    2829  // Empty 
     
    3233{ 
    3334  // Empty 
    34 } 
    35  
    36 void LicqDaemon::PluginManagerImpl::unloadAll() 
    37 { 
    38   myPlugins.clear(); 
    39   myCache.clear(); 
    40   myCache.unloadUnusedLibraries(); 
    41 } 
    42  
    43 void LicqDaemon::PluginManagerImpl::setPluginLogSink(Licq::LogSink* sink) 
    44 { 
    45   assert(myPluginLogSink == NULL); 
    46   myPluginLogSink = sink; 
    4735} 
    4836 
     
    6149loadPlugin(const std::string& filename, int argc, char** argv) 
    6250{ 
    63   assert(myPluginLogSink != NULL); 
    64  
    6551  const size_t index = myCache.getIndex(filename); 
    6652 
     
    7157 
    7258  boost::shared_ptr<PluginInstance> 
    73       instance(new PluginInstanceImpl(info, lib, *myPluginLogSink)); 
     59      instance(new PluginInstanceImpl(info, lib, myPluginLogSink)); 
    7460 
    7561  Licq::Plugin::Id id = instance->load(argc, argv); 
  • branches/newapi/licq/src/plugin/pluginmanagerimpl.h

    r5921 r5924  
    2525#include <map> 
    2626 
     27namespace Licq 
     28{ 
     29class LogSink; 
     30} 
     31 
    2732namespace LicqDaemon 
    2833{ 
     
    3540  /** 
    3641   * @param cache PluginCache to use. 
     42   * @param sink LogSink that plugins should use. 
    3743   */ 
    38   explicit PluginManagerImpl(PluginCache& cache); 
     44  PluginManagerImpl(PluginCache& cache, Licq::LogSink& sink); 
    3945 
    4046  ~PluginManagerImpl(); 
    41  
    42   void unloadAll(); 
    43  
    44   /** 
    45    * Set the LogSink that plugins will log to. This must have been 
    46    * called before the first call to loadPlugin(). 
    47    */ 
    48   void setPluginLogSink(Licq::LogSink* sink); 
    4947 
    5048  void getPluginInformation( 
     
    6159private: 
    6260  PluginCache& myCache; 
    63   Licq::LogSink* myPluginLogSink; 
     61  Licq::LogSink& myPluginLogSink; 
    6462   
    6563  typedef std::map<Licq::Plugin::Id, 
  • branches/newapi/licq/src/plugin/test/pluginlibrarytest.cpp

    r5921 r5924  
    1818 */ 
    1919 
     20#include "globals.h" 
    2021#include "licq/log.h" 
    2122#include "plugin/pluginlibraryimpl.h" 
     
    108109 
    109110  Licq::Plugin::Setup setup; 
    110   setup.log = Licq::Log::getThreadLog(); 
     111  setup.log = &LicqDaemon::Globals::getDaemonLog(); 
    111112  setup.argc = 1; 
    112113  setup.argv = (char**)argv; 
  • branches/newapi/licq/src/test/log.cpp

    r5906 r5924  
    1818 */ 
    1919 
     20#include "globals.h" 
    2021#include "test/log.h" 
    2122 
    22 Licq::Log* Licq::Log::getThreadLog() 
     23Licq::Log& LicqDaemon::Globals::getDaemonLog() 
    2324{ 
    2425  static LicqTest::Log testLog; 
    25   return &testLog; 
     26  return testLog; 
    2627} 
  • branches/newapi/licq/src/test/log.h

    r5911 r5924  
    3030/** 
    3131 * A log for unit-tests that prints to std::clog. The single instance 
    32  * of this class is returned from Licq::Log::getThreadLog(). 
     32 * of this class is returned from LicqDaemon::Globals::getDaemonLog(). 
    3333 */ 
    3434class Log : public Licq::Log 
     
    4949  ~Log() { /* Empty */ } 
    5050 
    51   friend class Licq::Log; 
     51  friend class LicqDaemon::Globals; 
    5252}; 
    5353 
  • branches/newapi/licq/src/util/log.h

    r5911 r5924  
    4444  Log(const std::string& owner, Licq::LogSink* sink); 
    4545 
    46   /** 
    47    * Makes @a log the default log for the calling thread. @a log will 
    48    * be automatically deleted when the thread exits. This method may 
    49    * only be called once per thread. 
    50    * 
    51    * @see getThreadLog() 
    52    */ 
    53   static void setThreadLog(const Log* log); 
    54  
    5546  // From Licq::Log 
    5647  void log(Level level, const std::string& msg);