libDwm-0.9.45
DwmDataLog.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2008, 2016
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#ifndef _DWMDATALOG_HH_
42#define _DWMDATALOG_HH_
43
44#include <cassert>
45#include <fstream>
46#include <iomanip>
47#include <sstream>
48#include <string>
49
50#include "DwmIOUtils.hh"
51#include "DwmStreamIO.hh"
52#include "DwmSysLogger.hh"
53#include "DwmTimeValue.hh"
54
55namespace 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 (StreamIO::Read(is, _timestamp))
108 StreamIO::Read(is, _value);
109 return(is);
110 }
111
112 //----------------------------------------------------------------------
114 //----------------------------------------------------------------------
115 std::ostream & Write(std::ostream & os) const
116 {
117 if (StreamIO::Write(os, _timestamp))
118 StreamIO::Write(os, _value);
119 return(os);
120 }
121
122 //----------------------------------------------------------------------
125 //----------------------------------------------------------------------
126 size_t StreamedLength() const
127 {
128 return(IOUtils::StreamedLength(_timestamp) + IOUtils::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_
286
287
288//---------------------------- emacs settings -----------------------------
289// Local Variables:
290// mode: C++
291// tab-width: 2
292// indent-tabs-mode: nil
293// c-basic-offset: 2
294// End:
295//-------------------------------------------------------------------------
Dwm::IOUtils class declaration and implementation.
Dwm::StreamIO class declaration.
Dwm::SysLogger class definition and Syslog() macro.
#define Syslog(...)
Used just like syslog(), but calls Dwm::SysLogger::Log() to allow the filename, line number and funct...
Definition DwmSysLogger.hh:285
Dwm::TimeValue class definition.
Encapsulates a single data log entry.
Definition DwmDataLog.hh:70
Entry()
Constructor.
Definition DwmDataLog.hh:75
size_t StreamedLength() const
Returns the number of bytes that would be written if the Write() member were called.
Definition DwmDataLog.hh:126
std::ostream & Write(std::ostream &os) const
Writes the Entry to the given ostream os.
Definition DwmDataLog.hh:115
const T & Value() const
Returns the value of the entry.
Definition DwmDataLog.hh:97
Entry(const T &value)
Construct from the given value.
Definition DwmDataLog.hh:82
const TimeValue & Timestamp() const
Returns the timestamp of the Entry.
Definition DwmDataLog.hh:89
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
Simple class template for data logging.
Definition DwmDataLog.hh:64
bool Open(std::ios_base::openmode mode)
Opens the log with the given mode. Returns true on success.
Definition DwmDataLog.hh:174
bool Rewind()
Rewinds the log to the beginning. Returns true on success.
Definition DwmDataLog.hh:185
bool GetNext(Entry &entry)
Fetches the next entry from the log. Returns true on succes.
Definition DwmDataLog.hh:216
bool Record(const T &value)
Writes an entry to the log. Returns true on success.
Definition DwmDataLog.hh:230
DataLog(const std::string &filename)
Constructor. filename is the name of the file to use.
Definition DwmDataLog.hh:157
bool Close()
Closes the log. Returns true on success.
Definition DwmDataLog.hh:205
~DataLog()
Destructor.
Definition DwmDataLog.hh:166
static uint64_t StreamedLength(char c)
Returns the number of bytes that would be written if we called Write() for a char.
Definition DwmIOUtils.hh:85
static std::istream & Read(std::istream &is, char &c)
Reads c from is. Returns is.
static std::ostream & Write(std::ostream &os, char c)
Writes c to os. Returns os.
This class encapsulates a time value beyond the UNIX epoch, with microsecond granularity.
Definition DwmTimeValue.hh:68
uint32_t Usecs() const
Returns the residual microseconds.