PIP 5.5.3
Platform-Independent Primitives
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | List of all members
PIBinaryLog Class Reference

Binary log. More...

#include <pibinarylog.h>

Inheritance diagram for PIBinaryLog:
Inheritance graph
[legend]

Classes

struct  BinLogIndex
 Struct contains position, ID and timestamp of record in file. More...
 
struct  BinLogInfo
 Struct contains full information about Binary Log file and about all Records using map of BinLogRecordInfo. More...
 
struct  BinLogRecordInfo
 Struct contains information about all records with same ID. More...
 

Public Types

enum  PlayMode { PlayRealTime , PlayVariableSpeed , PlayStaticDelay }
 Play modes for PIBinaryLog. More...
 
enum  SplitMode { SplitNone , SplitTime , SplitSize , SplitCount }
 Different split modes for writing PIBinaryLog, which can separate files by size, by time or by records count. More...
 
- Public Types inherited from PIIODevice
enum  DeviceMode { ReadOnly = 0x01 , WriteOnly = 0x02 , ReadWrite = 0x03 }
 Open modes for PIIODevice. More...
 
enum  DeviceOption { BlockingRead = 0x01 , BlockingWrite = 0x02 }
 Options for PIIODevice, works with some devices. More...
 
enum  DeviceInfoFlag { Sequential = 0x01 , Reliable = 0x02 }
 Characteristics of PIIODevice channel. More...
 

Public Member Functions

PlayMode playMode () const
 Current PlayMode.
 
SplitMode splitMode () const
 Current SplitMode.
 
PIString logDir () const
 Current directory where billogs wiil be saved.
 
PIString filePrefix () const
 Returns current file prefix.
 
int defaultID () const
 Default ID, used in write function.
 
double playSpeed () const
 Returns current play speed.
 
PISystemTime playDelay () const
 Returns current play delay.
 
PISystemTime splitTime () const
 Returns current binlog file split time.
 
llong splitFileSize () const
 Returns current binlog file split size.
 
int splitRecordCount () const
 Returns current binlog file split records count.
 
bool rapidStart () const
 Returns if rapid start enabled.
 
bool createIndexOnFly () const
 Returns if index creates while writing.
 
void createNewFile (const PIString &path)
 Create binlog file with Filename = path.
 
void setPlayMode (PlayMode mode)
 Set PlayMode.
 
void setSplitMode (SplitMode mode)
 Set SplitMode.
 
void setLogDir (const PIString &path)
 Set path to directory where binlogs will be saved.
 
void setFilePrefix (const PIString &prefix)
 Set file prefix, used to.
 
void setDefaultID (int id)
 Set defaultID, used in write function.
 
void setRapidStart (bool enabled)
 If enabled BinLog ThreadedRead starts without delay for first record, i.e. first record will be readed immediately.
 
void setCreateIndexOnFly (bool yes)
 Set index creation while writing.
 
void setPlaySpeed (double speed)
 Set play speed to "speed", default value is 1.0x Also this function set playMode to PlayVariableSpeed.
 
void setPlayDelay (const PISystemTime &delay)
 Setting static delay between records, default value is 1 sec Also this function set playMode to PlayStaticDelay.
 
void setPlayRealTime ()
 Set playMode to PlayRealTime.
 
void setSplitTime (const PISystemTime &time)
 Set binlog file split time Also this function set splitMode to SplitTime.
 
void setSplitFileSize (llong size)
 Set binlog file split size Also this function set splitMode to SplitSize.
 
void setSplitRecordCount (int count)
 Set binlog file split records count Also this function set splitMode to SplitCount.
 
void setPause (bool pause)
 Set pause while playing via threadedRead or writing via write.
 
void setFuncGetNewFilePath (std::function< PIString()> f)
 Set function wich returns new binlog file path when using split mode. Overrides internal file path generator (logdir() + prefix() + current_time()). To restore internal file path generator set this function to "nullptr".
 
int writeBinLog (int id, PIByteArray data)
 Write one record to BinLog file, with ID = id, id must be greather than 0.
 
int writeBinLog (int id, const void *data, int size)
 Write one record to BinLog file, with ID = id, id must be greather than 0.
 
int writeBinLog_raw (int id, const PISystemTime &time, const PIByteArray &data)
 Write one RAW record to BinLog file, with ID = id, Timestamp = time.
 
int writeCount () const
 Returns count of writed records.
 
