1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/target/HistoryLog.cc
16 #include "zypp/ZConfig.h"
17 #include "zypp/base/String.h"
18 #include "zypp/base/Logger.h"
20 #include "zypp/PathInfo.h"
21 #include "zypp/Date.h"
23 #include "zypp/PoolItem.h"
24 #include "zypp/Package.h"
25 #include "zypp/RepoInfo.h"
27 #include "zypp/HistoryLog.h"
28 #include "zypp/HistoryLogData.h"
35 inline string timestamp()
36 { return zypp::Date::now().form( HISTORY_LOG_DATE_FORMAT ); }
38 inline string userAtHostname()
42 char * tmp = ::cuserid(buf);
46 if (!::gethostname(buf, 255))
47 result += "@" + string(buf);
52 static std::string pidAndAppname()
54 static std::string _val;
57 pid_t mypid = getpid();
58 zypp::Pathname p( "/proc/"+zypp::str::numstring(mypid)+"/exe" );
59 zypp::Pathname myname( zypp::filesystem::readlink( p ) );
61 _val += zypp::str::numstring(mypid);
63 _val += myname.basename();
73 const char _sep = '|';
81 _fname = ZConfig::instance().historyLogFile();
84 _log.open( _fname.asString().c_str(), std::ios::out|std::ios::app );
86 ERR << "Could not open logfile '" << _fname << "'" << endl;
89 inline void closeLog()
102 inline void refDown()
110 ///////////////////////////////////////////////////////////////////
112 // CLASS NAME : HistoryLog
114 ///////////////////////////////////////////////////////////////////
116 HistoryLog::HistoryLog( const Pathname & rootdir )
122 HistoryLog::~HistoryLog()
127 void HistoryLog::setRoot( const Pathname & rootdir )
129 if ( ! rootdir.absolute() )
135 _fname = rootdir / ZConfig::instance().historyLogFile();
136 filesystem::assert_dir( _fname.dirname() );
137 MIL << "installation log file " << _fname << endl;
143 const Pathname & HistoryLog::fname()
145 if ( _fname.empty() )
146 _fname = ZConfig::instance().historyLogFile();
150 /////////////////////////////////////////////////////////////////////////
152 void HistoryLog::comment( const string & comment, bool timestamp )
159 _log << ::timestamp() << " ";
161 const char * s = comment.c_str();
163 unsigned size = comment.size();
165 // ignore the last newline
166 if (comment[size-1] == '\n')
169 for ( unsigned i = 0; i < size; ++i, ++c )
172 _log << string( s, c + 1 - s ) << "# ";
177 _log << std::string( s, c-s );
182 /////////////////////////////////////////////////////////////////////////
184 void HistoryLog::install( const PoolItem & pi )
186 const Package::constPtr p = asKind<Package>(pi.resolvable());
191 << timestamp() // 1 timestamp
192 << _sep << HistoryActionID::INSTALL.asString(true) // 2 action
193 << _sep << p->name() // 3 name
194 << _sep << p->edition() // 4 evr
195 << _sep << p->arch(); // 5 arch
197 // ApplLow is what the solver selected on behalf of the user.
198 if (pi.status().isByUser() || pi.status().isByApplLow() )
199 _log << _sep << userAtHostname(); // 6 reqested by
200 else if (pi.status().isByApplHigh())
201 _log << _sep << pidAndAppname();
206 << _sep << p->repoInfo().alias() // 7 repo alias
207 << _sep << p->checksum().checksum(); // 8 checksum
211 //_log << pi << endl;
215 void HistoryLog::remove( const PoolItem & pi )
217 const Package::constPtr p = asKind<Package>(pi.resolvable());
222 << timestamp() // 1 timestamp
223 << _sep << HistoryActionID::REMOVE.asString(true) // 2 action
224 << _sep << p->name() // 3 name
225 << _sep << p->edition() // 4 evr
226 << _sep << p->arch(); // 5 arch
228 // ApplLow is what the solver selected on behalf of the user.
229 if ( pi.status().isByUser() || pi.status().isByApplLow() )
230 _log << _sep << userAtHostname(); // 6 reqested by
231 else if (pi.status().isByApplHigh())
232 _log << _sep << pidAndAppname();
236 // we don't have checksum in rpm db
237 // << _sep << p->checksum().checksum(); // x checksum
241 //_log << pi << endl;
244 /////////////////////////////////////////////////////////////////////////
246 void HistoryLog::addRepository(const RepoInfo & repo)
249 << timestamp() // 1 timestamp
250 << _sep << HistoryActionID::REPO_ADD.asString(true) // 2 action
251 << _sep << str::escape(repo.alias(), _sep) // 3 alias
252 // what about the rest of the URLs??
253 << _sep << *repo.baseUrlsBegin() // 4 primary URL
258 void HistoryLog::removeRepository(const RepoInfo & repo)
261 << timestamp() // 1 timestamp
262 << _sep << HistoryActionID::REPO_REMOVE.asString(true) // 2 action
263 << _sep << str::escape(repo.alias(), _sep) // 3 alias
268 void HistoryLog::modifyRepository(
269 const RepoInfo & oldrepo, const RepoInfo & newrepo)
271 if (oldrepo.alias() != newrepo.alias())
274 << timestamp() // 1 timestamp
275 << _sep << HistoryActionID::REPO_CHANGE_ALIAS.asString(true) // 2 action
276 << _sep << str::escape(oldrepo.alias(), _sep) // 3 old alias
277 << _sep << str::escape(newrepo.alias(), _sep) // 4 new alias
281 if (*oldrepo.baseUrlsBegin() != *newrepo.baseUrlsBegin())
284 << timestamp() //1 timestamp
285 << _sep << HistoryActionID::REPO_CHANGE_URL.asString(true) // 2 action
286 << _sep << str::escape(oldrepo.alias(), _sep) // 3 old url
287 << _sep << *newrepo.baseUrlsBegin() // 4 new url
292 ///////////////////////////////////////////////////////////////////