- new History Log, first version (fate #110205)
authorJan Kupec <jkupec@suse.cz>
Wed, 24 Sep 2008 14:16:56 +0000 (14:16 +0000)
committerJan Kupec <jkupec@suse.cz>
Wed, 24 Sep 2008 14:16:56 +0000 (14:16 +0000)
- todo: HistoryFileReader API
- todo: log @System.solv changes (post 11.1 maybe?)

13 files changed:
zypp.conf
zypp/CMakeLists.txt
zypp/HistoryLog.cc [new file with mode: 0644]
zypp/HistoryLog.h [new file with mode: 0644]
zypp/RepoManager.cc
zypp/Target.cc
zypp/ZConfig.cc
zypp/ZConfig.h
zypp/target/CommitLog.cc [deleted file]
zypp/target/CommitLog.h [deleted file]
zypp/target/TargetImpl.cc
zypp/target/TargetImpl.h
zypp/target/rpm/RpmDb.cc

index 33a075f..a712aa8 100644 (file)
--- a/zypp.conf
+++ b/zypp.conf
 ## Default value: no
 ##
 # rpm.install.excludedocs = no
+
+##
+## Location of history log file.
+##
+## The history log is described at
+## http://en.opensuse.org/Libzypp/Package_History
+##
+## Valid values: absolute path to a file
+## Default value: /var/log/zypp/history
+##
+# history.logfile = /var/log/zypp/history
index 106b78a..5002e27 100644 (file)
@@ -24,6 +24,7 @@ SET( zypp_SRCS
   ExternalProgram.cc
   Fetcher.cc
   FileChecker.cc
+  HistoryLog.cc
   IdString.cc
   KeyRing.cc
   Locks.cc
@@ -102,6 +103,7 @@ SET( zypp_HEADERS
   ExternalProgram.h
   Fetcher.h
   FileChecker.h
+  HistoryLog.h
   IdString.h
   IdStringType.h
   KeyContext.h
@@ -530,7 +532,6 @@ SET( zypp_target_SRCS
   target/RequestedLocalesFile.cc
   target/SoftLocksFile.cc
   target/HardLocksFile.cc
-  target/CommitLog.cc
   target/CommitPackageCache.cc
   target/CommitPackageCacheImpl.cc
   target/CommitPackageCacheReadAhead.cc
@@ -543,7 +544,6 @@ SET( zypp_target_HEADERS
   target/RequestedLocalesFile.h
   target/SoftLocksFile.h
   target/HardLocksFile.h
-  target/CommitLog.h
   target/CommitPackageCache.h
   target/CommitPackageCacheImpl.h
   target/CommitPackageCacheReadAhead.h
diff --git a/zypp/HistoryLog.cc b/zypp/HistoryLog.cc
new file mode 100644 (file)
index 0000000..73a337c
--- /dev/null
@@ -0,0 +1,335 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file zypp/target/HistoryLog.cc
+ *
+ */
+#include <iostream>
+#include <fstream>
+#include <unistd.h>
+
+#include "zypp/ZConfig.h"
+#include "zypp/base/String.h"
+#include "zypp/base/Logger.h"
+
+#include "zypp/PathInfo.h"
+#include "zypp/Date.h"
+
+#include "zypp/PoolItem.h"
+#include "zypp/Package.h"
+#include "zypp/RepoInfo.h"
+
+#include "zypp/HistoryLog.h"
+
+using std::endl;
+using std::string;
+
+namespace
+{
+  inline string timestamp()
+  { return zypp::Date::now().form( "%Y-%m-%d %H:%M:%S" ); }
+
+  inline string userAtHostname()
+  {
+    static char buf[256];
+    string result;
+    char * tmp = ::cuserid(buf); 
+    if (tmp)
+    {
+      result = string(tmp);
+      if (!::gethostname(buf, 255))
+        result += "@" + string(buf);
+    }
+    return result;
+  }
+}
+
+namespace zypp
+{
+  ///////////////////////////////////////////////////////////////////
+  //
+  //    CLASS NAME : HistoryActionID
+  //
+  ///////////////////////////////////////////////////////////////////
+
+  static std::map<std::string,HistoryActionID::ID> _table;
+
+  const HistoryActionID HistoryActionID::NONE(HistoryActionID::NONE_e);
+  const HistoryActionID HistoryActionID::INSTALL(HistoryActionID::INSTALL_e);
+  const HistoryActionID HistoryActionID::REMOVE(HistoryActionID::REMOVE_e);
+  const HistoryActionID HistoryActionID::REPO_ADD(HistoryActionID::REPO_ADD_e);
+  const HistoryActionID HistoryActionID::REPO_REMOVE(HistoryActionID::REPO_REMOVE_e);
+  const HistoryActionID HistoryActionID::REPO_CHANGE_ALIAS(HistoryActionID::REPO_CHANGE_ALIAS_e);
+  const HistoryActionID HistoryActionID::REPO_CHANGE_URL(HistoryActionID::REPO_CHANGE_URL_e);
+
+  HistoryActionID::HistoryActionID(const std::string & strval_r)
+    : _id(parse(strval_r))
+  {}
+
+  HistoryActionID::ID HistoryActionID::parse(const std::string & strval_r)
+  {
+    if (_table.empty())
+    {
+      // initialize it
+      _table["install"] = INSTALL_e;
+      _table["remove"]  = REMOVE_e;
+      _table["radd"]    = REPO_ADD_e;
+      _table["rremove"] = REPO_REMOVE_e;
+      _table["ralias"]  = REPO_CHANGE_ALIAS_e;
+      _table["rurl"]    = REPO_CHANGE_URL_e;
+      _table["NONE"] = _table["none"] = HistoryActionID::NONE_e;
+    }
+
+    std::map<std::string,HistoryActionID::ID>::const_iterator it =
+      _table.find(strval_r);
+
+    if (it == _table.end())
+      WAR << "Unknown history action ID '" + strval_r + "'";
+
+    return it->second;
+  }
+
+
+  const std::string & HistoryActionID::asString(bool pad) const
+  {
+    static std::map<ID, std::string> _table;
+    if ( _table.empty() )
+    {
+      // initialize it
+      _table[INSTALL_e]           = "install";
+      _table[REMOVE_e]            = "remove";
+      _table[REPO_ADD_e]          = "radd";
+      _table[REPO_REMOVE_e]       = "rremove";
+      _table[REPO_CHANGE_ALIAS_e] = "ralias";
+      _table[REPO_CHANGE_URL_e]   = "rurl";
+      _table[NONE_e]              = "NONE";
+    }
+    // add spaces so that the size of the returned string is always 7 (for now)
+    if (pad)
+      return _table[_id].append(7 - _table[_id].size(), ' ');
+    return _table[_id];
+  }
+
+  std::ostream & operator << (std::ostream & str, const HistoryActionID & id)
+  { return str << id.asString(); }
+
+  ///////////////////////////////////////////////////////////////////
+
+  ///////////////////////////////////////////////////////////////////
+  //
+  //    CLASS NAME : HistoryLog
+  //
+  ///////////////////////////////////////////////////////////////////
+
+    Pathname HistoryLog::_fname(ZConfig::instance().historyLogFile());
+    std::ofstream HistoryLog::_log;
+    unsigned HistoryLog::_refcnt = 0;
+    const char HistoryLog::_sep = '|';
+
+    ///////////////////////////////////////////////////////////////////
+
+    HistoryLog::HistoryLog( const Pathname & rootdir )
+    {
+      refUp();
+      if (!rootdir.empty() && rootdir.absolute())
+        _fname = rootdir / ZConfig::instance().historyLogFile();
+    }
+
+    void HistoryLog::openLog()
+    {
+      if ( !_fname.empty() )
+      {
+        _log.clear();
+        _log.open( _fname.asString().c_str(), std::ios::out|std::ios::app );
+        if( !_log )
+          ERR << "Could not open logfile '" << _fname << "'" << endl;
+      }
+    }
+
+    void HistoryLog::closeLog()
+    {
+      _log.clear();
+      _log.close();
+    }
+
+    void HistoryLog::refUp()
+    {
+      if ( !_refcnt )
+        openLog();
+      ++_refcnt;
+    }
+
+    void HistoryLog::refDown()
+    {
+      --_refcnt;
+      if ( !_refcnt )
+        closeLog();
+    }
+
+
+    void HistoryLog::setRoot( const Pathname & rootdir )
+    {
+      if (rootdir.empty() || !rootdir.absolute())
+        return;
+
+      if ( _refcnt )
+        closeLog();
+
+      _fname = rootdir / "/var/log/zypp/history";
+      filesystem::assert_dir( _fname.dirname() );
+      MIL << "installation log file " << _fname << endl;
+
+      if ( _refcnt )
+        openLog();
+    }
+
+
+    const Pathname & HistoryLog::fname()
+    { return _fname; }
+
+    /////////////////////////////////////////////////////////////////////////
+
+    void HistoryLog::comment( const string & comment, bool timestamp )
+    {
+      if (comment.empty())
+        return;
+
+      _log << "# ";
+      if ( timestamp )
+        _log << ::timestamp() << " ";
+
+      const char * s = comment.c_str();
+      const char * c = s;
+      unsigned size = comment.size();
+
+      // ignore the last newline
+      if (comment[size-1] == '\n')
+        --size;
+
+      for ( unsigned i = 0; i < size; ++i, ++c )
+        if ( *c == '\n' )
+        {
+          _log << string( s, c + 1 - s ) << "# ";
+          s = c + 1;
+        }
+
+      if ( s < c )
+        _log << std::string( s, c-s );
+
+      _log << endl;
+    }
+
+    /////////////////////////////////////////////////////////////////////////
+    
+    void HistoryLog::install( const PoolItem & pi )
+    {
+      const Package::constPtr p = asKind<Package>(pi.resolvable());
+      if (!p)
+        return;
+
+      _log
+        << timestamp()                                   // 1 timestamp
+        << _sep << HistoryActionID::INSTALL.asString(true) // 2 action
+        << _sep << p->name()                             // 3 name
+        << _sep << p->edition()                          // 4 evr
+        << _sep << p->arch();                            // 5 arch
+
+      if (pi.status().isByUser())
+        _log << _sep << userAtHostname();                // 6 reqested by
+      //else if (pi.status().isByApplHigh() || pi.status().isByApplLow())
+      //  _log << _sep << "appl";
+      else
+        _log << _sep;
+
+      _log
+        << _sep << p->repoInfo().alias()                 // 7 repo alias
+        << _sep << p->checksum().checksum();             // 8 checksum
+
+      _log << endl; 
+
+      //_log << pi << endl;
+    }
+
+
+    void HistoryLog::remove( const PoolItem & pi )
+    {
+      const Package::constPtr p = asKind<Package>(pi.resolvable());
+      if (!p)
+        return;
+
+      _log
+        << timestamp()                                   // 1 timestamp
+        << _sep << HistoryActionID::REMOVE.asString(true) // 2 action
+        << _sep << p->name()                             // 3 name
+        << _sep << p->edition()                          // 4 evr
+        << _sep << p->arch();                            // 5 arch
+
+      if (pi.status().isByUser())
+        _log << _sep << userAtHostname();                // 6 reqested by
+      //else if (pi.status().isByApplHigh() || pi.status().isByApplLow())
+      //  _log << _sep << "appl";
+      else
+        _log << _sep;
+
+      // we don't have checksum in rpm db
+      //  << _sep << p->checksum().checksum();           // x checksum
+
+      _log << endl; 
+
+      //_log << pi << endl;
+    }
+
+    /////////////////////////////////////////////////////////////////////////
+
+    void HistoryLog::addRepository(const RepoInfo & repo)
+    {
+      _log
+        << timestamp()                                   // 1 timestamp
+        << _sep << HistoryActionID::REPO_ADD.asString(true) // 2 action 
+        << _sep << repo.alias()                          // 3 alias
+        // what about the rest of the URLs??
+        << _sep << *repo.baseUrlsBegin()                 // 4 primary URL
+        << endl;
+    }
+
+
+    void HistoryLog::removeRepository(const RepoInfo & repo)
+    {
+      _log
+        << timestamp()                                   // 1 timestamp
+        << _sep << HistoryActionID::REPO_REMOVE.asString(true) // 2 action 
+        << _sep << repo.alias()                          // 3 alias
+        << endl;
+    }
+
+
+    void HistoryLog::modifyRepository(
+        const RepoInfo & oldrepo, const RepoInfo & newrepo)
+    {
+      if (oldrepo.alias() != newrepo.alias())
+      {
+        _log
+          << timestamp()                                    // 1 timestamp
+          << _sep << HistoryActionID::REPO_CHANGE_ALIAS.asString(true) // 2 action
+          << _sep << oldrepo.alias()                        // 3 old alias
+          << _sep << newrepo.alias();                       // 4 new alias
+      }
+      
+      if (*oldrepo.baseUrlsBegin() != *newrepo.baseUrlsBegin())
+      {
+        _log
+          << timestamp()                                    // 1 timestamp
+          << _sep << HistoryActionID::REPO_CHANGE_URL.asString(true) // 2 action
+          << _sep << oldrepo.alias()                        // 3 old url
+          << _sep << newrepo.alias();                       // 4 new url
+      }
+    }
+
+    ///////////////////////////////////////////////////////////////////
+
+} // namespace zypp
diff --git a/zypp/HistoryLog.h b/zypp/HistoryLog.h
new file mode 100644 (file)
index 0000000..a70bb16
--- /dev/null
@@ -0,0 +1,129 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file zypp/target/HistoryLog.h
+ *
+ */
+#ifndef ZYPP_TARGET_COMMITLOG_H
+#define ZYPP_TARGET_COMMITLOG_H
+
+#include <iosfwd>
+
+#include "zypp/Pathname.h"
+
+namespace zypp
+{
+  class PoolItem;
+  class RepoInfo;
+  
+  struct HistoryActionID
+  {
+    static const HistoryActionID NONE;
+
+    static const HistoryActionID INSTALL;
+    static const HistoryActionID REMOVE;
+    static const HistoryActionID REPO_ADD;
+    static const HistoryActionID REPO_REMOVE;
+    static const HistoryActionID REPO_CHANGE_ALIAS;
+    static const HistoryActionID REPO_CHANGE_URL;
+
+    enum ID
+    {
+      NONE_e,
+
+      INSTALL_e,
+      REMOVE_e,
+      REPO_ADD_e,
+      REPO_REMOVE_e,
+      REPO_CHANGE_ALIAS_e,
+      REPO_CHANGE_URL_e
+    };
+
+    HistoryActionID() : _id(NONE_e) {}
+
+    HistoryActionID(ID id) : _id(id) {}
+
+    explicit HistoryActionID(const std::string & strval_r);
+
+    ID toEnum() const { return _id; }
+
+    HistoryActionID::ID parse(const std::string & strval_r);
+
+    const std::string & asString(bool pad = false) const;
+
+    ID _id;
+  };
+
+  /** \relates HistoryActionID */
+  std::ostream & operator << (std::ostream & str, const HistoryActionID & id);
+
+    ///////////////////////////////////////////////////////////////////
+    //
+    // CLASS NAME : HistoryLog
+    /**
+     * Simple wrapper for progress log. Refcnt, filename and corresponding
+     * ofstream are static members. Logfile constructor raises, destructor
+     * lowers refcounter. On refcounter changing from 0->1, file is opened.
+     * Changing from 1->0 the file is closed. Thus Logfile objects should be
+     * local to those functions, writing the log, and must not be stored
+     * permanently.
+     *
+     * Usage:
+     * <code>
+     *  some method ()
+     *  {
+     *    PoolItem pi;
+     *    ...
+     *    HistoryLog().install(pi);
+     *    ...
+     *    HistoryLog().comment(someMessage);
+     *  }
+     * </code>
+     * 
+     * \note Take care to set proper target root dir if needed. Either pass
+     *       it via the constructor, or set it via setRoot(Pathname) method.
+     *       The default location of the file is determined by
+     *       \ref ZConfig::historyLogPath() which defaults to
+     *       /var/log/zypp/history.
+     */
+    class HistoryLog
+    {
+      HistoryLog( const HistoryLog & );
+      HistoryLog & operator=( const HistoryLog & );
+    private:
+      static std::ofstream _log;
+      static unsigned _refcnt;
+      static Pathname _fname;
+      static const char _sep;
+
+      static void openLog();
+      static void closeLog();
+      static void refUp();
+      static void refDown();
+
+    public:
+      HistoryLog( const Pathname & rootdir = Pathname() );
+      ~HistoryLog()
+      { refDown(); }
+
+      static void setRoot( const Pathname & root );
+      static const Pathname & fname();
+
+      void comment( const std::string & comment, bool timestamp = false );
+      void install( const PoolItem & pi );
+      void remove( const PoolItem & pi );
+
+      void addRepository( const RepoInfo & repo );
+      void removeRepository( const RepoInfo & repo );
+      void modifyRepository( const RepoInfo & oldrepo, const RepoInfo & newrepo );
+    };
+    ///////////////////////////////////////////////////////////////////
+
+} // namespace zypp
+
+#endif // ZYPP_TARGET_COMMITLOG_H
index 19c25a7..b8522f4 100644 (file)
@@ -17,6 +17,7 @@
 #include <list>
 #include <map>
 #include <algorithm>
+
 #include "zypp/base/InputStream.h"
 #include "zypp/base/Logger.h"
 #include "zypp/base/Gettext.h"
@@ -24,8 +25,8 @@
 #include "zypp/base/Regex.h"
 #include "zypp/PathInfo.h"
 #include "zypp/TmpPath.h"
-#include "zypp/ServiceInfo.h"
 
+#include "zypp/ServiceInfo.h"
 #include "zypp/repo/RepoException.h"
 #include "zypp/RepoManager.h"
 
@@ -44,6 +45,7 @@
 
 #include "zypp/Target.h" // for Target::targetDistribution() for repo index services
 #include "zypp/ZYppFactory.h" // to get the Target from ZYpp instance
+#include "zypp/HistoryLog.h" // to write history :O)
 
 #include "zypp/ZYppCallbacks.h"
 
@@ -1237,6 +1239,8 @@ namespace zypp
           //! \todo use a method calling UI callbacks to ask where to save creds?
           cm.saveInUser(media::AuthData(*urlit));
     }
+    
+    HistoryLog().addRepository(tosave);
 
     progress.toMax();
     MIL << "done" << endl;
@@ -1286,7 +1290,10 @@ namespace zypp
       it->setFilepath(repofile.asString());
       it->dumpAsIniOn(file);
       _pimpl->repos.insert(*it);
+
+      HistoryLog(_pimpl->options.rootDir).addRepository(*it);
     }
+
     MIL << "done" << endl;
   }
 
@@ -1365,6 +1372,7 @@ namespace zypp
         cleanMetadata( todelete, cleansubprogrcv);
         _pimpl->repos.erase(todelete);
         MIL << todelete.alias() << " sucessfully deleted." << endl;
+        HistoryLog(_pimpl->options.rootDir).removeRepository(todelete);
         return;
       } // else filepath is empty
 
@@ -1427,6 +1435,7 @@ namespace zypp
 
       _pimpl->repos.erase(toedit);
       _pimpl->repos.insert(newinfo);
+      HistoryLog(_pimpl->options.rootDir).modifyRepository(toedit, newinfo);
     }
   }
 