PIByteArray readBinLog (int id=0, PISystemTime *time=0, int *readed_id=0)
 Read one record from BinLog file, with ID = id, if id = 0 than any id will be readed.
 
int readBinLog (int id, void *read_to, int max_size, PISystemTime *time=0, int *readed_id=0)
 Read one record from BinLog file, with ID = id, if id = 0 than any id will be readed.
 
llong logSize () const
 Returns binary log file size.
 
llong logPos () const
 Return position in current binlog file.
 
bool isEnd () const
 Return true, if position at the end of BinLog file.
 
bool isEmpty () const
 Returns if BinLog file is empty.
 
bool isPause () const
 Returns BinLog pause status.
 
int lastReadedID () const
 Returns id of last readed record.
 
PISystemTime lastReadedTimestamp () const
 Returns timestamp of last readed record.
 
PISystemTime logStartTimestamp () const
 Returns timestamp of log start.
 
void setHeader (const PIByteArray &header)
 Set custom file header, you can get it back when read this binlog.
 
PIByteArray getHeader () const
 Get custom file header.
 
int read (void *read_to, int max_size)
 Read one message from binlog file, with ID contains in "filterID" or any ID, if "filterID" is empty.
 
int write (const void *data, int size)
 Write one record to BinLog file, with ID = "defaultID".
 
void restart ()
 Go to begin of BinLog file.
 
BinLogInfo logInfo () const
 Get binlog info BinLogInfo.
 
const PIVector< BinLogIndex > & logIndex () const
 Get binlog index BinLogIndex, need createIndex before getting index.
 
bool createIndex ()
 Create index of current binlog file.
 
bool isIndexed ()
 Return if current binlog file is indexed.
 
int posForTime (const PISystemTime &time)
 Find nearest record of time "time". Returns -1 if not indexed or time less than first record.
 
void seekTo (int rindex)
 Go to record #index.
 
bool seek (const PISystemTime &time)
 Go to nearest record.
 
bool seek (llong filepos)
 Set position in file to reading/playing.
 
int pos () const
 Get current record index (position record in file)
 
- Public Member Functions inherited from PIIODevice
 PIIODevice ()
 Constructs a empty PIIODevice.
 
 PIIODevice (const PIString &path, DeviceMode mode=ReadWrite)
 Constructs PIIODevice with path "path" and open mode "mode".
 
DeviceMode mode () const
 Returns current open mode of device.
 
void setMode (DeviceMode m)
 Set open mode of device. Don`t reopen device.
 
DeviceOptions options () const
 Returns current device options.
 
bool isOptionSet (DeviceOption o) const
 Returns current device option "o" state.
 
void setOptions (DeviceOptions o)
 Set device options.
 
bool setOption (DeviceOption o, bool yes=true)
 Set device option "o" to "yes" and returns previous state.
 
DeviceInfoFlags infoFlags () const
 Returns device characteristic flags.
 
PIString path () const
 Returns current path of device.
 
void setPath (const PIString &path)
 Set path of device. Don`t reopen device.
 
bool isReadable () const
 Returns if mode is ReadOnly or ReadWrite.
 
bool isWriteable () const
 Returns if mode is WriteOnly or ReadWrite.
 
bool isOpened () const
 Returns if device is successfully opened.
 
bool isClosed () const
 Returns if device is closed.
 
virtual bool canRead () const
 Returns if device can read now.
 
virtual bool canWrite () const
 Returns if device can write now.
 
void setReopenEnabled (bool yes=true)
 Set calling of open() enabled while threaded read on closed device.
 
void setReopenTimeout (PISystemTime timeout)
 Set timeout between open() tryings if reopen is enabled.
 
bool isReopenEnabled () const
 Returns reopen enable.
 
PISystemTime reopenTimeout ()
 Returns reopen timeout.
 
void setThreadedReadSlot (ReadRetFunc func)
 Set threaded read callback. More...
 
void setThreadedReadData (void *d)
 Set custom data that will be passed to threaded read callback.
 
void setThreadedReadBufferSize (int new_size)
 Set size of threaded read buffer. More...
 
int threadedReadBufferSize () const
 Returns size of threaded read buffer.
 
const uchar * threadedReadBuffer () const
 Returns content of threaded read buffer.
 
void * threadedReadData () const
 Returns custom data that will be passed to threaded read callback.
 
bool isThreadedRead () const
 Returns if threaded read is started.
 
bool isThreadedReadStopping () const
 Returns if threaded read is stopping.
 
