libzypp  17.23.5
HistoryLogReader.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 
13 #include <iostream>
14 
15 #include <zypp/base/InputStream.h>
16 #include <zypp/base/IOStream.h>
17 #include <zypp/base/Logger.h>
19 
21 
22 using std::endl;
23 
25 namespace zypp
26 {
28  namespace parser
29  {
30 
32  //
33  // class HistoryLogReader::Impl
34  //
37  {
38  Impl( const Pathname & historyFile_r, const Options & options_r, const ProcessData & callback_r )
39  : _filename( historyFile_r )
40  , _options( options_r )
41  , _callback( callback_r )
42  {}
43 
44  bool parseLine( const std::string & line_r, unsigned int lineNr_r );
45 
46  void readAll( const ProgressData::ReceiverFnc & progress_r );
47  void readFrom( const Date & date_r, const ProgressData::ReceiverFnc & progress_r );
48  void readFromTo( const Date & fromDate_r, const Date & toDate_r, const ProgressData::ReceiverFnc & progress_r );
49 
50  void addActionFilter( const HistoryActionID & action_r )
51  {
52  if ( action_r == HistoryActionID::NONE )
53  _actionfilter.clear();
54  else
55  _actionfilter.insert( action_r.asString() );
56  }
57 
59  Options _options;
61  std::set<std::string> _actionfilter;
62  };
63 
64  bool HistoryLogReader::Impl::parseLine( const std::string & line_r, unsigned lineNr_r )
65  {
66  // parse into fields
68  str::splitEscaped( line_r, std::back_inserter(fields), "|", true );
69  if ( fields.size() >= 2 )
70  fields[1] = str::trim( std::move(fields[1]) ); // for whatever reason writer is padding the action field
71 
72  if ( !_actionfilter.empty() && !_actionfilter.count( fields[1] ) )
73  return true;
74 
75  // move into data class
77  try
78  {
79  data = HistoryLogData::create( fields );
80  }
81  catch ( const Exception & excpt )
82  {
83  ZYPP_CAUGHT( excpt );
84  if ( _options.testFlag( IGNORE_INVALID_ITEMS ) )
85  {
86  WAR << "Ignore invalid history log entry on line #" << lineNr_r << " '"<< line_r << "'" << endl;
87  return true;
88  }
89  else
90  {
91  ERR << "Invalid history log entry on line #" << lineNr_r << " '"<< line_r << "'" << endl;
92  ParseException newexcpt( str::Str() << "Error in history log on line #" << lineNr_r );
93  newexcpt.remember( excpt );
94  ZYPP_THROW( newexcpt );
95  }
96  }
97 
98  // consume data
99  if ( _callback && !_callback( data ) )
100  {
101  WAR << "Stop parsing requested by consumer callback on line #" << lineNr_r << endl;
102  return false;
103  }
104  return true;
105  }
106 
108  {
109  InputStream is( _filename );
110  iostr::EachLine line( is );
111 
112  ProgressData pd;
113  pd.sendTo( progress_r );
114  pd.toMin();
115 
116  for ( ; line; line.next(), pd.tick() )
117  {
118  // ignore comments
119  if ( (*line)[0] == '#' )
120  continue;
121 
122  if ( ! parseLine( *line, line.lineNo() ) )
123  break; // requested by consumer callback
124  }
125 
126  pd.toMax();
127  }
128 
129  void HistoryLogReader::Impl::readFrom( const Date & date_r, const ProgressData::ReceiverFnc & progress_r )
130  {
131  InputStream is( _filename );
132  iostr::EachLine line( is );
133 
134  ProgressData pd;
135  pd.sendTo( progress_r );
136  pd.toMin();
137 
138  bool pastDate = false;
139  for ( ; line; line.next(), pd.tick() )
140  {
141  const std::string & s = *line;
142 
143  // ignore comments
144  if ( s[0] == '#' )
145  continue;
146 
147  if ( pastDate )
148  {
149  if ( ! parseLine( s, line.lineNo() ) )
150  break; // requested by consumer callback
151  }
152  else
153  {
154  Date logDate( s.substr( 0, s.find('|') ), HISTORY_LOG_DATE_FORMAT );
155  if ( logDate > date_r )
156  {
157  pastDate = true;
158  if ( ! parseLine( s, line.lineNo() ) )
159  break; // requested by consumer callback
160  }
161  }
162  }
163 
164  pd.toMax();
165  }
166 
167  void HistoryLogReader::Impl::readFromTo( const Date & fromDate_r, const Date & toDate_r, const ProgressData::ReceiverFnc & progress_r )
168  {
169  InputStream is( _filename );
170  iostr::EachLine line( is );
171 
172  ProgressData pd;
173  pd.sendTo( progress_r );
174  pd.toMin();
175 
176  bool pastFromDate = false;
177  for ( ; line; line.next(), pd.tick() )
178  {
179  const std::string & s = *line;
180 
181  // ignore comments
182  if ( s[0] == '#' )
183  continue;
184 
185  Date logDate( s.substr( 0, s.find('|') ), HISTORY_LOG_DATE_FORMAT );
186 
187  // past toDate - stop reading
188  if ( logDate >= toDate_r )
189  break;
190 
191  // past fromDate - start reading
192  if ( !pastFromDate && logDate > fromDate_r )
193  pastFromDate = true;
194 
195  if ( pastFromDate )
196  {
197  if ( ! parseLine( s, line.lineNo() ) )
198  break; // requested by consumer callback
199  }
200  }
201 
202  pd.toMax();
203  }
204 
206  //
207  // class HistoryLogReader
208  //
210 
211  HistoryLogReader::HistoryLogReader( const Pathname & historyFile_r, const Options & options_r, const ProcessData & callback_r )
212  : _pimpl( new HistoryLogReader::Impl( historyFile_r, options_r, callback_r ) )
213  {}
214 
216  {}
217 
218  void HistoryLogReader::setIgnoreInvalidItems( bool ignoreInvalid_r )
219  { _pimpl->_options.setFlag( IGNORE_INVALID_ITEMS, ignoreInvalid_r ); }
220 
222  { return _pimpl->_options.testFlag( IGNORE_INVALID_ITEMS ); }
223 
225  { _pimpl->readAll( progress_r ); }
226 
227  void HistoryLogReader::readFrom( const Date & date_r, const ProgressData::ReceiverFnc & progress_r )
228  { _pimpl->readFrom( date_r, progress_r ); }
229 
230  void HistoryLogReader::readFromTo( const Date & fromDate_r, const Date & toDate_r, const ProgressData::ReceiverFnc & progress_r )
231  { _pimpl->readFromTo( fromDate_r, toDate_r, progress_r ); }
232 
234  { _pimpl->addActionFilter( action_r ); }
235 
236  } // namespace parser
238 } // namespace zypp
zypp::iostr::EachLine::next
bool next()
Advance to next line.
Definition: IOStream.cc:71
zypp::parser::HistoryLogReader::Impl::_actionfilter
std::set< std::string > _actionfilter
Definition: HistoryLogReader.cc:61
zypp::HistoryActionID::asString
const std::string & asString(bool pad=false) const
Definition: HistoryLogData.cc:76
zypp::parser::HistoryLogReader::Impl::readAll
void readAll(const ProgressData::ReceiverFnc &progress_r)
Definition: HistoryLogReader.cc:107
zypp::Exception
Base class for Exception.
Definition: Exception.h:145
zypp::HistoryLogData::FieldVector
std::vector< std::string > FieldVector
Definition: HistoryLogData.h:111
zypp::parser::HistoryLogReader::readFromTo
void readFromTo(const Date &fromDate, const Date &toDate, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Read log between fromDate and toDate.
Definition: HistoryLogReader.cc:230
zypp::parser::HistoryLogReader::Impl
Definition: HistoryLogReader.cc:36
zypp::parser::HistoryLogReader::Impl::Impl
Impl(const Pathname &historyFile_r, const Options &options_r, const ProcessData &callback_r)
Definition: HistoryLogReader.cc:38
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:392
zypp::parser::HistoryLogReader::ignoreInvalidItems
bool ignoreInvalidItems() const
Whether the reader is set to ignore invalid log entries.
Definition: HistoryLogReader.cc:221
zypp::parser::HistoryLogReader::Impl::readFromTo
void readFromTo(const Date &fromDate_r, const Date &toDate_r, const ProgressData::ReceiverFnc &progress_r)
Definition: HistoryLogReader.cc:167
zypp::parser::HistoryLogReader::Impl::_filename
Pathname _filename
Definition: HistoryLogReader.cc:58
IOStream.h
zypp::parser::HistoryLogReader::Impl::addActionFilter
void addActionFilter(const HistoryActionID &action_r)
Definition: HistoryLogReader.cc:50
ParseException.h
zypp::HistoryActionID::NONE
static const HistoryActionID NONE
Definition: HistoryLogData.h:39
zypp::parser::HistoryLogReader
Zypp history file parser.
Definition: HistoryLogReader.h:66
zypp::Exception::remember
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:105
zypp::parser::HistoryLogReader::readFrom
void readFrom(const Date &date, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Read log from specified date.
Definition: HistoryLogReader.cc:227
zypp::ProgressData::tick
bool tick()
Leave counter value unchanged (still alive).
Definition: ProgressData.h:277
Logger.h
WAR
#define WAR
Definition: Logger.h:80
zypp::parser::ParseException
Definition: ParseException.h:33
zypp::str::splitEscaped
unsigned splitEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
Definition: String.h:591
zypp::parser::HistoryLogReader::Impl::readFrom
void readFrom(const Date &date_r, const ProgressData::ReceiverFnc &progress_r)
Definition: HistoryLogReader.cc:129
zypp::parser::HistoryLogReader::setIgnoreInvalidItems
void setIgnoreInvalidItems(bool ignoreInvalid=false)
Set the reader to ignore invalid log entries and continue with the rest.
Definition: HistoryLogReader.cc:218
zypp::InputStream
Helper to create and pass std::istream.
Definition: InputStream.h:56
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::parser::HistoryLogReader::Impl::_callback
ProcessData _callback
Definition: HistoryLogReader.cc:60
zypp::parser::HistoryLogReader::Impl::_options
Options _options
Definition: HistoryLogReader.cc:59
HistoryLogReader.h
zypp::iostr::EachLine::lineNo
unsigned lineNo() const
Return the current line number.
Definition: IOStream.h:126
zypp::str::Str
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:208
zypp::parser::HistoryLogReader::ProcessData
function< bool(const HistoryLogData::Ptr &)> ProcessData
Callback type to consume a single history line split into fields.
Definition: HistoryLogReader.h:80
zypp::parser::HistoryLogReader::IGNORE_INVALID_ITEMS
ignore invalid items and continue parsing
Definition: HistoryLogReader.h:72
zypp::parser::HistoryLogReader::_pimpl
RW_pointer< Impl, rw_pointer::Scoped< Impl > > _pimpl
Definition: HistoryLogReader.h:156
zypp::parser::HistoryLogReader::addActionFilter
void addActionFilter(const HistoryActionID &action_r)
Process only specific HistoryActionIDs.
Definition: HistoryLogReader.cc:233
zypp::ProgressData::ReceiverFnc
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: ProgressData.h:139
zypp::parser::HistoryLogReader::~HistoryLogReader
~HistoryLogReader()
Definition: HistoryLogReader.cc:215
zypp::Date
Store and operate on date (time_t).
Definition: Date.h:32
zypp::ProgressData::sendTo
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition: ProgressData.h:226
zypp::ProgressData::toMax
bool toMax()
Set counter value to current max value (unless no range).
Definition: ProgressData.h:273
zypp::HistoryLogData::create
static Ptr create(FieldVector &fields_r)
Factory method creating HistoryLogData classes.
Definition: HistoryLogData.cc:183
zypp::filesystem::Pathname
Pathname.
Definition: Pathname.h:44
zypp::iostr::EachLine
Simple lineparser: Traverse each line in a file.
Definition: IOStream.h:111
HISTORY_LOG_DATE_FORMAT
#define HISTORY_LOG_DATE_FORMAT
Definition: HistoryLogData.h:26
ERR
#define ERR
Definition: Logger.h:81
zypp::parser::HistoryLogReader::readAll
void readAll(const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Read the whole log file.
Definition: HistoryLogReader.cc:224
zypp::ProgressData::toMin
bool toMin()
Set counter value to current min value.
Definition: ProgressData.h:269
InputStream.h
zypp::parser::HistoryLogReader::Impl::parseLine
bool parseLine(const std::string &line_r, unsigned int lineNr_r)
Definition: HistoryLogReader.cc:64
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:396
zypp::HistoryActionID
Enumeration of known history actions.
Definition: HistoryLogData.h:37
zypp::parser::HistoryLogReader::HistoryLogReader
HistoryLogReader(const Pathname &historyFile_r, const Options &options_r, const ProcessData &callback_r)
Ctor taking file to parse and data consumer callback.
Definition: HistoryLogReader.cc:211
zypp::str::trim
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:216
zypp::ProgressData
Maintain [min,max] and counter (value) for progress counting.
Definition: ProgressData.h:130
zypp::HistoryLogData::Ptr
shared_ptr< HistoryLogData > Ptr
Definition: HistoryLogData.h:108