libzypp  17.25.1
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 
70  if ( fields.size() < 2 ) {
71  WAR << "Ignore invalid history log entry on line #" << lineNr_r << " '"<< line_r << "'" << endl;
72  return true; // At least an action field[1] is needed!
73  }
74  fields[1] = str::trim( std::move(fields[1]) ); // for whatever reason writer is padding the action field
75 
76  if ( !_actionfilter.empty() && !_actionfilter.count( fields[1] ) )
77  return true;
78 
79  // move into data class
81  try
82  {
83  data = HistoryLogData::create( fields );
84  }
85  catch ( const Exception & excpt )
86  {
87  ZYPP_CAUGHT( excpt );
88  if ( _options.testFlag( IGNORE_INVALID_ITEMS ) )
89  {
90  WAR << "Ignore invalid history log entry on line #" << lineNr_r << " '"<< line_r << "'" << endl;
91  return true;
92  }
93  else
94  {
95  ERR << "Invalid history log entry on line #" << lineNr_r << " '"<< line_r << "'" << endl;
96  ParseException newexcpt( str::Str() << "Error in history log on line #" << lineNr_r );
97  newexcpt.remember( excpt );
98  ZYPP_THROW( newexcpt );
99  }
100  }
101 
102  // consume data
103  if ( _callback && !_callback( data ) )
104  {
105  WAR << "Stop parsing requested by consumer callback on line #" << lineNr_r << endl;
106  return false;
107  }
108  return true;
109  }
110 
112  {
113  InputStream is( _filename );
114  iostr::EachLine line( is );
115 
116  ProgressData pd;
117  pd.sendTo( progress_r );
118  pd.toMin();
119 
120  for ( ; line; line.next(), pd.tick() )
121  {
122  // ignore comments
123  if ( (*line)[0] == '#' )
124  continue;
125 
126  if ( ! parseLine( *line, line.lineNo() ) )
127  break; // requested by consumer callback
128  }
129 
130  pd.toMax();
131  }
132 
133  void HistoryLogReader::Impl::readFrom( const Date & date_r, const ProgressData::ReceiverFnc & progress_r )
134  {
135  InputStream is( _filename );
136  iostr::EachLine line( is );
137 
138  ProgressData pd;
139  pd.sendTo( progress_r );
140  pd.toMin();
141 
142  bool pastDate = false;
143  for ( ; line; line.next(), pd.tick() )
144  {
145  const std::string & s = *line;
146 
147  // ignore comments
148  if ( s[0] == '#' )
149  continue;
150 
151  if ( pastDate )
152  {
153  if ( ! parseLine( s, line.lineNo() ) )
154  break; // requested by consumer callback
155  }
156  else
157  {
158  Date logDate( s.substr( 0, s.find('|') ), HISTORY_LOG_DATE_FORMAT );
159  if ( logDate > date_r )
160  {
161  pastDate = true;
162  if ( ! parseLine( s, line.lineNo() ) )
163  break; // requested by consumer callback
164  }
165  }
166  }
167 
168  pd.toMax();
169  }
170 
171  void HistoryLogReader::Impl::readFromTo( const Date & fromDate_r, const Date & toDate_r, const ProgressData::ReceiverFnc & progress_r )
172  {
173  InputStream is( _filename );
174  iostr::EachLine line( is );
175 
176  ProgressData pd;
177  pd.sendTo( progress_r );
178  pd.toMin();
179 
180  bool pastFromDate = false;
181  for ( ; line; line.next(), pd.tick() )
182  {
183  const std::string & s = *line;
184 
185  // ignore comments
186  if ( s[0] == '#' )
187  continue;
188 
189  Date logDate( s.substr( 0, s.find('|') ), HISTORY_LOG_DATE_FORMAT );
190 
191  // past toDate - stop reading
192  if ( logDate >= toDate_r )
193  break;
194 
195  // past fromDate - start reading
196  if ( !pastFromDate && logDate > fromDate_r )
197  pastFromDate = true;
198 
199  if ( pastFromDate )
200  {
201  if ( ! parseLine( s, line.lineNo() ) )
202  break; // requested by consumer callback
203  }
204  }
205 
206  pd.toMax();
207  }
208 
210  //
211  // class HistoryLogReader
212  //
214 
215  HistoryLogReader::HistoryLogReader( const Pathname & historyFile_r, const Options & options_r, const ProcessData & callback_r )
216  : _pimpl( new HistoryLogReader::Impl( historyFile_r, options_r, callback_r ) )
217  {}
218 
220  {}
221 
222  void HistoryLogReader::setIgnoreInvalidItems( bool ignoreInvalid_r )
223  { _pimpl->_options.setFlag( IGNORE_INVALID_ITEMS, ignoreInvalid_r ); }
224 
226  { return _pimpl->_options.testFlag( IGNORE_INVALID_ITEMS ); }
227 
229  { _pimpl->readAll( progress_r ); }
230 
231  void HistoryLogReader::readFrom( const Date & date_r, const ProgressData::ReceiverFnc & progress_r )
232  { _pimpl->readFrom( date_r, progress_r ); }
233 
234  void HistoryLogReader::readFromTo( const Date & fromDate_r, const Date & toDate_r, const ProgressData::ReceiverFnc & progress_r )
235  { _pimpl->readFromTo( fromDate_r, toDate_r, progress_r ); }
236 
238  { _pimpl->addActionFilter( action_r ); }
239 
240  } // namespace parser
242 } // 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:111
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:234
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:225
zypp::parser::HistoryLogReader::Impl::readFromTo
void readFromTo(const Date &fromDate_r, const Date &toDate_r, const ProgressData::ReceiverFnc &progress_r)
Definition: HistoryLogReader.cc:171
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:231
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:133
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:222
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:237
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:219
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:228
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:215
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