void startThreadedRead ()
 Start threaded read.
 
void startThreadedRead (ReadRetFunc func)
 Start threaded read and assign threaded read callback to "func".
 
void stopThreadedRead ()
 Stop threaded read.
 
void terminateThreadedRead ()
 Terminate threaded read. More...
 
bool waitThreadedReadFinished (PISystemTime timeout={})
 Wait for threaded read finish no longer than "timeout".
 
bool isThreadedWrite () const
 Returns if threaded write is started.
 
void startThreadedWrite ()
 Start threaded write.
 
void stopThreadedWrite ()
 Stop threaded write.
 
void terminateThreadedWrite ()
 Terminate threaded write. More...
 
bool waitThreadedWriteFinished (PISystemTime timeout={})
 Wait for threaded write finish no longer than "timeout".
 
void clearThreadedWriteQueue ()
 Clear threaded write task queue.
 
void start ()
 Start both threaded read and threaded write.
 
void stop ()
 Stop both threaded read and threaded write.
 
void stopAndWait (PISystemTime timeout={})
 Stop both threaded read and threaded write and wait for finish.
 
virtual void interrupt ()
 Interrupt blocking operation.
 
ssize_t read (void *read_to, ssize_t max_size)
 Read from device maximum "max_size" bytes to "read_to".
 
ssize_t read (PIMemoryBlock mb)
 Read from device to memory block "mb".
 
PIByteArray read (ssize_t max_size)
 Read from device maximum "max_size" bytes and returns them as PIByteArray.
 
virtual ssize_t bytesAvailable () const
 Returns the number of bytes that are available for reading. More...
 
ssize_t write (const void *data, ssize_t max_size)
 Write maximum "max_size" bytes of "data" to device.
 
PIByteArray readForTime (PISystemTime timeout)
 Read from device for "timeout" and return readed data as PIByteArray.
 
ullong writeThreaded (const void *data, ssize_t max_size)
 Add task to threaded write queue and return task ID.
 
ullong writeThreaded (const PIByteArray &data)
 Add task to threaded write queue and return task ID.
 
bool configure (const PIString &config_file, const PIString &section, bool parent_section=false)
 Configure device from section "section" of file "config_file", if "parent_section" parent section also will be read.
 
virtual PIConstChars fullPathPrefix () const
 Returns full unambiguous string prefix. Creating devices by unambiguous string.
 
PIString constructFullPath () const
 Returns full unambiguous string, describes this device, fullPathPrefix() + "://" + ...
 
void configureFromFullPath (const PIString &full_path)
 Configure device with parameters of full unambiguous string.
 
PIVariantTypes::IODevice constructVariant () const
 Returns PIVariantTypes::IODevice, describes this device.
 
void configureFromVariant (const PIVariantTypes::IODevice &d)
 Configure device from PIVariantTypes::IODevice.
 
ssize_t write (const PIMemoryBlock &mb)
 Write memory block "mb" to device.
 
bool open ()
 Open device.
 
bool open (const PIString &_path)
 Open device with path "path".
 
bool close ()
 Close device.
 
ssize_t write (PIByteArray data)
 Write "data" to device.
 
virtual void flush ()
 Immediate write all buffers.
 
void opened ()
 Raise if succesfull open.
 
void closed ()
 Raise if succesfull close.
 
void threadedReadEvent (const uchar *readed, ssize_t size)
 Raise if read thread succesfull read some data.
 
void threadedWriteEvent (ullong id, ssize_t written_size)
 Raise if write thread successfull write some data of task with ID "id".
 
- Public Member Functions inherited from PIObject
 PIObject (const PIString &name=PIString())
 Contructs PIObject with name "name".
 
PIString name () const
 Returns object name.
 
virtual const char * className () const
 Returns object class name.
 
virtual const char * parentClassName () const
 Returns parent class name.
 
bool debug () const
 Return if piCoutObj of this object is active.
 
void setName (const PIString &name)
 Set object name.
 
void setDebug (bool debug)
 Set object piCoutObj active.
 
PIVariant property (const char *name) const
 Returns property with name "name".
 
void setProperty (const char *name, const PIVariant &value)
 Set property with name "name" to "value". If there is no such property in object it will be added.
 
bool isPropertyExists (const char *name) const
 Returns if property with name "name" exists.
 
PIStringList scopeList () const
 Returns subclass scope of this object (including this class name)
 
