From 474089ebc8fc645bebad52441d7215f6ea3c598b Mon Sep 17 00:00:00 2001 From: Michael Crawford Date: Tue, 11 Nov 2014 23:06:39 -0500 Subject: [PATCH] Added FileLogWriter --- include/log/FileLogWriter.h | 84 ++++++++++++++++++++++++++++++++++++ msvc/libutil.vcxproj | 2 + msvc/libutil.vcxproj.filters | 6 +++ src/log/FileLogWriter.cpp | 65 ++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 include/log/FileLogWriter.h create mode 100644 src/log/FileLogWriter.cpp diff --git a/include/log/FileLogWriter.h b/include/log/FileLogWriter.h new file mode 100644 index 0000000..16c17a6 --- /dev/null +++ b/include/log/FileLogWriter.h @@ -0,0 +1,84 @@ +/* The MIT License (MIT) +* +* Copyright (c) 2014 Michael Crawford +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + +#ifndef FILELOGWRITER_H_INCLUDED__ +#define FILELOGWRITER_H_INCLUDED__ + +#include "prereqs.h" +#include "log/LogWriter.h" +#include +#include + +namespace util +{ + +/** + * A log writer that directs its output to a file. Not thread safe. + */ +class FileLogWriter final + : public LogWriter +{ +private: + std::ofstream file; + +public: + // constructors + FileLogWriter() = default; + FileLogWriter(const FileLogWriter&) = delete; + + /** + * Creates a writer opening a file immediately. Same behavior as open(). + * Check isOpen() before using the file. + */ + explicit FileLogWriter(const std::string& filename, bool append = false); + + // operators + FileLogWriter& operator=(const FileLogWriter&) = delete; + + /** + * Opens a file for writing. If a file is currently open it will be closed + * before the new file is opened. + * \param filename The path of the file to open. + * \param append If true, appends to an existing file. Otherwise replaces + * the existing file. + * \return True if the file was opened. + */ + bool open(const std::string& filename, bool append = false); + + /** + * Closes the file, if one is open. + */ + void close(); + + /** + * Returns true if a file is open. + */ + bool isOpen() const; + +protected: + virtual void output(const std::string& msg) override; +}; + +} + +#endif diff --git a/msvc/libutil.vcxproj b/msvc/libutil.vcxproj index 842c3f1..1d59ecc 100644 --- a/msvc/libutil.vcxproj +++ b/msvc/libutil.vcxproj @@ -80,6 +80,7 @@ + @@ -97,6 +98,7 @@ + diff --git a/msvc/libutil.vcxproj.filters b/msvc/libutil.vcxproj.filters index bda2d70..c99c207 100644 --- a/msvc/libutil.vcxproj.filters +++ b/msvc/libutil.vcxproj.filters @@ -71,6 +71,9 @@ Header Files\log + + Header Files\log + @@ -88,5 +91,8 @@ Source Files\log + + Source Files\log + \ No newline at end of file diff --git a/src/log/FileLogWriter.cpp b/src/log/FileLogWriter.cpp new file mode 100644 index 0000000..f0597e1 --- /dev/null +++ b/src/log/FileLogWriter.cpp @@ -0,0 +1,65 @@ +/* The MIT License (MIT) +* +* Copyright (c) 2014 Michael Crawford +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + +#include "log/FileLogWriter.h" + +namespace util +{ + +FileLogWriter::FileLogWriter(const std::string& filename, bool append) + : FileLogWriter() +{ + open(filename, append); +} + +bool FileLogWriter::open(const std::string& filename, bool append) +{ + close(); + + assert(!filename.empty()); + auto mode = std::ios::out | + (append ? std::ios::app : std::ios::trunc); + file.open(filename.c_str(), mode); + return file.is_open(); +} + +void FileLogWriter::close() +{ + if (file.is_open()) + { + file.close(); + } +} + +bool FileLogWriter::isOpen() const +{ + return file.is_open(); +} + +void FileLogWriter::output(const std::string& msg) +{ + assert(file.is_open()); + file << msg << std::endl; +} + +}