WARPXM v1.10.0
Loading...
Searching...
No Matches
wxlogrecordhandler.h
Go to the documentation of this file.
1#ifndef wxrecordhandler_h
2#define wxrecordhandler_h
3
4// WarpX includes
5#include "wxobject.h"
6#include "wxcreator.h"
7#include "wxcryptset.h"
8
9// std includes
10#include <iostream>
11#include <string>
12#include <fstream>
13#include <ios>
14
25{
26public:
30 WxLogRecordHandler() : _outStream(0), _owns(false)
31 {
32 _outStream = &std::cout;
33 }
34
36 {
37 // delete stream if owning it
38 if (_owns)
39 delete _outStream;
40 }
41
47 void setStream(std::ostream* stream)
48 {
49 _outStream = stream;
50 }
51
55 void write(const std::string& msg)
56 {
57 *_outStream << msg;
58 *_outStream << std::flush;
59 }
60
68 void setOwns(bool handlerOwns)
69 {
70 _owns = handlerOwns;
71 }
72
76 bool owns() const
77 {
78 return _owns;
79 }
80
81private:
82 std::ostream* _outStream;
83 bool _owns;
84};
85
90{
91public:
93 {
94 }
95
99 WxStreamHandler(std::ostream* stream)
100 {
101 setStream(stream);
102 }
103};
104
110{
111public:
113 {
114 }
115
121 WxFileHandler(const std::string& fname,
122 std::ios_base::openmode mode = std::ios_base::trunc)
123 {
124 // open a file with specified mode and set it as the output stream
125 _file = new std::ofstream(fname.c_str(), mode);
126 setStream(_file);
127 }
128
130 {
131 delete _file;
132 }
133
137 void setup(const WxCryptSet& wxc)
138 {
139 // read file and open it for logging
140 std::string fname = wxc.get<std::string>("file");
141 if (_file)
142 // need to close _file if it is already open
143 delete _file;
144 _file = new std::ofstream(fname.c_str());
145 setStream(_file);
146 }
147
148private:
149 std::ofstream* _file;
150};
151
153#endif // wxrecordhandler_h
VALUETYPE get(const std::string &name) const
Retrieve the value associated with name.
Definition: wxcrypt.h:80
WxCryptSet extends WxCrypt by providing, in addition to name-value pairs, an set of named WxCryptSets...
Definition: wxcryptset.h:35
Class to log messages to a file.
Definition: wxlogrecordhandler.h:110
WxFileHandler()
Definition: wxlogrecordhandler.h:112
void setup(const WxCryptSet &wxc)
Setup handler from supplied cryptset.
Definition: wxlogrecordhandler.h:137
~WxFileHandler()
Definition: wxlogrecordhandler.h:129
WxFileHandler(const std::string &fname, std::ios_base::openmode mode=std::ios_base::trunc)
Create new handler to write log messages to specified file.
Definition: wxlogrecordhandler.h:121
Class to handle log messages generated by WarpX logging system.
Definition: wxlogrecordhandler.h:25
void setStream(std::ostream *stream)
Set the output stream to one specified.
Definition: wxlogrecordhandler.h:47
void setOwns(bool handlerOwns)
Set if the stream to which data is being written is owned by the handler.
Definition: wxlogrecordhandler.h:68
bool owns() const
Returns true if handler owns the stream and false otherwise.
Definition: wxlogrecordhandler.h:76
WxLogRecordHandler()
Create new handler and attach std::cout to it.
Definition: wxlogrecordhandler.h:30
void write(const std::string &msg)
Write message 'msg' to the output stream.
Definition: wxlogrecordhandler.h:55
virtual ~WxLogRecordHandler()
Definition: wxlogrecordhandler.h:35
WxObject is a base class for WarpX classes which need to go through a creation/destruction cycle with...
Definition: wxobject.h:54
Class to log messages to an open stream.
Definition: wxlogrecordhandler.h:90
WxStreamHandler()
Definition: wxlogrecordhandler.h:92
WxStreamHandler(std::ostream *stream)
Create new handler with the specified output stream.
Definition: wxlogrecordhandler.h:99