LibPkg
log.h
Go to the documentation of this file.
1 // This file is part of LibPkg.
2 //
3 // Copyright 2003-2020 Graham Shaw
4 // Copyright 2013-2020 Alan Buckley
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 
18 #ifndef LIBPKG_LOG
19 #define LIBPKG_LOG
20 
21 #include <string>
22 #include <vector>
23 #include <ostream>
24 
25 namespace pkg {
26 
30  enum LogCode {
53  LOG_TRACE = 0x20000,
136  };
137 
141  class log_entry
142  {
144  LogCode _code;
146  int _when;
148  char *_param1;
150  char *_param2;
151 
152  public:
156  log_entry();
157 
161  log_entry(const log_entry &other);
162 
164  ~log_entry();
165 
169  log_entry &operator=(const log_entry &other);
170 
178  log_entry(LogCode code, const char *param1 = 0, const char *param2 = 0);
179 
185  int code() const { return _code; }
186 
192  int type() const { return (_code >> 16); }
193 
199  int sub_code() const { return (_code & 0xFFFF); }
200 
206  int when() const { return _when; }
207 
213  std::string when_text() const;
214 
220  std::string text() const;
221  };
222 
226  class log
227  {
229  std::vector<log_entry> _entries;
231  unsigned int _counts[2];
233  bool _bad;
234 
235  public:
239  log();
240 
246  bool bad() const { return _bad; }
247 
255  void message(LogCode code, const char *param1 = 0, const char *param2 = 0);
256 
263  void message(LogCode code, const std::string &param) { message(code, param.c_str()); }
271  void message(LogCode code, const std::string &param1, const std::string &param2) { message(code, param1.c_str(), param2.c_str()); }
272 
274  typedef std::vector<log_entry>::const_iterator const_iterator;
276  const_iterator begin() const { return _entries.begin(); }
278  const_iterator end() const { return _entries.end(); }
280  unsigned int size() const { return _entries.size(); }
282  unsigned int errors() const { return _counts[0]; }
284  unsigned int warnings() const { return _counts[1]; }
285 
287  const log_entry &operator[](int index) const { return _entries[index]; }
289  const log_entry &entry(int index) const { return _entries[index]; }
290 
292  friend std::ostream &operator<<(std::ostream &stream, const log &olog);
293  };
294 
295  std::ostream &operator<<(std::ostream &stream, const log &olog);
296 };
297 
298 #endif
Definition: log.h:81
Definition: log.h:77
Definition: log.h:94
Definition: log.h:113
Definition: log.h:100
std::ostream & operator<<(std::ostream &out, const component &comp)
Write component record to output stream.
Definition: component.cc:190
int sub_code() const
Log entry sub code.
Definition: log.h:199
~log_entry()
Destroy entry freeing memory.
Definition: log.cc:184
const_iterator begin() const
Iterator to first log entry.
Definition: log.h:276
log_entry()
Construct uninitialised log entry.
Definition: log.cc:172
std::string when_text() const
Time of log entry as text.
Definition: log.cc:210
void message(LogCode code, const std::string &param1, const std::string &param2)
Add a new entry to the log from two standard strings.
Definition: log.h:271
Definition: log.h:131
Definition: log.h:132
void message(LogCode code, const std::string &param)
Add a new entry to the log from a standard string.
Definition: log.h:263
std::vector< log_entry >::const_iterator const_iterator
Iterator type for the log entries.
Definition: log.h:274
Definition: log.h:82
unsigned int errors() const
Number of errors.
Definition: log.h:282
Definition: log.h:79
Definition: log.h:104
Definition: log.h:88
Definition: log.h:110
The namespace used to hold the package management library.
Definition: auto_dir.cc:23
int type() const
Log entry type.
Definition: log.h:192
Definition: log.h:36
Definition: log.h:80
Definition: log.h:102
unsigned int warnings() const
Number of warnings.
Definition: log.h:284
const log_entry & entry(int index) const
Return entry for a given index.
Definition: log.h:289
Definition: log.h:71
Definition: log.h:118
Definition: log.h:87
Definition: log.h:78
Definition: log.h:103
log_entry & operator=(const log_entry &other)
Assignment.
Definition: log.cc:190
Definition: log.h:53
Definition: log.h:34
Definition: log.h:134
Definition: log.h:116
Definition: log.h:85
Definition: log.h:115
Definition: log.h:55
const log_entry & operator[](int index) const
Return entry for a given index.
Definition: log.h:287
Definition: log.h:125
Definition: log.h:61
Definition: log.h:109
Definition: log.h:117
Definition: log.h:89
Definition: log.h:64
Definition: log.h:38
Definition: log.h:68
Definition: log.h:133
Definition: log.h:73
Definition: log.h:65
int when() const
Time since midnight of log entry.
Definition: log.h:206
Definition: log.h:97
Definition: log.h:107
Definition: log.h:108
Definition: log.h:76
Class to log actions that occur in LibPkg.
Definition: log.h:226
Definition: log.h:101
Definition: log.h:62
Definition: log.h:122
Definition: log.h:39
bool bad() const
The log failed to add one or more items.
Definition: log.h:246
Definition: log.h:114
Definition: log.h:98
std::string text() const
Description of this log entry.
Definition: log.cc:225
Definition: log.h:54
Definition: log.h:112
Definition: log.h:75
Definition: log.h:67
Definition: log.h:91
Definition: log.h:31
Definition: log.h:86
Definition: log.h:72
Definition: log.h:74
Definition: log.h:119
int code() const
Log entry error code.
Definition: log.h:185
Definition: log.h:111
unsigned int size() const
Total number of entries.
Definition: log.h:280
const_iterator end() const
Iterator to last log entry.
Definition: log.h:278
Definition: log.h:41
A class to represent one log entry.
Definition: log.h:141
LogCode
An enumeration of all the items that can be logged.
Definition: log.h:30
Definition: log.h:60
Definition: log.h:121
Definition: log.h:135

Reference Manual LibPkg Version 0.9.0 (4 Sep 2020)