index 7feaa26..d38fe45 100644 (file)
@@ -91,9 +91,6 @@ namespace zypp
   std::ostream & Target::dumpOn( std::ostream & str ) const
   { return _pimpl->dumpOn( str ); }
 
-  bool Target::setInstallationLogfile(const Pathname & path_r)
-  { return _pimpl->setInstallationLogfile(path_r); }
-
   Date Target::timestamp() const
   { return _pimpl->timestamp(); }
 
index 92eee7e..f9a0dc1 100644 (file)
@@ -282,6 +282,10 @@ namespace zypp
                 {
                   rpmInstallFlags.setFlag( target::rpm::RPMINST_EXCLUDEDOCS );
                 }
+                else if ( entry == "history.logfile" )
+                {
+                  history_log_path = Pathname(value);
+                }
               }
             }
           }
@@ -343,7 +347,8 @@ namespace zypp
     bool apply_locks_file;
 
     target::rpm::RpmInstFlags rpmInstallFlags;
-
+    
+    Pathname history_log_path;
   };
   ///////////////////////////////////////////////////////////////////
 
@@ -497,12 +502,6 @@ namespace zypp
 
   ///////////////////////////////////////////////////////////////////
 
-  const std::string & ZConfig::cacheDBSplitJoinSeparator() const
-  {
-    static std::string s("!@$");
-    return s;
-  }
-
   bool ZConfig::repo_add_probe() const
   {
     return _pimpl->repo_add_probe;
@@ -562,6 +561,14 @@ namespace zypp
   target::rpm::RpmInstFlags ZConfig::rpmInstallFlags() const
   { return _pimpl->rpmInstallFlags; }
 
+
+  Pathname ZConfig::historyLogFile() const
+  {
+    return ( _pimpl->history_log_path.empty() ?
+        Pathname("/var/log/zypp/history") : _pimpl->history_log_path );
+  }
+
+
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////
index 6045f13..a79709a 100644 (file)
@@ -139,12 +139,6 @@ namespace zypp
       Pathname knownServicesPath() const;
 
       /**
-       * Separator string for storing/reading sets of strings to/from
-       * metadata cache DB.
-       */
-      const std::string & cacheDBSplitJoinSeparator() const;
-
-      /**
        * Whether repository urls should be probed.
        / config option
        * repo.add.probe
@@ -220,7 +214,6 @@ namespace zypp
       */
       Pathname update_messagesPath() const;
 
-    public:
       /** \name Options for package installation */
       //@{
       /** The default \ref target::rpm::RpmInstFlags for \ref ZYppCommitPolicy.
@@ -232,6 +225,8 @@ namespace zypp
       target::rpm::RpmInstFlags rpmInstallFlags() const;
       //@}
 
+      Pathname historyLogFile() const;
+
     public:
       class Impl;
       /** Dtor */
diff --git a/zypp/target/CommitLog.cc b/zypp/target/CommitLog.cc
deleted file mode 100644 (file)
index 1d4d97b..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
- /*---------------------------------------------------------------------\
-|                          ____ _   __ __ ___                          |
-|                         |__  / \ / / . \ . \                         |
-|                           / / \ V /|  _/  _/                         |
-|                          / /__ | | | | | |                           |
-|                         /_____||_| |_| |_|                           |
-|                                                                      |
-\---------------------------------------------------------------------*/
-/** \file zypp/target/CommitLog.cc
- *
-*/
-#include <iostream>
-#include <fstream>
-
-#include "zypp/base/Logger.h"
-
-#include "zypp/target/CommitLog.h"
-#include "zypp/PathInfo.h"
-#include "zypp/Date.h"
-
-using std::endl;
-
-namespace zypp {
-  namespace target {
-
-    ///////////////////////////////////////////////////////////////////
-
-    Pathname CommitLog::_fname;
-    std::ofstream CommitLog::_log;
-    unsigned CommitLog::_refcnt = 0;
-
-    ///////////////////////////////////////////////////////////////////
-
-    void CommitLog::openLog() {
-      if ( !_fname.empty() ) {
-        _log.clear();
-        _log.open( _fname.asString().c_str(), std::ios::out|std::ios::app );
-        if( !_log )
-          ERR << "Could not open logfile '" << _fname << "'" << endl;
-      }
-    }
-    void CommitLog::closeLog() {
-      _log.clear();
-      _log.close();
-    }
-    void CommitLog::refUp() {
-      if ( !_refcnt )
-        openLog();
-      ++_refcnt;
-    }
-    void CommitLog::refDown() {
-      --_refcnt;
-      if ( !_refcnt )
-        closeLog();
-    }
-
-    std::ostream & CommitLog::operator()( bool timestamp ) {
-      if ( timestamp ) {
-        _log << Date(Date::now()).form( "%Y-%m-%d %H:%M:%S ");
-      }
-      return _log;
-    }
-
-    void CommitLog::setFname( const Pathname & fname_r ) {
-      MIL << "installation log file " << fname_r << endl;
-      if ( _refcnt )
-        closeLog();
-      _fname = fname_r;
-      if ( ! _fname.empty() )
-        filesystem::assert_dir( _fname.dirname() );
-      if ( _refcnt )
-        openLog();
-    }
-
-    const Pathname & CommitLog::fname()
-    { return _fname; }
-
-  } // namespace target
-} // namespace zypp
diff --git a/zypp/target/CommitLog.h b/zypp/target/CommitLog.h
deleted file mode 100644 (file)
index 90a41bc..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
- /*---------------------------------------------------------------------\
-|                          ____ _   __ __ ___                          |
-|                         |__  / \ / / . \ . \                         |
-|                           / / \ V /|  _/  _/                         |
-|                          / /__ | | | | | |                           |
-|                         /_____||_| |_| |_|                           |
-|                                                                      |
-\---------------------------------------------------------------------*/
-/** \file zypp/target/CommitLog.h
- *
-*/
-#ifndef ZYPP_TARGET_COMMITLOG_H
-#define ZYPP_TARGET_COMMITLOG_H
-
-#include <iosfwd>
-
-#include "zypp/Pathname.h"
-
-namespace zypp {
-  namespace target {
-
-    ///////////////////////////////////////////////////////////////////
-    //
-    // CLASS NAME : CommitLog
-    /**
-     * Simple wrapper for progress log. Refcnt, filename and corresponding
-     * ofstream are static members. Logfile constructor raises, destructor
-     * lowers refcounter. On refcounter changing from 0->1, file is opened.
-     * Changing from 1->0 the file is closed. Thus Logfile objects should be
-     * local to those functions, writing the log, and must not be stored
-     * permanently;
-     *
-     * Usage:
-     *  some methothd ()
-     *  {
-     *    CommitLog progresslog;
-     *    ...
-     *    progresslog() << "some message" << endl;
-     *    ...
-     *  }
-     **/
-    class CommitLog {
-      CommitLog( const CommitLog & );
-      CommitLog & operator=( const CommitLog & );
-    private:
-      static std::ofstream _log;
-      static unsigned _refcnt;
-      static Pathname _fname;
-
-      static void openLog();
-      static void closeLog();
-      static void refUp();
-      static void refDown();
-    public:
-      CommitLog() { refUp(); }
-      ~CommitLog() { refDown(); }
-      std::ostream & operator()( bool timestamp = false );
-      static void setFname( const Pathname & fname_r );
-      static const Pathname & fname();
-    };
-    ///////////////////////////////////////////////////////////////////
-
-  } // namespace target
-} // namespace zypp
-
-
-#endif // ZYPP_TARGET_COMMITLOG_H
index 3c14ccc..e190f1a 100644 (file)
@@ -37,7 +37,7 @@
 #include "zypp/Repository.h"
 
 #include "zypp/ResFilters.h"
-#include "zypp/target/CommitLog.h"
+#include "zypp/HistoryLog.h"
 #include "zypp/target/TargetImpl.h"
 #include "zypp/target/TargetCallbackReceiver.h"
 #include "zypp/target/rpm/librpmDb.h"
@@ -81,13 +81,13 @@ namespace zypp
       {
         MIL << "Execute script " << PathInfo(script_r) << endl;
 
-        CommitLog progresslog;
-        progresslog(/*timestamp*/true) << script_r << _(" executed") << endl;
+        HistoryLog historylog;
+        historylog.comment(script_r.asString() + _(" executed"), /*timestamp*/true);
         ExternalProgram prog( script_r.asString(), ExternalProgram::Stderr_To_Stdout, false, -1, true /*, root_r*/ );
 
         for ( std::string output = prog.receiveLine(); output.length(); output = prog.receiveLine() )
         {
-          progresslog() << output;
+          historylog.comment(output);
           if ( ! report_r->progress( PatchScriptReport::OUTPUT, output ) )
           {
             WAR << "User request to abort script " << script_r << endl;
@@ -103,7 +103,9 @@ namespace zypp
         {
           ret.second = report_r->problem( prog.execError() );
           WAR << "ACTION" << ret.second << "(" << prog.execError() << ")" << endl;
-          progresslog(/*timestamp*/true)<< script_r << _(" execution failed") << " (" << prog.execError() << ")" << endl;
+          std::ostringstream sstr;
+          sstr << script_r << _(" execution failed") << " (" << prog.execError() << ")" << endl;
+          historylog.comment(sstr.str(), /*timestamp*/true);
           return ret;
         }
 
@@ -187,7 +189,9 @@ namespace zypp
             if ( abort || aborting_r )
             {
               WAR << "Aborting: Skip patch script " << *sit << endl;
-              CommitLog()(/*timestamp*/true) << script.path() << _(" execution skipped while aborting") << endl;
+              HistoryLog().comment(
+                  script.path().asString() + _(" execution skipped while aborting"),
+                  /*timestamp*/true);
             }
             else
             {
@@ -288,6 +292,8 @@ namespace zypp
     {
       _rpm.initDatabase( root_r, Pathname(), doRebuild_r );
 
+      HistoryLog::setRoot(_root);
+
       // create the anonymous unique id
       // this value is used for statistics
       Pathname idpath( home() / "AnonymousUniqueId");
@@ -551,12 +557,8 @@ namespace zypp
       ///////////////////////////////////////////////////////////////////
       // Process packages:
       ///////////////////////////////////////////////////////////////////
-      if ( root() == "/" && CommitLog::fname().empty() )
-      {
-        // Yes, we simply hijack /var/log/YaST2/y2logRPM
-        // until we maintain some zypp history database.
-        CommitLog::setFname( "/var/log/YaST2/y2logRPM" );
-      }
+
+      DBG << "commit log file is set to: " << HistoryLog::fname() << endl;
 
       ZYppCommitResult result;
 
@@ -739,6 +741,7 @@ namespace zypp
             {
               progress.tryLevel( target::rpm::InstallResolvableReport::RPM_NODEPS_FORCE );
               rpm().installPackage( localfile, flags );
+              HistoryLog().install(*it);
 
               if ( progress.aborted() )
               {
@@ -785,6 +788,7 @@ namespace zypp
             try
             {
               rpm().removePackage( p, flags );
+              HistoryLog().remove(*it);
 
               if ( progress.aborted() )
               {
@@ -852,12 +856,6 @@ namespace zypp
       return _rpm.hasFile(path_str, name_str);
     }
 
-    /** Set the log file for target */
-    bool TargetImpl::setInstallationLogfile(const Pathname & path_r)
-    {
-      CommitLog::setFname(path_r);
-      return true;
-    }
 
     Date TargetImpl::timestamp() const
     {
index 5dd7843..7fe1f22 100644 (file)
@@ -135,9 +135,6 @@ namespace zypp
       std::string whoOwnsFile (const std::string & path_str) const
       { return _rpm.whoOwnsFile (path_str); }
 
-      /** Set the log file for target */
-      bool setInstallationLogfile(const Pathname & path_r);
-
       /** return the last modification date of the target */
       Date timestamp() const;
 
index 66d2aa3..383c659 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <iostream>
 #include <fstream>
+#include <sstream>
 #include <list>
 #include <map>
 #include <set>
@@ -28,7 +29,6 @@
 
 #include "zypp/base/Logger.h"
 #include "zypp/base/String.h"
-#include "zypp/base/Regex.h"
 #include "zypp/base/Gettext.h"
 
 #include "zypp/Date.h"
@@ -39,7 +39,7 @@
 #include "zypp/target/rpm/RpmDb.h"
 #include "zypp/target/rpm/RpmCallbacks.h"
 
-#include "zypp/target/CommitLog.h"
+#include "zypp/HistoryLog.h"
 #include "zypp/target/rpm/librpmDb.h"
 #include "zypp/target/rpm/RpmException.h"
 #include "zypp/TmpPath.h"
@@ -1973,7 +1973,7 @@ void RpmDb::installPackage( const Pathname & filename, RpmInstFlags flags )
 void RpmDb::doInstallPackage( const Pathname & filename, RpmInstFlags flags, callback::SendReport<RpmInstallReport> & report )
 {
   FAILIFNOTINITIALIZED;
-  CommitLog progresslog;
+  HistoryLog historylog;
 
   MIL << "RpmDb::installPackage(" << filename << "," << flags << ")" << endl;
 
@@ -2072,8 +2072,10 @@ void RpmDb::doInstallPackage( const Pathname & filename, RpmInstFlags flags, cal
   if ( rpm_status != 0 )
   {
     // %s = filename of rpm package
-    progresslog(/*timestamp*/true) << str::form(_("%s install failed"), Pathname::basename(filename).c_str()) << endl;
-    progresslog() << _("rpm output:") << endl << rpmmsg << endl;
+    // historylog(/*timestamp*/true) << str::form(_("%s install failed"), Pathname::basename(filename).c_str()) << endl;
+    ostringstream sstr;
+    sstr << _("rpm output:") << endl << rpmmsg << endl;
+    historylog.comment(sstr.str());
     //TranslatorExplanation after semicolon is error message
     ZYPP_THROW(RpmSubprocessException(string(_("RPM failed: ")) +
                (rpmmsg.empty() ? error_message : rpmmsg)));
@@ -2081,10 +2083,14 @@ void RpmDb::doInstallPackage( const Pathname & filename, RpmInstFlags flags, cal
   else
   {
     // %s = filename of rpm package
-    progresslog(/*timestamp*/true) << str::form(_("%s installed ok"), Pathname::basename(filename).c_str()) << endl;
+    // historylog.comment(
+    //    str::form(_("%s installed ok"), Pathname::basename(filename).c_str()),
+    //    /*timestamp*/true);
     if ( ! rpmmsg.empty() )
     {
-      progresslog() << _("Additional rpm output:") << endl << rpmmsg << endl;
+      ostringstream sstr;
+      sstr << _("Additional rpm output:") << endl << rpmmsg << endl;
+      historylog.comment(sstr.str());
     }
   }
 }
@@ -2144,7 +2150,7 @@ void RpmDb::removePackage( const string & name_r, RpmInstFlags flags )
 void RpmDb::doRemovePackage( const string & name_r, RpmInstFlags flags, callback::SendReport<RpmRemoveReport> & report )
 {
   FAILIFNOTINITIALIZED;
-  CommitLog progresslog;
+  HistoryLog historylog;
 
   MIL << "RpmDb::doRemovePackage(" << name_r << "," << flags << ")" << endl;
 
@@ -2206,18 +2212,23 @@ void RpmDb::doRemovePackage( const string & name_r, RpmInstFlags flags, callback
   if ( rpm_status != 0 )
   {
     // %s = name of rpm package
-    progresslog(/*timestamp*/true) << str::form(_("%s remove failed"), name_r.c_str()) << endl;
-    progresslog() << _("rpm output:") << endl << rpmmsg << endl;
-    //TranslatorExplanation after semicolon is error message
+    historylog.comment(
+        str::form(_("%s remove failed"), name_r.c_str()), /*timestamp*/true);
+    ostringstream sstr;
+    sstr << _("rpm output:") << endl << rpmmsg << endl;
+    historylog.comment(sstr.str());
+    // TranslatorExplanation after semicolon is error message
     ZYPP_THROW(RpmSubprocessException(string(_("RPM failed: ")) +
                (rpmmsg.empty() ? error_message: rpmmsg)));
   }
   else
   {
-    progresslog(/*timestamp*/true) << str::form(_("%s remove ok"), name_r.c_str()) << endl;
+    // historylog.comment(str::form(_("%s remove ok"), name_r.c_str()), /*timestamp*/true);
     if ( ! rpmmsg.empty() )
     {
-      progresslog() << _("Additional rpm output:") << endl << rpmmsg << endl;
+      ostringstream sstr;
+      sstr << _("Additional rpm output:") << endl << rpmmsg << endl;
+      historylog.comment(sstr.str());
     }
   }
 }
@@ -2245,7 +2256,7 @@ bool RpmDb::backupPackage( const Pathname & filename )
 //
 bool RpmDb::backupPackage(const string& packageName)
 {
-  CommitLog progresslog;
+  HistoryLog progresslog;
   bool ret = true;
   Pathname backupFilename;
   Pathname filestobackupfile = _root+_backuppath+FILEFORBACKUPFILES;
@@ -2359,7 +2370,9 @@ bool RpmDb::backupPackage(const string& packageName)
     else
     {
       MIL << "tar backup ok" << endl;
-      progresslog(/*timestamp*/true) << str::form(_("created backup %s"), backupFilename.asString().c_str()) << endl;
+      progresslog.comment(
+          str::form(_("created backup %s"), backupFilename.asString().c_str())
+          , /*timestamp*/true);
     }
 
     filesystem::unlink(filestobackupfile);