doc
[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     static HistoryActionID::ID parse(const std::string & strval_r);
65
66     const std::string & asString(bool pad = false) const;
67
68     private:
69     ID _id;
70   };
71
72   /** \relates HistoryActionID */
73   std::ostream & operator << (std::ostream & str, const HistoryActionID & id);
74   ///////////////////////////////////////////////////////////////////
75
76
77   ///////////////////////////////////////////////////////////////////
78   //
79   //    CLASS NAME : HistoryLog
80   /**
81    * Simple wrapper for progress log. Refcnt, filename and corresponding
82    * ofstream are static members. Logfile constructor raises, destructor
83    * lowers refcounter. On refcounter changing from 0->1, file is opened.
84    * Changing from 1->0 the file is closed. Thus Logfile objects should be
85    * local to those functions, writing the log, and must not be stored
86    * permanently.
87    *
88    * Usage:
89    * <code>
90    *  some method ()
91    *  {
92    *    PoolItem pi;
93    *    ...
94    *    HistoryLog().install(pi);
95    *    ...
96    *    HistoryLog().comment(someMessage);
97    *  }
98    * </code>
99    *
100    * \note Take care to set proper target root dir if needed. Either pass
101    *       it via the constructor, or set it via setRoot(Pathname) method.
102    *       The default location of the file is determined by
103    *       \ref ZConfig::historyLogPath() which defaults to
104    *       /var/log/zypp/history.
105    *
106    * \see http://en.opensuse.org/Libzypp/Package_History
107    *
108    * \todo Static private stuff does not need to be mentioned here in the
109    * header (use an annon. namespace in the .cc). Appart from that the
110    * implementation as signleton is questionable. Use shared_ptr instead of
111    * handcrafted ref/unref. Manage multiple logs at different locations.
112    */
113   class HistoryLog
114   {
115     HistoryLog( const HistoryLog & );
116     HistoryLog & operator=( const HistoryLog & );
117   private:
118     static std::ofstream _log;
119     static unsigned _refcnt;
120     static Pathname _fname;
121     static const char _sep;
122
123     static void openLog();
124     static void closeLog();
125     static void refUp();
126     static void refDown();
127
128   public:
129     /**
130      * Constructor with an optional root directory.
131      *
132      * \param rootdir actual target root directory
133      */
134     HistoryLog( const Pathname & rootdir = Pathname() );
135     ~HistoryLog()
136     { refDown(); }
137
138     /**
139      * Set new root directory to the default history log file path.
140      *
141      * This path will be prepended to the default log file path. This should
142      * be done where there is a potential that the target root has changed.
143      *
144      * \param root new root directory.
145      */
146     static void setRoot( const Pathname & root );
147
148     /**
149      * Get the current log file path.
150      */
151     static const Pathname & fname();
152
153     /**
154      * Log a comment (even multiline).
155      *
156      * \param comment the comment
157      * \param timestamp whether to include a timestamp at the start of the comment
158      */
159     void comment( const std::string & comment, bool timestamp = false );
160
161     /**
162      * Log installation (or update) of a package.
163      */
164     void install( const PoolItem & pi );
165
166     /**
167      * Log removal of a package
168      */
169     void remove( const PoolItem & pi );
170
171     /**
172      * Log a newly added repository.
173      *
174      * \param repo info about the added repository
175      */
176     void addRepository( const RepoInfo & repo );
177
178     /**
179      * Log recently removed repository.
180      *
181      * \param repo info about the removed repository
182      */
183     void removeRepository( const RepoInfo & repo );
184
185     /**
186      * Log certain modifications to a repository.
187      *
188      * \param oldrepo info about the old repository
189      * \param newrepo info about the new repository
190      */
191     void modifyRepository( const RepoInfo & oldrepo, const RepoInfo & newrepo );
192   };
193   ///////////////////////////////////////////////////////////////////
194
195 } // namespace zypp
196
197 #endif // ZYPP_TARGET_COMMITLOG_H