LibPkg
log.h
Go to the documentation of this file.
1 // This file is part of LibPkg.
2 // Copyright � 2003-2005 Graham Shaw.
3 // Copyright � 2013 Alan Buckley
4 // Distribution and use are subject to the GNU Lesser General Public License,
5 // a copy of which may be found in the file !LibPkg.Copyright.
6 
7 #ifndef LIBPKG_LOG
8 #define LIBPKG_LOG
9 
10 #include <string>
11 #include <vector>
12 #include <ostream>
13 
14 namespace pkg {
15 
19  enum LogCode {
42  LOG_TRACE = 0x20000,
116  };
117 
121  class log_entry
122  {
124  LogCode _code;
126  int _when;
128  char *_param1;
130  char *_param2;
131 
132  public:
136  log_entry();
137 
141  log_entry(const log_entry &other);
142 
144  ~log_entry();
145 
149  log_entry &operator=(const log_entry &other);
150 
158  log_entry(LogCode code, const char *param1 = 0, const char *param2 = 0);
159 
165  int code() const { return _code; }
166 
172  int type() const { return (_code >> 16); }
173 
179  int sub_code() const { return (_code & 0xFFFF); }
180 
186  int when() const { return _when; }
187 
193  std::string when_text() const;
194 
200  std::string text() const;
201  };
202 
206  class log
207  {
209  std::vector<log_entry> _entries;
211  unsigned int _counts[2];
213  bool _bad;
214 
215  public:
219  log();
220 
226  bool bad() const { return _bad; }
227 
235  void message(LogCode code, const char *param1 = 0, const char *param2 = 0);
236 
243  void message(LogCode code, const std::string &param) { message(code, param.c_str()); }
251  void message(LogCode code, const std::string &param1, const std::string &param2) { message(code, param1.c_str(), param2.c_str()); }
252 
254  typedef std::vector<log_entry>::const_iterator const_iterator;
256  const_iterator begin() const { return _entries.begin(); }
258  const_iterator end() const { return _entries.end(); }
260  unsigned int size() const { return _entries.size(); }
262  unsigned int errors() const { return _counts[0]; }
264  unsigned int warnings() const { return _counts[1]; }
265 
267  const log_entry &operator[](int index) const { return _entries[index]; }
269  const log_entry &entry(int index) const { return _entries[index]; }
270 
272  friend std::ostream &operator<<(std::ostream &stream, const log &olog);
273  };
274 
275  std::ostream &operator<<(std::ostream &stream, const log &olog);
276 };
277 
278 #endif
Definition: log.h:69
Definition: log.h:65
Definition: log.h:79
Definition: log.h:97
Definition: log.h:84
std::ostream & operator<<(std::ostream &out, const component &comp)
Write component record to output stream.
Definition: component.cc:179
int type() const
Log entry type.
Definition: log.h:172
~log_entry()
Destroy entry freeing memory.
Definition: log.cc:164
log_entry()
Construct uninitialised log entry.
Definition: log.cc:152
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:251
Definition: log.h:115
void message(LogCode code, const std::string &param)
Add a new entry to the log from a standard string.
Definition: log.h:243
unsigned int errors() const
Number of errors.
Definition: log.h:262
std::vector< log_entry >::const_iterator const_iterator
Iterator type for the log entries.
Definition: log.h:254
Definition: log.h:70
int code() const
Log entry error code.
Definition: log.h:165
Definition: log.h:67
Definition: log.h:88
Definition: log.h:75
Definition: log.h:94
const_iterator begin() const
Iterator to first log entry.
Definition: log.h:256
Definition: log.h:25
Definition: log.h:68
Definition: log.h:86
friend std::ostream & operator<<(std::ostream &stream, const log &olog)
Output log as text.
Definition: log.cc:274
const log_entry & entry(int index) const
Return entry for a given index.
Definition: log.h:269
Definition: log.h:59
Definition: log.h:102
Definition: log.h:74
Definition: log.h:66
Definition: log.h:87
log_entry & operator=(const log_entry &other)
Assignment.
Definition: log.cc:170
const_iterator end() const
Iterator to last log entry.
Definition: log.h:258
Definition: log.h:42
Definition: log.h:23
log()
Construct an empty log.
Definition: log.cc:254
Definition: log.h:100
Definition: log.h:72
Definition: log.h:99
Definition: log.h:44
Definition: log.h:109
int sub_code() const
Log entry sub code.
Definition: log.h:179
Definition: log.h:50
Definition: log.h:93
Definition: log.h:101
Definition: log.h:76
Definition: log.h:53
Definition: log.h:27
Definition: log.h:61
Definition: log.h:54
std::string text() const
Description of this log entry.
Definition: log.cc:205
Definition: log.h:81
Definition: log.h:91
Definition: log.h:92
Definition: log.h:64
Class to log actions that occur in LibPkg.
Definition: log.h:206
Definition: log.h:85
Definition: log.h:51
Definition: log.h:106
const log_entry & operator[](int index) const
Return entry for a given index.
Definition: log.h:267
Definition: log.h:28
Definition: log.h:98
void message(LogCode code, const char *param1=0, const char *param2=0)
Add a new entry to the log.
Definition: log.cc:259
Definition: log.h:82
Definition: log.h:43
Definition: log.h:96
Definition: log.h:63
Definition: log.h:56
Definition: log.h:77
Definition: log.h:20
Definition: log.h:73
unsigned int size() const
Total number of entries.
Definition: log.h:260
std::string when_text() const
Time of log entry as text.
Definition: log.cc:190
Definition: log.h:60
Definition: log.h:62
Definition: log.h:103
Definition: log.h:95
unsigned int warnings() const
Number of warnings.
Definition: log.h:264
Definition: log.h:30
A class to represent one log entry.
Definition: log.h:121
LogCode
An enumeration of all the items that can be logged.
Definition: log.h:19
int when() const
Time since midnight of log entry.
Definition: log.h:186
Definition: log.h:49
Definition: log.h:105
bool bad() const
The log failed to add one or more items.
Definition: log.h:226

Reference Manual LibPkg Version 0.6.1 (28 Jan 2015)