Lugdunum  0.1.0
FileHandler.cpp
Go to the documentation of this file.
3 
4 namespace lug {
5 namespace System {
6 namespace Logger {
7 
8 FileHandler::FileHandler(const std::string& name, const std::string& filename, bool truncate) : Handler(name) {
9  std::ios_base::openmode mode = std::ofstream::out;
10 
11  if (truncate) {
12  mode |= std::ofstream::trunc;
13  }
14 
15  _ofs.open(filename, mode);
16 
17  if (!_ofs.good()) {
18  // TODO: add file to lug except when it handles variadic args
19  LUG_EXCEPT(FileNotFoundException, "Failed to open file");
20  }
21 }
22 
24  _ofs.close();
25 }
26 
27 void FileHandler::handle(const priv::Message& msg) {
28  _ofs << msg.formatted.c_str();
29 }
30 
32  _ofs.flush();
33 }
34 
35 } // Logger
36 } // System
37 } // lug
FileHandler(const std::string &name, const std::string &filename, bool truncate)
Definition: FileHandler.cpp:8
#define LUG_EXCEPT(type, desc)
Definition: Exception.hpp:124
void handle(const priv::Message &msg)
Definition: FileHandler.cpp:27