Flexiport 2.0.0

logfile.h

Go to the documentation of this file.
00001 /* Flexiport
00002  *
00003  * Header file for the log file class.
00004  *
00005  * Copyright 2008-2011 Geoffrey Biggs geoffrey.biggs@aist.go.jp
00006  *     RT-Synthesis Research Group
00007  *     Intelligent Systems Research Institute,
00008  *     National Institute of Advanced Industrial Science and Technology (AIST),
00009  *     Japan
00010  *     All rights reserved.
00011  *
00012  * This file is part of Flexiport.
00013  *
00014  * Flexiport is free software; you can redistribute it and/or modify it
00015  * under the terms of the GNU Lesser General Public License as published
00016  * by the Free Software Foundation; either version 2.1 of the License,
00017  * or (at your option) any later version.
00018  *
00019  * Flexiport is distributed in the hope that it will be useful, but
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with Flexiport. If not, see
00026  * <http://www.gnu.org/licenses/>.
00027  */
00028 
00029 #ifndef __LOGFILE_H
00030 #define __LOGFILE_H
00031 
00032 #if defined (WIN32)
00033     #include <winsock2.h> // For timeval
00034 #else
00035     #include <sys/time.h>
00036 #endif
00037 #include <string>
00038 #include <vector>
00039 
00040 #include <flexiport/timeout.h>
00041 #include <flexiport/flexiport_types.h>
00042 
00043 namespace flexiport
00044 {
00045 
00046 // Class for managing a log file pair
00047 class LogFile
00048 {
00049     public:
00050         LogFile (unsigned int debug);
00051         ~LogFile ();
00052 
00053         void Open (std::string fileName, bool read, bool ignoreTimes = false);
00054         void Close ();
00055         bool IsOpen () const;
00056         void ResetFile ();
00057 
00058         // File reading
00059         ssize_t Read (void *data, size_t count, Timeout &timeout);
00060         ssize_t BytesAvailable (const Timeout &timeout);
00061         bool CheckWrite (const void * const data, const size_t count, size_t * const numWritten,
00062                 const Timeout * const timeout = NULL);
00063         void Flush ();
00064         void Drain ();
00065 
00066         // File writing
00067         void WriteRead (const void * const data, size_t count);
00068         void WriteWrite (const void * const data, size_t count);
00069 
00070     private:
00071         std::string _fileName;
00072         bool _read;
00073         FILE *_readFile, *_writeFile;
00074         long _readFileSize, _writeFileSize;
00075         // When writing, this is the time the file was opened. When
00076         // reading, it's the reset time.
00077         struct timeval _openTime;
00078         unsigned int _debug;
00079         size_t _readUsage, _writeUsage;
00080         size_t _readSize, _writeSize;
00081         uint8_t *_readBuffer, *_writeBuffer;
00082         bool _ignoreTimes;
00083 
00084         void AllocateReadBuffer (unsigned int size = 0);
00085         void AllocateWriteBuffer (unsigned int size = 0);
00086         void DeallocateReadBuffer ();
00087         void DeallocateWriteBuffer ();
00088 
00089         void GetCurrentFileTime (struct timeval &dest);
00090         bool DataAvailableWithinLimit (FILE * const file, const struct timeval &limit);
00091         void GetNextChunkInfo (FILE * const file, struct timeval &timeStamp, size_t &size);
00092         size_t GetChunksToTimeLimit (FILE * const file, void *data, size_t count,
00093                                 const struct timeval &limit);
00094         size_t GetChunkSizesToTimeLimit (FILE * const file, const struct timeval &limit);
00095         size_t GetSingleChunk (FILE * const file, void *data, size_t count,
00096                                 struct timeval &timeStamp, size_t &size);
00097         size_t GetFileSize (FILE * const file);
00098 
00099         void ReadFromFile (FILE * const file, void * const dest, size_t count);
00100         void WriteToFile (FILE * const file, const void * const data, size_t count);
00101         void WriteTimeStamp (FILE * const file);
00102 };
00103 
00104 } // namespace flexiport
00105 
00106 #endif // __LOGFILE_H
00107 
 All Classes Namespaces Files Functions Variables Defines