void piDisconnect (const PIString &sig, PIObject *dest, void *ev_h)
 Disconnect object from all connections with event name "sig", connected to destination object "dest" and handler "ev_h".
 
void piDisconnect (const PIString &sig, PIObject *dest)
 Disconnect object from all connections with event name "sig", connected to destination object "dest".
 
void piDisconnect (const PIString &sig)
 Disconnect object from all connections with event name "sig".
 
bool isPIObject () const
 Returns if this is valid PIObject (check signature)
 
template<typename T >
bool isTypeOf () const
 Returns if this is valid PIObject subclass "T" (check signature and classname)
 
template<typename T >
T * cast () const
 Returns cast to T if this is valid subclass "T" (check by isTypeOf()) or "nullptr".
 
void callQueuedEvents ()
 Execute all posted events from CONNECTU_QUEUED connections.
 
bool maybeCallQueuedEvents ()
 Check if any CONNECTU_QUEUED connections to this object and execute them. More...
 
void deleteLater ()
 Mark object to delete. More...
 
void deleted (PIObject *o)
 Raise before object delete. More...
 

Static Public Member Functions

static BinLogInfo getLogInfo (const PIString &path)
 Get binlog info and statistic.
 
static bool cutBinLog (const BinLogInfo &src, const PIString &dst, int from, int to)
 Create new binlog from part of "src" with allowed IDs and "from" to "to" file position.
 
static bool joinBinLogsSerial (const PIStringList &src, const PIString &dst, std::function< bool(const PIString &, PISystemTime)> progress=nullptr)
 Create new binlog from serial splitted binlogs "src".
 
- Static Public Member Functions inherited from PIIODevice
static PIIODevicecreateFromFullPath (const PIString &full_path)
 Try to create new device by prefix, configure it with configureFromFullPath() and returns it. More...
 
static PIIODevicecreateFromVariant (const PIVariantTypes::IODevice &d)
 Try to create new device by prefix, configure it with configureFromVariant() and returns it.
 
static PIStringList availablePrefixes ()
 Returns fullPath prefixes of all registered devices.
 
static PIStringList availableClasses ()
 Returns class names of all registered devices.
 
- Static Public Member Functions inherited from PIObject
static void piDisconnect (PIObject *src, const PIString &sig, PIObject *dest, void *ev_h)
 Disconnect object "src" from all connections with event name "sig", connected to destination object "dest" and handler "ev_h".
 
static void piDisconnect (PIObject *src, const PIString &sig, PIObject *dest)
 Disconnect object "src" from all connections with event name "sig", connected to destination object "dest".
 
static void piDisconnect (PIObject *src, const PIString &sig)
 Disconnect object "src" from all connections with event name "sig".
 
static PIObjectfindByName (const PIString &name)
 Returns PIObject* with name "name" or 0, if there is no object found.
 
static bool isPIObject (const PIObject *o)
 Returns if "o" is valid PIObject (check signature)
 
template<typename T >
static bool isTypeOf (const PIObject *o)
 Returns if "o" is valid PIObject subclass "T" (check signature and classname)
 

Public Attributes

PIVector< int > filterID
 Array of ID, that BinLog can read from binlog file, when use read function, or in ThreadedRead.
 
- Public Attributes inherited from PIIODevice
bool reopenEnabled
 setReopenEnabled, default "true"
 
int reopenTimeout
 setReopenTimeout, default 1_s
 
int threadedReadBufferSize
 setThreadedReadBufferSize in bytes, default 4096
 

Protected Member Functions

PIString constructFullPathDevice () const override
 Reimplement to construct full unambiguous string, describes this device. Default implementation returns path()
 
void configureFromFullPathDevice (const PIString &full_path) override
 Reimplement to configure your device with parameters of full unambiguous string. Default implementation call setPath()
 
PIPropertyStorage constructVariantDevice () const override
 Reimplement to construct device properties. Default implementation return PIPropertyStorage with "path" entry.
 
void configureFromVariantDevice (const PIPropertyStorage &d) override
 Reimplement to configure your device from PIPropertyStorage. Options and mode already applied. Default implementation apply "path" entry.
 
ssize_t readDevice (void *read_to, ssize_t max_size) override
 Reimplement this function to read from your device.
 
ssize_t writeDevice (const void *data, ssize_t size) override
 Reimplement this function to write to your device.
 
bool openDevice () override
 Reimplement to open device, return value will be set to "opened_" variable. Don't call this function in subclass, use open()!
 
