libDwm-0.6.0
DwmDataLog.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmDataLog.hh 8401 $
3 // @(#) $Id: DwmDataLog.hh 8401 2016-04-17 06:44:31Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2008, 2016
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMDATALOG_HH_
43 #define _DWMDATALOG_HH_
44 
45 #include <cassert>
46 #include <fstream>
47 #include <iomanip>
48 #include <sstream>
49 #include <string>
50 
51 #include "DwmIO.hh"
52 #include "DwmSysLogger.hh"
53 #include "DwmTimeValue.hh"
54 
55 namespace Dwm {
56 
57  //--------------------------------------------------------------------------
61  //--------------------------------------------------------------------------
62  template <typename T>
63  class DataLog
64  {
65  public:
66  //------------------------------------------------------------------------
68  //------------------------------------------------------------------------
69  class Entry
70  {
71  public:
72  //----------------------------------------------------------------------
74  //----------------------------------------------------------------------
76  : _timestamp(0,0), _value()
77  {}
78 
79  //----------------------------------------------------------------------
81  //----------------------------------------------------------------------
82  Entry(const T & value)
83  : _timestamp(true), _value(value)
84  {}
85 
86  //----------------------------------------------------------------------
88  //----------------------------------------------------------------------
89  const TimeValue & Timestamp() const
90  {
91  return(_timestamp);
92  }
93 
94  //----------------------------------------------------------------------
96  //----------------------------------------------------------------------
97  const T & Value() const
98  {
99  return(_value);
100  }
101 
102  //----------------------------------------------------------------------
104  //----------------------------------------------------------------------
105  std::istream & Read(std::istream & is)
106  {
107  if (IO::Read(is, _timestamp))
108  IO::Read(is, _value);
109  return(is);
110  }
111 
112  //----------------------------------------------------------------------
114  //----------------------------------------------------------------------
115  std::ostream & Write(std::ostream & os) const
116  {
117  if (IO::Write(os, _timestamp))
118  IO::Write(os, _value);
119  return(os);
120  }
121 
122  //----------------------------------------------------------------------
125  //----------------------------------------------------------------------
126  size_t StreamedLength() const
127  {
128  return(IO::StreamedLength(_timestamp) + IO::StreamedLength(_value));
129  }
130 
131  //----------------------------------------------------------------------
134  //----------------------------------------------------------------------
135  friend std::ostream &
136  operator << (std::ostream & os, const Entry & entry)
137  {
138  struct tm localTime = entry._timestamp.LocalTime();
139  char buf[32];
140  size_t n = strftime(buf, 32, "%m/%d/%Y %H:%M:%S", &localTime);
141  std::ostringstream ostr;
142  ostr << buf << '.'
143  << std::setfill('0')
144  << std::setw(6) << entry._timestamp.Usecs();
145  os << ostr.str() << " " << entry._value;
146  return(os);
147  }
148 
149  private:
150  TimeValue _timestamp;
151  T _value;
152  };
153 
154  //------------------------------------------------------------------------
156  //------------------------------------------------------------------------
157  DataLog(const std::string & filename)
158  : _filename(filename), _s(0), _mode(), _mtx()
159  {
160  assert(! _filename.empty());
161  }
162 
163  //------------------------------------------------------------------------
165  //------------------------------------------------------------------------
167  {
168  Close();
169  }
170 
171  //------------------------------------------------------------------------
173  //------------------------------------------------------------------------
174  bool Open(std::ios_base::openmode mode)
175  {
176  _mtx.lock();
177  bool rc = OpenNoLock(mode);
178  _mtx.unlock();
179  return(rc);
180  }
181 
182  //------------------------------------------------------------------------
184  //------------------------------------------------------------------------
185  bool Rewind()
186  {
187  bool rc = false;
188  _mtx.lock();
189  if (_s) {
190  if ((_mode & std::ios_base::in) == std::ios_base::in) {
191  _s->seekg(0);
192  }
193  if ((_mode & std::ios_base::out) == std::ios_base::out) {
194  _s->seekp(0);
195  }
196  rc = true;
197  }
198  _mtx.unlock();
199  return(rc);
200  }
201 
202  //------------------------------------------------------------------------
204  //------------------------------------------------------------------------
205  bool Close()
206  {
207  _mtx.lock();
208  bool rc = CloseNoLock();
209  _mtx.unlock();
210  return(rc);
211  }
212 
213  //------------------------------------------------------------------------
215  //------------------------------------------------------------------------
216  bool GetNext(Entry & entry)
217  {
218  bool rc = false;
219  _mtx.lock();
220  if (_s) {
221  rc = entry.Read(*_s) ? true : false;
222  }
223  _mtx.unlock();
224  return(rc);
225  }
226 
227  //------------------------------------------------------------------------
229  //------------------------------------------------------------------------
230  bool Record(const T & value)
231  {
232  bool rc = false;
233  _mtx.lock();
234  if (_s) {
235  Entry entry(value);
236  rc = entry.Write(*_s) ? true : false;
237  }
238  _mtx.unlock();
239  return(rc);
240  }
241 
242  private:
243  std::string _filename;
244  std::fstream *_s;
245  std::ios_base::openmode _mode;
246  std::mutex _mtx;
247 
248  //------------------------------------------------------------------------
250  //------------------------------------------------------------------------
251  bool OpenNoLock(std::ios_base::openmode mode)
252  {
253  assert(! _s);
254  _s = new std::fstream(_filename.c_str(), mode);
255  if (_s && *_s) {
256  _mode = mode;
257  Syslog(LOG_INFO, "DataLog %s opened", _filename.c_str());
258  }
259  else {
260  Syslog(LOG_ERR, "Failed to open DataLog %s: %m", _filename.c_str());
261  }
262  return(*_s ? true : false);
263  }
264 
265  //------------------------------------------------------------------------
267  //------------------------------------------------------------------------
268  bool CloseNoLock()
269  {
270  bool rc = false;
271  if (_s) {
272  _s->close();
273  delete(_s);
274  _s = 0;
275  rc = true;
276  Syslog(LOG_INFO, "DataLog %s closed", _filename.c_str());
277  }
278  return(rc);
279  }
280 
281  };
282 
283 } // namespace Dwm
284 
285 #endif // _DWMDATALOG_HH_
bool Rewind()
Rewinds the log to the beginning. Returns true on success.
Definition: DwmDataLog.hh:185
uint32_t Usecs() const
Returns the residual microseconds.
Encapsulates a single data log entry.
Definition: DwmDataLog.hh:69
size_t StreamedLength() const
Returns the number of bytes that would be written if the Write() member were called.
Definition: DwmDataLog.hh:126
This class encapsulates a time value beyond the UNIX epoch, with microsecond granularity.
Definition: DwmTimeValue.hh:72
std::istream & Read(std::istream &is)
Reads the Entry from the given istream is.
Definition: DwmDataLog.hh:105
friend std::ostream & operator<<(std::ostream &os, const Entry &entry)
Prints the Entry to an ostream in human-readable form.
Definition: DwmDataLog.hh:136
bool GetNext(Entry &entry)
Fetches the next entry from the log. Returns true on succes.
Definition: DwmDataLog.hh:216
bool Open(std::ios_base::openmode mode)
Opens the log with the given mode. Returns true on success.
Definition: DwmDataLog.hh:174
std::ostream & Write(std::ostream &os) const
Writes the Entry to the given ostream os.
Definition: DwmDataLog.hh:115
~DataLog()
Destructor.
Definition: DwmDataLog.hh:166
DataLog(const std::string &filename)
Constructor. filename is the name of the file to use.
Definition: DwmDataLog.hh:157
static std::ostream & Write(std::ostream &os, char c)
Writes c to os. Returns os.
Dwm::TimeValue class definition.
bool Close()
Closes the log. Returns true on success.
Definition: DwmDataLog.hh:205
Dwm::SysLogger class definition and Syslog() macro.
const TimeValue & Timestamp() const
Returns the timestamp of the Entry.
Definition: DwmDataLog.hh:89
Entry(const T &value)
Construct from the given value.
Definition: DwmDataLog.hh:82
Entry()
Constructor.
Definition: DwmDataLog.hh:75
static std::istream & Read(std::istream &is, char &c)
Reads c from is. Returns is.
#define Syslog(...)
Used just like syslog(), but calls Dwm::SysLogger::Log() to allow the filename, line number and funct...
Definition: DwmSysLogger.hh:236
Definition: DwmBZ2IO.hh:67
Dwm::IO class definition.
Simple class template for data logging.
Definition: DwmDataLog.hh:63
static uint32_t StreamedLength(char c)
Returns the number of bytes that would be written if we called Write() for a char.
Definition: DwmIO.hh:92
const T & Value() const
Returns the value of the entry.
Definition: DwmDataLog.hh:97
bool Record(const T &value)
Writes an entry to the log. Returns true on success.
Definition: DwmDataLog.hh:230