- make SourceManager log
authorMichael Andres <ma@suse.de>
Fri, 10 Mar 2006 23:56:09 +0000 (23:56 +0000)
committerMichael Andres <ma@suse.de>
Fri, 10 Mar 2006 23:56:09 +0000 (23:56 +0000)
zypp/SourceManager.cc
zypp/source/SourceImpl.cc

index cf5bed7..1183997 100644 (file)
@@ -13,6 +13,7 @@
 #include <map>
 
 #include "zypp/base/Logger.h"
+#include "zypp/base/Algorithm.h"
 #include "zypp/base/Gettext.h"
 
 #include "zypp/ZYpp.h"
 #include "zypp/Pathname.h"
 #include "zypp/PathInfo.h"
 
+///////////////////////////////////////////////////////////////////
+#undef  ZYPP_BASE_LOGGER_LOGGROUP
+#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::SourceManager"
+///////////////////////////////////////////////////////////////////
+
 #define ZYPP_METADATA_PREFIX ( getZYpp()->homePath().asString()+"/cache/" )
 
 using std::endl;
@@ -47,6 +53,57 @@ namespace zypp
 
     static SourceMap _sources;
     static SourceMap _deleted_sources;
+
+    struct PrintSourceMapEntry
+    {
+      void operator()( const SourceMap::value_type & el ) const
+      {
+        _str << endl << "    - " << el.second;
+      }
+      PrintSourceMapEntry( std::ostream & str )
+      : _str( str )
+      {}
+      std::ostream & _str;
+    };
+
+    inline std::ostream & dumpSourceTableOn( std::ostream & str, bool trailingENDL = true )
+    {
+      str << "SourceManager: =========================" << endl
+          << "  known Sources " << _sources.size();
+      std::for_each( _sources.begin(), _sources.end(), PrintSourceMapEntry(str) );
+
+      str << endl
+          << "  deleted Sources " << _deleted_sources.size();
+      std::for_each( _deleted_sources.begin(), _deleted_sources.end(), PrintSourceMapEntry(str) );
+      str << endl
+          << "========================================";
+      if ( trailingENDL )
+        str << endl;
+      return str;
+    }
+
+    inline bool sourceTableRemove( SourceMap::iterator it )
+    {
+      if ( it == _sources.end() )
+        return false;
+
+      MIL << "SourceManager remove " << it->second << endl;
+      _deleted_sources[it->second.numericId()] = it->second;
+      _sources.erase(it);
+
+      dumpSourceTableOn( DBG );
+      return true;
+    }
+
+    inline SourceManager::SourceId sourceTableAdd( Source_Ref source_r )
+    {
+      MIL << "SourceManager add " << source_r << endl;
+      _sources[source_r.numericId()] = source_r;
+
+      dumpSourceTableOn( DBG );
+      return source_r.numericId();
+    }
+
   }
 
   ///////////////////////////////////////////////////////////////////
@@ -55,7 +112,9 @@ namespace zypp
   //   METHOD TYPE : Ctor
   //
   SourceManager::SourceManager()
-  {}
+  {
+    MIL << "Created SourceManager Singleton." << endl;
+  }
 
   ///////////////////////////////////////////////////////////////////
   //
@@ -63,79 +122,81 @@ namespace zypp
   //   METHOD TYPE : Dtor
   //
   SourceManager::~SourceManager()
-  {}
+  {
+    MIL << "Deleted SourceManager Singleton." << endl;
+  }
 
   void SourceManager::reset()
   {
-    MIL << "Cleaning up all sources" << endl;
-
+    MIL << "SourceManager reset (forget all sources)" << endl;
     _sources.clear();
     _deleted_sources.clear();
+  }
 
-    // don't change the _next_id to avoid using
-    // the same ID for dangling old sources and newly introduced sources
+  SourceManager::SourceId SourceManager::addSource( Source_Ref source_r )
+  {
+    return sourceTableAdd( source_r );
   }
 
