- new History Log, first version (fate #110205)
[platform/upstream/libzypp.git] / zypp / HistoryLog.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/target/HistoryLog.h
10  *
11  */
12 #ifndef ZYPP_TARGET_COMMITLOG_H
13 #define ZYPP_TARGET_COMMITLOG_H
14
15 #include <iosfwd>
16
17 #include "zypp/Pathname.h"
18
19 namespace zypp
20 {
21   class PoolItem;
22   class RepoInfo;
23   
24   struct HistoryActionID
25   {
26     static const HistoryActionID NONE;
27
28     static const HistoryActionID INSTALL;
29     static const HistoryActionID REMOVE;
30     static const HistoryActionID REPO_ADD;
31     static const HistoryActionID REPO_REMOVE;
32     static const HistoryActionID REPO_CHANGE_ALIAS;
33     static const HistoryActionID REPO_CHANGE_URL;
34
35     enum ID
36     {
37       NONE_e,
38
39       INSTALL_e,
40       REMOVE_e,
41       REPO_ADD_e,
42       REPO_REMOVE_e,
43       REPO_CHANGE_ALIAS_e,
44       REPO_CHANGE_URL_e
45     };
46
47     HistoryActionID() : _id(NONE_e) {}
48
49     HistoryActionID(ID id) : _id(id) {}
50
51     explicit HistoryActionID(const std::string & strval_r);
52
53     ID toEnum() const { return _id; }
54
55     HistoryActionID::ID parse(const std::string & strval_r);
56
57     const std::string & asString(bool pad = false) const;
58
59     ID _id;
60   };
61
62   /** \relates HistoryActionID */
63   std::ostream & operator << (std::ostream & str, const HistoryActionID & id);
64
65     ///////////////////////////////////////////////////////////////////
66     //
67     //  CLASS NAME : HistoryLog
68     /**
69      * Simple wrapper for progress log. Refcnt, filename and corresponding
70      * ofstream are static members. Logfile constructor raises, destructor
71      * lowers refcounter. On refcounter changing from 0->1, file is opened.
72      * Changing from 1->0 the file is closed. Thus Logfile objects should be
73      * local to those functions, writing the log, and must not be stored
74      * permanently.
75      *
76      * Usage:
77      * <code>
78      *  some method ()
79      *  {
80      *    PoolItem pi;
81      *    ...
82      *    HistoryLog().install(pi);
83      *    ...
84      *    HistoryLog().comment(someMessage);
85      *  }
86      * </code>
87      * 
88      * \note Take care to set proper target root dir if needed. Either pass
89      *       it via the constructor, or set it via setRoot(Pathname) method.
90      *       The default location of the file is determined by
91      *       \ref ZConfig::historyLogPath() which defaults to
92      *       /var/log/zypp/history.
93      */
94     class HistoryLog
95     {
96       HistoryLog( const HistoryLog & );
97       HistoryLog & operator=( const HistoryLog & );
98     private:
99       static std::ofstream _log;
100       static unsigned _refcnt;
101       static Pathname _fname;
102       static const char _sep;
103
104       static void openLog();
105       static void closeLog();
106       static void refUp();
107       static void refDown();
108
109     public:
110       HistoryLog( const Pathname & rootdir = Pathname() );
111       ~HistoryLog()
112       { refDown(); }
113
114       static void setRoot( const Pathname & root );
115       static const Pathname & fname();
116
117       void comment( const std::string & comment, bool timestamp = false );
118       void install( const PoolItem & pi );
119       void remove( const PoolItem & pi );
120
121       void addRepository( const RepoInfo & repo );
122       void removeRepository( const RepoInfo & repo );
123       void modifyRepository( const RepoInfo & oldrepo, const RepoInfo & newrepo );
124     };
125     ///////////////////////////////////////////////////////////////////
126
127 } // namespace zypp
128
129 #endif // ZYPP_TARGET_COMMITLOG_H