6#ifndef logger_logFileRaw_hpp
7#define logger_logFileRaw_hpp
11#include <mx/ioutils/fileUtils.hpp>
12#include <mx/ioutils/stringUtils.hpp>
16#include "../file/fileTimes.hpp"
35template <
class verboseT = XWC_DEFAULT_VERBOSITY>
77 mx::error_t
logPath(
const std::string &newPath );
91 mx::error_t
logName(
const std::string &newName );
105 mx::error_t
logExt(
const std::string &newExt );
164template <
class verboseT>
169template <
class verboseT>
175template <
class verboseT>
182 catch(
const std::bad_alloc &e )
184 std::throw_with_nested(
xwcException(
"string assignment" ) );
186 catch(
const std::exception &e )
188 return mx::error_report( mx::error_t::std_exception, std::string(
"string assignment: " ) + e.what() );
191 return mx::error_t::noerror;
194template <
class verboseT>
200template <
class verboseT>
207 catch(
const std::bad_alloc &e )
209 std::throw_with_nested(
xwcException(
"string assignment" ) );
211 catch(
const std::exception &e )
213 return mx::error_report( mx::error_t::std_exception, std::string(
"string assignment: " ) + e.what() );
216 return mx::error_t::noerror;
219template <
class verboseT>
225template <
class verboseT>
232 catch(
const std::bad_alloc &e )
234 std::throw_with_nested(
xwcException(
"string assignment" ) );
236 catch(
const std::exception &e )
238 return mx::error_report( mx::error_t::std_exception, std::string(
"string assignment: " ) + e.what() );
241 return mx::error_t::noerror;
244template <
class verboseT>
250template <
class verboseT>
255 m_maxLogSize = newMaxFileSize;
257 catch(
const std::bad_alloc &e )
259 std::throw_with_nested(
xwcException(
"string assignment" ) );
261 catch(
const std::exception &e )
263 return mx::error_report( mx::error_t::std_exception, std::string(
"string assignment: " ) + e.what() );
266 return mx::error_t::noerror;
269template <
class verboseT>
275template <
class verboseT>
281 if( m_currFileSize + N > m_maxLogSize || m_fout == 0 )
285 mx_error_check( createFile( ts ) );
288 size_t nwr = fwrite( data.get(),
sizeof(
char ), N, m_fout );
290 if( nwr != N *
sizeof(
char ) )
292 return mx::error_report<verboseT>( mx::errno2error_t( errno ),
"Error from fwrite" );
297 return mx::error_t::noerror;
300template <
class verboseT>
307 if( fflush( m_fout ) != 0 )
309 return mx::error_report<verboseT>( mx::errno2error_t( errno ),
"Error from fflush" );
312 return mx::error_t::noerror;
315template <
class verboseT>
322 if( fclose( m_fout ) != 0 )
326 return mx::error_report<verboseT>( mx::errno2error_t( errno ),
"Error from fflush" );
332 return mx::error_t::noerror;
335template <
class verboseT>
338 std::string fileName;
347 return mx::error_report<verboseT>( errc );
352 std::throw_with_nested( mx::exception<verboseT>(mx::error_t::exception));
355 std::string fullPath = m_logPath +
'/' + relPath +
'/';
358 mx::error_t errc = mx::ioutils::createDirectories( fullPath );
362 return mx::error_report<verboseT>( errc,
"creating directory" );
365 fullPath += fileName;
367 if( mx::ioutils::exists( fullPath, errc ) )
369 return mx::error_report<verboseT>( mx::error_t::eexist,
"file " + fullPath +
" exists" );
374 return mx::error_report<verboseT>( errc,
"checking directory" );
379 if( errc != mx::error_t::noerror )
381 mx::error_report<verboseT>( errc,
"Error from close, attempting to continue");
386 m_fout = fopen( fullPath.c_str(),
"wb" );
390 return mx::error_report<verboseT>( mx::errno2error_t( errno ),
"Error from fopen on " + fullPath );
396 return mx::error_t::noerror;
A class to manage raw binary log files.
logFileRaw()
Default constructor.
size_t maxLogSize()
Get the maximum file size.
FILE * m_fout
The file pointer.
mx::error_t logExt(const std::string &newExt)
Set the log extension.
mx::error_t writeLog(flatlogs::bufferPtrT &data)
Write a log entry to the file.
std::string logName()
Get the name.
mx::error_t logPath(const std::string &newPath)
Set the path.
std::string logPath()
Get the path.
mx::error_t maxLogSize(size_t newMaxFileSize)
Set the maximum file size.
mx::error_t flush()
Flush the stream.
mx::error_t logName(const std::string &newName)
Set the log name.
std::string m_logName
The base name for the log files.
std::string m_logPath
The base path for the log files.
mx::error_t close()
Close the file pointer.
size_t m_maxLogSize
The maximum file size in bytes. Default is 10 MB.
mx::error_t createFile(flatlogs::timespecX &ts)
Create a new file.
std::string logExt()
Get the log extension.
std::string m_logExt
The extension for the log files.
size_t m_currFileSize
The current file size.
Augments an exception with the source file and line.
Flatlogs single include file.
#define MAGAOX_default_logExt
The extension for MagAO-X binary log files.
#define MAGAOX_default_max_logSize
The default maximum log 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.
static int timespec(bufferPtrT &logBuffer, const timespecX &ts)
Set the timespec of a log entry.
mx::error_t fileTimeRelPath(std::string &tstamp, std::string &relPath, time_t ts_sec, long ts_nsec)
Get the timestamp and the relative path based on a time.
A fixed-width timespec structure.
nanosecT time_ns
Nanoseconds.
secT time_s
Time since the Unix epoch.