+  /** \deprecate SourceManager should operate on built sources. */
   SourceManager::SourceId SourceManager::addSource(const Url & url_r, const Pathname & path_r, const std::string & alias_r, const Pathname & cache_dir_r)
   {
+    MIL << "SourceManager create and add Source( Url:" << url_r << ", Path:" << path_r << ", Alias:" <<  alias_r << ", Cache:" << cache_dir_r << endl;
     Source_Ref src( SourceFactory().createFrom(url_r, path_r, alias_r, cache_dir_r) );
-    _sources[src.numericId()] = src;
-    return src.numericId();
+    return sourceTableAdd( src );
   }
 
-  SourceManager::SourceId SourceManager::addSource(Source_Ref source_r)
+  void SourceManager::removeSource(SourceManager::SourceId id)
   {
-    _sources[source_r.numericId()] = source_r;
-    return source_r.numericId();
+    if ( ! sourceTableRemove( _sources.find(id) ) )
+      {
+        WAR << "SourceManager remove: no source with SourceId " << id << endl;
+      }
   }
 
-  void SourceManager::removeSource(SourceManager::SourceId id)
+  void SourceManager::removeSource( const std::string & alias_r )
   {
-    SourceMap::iterator it = _sources.find(id);
-    if (it != _sources.end())
-    {
-      _deleted_sources[id] = it->second;
-      _sources.erase(it);
-    }
+    SourceMap::iterator it = _sources.begin();
+    for ( ; it != _sources.end() && it->second.alias() != alias_r; ++it )
+      ; // empty body
+
+    if ( ! sourceTableRemove( it ) )
+      {
+        WAR << "SourceManager remove: no source with alias " << alias_r << endl;
+      }
   }
 
   void SourceManager::releaseAllSources()
   {
+    MIL << "SourceManager releasing all sources ..." << endl;
     for (SourceMap::iterator it = _sources.begin();
         it != _sources.end(); it++)
     {
       it->second.release();
     }
+    MIL << "SourceManager releasing all sources done." << endl;
   }
 
   void SourceManager::reattachSources(const Pathname &attach_point)
   {
+    MIL << "SourceManager reattach all sources to '" << attach_point << " ..." << endl;
     for (SourceMap::iterator it = _sources.begin();
         it != _sources.end(); it++)
     {
       it->second.reattach(attach_point);
     }
+    MIL << "SourceManager reattach all sources to '" << attach_point << " done." << endl;
   }
 
 
-  void SourceManager::removeSource(const std::string & alias_r)
-  {
-    for (SourceMap::iterator it = _sources.begin(); it != _sources.end(); ++it)
-    {
-       if (it->second.alias() == alias_r) {
-            _deleted_sources[it->first] = it->second;
-           _sources.erase(it);
-           break;
-       }
-    }
-  }
-
   void SourceManager::disableAllSources()
   {
+    MIL << "SourceManager disable all sources ..." << endl;
     for( SourceMap::iterator it = _sources.begin(); it != _sources.end(); it++)
     {
        it->second.disable();
     }
+    MIL << "SourceManager disable all sources done." << endl;
   }
 
   std::list<SourceManager::SourceId> SourceManager::enabledSources() const
@@ -165,6 +226,9 @@ namespace zypp
 
   void SourceManager::store(Pathname root_r, bool metadata_cache )
   {
+    MIL << "SourceManager store '" << root_r << ( metadata_cache ? "' (metadata_cache)" : "'" )
+        << " ..." << endl;
+
     storage::PersistentStorage store;
     store.init( root_r );
 
@@ -253,13 +317,18 @@ namespace zypp
     }
 
     _deleted_sources.clear();
+
+    MIL << "SourceManager store done." << endl;
   }
 
   /** \todo Broken design: either use return value or Exception to
   * indicate errors, not both.
   */
-  bool SourceManager::restore(Pathname root_r, bool use_caches )
+  bool SourceManager::restore( Pathname root_r, bool use_caches )
   {
+    MIL << "SourceManager restore '" << root_r << ( use_caches ? "' (use_caches)" : "'" )
+        << " ..." << endl;
+
     if (! _sources.empty() )
        ZYPP_THROW(Exception ( N_("At least one source already registered, cannot restore sources from persistent store.") ) );
 
@@ -306,6 +375,9 @@ namespace zypp
     {
        ZYPP_THROW(report);
     }
+
+    MIL << "SourceManager restore done." << endl;
+    dumpSourceTableOn( DBG );
     return true;
   }
 