bool closeDevice () override
 Reimplement to close device, inverse return value will be set to "opened_" variable.
 
void propertyChanged (const char *s) override
 Virtual function executes after property with name "name" has been changed.
 
bool threadedRead (const uchar *readed, ssize_t size) override
 Function executed when thread read some data, default implementation execute external callback "ret_func_".
 
DeviceInfoFlags deviceInfoFlags () const override
 Reimplement to return correct DeviceInfoFlags. Default implementation returns 0.
 
- Protected Member Functions inherited from PIIODevice
virtual bool configureDevice (const void *e_main, const void *e_parent=0)
 Reimplement to configure device from entries "e_main" and "e_parent", cast arguments to PIConfig::Entry*.
 
virtual bool openDevice ()=0
 Reimplement to open device, return value will be set to "opened_" variable. Don't call this function in subclass, use open()!
 
virtual bool closeDevice ()
 Reimplement to close device, inverse return value will be set to "opened_" variable.
 
virtual ssize_t readDevice (void *read_to, ssize_t max_size)
 Reimplement this function to read from your device.
 
virtual ssize_t writeDevice (const void *data, ssize_t max_size)
 Reimplement this function to write to your device.
 
virtual bool threadedRead (const uchar *readed, ssize_t size)
 Function executed when thread read some data, default implementation execute external callback "ret_func_".
 
virtual PIString constructFullPathDevice () const
 Reimplement to construct full unambiguous string, describes this device. Default implementation returns path()
 
virtual void configureFromFullPathDevice (const PIString &full_path)
 Reimplement to configure your device with parameters of full unambiguous string. Default implementation call setPath()
 
virtual PIPropertyStorage constructVariantDevice () const
 Reimplement to construct device properties. Default implementation return PIPropertyStorage with "path" entry.
 
virtual void configureFromVariantDevice (const PIPropertyStorage &d)
 Reimplement to configure your device from PIPropertyStorage. Options and mode already applied. Default implementation apply "path" entry.
 
virtual void optionsChanged ()
 Reimplement to apply new device options.
 
virtual DeviceInfoFlags deviceInfoFlags () const
 Reimplement to return correct DeviceInfoFlags. Default implementation returns 0.
 
virtual void threadedReadBufferSizeChanged ()
 Reimplement to apply new threadedReadBufferSize()
 
- Protected Member Functions inherited from PIObject
PIObjectemitter () const
 Returns PIObject* which has raised an event. This value is correct only in definition of some event handler.
 
virtual void propertyChanged (const char *name)
 Virtual function executes after property with name "name" has been changed.
 

Handlers

PIString createNewFile ()
 Create new binlog file in logDir, if successful returns filename, else returns empty string. Filename is like filePrefix + "yyyy_MM_dd__hh_mm_ss.binlog".
 

Events

void fileEnd ()
 Raise on file end while reading.
 
void fileError ()
 Raise on file creation error.
 
void newFile (const PIString &filename)
 Raise on new file created.
 

Additional Inherited Members

Detailed Description

Binary log.

Class for read and write binary data to logfile, and playback this data in realtime, or custom speed.

Synopsis

Binary Log is a file with simple header, where you can read and write some binary data. Any written data include special header with ID, size and timestamp. This header provides separation different messages from the one file by choosing different IDs. With filterID or special functions, like readBinLog() you can choose IDs what you want to read. With function writeBinLog() or setDefaultID() you can choose ID that mark you data. By default ID = 1, and filterID is empty, that mean you read any ID without filtering. ThreadedRead provide you playback data, with delay that you write data. You can choose different playbak modes by set PlayMode.

Basic usage

This class provide all functions of PIIODevice, such open(), close(), read() ,write(), and threaded read/write. function setLogDir() need to set directory for BinLog files function createNewFile() need to create new binlog file function restart() need start from the begining of binlog file

Member Enumeration Documentation

◆ PlayMode

Play modes for PIBinaryLog.

Enumerator
PlayRealTime 

Play in system realtime, default mode

PlayVariableSpeed 

Play in software realtime with speed, set by setSpeed

PlayStaticDelay 

Play with custom static delay, ignoring timestamp

◆ SplitMode

Different split modes for writing PIBinaryLog, which can separate files by size, by time or by records count.

Enumerator
SplitNone 

Without separate, default mode

SplitTime 

Separate files by record time

SplitSize 

Separate files by size

SplitCount 

Separate files by records count