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"
19 #include "zypp/base/IOStream.h"
21 #include "zypp/PathInfo.h"
22 #include "zypp/Date.h"
24 #include "zypp/PoolItem.h"
25 #include "zypp/Package.h"
26 #include "zypp/RepoInfo.h"
28 #include "zypp/HistoryLog.h"
29 #include "zypp/HistoryLogData.h"
38 inline string timestamp()
39 { return zypp::Date::now().form( HISTORY_LOG_DATE_FORMAT ); }
41 inline string userAtHostname()
45 char * tmp = ::cuserid(buf);
49 if (!::gethostname(buf, 255))
50 result += "@" + string(buf);
55 static std::string pidAndAppname()
57 static std::string _val;
60 pid_t mypid = getpid();
61 zypp::Pathname p( "/proc/"+zypp::str::numstring(mypid)+"/exe" );
62 zypp::Pathname myname( zypp::filesystem::readlink( p ) );
64 _val += zypp::str::numstring(mypid);
66 _val += myname.basename();
71 static std::string cmdline()
73 static std::string _val;
76 pid_t mypid = getpid();
78 std::ifstream cmdlineStr( Pathname("/proc/"+zypp::str::numstring(mypid)+"/cmdline").c_str() );
80 const char * sep = "'";
81 while ( cmdlineStr && cmdlineStr.get( ch ) )
90 case '\0': _val += '\''; sep = " '"; break;
91 case '\n': _val += ' '; break;
92 case '\\': _val += '\\'; _val += '\\'; break;
93 case '|': _val += '\\'; _val += '|'; break;
94 default: _val += ch; break;
105 const char _sep = '|';
107 unsigned _refcnt = 0;
109 Pathname _fnameLastFail;
111 inline void openLog()
113 if ( _fname.empty() )
114 _fname = ZConfig::instance().historyLogFile();
117 _log.open( _fname.asString().c_str(), std::ios::out|std::ios::app );
118 if( !_log && _fnameLastFail != _fname )
120 ERR << "Could not open logfile '" << _fname << "'" << endl;
121 _fnameLastFail = _fname;
125 inline void closeLog()
138 inline void refDown()
146 ///////////////////////////////////////////////////////////////////
148 // CLASS NAME : HistoryLog
150 ///////////////////////////////////////////////////////////////////
152 HistoryLog::HistoryLog( const Pathname & rootdir )
158 HistoryLog::~HistoryLog()
163 void HistoryLog::setRoot( const Pathname & rootdir )
165 if ( ! rootdir.absolute() )
171 _fname = rootdir / ZConfig::instance().historyLogFile();
172 filesystem::assert_dir( _fname.dirname() );
173 MIL << "installation log file " << _fname << endl;
179 const Pathname & HistoryLog::fname()
181 if ( _fname.empty() )
182 _fname = ZConfig::instance().historyLogFile();
186 /////////////////////////////////////////////////////////////////////////
188 void HistoryLog::comment( const string & comment, bool timestamp_r )
195 _log << timestamp() << " ";
197 const char * s = comment.c_str();
199 unsigned size = comment.size();
201 // ignore the last newline
202 if (comment[size-1] == '\n')
205 for ( unsigned i = 0; i < size; ++i, ++c )
208 _log << string( s, c + 1 - s ) << "# ";
213 _log << std::string( s, c-s );
218 /////////////////////////////////////////////////////////////////////////
220 void HistoryLog::stampCommand()
223 << timestamp() // 1 timestamp
224 << _sep << HistoryActionID::STAMP_COMMAND.asString(true) // 2 action
225 << _sep << userAtHostname() // 3 requested by
226 << _sep << cmdline() // 4 command
227 << _sep << str::escape(ZConfig::instance().userData(), _sep) // 6 userdata
232 void HistoryLog::install( const PoolItem & pi )
234 const Package::constPtr p = asKind<Package>(pi.resolvable());
239 << timestamp() // 1 timestamp
240 << _sep << HistoryActionID::INSTALL.asString(true) // 2 action
241 << _sep << p->name() // 3 name
242 << _sep << p->edition() // 4 evr
243 << _sep << p->arch(); // 5 arch
245 // ApplLow is what the solver selected on behalf of the user.
246 if (pi.status().isByUser() || pi.status().isByApplLow() )
247 _log << _sep << userAtHostname(); // 6 requested by
248 else if (pi.status().isByApplHigh())
249 _log << _sep << pidAndAppname();
254 << _sep << p->repoInfo().alias() // 7 repo alias
255 << _sep << p->checksum().checksum() // 8 checksum
256 << _sep << str::escape(ZConfig::instance().userData(), _sep) // 9 userdata
261 void HistoryLog::remove( const PoolItem & pi )
263 const Package::constPtr p = asKind<Package>(pi.resolvable());
268 << timestamp() // 1 timestamp
269 << _sep << HistoryActionID::REMOVE.asString(true) // 2 action
270 << _sep << p->name() // 3 name
271 << _sep << p->edition() // 4 evr
272 << _sep << p->arch(); // 5 arch
274 // ApplLow is what the solver selected on behalf of the user.
275 if ( pi.status().isByUser() || pi.status().isByApplLow() )
276 _log << _sep << userAtHostname(); // 6 requested by
277 else if (pi.status().isByApplHigh())
278 _log << _sep << pidAndAppname();
283 << _sep << str::escape(ZConfig::instance().userData(), _sep) // 7 userdata
287 /////////////////////////////////////////////////////////////////////////
289 void HistoryLog::addRepository(const RepoInfo & repo)
292 << timestamp() // 1 timestamp
293 << _sep << HistoryActionID::REPO_ADD.asString(true) // 2 action
294 << _sep << str::escape(repo.alias(), _sep) // 3 alias
295 << _sep << *repo.baseUrlsBegin() // 4 primary URL
296 << _sep << str::escape(ZConfig::instance().userData(), _sep) // 5 userdata
301 void HistoryLog::removeRepository(const RepoInfo & repo)
304 << timestamp() // 1 timestamp
305 << _sep << HistoryActionID::REPO_REMOVE.asString(true) // 2 action
306 << _sep << str::escape(repo.alias(), _sep) // 3 alias
307 << _sep << str::escape(ZConfig::instance().userData(), _sep) // 4 userdata
312 void HistoryLog::modifyRepository(
313 const RepoInfo & oldrepo, const RepoInfo & newrepo)
315 if (oldrepo.alias() != newrepo.alias())
318 << timestamp() // 1 timestamp
319 << _sep << HistoryActionID::REPO_CHANGE_ALIAS.asString(true) // 2 action
320 << _sep << str::escape(oldrepo.alias(), _sep) // 3 old alias
321 << _sep << str::escape(newrepo.alias(), _sep) // 4 new alias
322 << _sep << str::escape(ZConfig::instance().userData(), _sep) // 5 userdata
325 if (*oldrepo.baseUrlsBegin() != *newrepo.baseUrlsBegin())
328 << timestamp() // 1 timestamp
329 << _sep << HistoryActionID::REPO_CHANGE_URL.asString(true) // 2 action
330 << _sep << str::escape(oldrepo.alias(), _sep) // 3 old url
331 << _sep << *newrepo.baseUrlsBegin() // 4 new url
332 << _sep << str::escape(ZConfig::instance().userData(), _sep) // 5 userdata
337 ///////////////////////////////////////////////////////////////////