file mode

This commit is contained in:
john30
2016-12-04 18:03:43 +01:00
parent 8eb2f72258
commit b4a596da5a
2 changed files with 191 additions and 191 deletions
+96 -96
View File
@@ -1,96 +1,96 @@
/* /*
* ebusd - daemon for communication with eBUS heating systems. * ebusd - daemon for communication with eBUS heating systems.
* Copyright (C) 2016 John Baier <ebusd@ebusd.eu> * Copyright (C) 2016 John Baier <ebusd@ebusd.eu>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include <config.h> #include <config.h>
#endif #endif
#include "rotatefile.h" #include "rotatefile.h"
#include "clock.h" #include "clock.h"
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <fcntl.h> #include <fcntl.h>
#include <fstream> #include <fstream>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/file.h> #include <sys/file.h>
#include <errno.h> #include <errno.h>
using namespace std; using namespace std;
RotateFile::~RotateFile() RotateFile::~RotateFile()
{ {
if (m_stream) { if (m_stream) {
fclose(m_stream); fclose(m_stream);
m_stream = NULL; m_stream = NULL;
} }
} }
bool RotateFile::setEnabled(bool enabled) bool RotateFile::setEnabled(bool enabled)
{ {
if (enabled==m_enabled) { if (enabled==m_enabled) {
return false; return false;
} }
m_enabled = enabled; m_enabled = enabled;
if (m_stream) { if (m_stream) {
fclose(m_stream); fclose(m_stream);
m_stream = NULL; m_stream = NULL;
} }
if (enabled) { if (enabled) {
m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb"); m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb");
m_fileSize = 0; m_fileSize = 0;
} }
return true; return true;
} }
void RotateFile::write(unsigned char* value, unsigned int size, bool received) void RotateFile::write(unsigned char* value, unsigned int size, bool received)
{ {
if (!m_enabled || !m_stream) { if (!m_enabled || !m_stream) {
return; return;
} }
if (m_textMode) { if (m_textMode) {
struct timespec ts; struct timespec ts;
struct tm* tm; struct tm* tm;
clockGettime(&ts); clockGettime(&ts);
tm = localtime(&ts.tv_sec); tm = localtime(&ts.tv_sec);
char* buf; char* buf;
fprintf(m_stream, "%04d-%02d-%02d %02d:%02d:%02d.%03ld %c", fprintf(m_stream, "%04d-%02d-%02d %02d:%02d:%02d.%03ld %c",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec/1000000, tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec/1000000,
received ? '<' : '>' received ? '<' : '>'
); );
for (unsigned int pos=0; pos<size; pos++) { for (unsigned int pos=0; pos<size; pos++) {
fprintf(m_stream, "%2.2x ", value[pos]); fprintf(m_stream, "%2.2x ", value[pos]);
} }
fprintf(m_stream, "\n"); fprintf(m_stream, "\n");
m_fileSize += 25+3*size+1; m_fileSize += 25+3*size+1;
} else { } else {
fwrite(value, (streamsize)size, 1, m_stream); fwrite(value, (streamsize)size, 1, m_stream);
m_fileSize += size; m_fileSize += size;
} }
if ((m_fileSize%1024) == 0) { if ((m_fileSize%1024) == 0) {
fflush(m_stream); fflush(m_stream);
} }
if (m_fileSize >= m_maxSize * 1024) { if (m_fileSize >= m_maxSize * 1024) {
string oldfile = string(m_fileName)+".old"; string oldfile = string(m_fileName)+".old";
if (rename(m_fileName.c_str(), oldfile.c_str()) == 0) { if (rename(m_fileName.c_str(), oldfile.c_str()) == 0) {
fclose(m_stream); fclose(m_stream);
m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb"); m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb");
m_fileSize = 0; m_fileSize = 0;
} }
} }
} }
+95 -95
View File
@@ -1,95 +1,95 @@
/* /*
* ebusd - daemon for communication with eBUS heating systems. * ebusd - daemon for communication with eBUS heating systems.
* Copyright (C) 2016 John Baier <ebusd@ebusd.eu> * Copyright (C) 2016 John Baier <ebusd@ebusd.eu>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef LIBUTILS_ROTATEFILE_H_ #ifndef LIBUTILS_ROTATEFILE_H_
#define LIBUTILS_ROTATEFILE_H_ #define LIBUTILS_ROTATEFILE_H_
#include <unistd.h> #include <unistd.h>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
/** @file rotatefile.h /** @file rotatefile.h
* Helpers for writing to rotating files. * Helpers for writing to rotating files.
*/ */
using namespace std; using namespace std;
/** /**
* Helper class for writing to a rotating file with maximum size. * Helper class for writing to a rotating file with maximum size.
*/ */
class RotateFile class RotateFile
{ {
public: public:
/** /**
* Construct a new instance. * Construct a new instance.
* @param fileName the name of the file write to. * @param fileName the name of the file write to.
* @param maxSize the maximum size of the file to 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. * @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) 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) {} : m_enabled(false), m_fileName(fileName), m_maxSize(maxSize), m_textMode(textMode), m_stream(), m_fileSize(0) {}
/** /**
* Destructor. * Destructor.
*/ */
virtual ~RotateFile(); virtual ~RotateFile();
/** /**
* Enable or disable writing to the file. * Enable or disable writing to the file.
* @param enabled @p true to enable writing to the file, @p false to disable it. * @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. * @return @p true when the state was changed, @p false otherwise.
*/ */
bool setEnabled(bool enabled=true); bool setEnabled(bool enabled=true);
/** /**
* Return whether writing to the file is enabled. * Return whether writing to the file is enabled.
* @return whether writing to the file is enabled. * @return whether writing to the file is enabled.
*/ */
bool isEnabled() { return m_enabled; } bool isEnabled() { return m_enabled; }
/** /**
* Write a number of bytes to the stream. * Write a number of bytes to the stream.
* @param value the pointer to the bytes to write. * @param value the pointer to the bytes to write.
* @param size the number of 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). * @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); void write(unsigned char* value, unsigned int size, bool received=true);
private: private:
/** whether writing to the file is enabled. */ /** whether writing to the file is enabled. */
bool m_enabled; bool m_enabled;
/** the name of the file write to. */ /** the name of the file write to. */
const string m_fileName; const string m_fileName;
/** the maximum size of @a m_file, or 0 for infinite. */ /** the maximum size of @a m_file, or 0 for infinite. */
const long m_maxSize; const long m_maxSize;
/** whether to write each byte with prefixed timestamp and direction as text. */ /** whether to write each byte with prefixed timestamp and direction as text. */
const bool m_textMode; const bool m_textMode;
/** the @a FILE to writing to. */ /** the @a FILE to writing to. */
FILE* m_stream; FILE* m_stream;
/** the number of bytes already written to the @a m_file. */ /** the number of bytes already written to the @a m_file. */
unsigned long m_fileSize; unsigned long m_fileSize;
}; };
#endif // LIBUTILS_ROTATEFILE_H_ #endif // LIBUTILS_ROTATEFILE_H_