use non-throwing test for presence of Target
[platform/upstream/libzypp.git] / zypp / target / CommitLog.cc
1  /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/target/CommitLog.cc
10  *
11 */
12 #include <iostream>
13 #include <fstream>
14
15 #include "zypp/base/Logger.h"
16
17 #include "zypp/target/CommitLog.h"
18 #include "zypp/PathInfo.h"
19 #include "zypp/Date.h"
20
21 using std::endl;
22
23 namespace zypp {
24   namespace target {
25
26     ///////////////////////////////////////////////////////////////////
27
28     Pathname CommitLog::_fname;
29     std::ofstream CommitLog::_log;
30     unsigned CommitLog::_refcnt = 0;
31
32     ///////////////////////////////////////////////////////////////////
33
34     void CommitLog::openLog() {
35       if ( !_fname.empty() ) {
36         _log.clear();
37         _log.open( _fname.asString().c_str(), std::ios::out|std::ios::app );
38         if( !_log )
39           ERR << "Could not open logfile '" << _fname << "'" << endl;
40       }
41     }
42     void CommitLog::closeLog() {
43       _log.clear();
44       _log.close();
45     }
46     void CommitLog::refUp() {
47       if ( !_refcnt )
48         openLog();
49       ++_refcnt;
50     }
51     void CommitLog::refDown() {
52       --_refcnt;
53       if ( !_refcnt )
54         closeLog();
55     }
56
57     std::ostream & CommitLog::operator()( bool timestamp ) {
58       if ( timestamp ) {
59         _log << Date(Date::now()).form( "%Y-%m-%d %H:%M:%S ");
60       }
61       return _log;
62     }
63
64     void CommitLog::setFname( const Pathname & fname_r ) {
65       MIL << "installation log file " << fname_r << endl;
66       if ( _refcnt )
67         closeLog();
68       _fname = fname_r;
69       if ( ! _fname.empty() )
70         filesystem::assert_dir( _fname.dirname() );
71       if ( _refcnt )
72         openLog();
73     }
74
75     const Pathname & CommitLog::fname()
76     { return _fname; }
77
78   } // namespace target
79 } // namespace zypp