diff --git a/src/lib/utils/rotatefile.cpp b/src/lib/utils/rotatefile.cpp index 65cfe511..a3acd035 100644 --- a/src/lib/utils/rotatefile.cpp +++ b/src/lib/utils/rotatefile.cpp @@ -1,96 +1,96 @@ -/* - * ebusd - daemon for communication with eBUS heating systems. - * Copyright (C) 2016 John Baier - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include "rotatefile.h" -#include "clock.h" -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -RotateFile::~RotateFile() -{ - if (m_stream) { - fclose(m_stream); - m_stream = NULL; - } -} - -bool RotateFile::setEnabled(bool enabled) -{ - if (enabled==m_enabled) { - return false; - } - m_enabled = enabled; - if (m_stream) { - fclose(m_stream); - m_stream = NULL; - } - if (enabled) { - m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb"); - m_fileSize = 0; - } - return true; -} - -void RotateFile::write(unsigned char* value, unsigned int size, bool received) -{ - if (!m_enabled || !m_stream) { - return; - } - if (m_textMode) { - struct timespec ts; - struct tm* tm; - clockGettime(&ts); - tm = localtime(&ts.tv_sec); - char* buf; - fprintf(m_stream, "%04d-%02d-%02d %02d:%02d:%02d.%03ld %c", - tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec/1000000, - received ? '<' : '>' - ); - for (unsigned int pos=0; pos= m_maxSize * 1024) { - string oldfile = string(m_fileName)+".old"; - if (rename(m_fileName.c_str(), oldfile.c_str()) == 0) { - fclose(m_stream); - m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb"); - m_fileSize = 0; - } - } -} +/* + * ebusd - daemon for communication with eBUS heating systems. + * Copyright (C) 2016 John Baier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "rotatefile.h" +#include "clock.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +RotateFile::~RotateFile() +{ + if (m_stream) { + fclose(m_stream); + m_stream = NULL; + } +} + +bool RotateFile::setEnabled(bool enabled) +{ + if (enabled==m_enabled) { + return false; + } + m_enabled = enabled; + if (m_stream) { + fclose(m_stream); + m_stream = NULL; + } + if (enabled) { + m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb"); + m_fileSize = 0; + } + return true; +} + +void RotateFile::write(unsigned char* value, unsigned int size, bool received) +{ + if (!m_enabled || !m_stream) { + return; + } + if (m_textMode) { + struct timespec ts; + struct tm* tm; + clockGettime(&ts); + tm = localtime(&ts.tv_sec); + char* buf; + fprintf(m_stream, "%04d-%02d-%02d %02d:%02d:%02d.%03ld %c", + tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec/1000000, + received ? '<' : '>' + ); + for (unsigned int pos=0; pos= m_maxSize * 1024) { + string oldfile = string(m_fileName)+".old"; + if (rename(m_fileName.c_str(), oldfile.c_str()) == 0) { + fclose(m_stream); + m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb"); + m_fileSize = 0; + } + } +} diff --git a/src/lib/utils/rotatefile.h b/src/lib/utils/rotatefile.h index fd4dfd10..ffdc5158 100644 --- a/src/lib/utils/rotatefile.h +++ b/src/lib/utils/rotatefile.h @@ -1,95 +1,95 @@ -/* - * ebusd - daemon for communication with eBUS heating systems. - * Copyright (C) 2016 John Baier - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef LIBUTILS_ROTATEFILE_H_ -#define LIBUTILS_ROTATEFILE_H_ - -#include -#include -#include - -/** @file rotatefile.h - * Helpers for writing to rotating files. - */ - -using namespace std; - -/** - * Helper class for writing to a rotating file with maximum size. - */ -class RotateFile -{ -public: - /** - * Construct a new instance. - * @param fileName the name of the file write to. - * @param maxSize the maximum size of the file to write to. - * @param textMode whether to write each byte with prefixed timestamp and direction as text. - */ - RotateFile(const string fileName, const long maxSize, const bool textMode=false) - : m_enabled(false), m_fileName(fileName), m_maxSize(maxSize), m_textMode(textMode), m_stream(), m_fileSize(0) {} - - /** - * Destructor. - */ - virtual ~RotateFile(); - - /** - * Enable or disable writing to the file. - * @param enabled @p true to enable writing to the file, @p false to disable it. - * @return @p true when the state was changed, @p false otherwise. - */ - bool setEnabled(bool enabled=true); - - /** - * Return whether writing to the file is enabled. - * @return whether writing to the file is enabled. - */ - bool isEnabled() { return m_enabled; } - - /** - * Write a number of bytes to the stream. - * @param value the pointer to the bytes to write. - * @param size the number of bytes to write. - * @param received @a true on reception, @a false on sending (only relevant in text mode). - */ - void write(unsigned char* value, unsigned int size, bool received=true); - -private: - /** whether writing to the file is enabled. */ - bool m_enabled; - - /** the name of the file write to. */ - const string m_fileName; - - /** the maximum size of @a m_file, or 0 for infinite. */ - const long m_maxSize; - - /** whether to write each byte with prefixed timestamp and direction as text. */ - const bool m_textMode; - - /** the @a FILE to writing to. */ - FILE* m_stream; - - /** the number of bytes already written to the @a m_file. */ - unsigned long m_fileSize; - -}; - -#endif // LIBUTILS_ROTATEFILE_H_ - +/* + * ebusd - daemon for communication with eBUS heating systems. + * Copyright (C) 2016 John Baier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LIBUTILS_ROTATEFILE_H_ +#define LIBUTILS_ROTATEFILE_H_ + +#include +#include +#include + +/** @file rotatefile.h + * Helpers for writing to rotating files. + */ + +using namespace std; + +/** + * Helper class for writing to a rotating file with maximum size. + */ +class RotateFile +{ +public: + /** + * Construct a new instance. + * @param fileName the name of the file write to. + * @param maxSize the maximum size of the file to write to. + * @param textMode whether to write each byte with prefixed timestamp and direction as text. + */ + RotateFile(const string fileName, const long maxSize, const bool textMode=false) + : m_enabled(false), m_fileName(fileName), m_maxSize(maxSize), m_textMode(textMode), m_stream(), m_fileSize(0) {} + + /** + * Destructor. + */ + virtual ~RotateFile(); + + /** + * Enable or disable writing to the file. + * @param enabled @p true to enable writing to the file, @p false to disable it. + * @return @p true when the state was changed, @p false otherwise. + */ + bool setEnabled(bool enabled=true); + + /** + * Return whether writing to the file is enabled. + * @return whether writing to the file is enabled. + */ + bool isEnabled() { return m_enabled; } + + /** + * Write a number of bytes to the stream. + * @param value the pointer to the bytes to write. + * @param size the number of bytes to write. + * @param received @a true on reception, @a false on sending (only relevant in text mode). + */ + void write(unsigned char* value, unsigned int size, bool received=true); + +private: + /** whether writing to the file is enabled. */ + bool m_enabled; + + /** the name of the file write to. */ + const string m_fileName; + + /** the maximum size of @a m_file, or 0 for infinite. */ + const long m_maxSize; + + /** whether to write each byte with prefixed timestamp and direction as text. */ + const bool m_textMode; + + /** the @a FILE to writing to. */ + FILE* m_stream; + + /** the number of bytes already written to the @a m_file. */ + unsigned long m_fileSize; + +}; + +#endif // LIBUTILS_ROTATEFILE_H_ +