Skip to content

Commit

Permalink
Added FileLogWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
mbcrawfo committed Nov 12, 2014
1 parent 2c62ed6 commit 474089e
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
84 changes: 84 additions & 0 deletions include/log/FileLogWriter.h
Original file line number Diff line number Diff line change
@@ -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 <fstream>
#include <string>

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
2 changes: 2 additions & 0 deletions msvc/libutil.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\include\log\FileLogWriter.h" />
<ClInclude Include="..\include\log\ILogFormatter.h" />
<ClInclude Include="..\include\log\Log.h" />
<ClInclude Include="..\include\log\LogMessage.h" />
Expand All @@ -97,6 +98,7 @@
<ClInclude Include="..\include\utility\Singularity.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\log\FileLogWriter.cpp" />
<ClCompile Include="..\src\log\Log.cpp" />
<ClCompile Include="..\src\log\LogMessage.cpp" />
<ClCompile Include="..\src\log\LogWriter.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions msvc/libutil.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<ClInclude Include="..\include\log\Log.h">
<Filter>Header Files\log</Filter>
</ClInclude>
<ClInclude Include="..\include\log\FileLogWriter.h">
<Filter>Header Files\log</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\test\test.cpp">
Expand All @@ -88,5 +91,8 @@
<ClCompile Include="..\src\log\LogWriter.cpp">
<Filter>Source Files\log</Filter>
</ClCompile>
<ClCompile Include="..\src\log\FileLogWriter.cpp">
<Filter>Source Files\log</Filter>
</ClCompile>
</ItemGroup>
</Project>
65 changes: 65 additions & 0 deletions src/log/FileLogWriter.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 474089e

Please sign in to comment.