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 
37  LOG_TRACE = 0x20000,
99  };
100 
104  class log_entry
105  {
107  LogCode _code;
109  int _when;
111  char *_param1;
113  char *_param2;
114 
115  public:
119  log_entry();
120 
124  log_entry(const log_entry &other);
125 
127  ~log_entry();
128 
132  log_entry &operator=(const log_entry &other);
133 
141  log_entry(LogCode code, const char *param1 = 0, const char *param2 = 0);
142 
148  int code() const {return _code;}
149 
155  int type() const {return (_code >> 16);}
156 
162  int sub_code() const {return (_code & 0xFFFF);}
163 
169  int when() const {return _when;}
170 
176  std::string when_text() const;
177 
183  std::string text() const;
184  };
185 
189  class log
190  {
192  std::vector<log_entry> _entries;
194  unsigned int _counts[2];
196  bool _bad;
197 
198  public:
202  log();
203 
209  bool bad() const {return _bad;}
210 
218  void message(LogCode code, const char *param1 = 0, const char *param2 = 0);
219 
226  void message(LogCode code, const std::string &param) {message(code, param.c_str());}
234  void message(LogCode code, const std::string &param1, const std::string &param2) {message(code, param1.c_str(), param2.c_str());}
235 
237  typedef std::vector<log_entry>::const_iterator const_iterator;
239  const_iterator begin() const {return _entries.begin();}
241  const_iterator end() const {return _entries.end();}
243  unsigned int size() const {return _entries.size();}
245  unsigned int errors() const {return _counts[0];}
247  unsigned int warnings() const {return _counts[1];}
248 
250  const log_entry &operator[](int index) const {return _entries[index];}
252  const log_entry &entry(int index) const {return _entries[index];}
253 
255  friend std::ostream &operator<<(std::ostream &stream, const log &olog);
256  };
257 
258  std::ostream &operator<<(std::ostream &stream, const log &olog);
259 };
260 
261 #endif
Definition: log.h:64
Definition: log.h:60
Definition: log.h:74
Definition: log.h:92
Definition: log.h:79
std::ostream & operator<<(std::ostream &out, const component &comp)
Write component record to output stream.
Definition: component.cc:175
int type() const
Log entry type.
Definition: log.h:155
~log_entry()
Destroy entry freeing memory.
Definition: log.cc:148
log_entry()
Construct uninitialised log entry.
Definition: log.cc:136
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:234
void message(LogCode code, const std::string &param)
Add a new entry to the log from a standard string.
Definition: log.h:226
unsigned int errors() const
Number of errors.
Definition: log.h:245
std::vector< log_entry >::const_iterator const_iterator
Iterator type for the log entries.
Definition: log.h:237
Definition: log.h:65
int code() const
Log entry error code.
Definition: log.h:148
Definition: log.h:62
Definition: log.h:83
Definition: log.h:70
Definition: log.h:89
const_iterator begin() const
Iterator to first log entry.
Definition: log.h:239
Definition: log.h:24
Definition: log.h:63
Definition: log.h:81
friend std::ostream & operator<<(std::ostream &stream, const log &olog)
Output log as text.
Definition: log.cc:256
const log_entry & entry(int index) const
Return entry for a given index.
Definition: log.h:252
Definition: log.h:54
Definition: log.h:97
Definition: log.h:69
Definition: log.h:61
Definition: log.h:82
log_entry & operator=(const log_entry &other)
Assignment.
Definition: log.cc:154
const_iterator end() const
Iterator to last log entry.
Definition: log.h:241
Definition: log.h:37
Definition: log.h:22
log()
Construct an empty log.
Definition: log.cc:237
Definition: log.h:95
Definition: log.h:67
Definition: log.h:94
Definition: log.h:39
int sub_code() const
Log entry sub code.
Definition: log.h:162
Definition: log.h:45
Definition: log.h:88
Definition: log.h:96
Definition: log.h:71
Definition: log.h:48
Definition: log.h:26
Definition: log.h:56
Definition: log.h:49
std::string text() const
Description of this log entry.
Definition: log.cc:189
Definition: log.h:76
Definition: log.h:86
Definition: log.h:87
Definition: log.h:59
Class to log actions that occur in LibPkg.
Definition: log.h:189
Definition: log.h:80
Definition: log.h:46
const log_entry & operator[](int index) const
Return entry for a given index.
Definition: log.h:250
Definition: log.h:27
Definition: log.h:93
void message(LogCode code, const char *param1=0, const char *param2=0)
Add a new entry to the log.
Definition: log.cc:242
Definition: log.h:77
Definition: log.h:38
Definition: log.h:91
Definition: log.h:58
Definition: log.h:51
Definition: log.h:72
Definition: log.h:19
Definition: log.h:68
unsigned int size() const
Total number of entries.
Definition: log.h:243
std::string when_text() const
Time of log entry as text.
Definition: log.cc:174
Definition: log.h:55
Definition: log.h:57
Definition: log.h:98
Definition: log.h:90
unsigned int warnings() const
Number of warnings.
Definition: log.h:247
Definition: log.h:28
A class to represent one log entry.
Definition: log.h:104
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:169
Definition: log.h:44
bool bad() const
The log failed to add one or more items.
Definition: log.h:209

Reference Manual LibPkg Version 0.6.1 (28 Jan 2015)