Flexiport  2.0.0
logfile.h
Go to the documentation of this file.
1 /* Flexiport
2  *
3  * Header file for the log file class.
4  *
5  * Copyright 2008-2011 Geoffrey Biggs geoffrey.biggs@aist.go.jp
6  * RT-Synthesis Research Group
7  * Intelligent Systems Research Institute,
8  * National Institute of Advanced Industrial Science and Technology (AIST),
9  * Japan
10  * All rights reserved.
11  *
12  * This file is part of Flexiport.
13  *
14  * Flexiport is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU Lesser General Public License as published
16  * by the Free Software Foundation; either version 2.1 of the License,
17  * or (at your option) any later version.
18  *
19  * Flexiport is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with Flexiport. If not, see
26  * <http://www.gnu.org/licenses/>.
27  */
28 
29 #ifndef __LOGFILE_H
30 #define __LOGFILE_H
31 
32 #if defined (WIN32)
33  #include <winsock2.h> // For timeval
34 #else
35  #include <sys/time.h>
36 #endif
37 #include <string>
38 #include <vector>
39 
40 #include <flexiport/timeout.h>
42 
43 namespace flexiport
44 {
45 
46 // Class for managing a log file pair
47 class LogFile
48 {
49  public:
50  LogFile (unsigned int debug);
51  ~LogFile ();
52 
53  void Open (std::string fileName, bool read, bool ignoreTimes = false);
54  void Close ();
55  bool IsOpen () const;
56  void ResetFile ();
57 
58  // File reading
59  ssize_t Read (void *data, size_t count, Timeout &timeout);
60  ssize_t BytesAvailable (const Timeout &timeout);
61  bool CheckWrite (const void * const data, const size_t count, size_t * const numWritten,
62  const Timeout * const timeout = NULL);
63  void Flush ();
64  void Drain ();
65 
66  // File writing
67  void WriteRead (const void * const data, size_t count);
68  void WriteWrite (const void * const data, size_t count);
69 
70  private:
71  std::string _fileName;
72  bool _read;
73  FILE *_readFile, *_writeFile;
74  long _readFileSize, _writeFileSize;
75  // When writing, this is the time the file was opened. When
76  // reading, it's the reset time.
77  struct timeval _openTime;
78  unsigned int _debug;
79  size_t _readUsage, _writeUsage;
80  size_t _readSize, _writeSize;
81  uint8_t *_readBuffer, *_writeBuffer;
82  bool _ignoreTimes;
83 
84  void AllocateReadBuffer (unsigned int size = 0);
85  void AllocateWriteBuffer (unsigned int size = 0);
86  void DeallocateReadBuffer ();
87  void DeallocateWriteBuffer ();
88 
89  void GetCurrentFileTime (struct timeval &dest);
90  bool DataAvailableWithinLimit (FILE * const file, const struct timeval &limit);
91  void GetNextChunkInfo (FILE * const file, struct timeval &timeStamp, size_t &size);
92  size_t GetChunksToTimeLimit (FILE * const file, void *data, size_t count,
93  const struct timeval &limit);
94  size_t GetChunkSizesToTimeLimit (FILE * const file, const struct timeval &limit);
95  size_t GetSingleChunk (FILE * const file, void *data, size_t count,
96  struct timeval &timeStamp, size_t &size);
97  size_t GetFileSize (FILE * const file);
98 
99  void ReadFromFile (FILE * const file, void * const dest, size_t count);
100  void WriteToFile (FILE * const file, const void * const data, size_t count);
101  void WriteTimeStamp (FILE * const file);
102 };
103 
104 } // namespace flexiport
105 
106 #endif // __LOGFILE_H
107 
ssize_t Read(void *data, size_t count, Timeout &timeout)
void WriteWrite(const void *const data, size_t count)
bool CheckWrite(const void *const data, const size_t count, size_t *const numWritten, const Timeout *const timeout=NULL)
void WriteRead(const void *const data, size_t count)
void Open(std::string fileName, bool read, bool ignoreTimes=false)
ssize_t BytesAvailable(const Timeout &timeout)
LogFile(unsigned int debug)
bool IsOpen() const
An object used to represent timeouts.
Definition: timeout.h:63