- indent & some comments
[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   ///////////////////////////////////////////////////////////////////
25   //
26   //  CLASS NAME : HistoryLog
27   //
28   /**
29    * Enumeration of known history actions.
30    * 
31    * \ingroup g_EnumerationClass
32    */
33   struct HistoryActionID
34   {
35     static const HistoryActionID NONE;
36
37     static const HistoryActionID INSTALL;
38     static const HistoryActionID REMOVE;
39     static const HistoryActionID REPO_ADD;
40     static const HistoryActionID REPO_REMOVE;
41     static const HistoryActionID REPO_CHANGE_ALIAS;
42     static const HistoryActionID REPO_CHANGE_URL;
43
44     enum ID
45     {
46       NONE_e,
47
48       INSTALL_e,
49       REMOVE_e,
50       REPO_ADD_e,
51       REPO_REMOVE_e,
52       REPO_CHANGE_ALIAS_e,
53       REPO_CHANGE_URL_e
54     };
55
56     HistoryActionID() : _id(NONE_e) {}
57
58     HistoryActionID(ID id) : _id(id) {}
59
60     explicit HistoryActionID(const std::string & strval_r);
61
62     ID toEnum() const { return _id; }
63
64     HistoryActionID::ID parse(const std::string & strval_r);
65
66     const std::string & asString(bool pad = false) const;
67
68     ID _id;
69   };
70
71   /** \relates HistoryActionID */
72   std::ostream & operator << (std::ostream & str, const HistoryActionID & id);
73   ///////////////////////////////////////////////////////////////////
74
75
76   ///////////////////////////////////////////////////////////////////
77   //
78   //    CLASS NAME : HistoryLog
79   /**
80    * Simple wrapper for progress log. Refcnt, filename and corresponding
81    * ofstream are static members. Logfile constructor raises, destructor
82    * lowers refcounter. On refcounter changing from 0->1, file is opened.
83    * Changing from 1->0 the file is closed. Thus Logfile objects should be
84    * local to those functions, writing the log, and must not be stored
85    * permanently.
86    *
87    * Usage:
88    * <code>
89    *  some method ()
90    *  {
91    *    PoolItem pi;
92    *    ...
93    *    HistoryLog().install(pi);
94    *    ...
95    *    HistoryLog().comment(someMessage);
96    *  }
97    * </code>
98    * 
99    * \note Take care to set proper target root dir if needed. Either pass
100    *       it via the constructor, or set it via setRoot(Pathname) method.
101    *       The default location of the file is determined by
102    *       \ref ZConfig::historyLogPath() which defaults to
103    *       /var/log/zypp/history.
104    * 
105    * \see http://en.opensuse.org/Libzypp/Package_History
106    */
107   class HistoryLog
108   {
109     HistoryLog( const HistoryLog & );
110     HistoryLog & operator=( const HistoryLog & );
111   private:
112     static std::ofstream _log;
113     static unsigned _refcnt;
114     static Pathname _fname;
115     static const char _sep;
116
117     static void openLog();
118     static void closeLog();
119     static void refUp();
120     static void refDown();
121
122   public:
123     /**
124      * Constructor with an optional root directory.
125      *
126      * \param rootdir actual target root directory
127      */
128     HistoryLog( const Pathname & rootdir = Pathname() );
129     ~HistoryLog()
130     { refDown(); }
131
132     /**
133      * Set new root directory to the default history log file path.
134      * 
135      * This path will be prepended to the default log file path. This should
136      * be done where there is a potential that the target root has changed.
137      * 
138      * \param root new root directory.
139      */
140     static void setRoot( const Pathname & root );
141
142     /**
143      * Get the current log file path.
144      */
145     static const Pathname & fname();
146
147     /**
148      * Log a comment (even multiline).
149      * 
150      * \param comment the comment
151      * \param timestamp whether to include a timestamp at the start of the comment
152      */
153     void comment( const std::string & comment, bool timestamp = false );
154     
155     /**
156      * Log installation (or update) of a package.
157      */
158     void install( const PoolItem & pi );
159     
160     /**
161      * Log removal of a package
162      */
163     void remove( const PoolItem & pi );
164
165     /**
166      * Log a newly added repository.
167      * 
168      * \param repo info about the added repository
169      */
170     void addRepository( const RepoInfo & repo );
171     
172     /**
173      * Log recently removed repository.
174      * 
175      * \param repo info about the removed repository
176      */
177     void removeRepository( const RepoInfo & repo );
178     
179     /**
180      * Log certain modifications to a repository.
181      * 
182      * \param oldrepo info about the old repository
183      * \param newrepo info about the new repository
184      */ 
185     void modifyRepository( const RepoInfo & oldrepo, const RepoInfo & newrepo );
186   };
187   ///////////////////////////////////////////////////////////////////
188
189 } // namespace zypp
190
191 #endif // ZYPP_TARGET_COMMITLOG_H