@@ -316,7 +388,7 @@ namespace zypp
   */
   std::ostream & operator<<( std::ostream & str, const SourceManager & obj )
   {
-    return str << "Source Manager has " << " sources" << endl;
+    return dumpSourceTableOn( str, /*tailingENDL*/false );
   }
 
   Source_Ref SourceManager::findSource(SourceId id)
index 2e7a1cd..c51a321 100644 (file)
@@ -88,7 +88,7 @@ namespace zypp
       if (_media_set) {
        media::MediaAccessId _media = _media_set->getMediaAccessId( 1 );
        media_mgr.release (_media, false);
-      } 
+      }
     }
 
     const ResStore & SourceImpl::resolvables() const
@@ -118,7 +118,7 @@ namespace zypp
       media::MediaAccessId _media = _media_set->getMediaAccessId( media_nr );
       media_mgr.dirInfo( _media, retlist, path_r, dots );
     }
-    
+
     const Pathname SourceImpl::provideFile(const Pathname & file_r,
                                           const unsigned media_nr,
                                           bool cached,
@@ -133,7 +133,7 @@ namespace zypp
       do {
         try {
          DBG << "Going to try provide file " << file_r << " from " << media_nr << endl;
-         
+
          // try to attach the media
          _media = _media_set->getMediaAccessId( media_nr ); // in case of redirect
          media_mgr.provideFile (_media, file_r, cached, checkonly);
@@ -165,7 +165,7 @@ namespace zypp
              );
 
            DBG << "ProvideFile exception caught, callback answer: " << user << endl;
-         
+
            if( user == media::MediaChangeReport::ABORT )
            {
              DBG << "Aborting" << endl;
@@ -183,7 +183,7 @@ namespace zypp
            {
              // retry
              DBG << "Going to attach again" << endl;
-           
+
              media_mgr.attach( _media );
 
              break;
@@ -243,7 +243,7 @@ namespace zypp
 
     void SourceImpl::storeMetadata(const Pathname & cache_dir_r)
     {}
-    
+
     void SourceImpl::refresh()
     {
        // TODO: will this work in chroot?
@@ -270,7 +270,7 @@ namespace zypp
 
     media::MediaVerifierRef SourceImpl::verifier(unsigned media_nr)
     { return media::MediaVerifierRef(new media::NoVerifier()); }
-    
+
     /////////////////////////////////////////////////////////////////
     // attribute accessors
 
@@ -295,7 +295,7 @@ namespace zypp
     void SourceImpl::setPriorityUnsubscribed (unsigned p)
     { _priority_unsubscribed = p; }
 
-    const Pathname & SourceImpl::cacheDir (void) 
+    const Pathname & SourceImpl::cacheDir (void)
     { return _cache_dir; }
 
     Url SourceImpl::url (void) const
@@ -330,7 +330,7 @@ namespace zypp
 
     const std::list<Pathname> SourceImpl::publicKeys()
     { return std::list<Pathname>(); }
-    
+
     std::string SourceImpl::unique_id (void) const
     { return ""; }
 
@@ -353,9 +353,21 @@ namespace zypp
 
     /////////////////////////////////////////////////////////////////
 
-
     std::ostream & SourceImpl::dumpOn( std::ostream & str ) const
-    { return str << (_alias.empty() ? "SourceImpl" : _alias); }
+    {
+      str << "Source[" << numericId() << "|" << type();
+      if ( !_alias.empty() )
+        str << "|" << _alias;
+      str << "]";
+
+      str << "{"
+          << _url << "(" << _path << ")";
+      if ( ! _cache_dir.empty() )
+        str << "; cache " << _cache_dir;
+      str << "}";
+
+      return str;
+    }
 
     SourceImpl::Verifier::Verifier(const std::string & vendor_r, const std::string & id_r, const media::MediaNr media_nr)
     : _media_vendor(vendor_r)