API
 
Loading...
Searching...
No Matches
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
12namespace MagAOX
13{
14namespace logger
15{
16
20
25
26int logFileRaw::logPath( const std::string & newPath)
27{
28 m_logPath = newPath;
29 return 0;
30}
31
33{
34 return m_logPath;
35}
36
37int logFileRaw::logName( const std::string & newName)
38{
39 m_logName = newName;
40 return 0;
41}
42
44{
45 return m_logName;
46}
47
48int logFileRaw::logExt( const std::string & newExt)
49{
50 m_logExt = newExt;
51 return 0;
52}
53
54std::string logFileRaw::logExt()
55{
56 return m_logExt;
57}
58
59int 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.
std::string logExt()
Get the log extension.
std::string m_logPath
The base path for the log files.
logFileRaw()
Default constructor.
int close()
Close the file pointer.
size_t m_maxLogSize
The maximum file size in bytes. Default is 10 MB.
std::string m_logExt
The extension for the log files.
int createFile(flatlogs::timespecX &ts)
Create a new file.
std::string logName()
Get the name.
int writeLog(flatlogs::bufferPtrT &data)
Write a log entry to the file.
size_t maxLogSize()
Get the maximum file size.
int flush()
Flush the stream.
FILE * m_fout
The file pointer.
std::string logPath()
Get the path.
size_t m_currFileSize
The current file size.
static size_t totalSize(bufferPtrT &logBuffer)
Get the total size of the log entry, including the message buffer.
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.
Manage a raw log file.
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.