41#ifndef _DWMDATALOG_HH_
42#define _DWMDATALOG_HH_
76 : _timestamp(0,0), _value()
83 : _timestamp(true), _value(value)
105 std::istream &
Read(std::istream & is)
115 std::ostream &
Write(std::ostream & os)
const
135 friend std::ostream &
138 struct tm localTime = entry._timestamp.LocalTime();
140 size_t n = strftime(buf, 32,
"%m/%d/%Y %H:%M:%S", &localTime);
141 std::ostringstream ostr;
144 << std::setw(6) << entry._timestamp.
Usecs();
145 os << ostr.str() <<
" " << entry._value;
158 : _filename(filename), _s(0), _mode(), _mtx()
160 assert(! _filename.empty());
174 bool Open(std::ios_base::openmode mode)
177 bool rc = OpenNoLock(mode);
190 if ((_mode & std::ios_base::in) == std::ios_base::in) {
193 if ((_mode & std::ios_base::out) == std::ios_base::out) {
208 bool rc = CloseNoLock();
221 rc = entry.
Read(*_s) ? true :
false;
236 rc = entry.
Write(*_s) ? true :
false;
243 std::string _filename;
245 std::ios_base::openmode _mode;
251 bool OpenNoLock(std::ios_base::openmode mode)
254 _s =
new std::fstream(_filename.c_str(), mode);
257 Syslog(LOG_INFO,
"DataLog %s opened", _filename.c_str());
260 Syslog(LOG_ERR,
"Failed to open DataLog %s: %m", _filename.c_str());
262 return(*_s ?
true : false);
276 Syslog(LOG_INFO,
"DataLog %s closed", _filename.c_str());
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.