API
logFileRaw.cpp
Go to the documentation of this file.
1 /** \file logFileRaw.cpp
2  * \brief Manage a raw log file.
3  * \author Jared R. Males (jaredmales@gmail.com)
4  *
5  * \ingroup logger_files
6  *
7  */
8 
9 #include <cstring>
10 #include "logFileRaw.hpp"
11 
12 namespace MagAOX
13 {
14 namespace logger
15 {
16 
18 {
19 }
20 
22 {
23  close();
24 }
25 
26 int logFileRaw::logPath( const std::string & newPath)
27 {
28  m_logPath = newPath;
29  return 0;
30 }
31 
32 std::string logFileRaw::logPath()
33 {
34  return m_logPath;
35 }
36 
37 int logFileRaw::logName( const std::string & newName)
38 {
39  m_logName = newName;
40  return 0;
41 }
42 
43 std::string logFileRaw::logName()
44 {
45  return m_logName;
46 }
47 
48 int logFileRaw::logExt( const std::string & newExt)
49 {
50  m_logExt = newExt;
51  return 0;
52 }
53 
54 std::string logFileRaw::logExt()
55 {
56  return m_logExt;
57 }
58 
59 int logFileRaw::maxLogSize( size_t newMaxFileSize )
60 {
61  m_maxLogSize = newMaxFileSize;
62  return 0;
63 }
64 
66 {
67  return m_maxLogSize;
68 }
69 
71 {
72  size_t N = flatlogs::logHeader::totalSize(data);
73 
74  //Check if we need a new file
75  if(m_currFileSize + N > m_maxLogSize || m_fout == 0)
76  {
78  if( createFile(ts) < 0 ) return -1;
79  }
80 
81  size_t nwr = fwrite( data.get(), sizeof(char), N, m_fout);
82 
83  if(nwr != N*sizeof(char))
84  {
85  std::cerr << "logFileRaw::writeLog: Error by fwrite. At: " << __FILE__ << " " << __LINE__ << "\n";
86  std::cerr << "logFileRaw::writeLog: errno says: " << strerror(errno) << "\n";
87  return -1;
88  }
89 
90  m_currFileSize += N;
91 
92  return 0;
93 }
94 
96 {
97  ///\todo this probably should be fsync, with appropriate error handling (see fsyncgate)
98  if(m_fout) fflush(m_fout);
99 
100  return 0;
101 }
102 
104 {
105  if(m_fout) fclose(m_fout);
106 
107  return 0;
108 }
109 
111 {
112  std::string tstamp = ts.timeStamp();
113 
114  //Create the standard log name
115  std::string fname = m_logPath + "/" + m_logName + "_" + tstamp + "." + m_logExt;
116 
117  if(m_fout) fclose(m_fout);
118 
119  errno = 0;
120  ///\todo handle case where file exists (only if another instance tries at same ns -- pathological)
121  m_fout = fopen(fname.c_str(), "wb");
122 
123  if(m_fout == 0)
124  {
125  std::cerr << "logFileRaw::createFile: Error by fopen. At: " << __FILE__ << " " << __LINE__ << "\n";
126  std::cerr << "logFileRaw::createFile: errno says: " << strerror(errno) << "\n";
127  std::cerr << "logFileRaw::createFile: fname = " << fname << "\n";
128  return -1;
129  }
130 
131  //Reset counters.
132  m_currFileSize = 0;
133 
134  return 0;
135 }
136 
137 } //namespace logger
138 } //namespace MagAOX
139 
std::string m_logName
The base name for the log files.
Definition: logFileRaw.hpp:50
std::string logExt()
Get the log extension.
Definition: logFileRaw.cpp:54
std::string m_logPath
The base path for the log files.
Definition: logFileRaw.hpp:49
logFileRaw()
Default constructor.
Definition: logFileRaw.cpp:17
int close()
Close the file pointer.
Definition: logFileRaw.cpp:103
size_t m_maxLogSize
The maximum file size in bytes. Default is 10 MB.
Definition: logFileRaw.hpp:53
std::string m_logExt
The extension for the log files.
Definition: logFileRaw.hpp:51
int createFile(flatlogs::timespecX &ts)
Create a new file.
Definition: logFileRaw.cpp:110
std::string logName()
Get the name.
Definition: logFileRaw.cpp:43
int writeLog(flatlogs::bufferPtrT &data)
Write a log entry to the file.
Definition: logFileRaw.cpp:70
size_t maxLogSize()
Get the maximum file size.
Definition: logFileRaw.cpp:65
int flush()
Flush the stream.
Definition: logFileRaw.cpp:95
FILE * m_fout
The file pointer.
Definition: logFileRaw.hpp:60
std::string logPath()
Get the path.
Definition: logFileRaw.cpp:32
size_t m_currFileSize
The current file size.
Definition: logFileRaw.hpp:62
static size_t totalSize(bufferPtrT &logBuffer)
Get the total size of the log entry, including the message buffer.
Definition: logHeader.hpp:602
std::shared_ptr< char > bufferPtrT
The log entry buffer smart pointer.
Definition: logHeader.hpp:58
static int timespec(bufferPtrT &logBuffer, const timespecX &ts)
Set the timespec of a log entry.
Definition: logHeader.hpp:435
Manage a raw log file.
std::ostream & cerr()
Definition: dm.hpp:24
A fixed-width timespec structure.
Definition: timespecX.hpp:35
int timeStamp(std::string &tstamp)
Get the filename timestamp for this timespecX.
Definition: timespecX.hpp:108