Pol  Revision:cb584c9
streamsaver.h
Go to the documentation of this file.
1 #ifndef CLIB_STREAMSAVER_H
2 #define CLIB_STREAMSAVER_H
3 
4 #include <boost/noncopyable.hpp>
5 #include <iosfwd>
6 #include <memory>
7 #include <string>
8 #include <thread>
9 
10 #include <format/format.h>
11 #include "compilerspecifics.h"
12 #include "message_queue.h"
13 #if 0
14 #include "timer.h"
15 #endif
16 
17 namespace Pol
18 {
19 namespace Clib
20 {
21 class StreamWriter : boost::noncopyable
22 {
23 public:
24  StreamWriter();
25  virtual ~StreamWriter();
26  fmt::Writer& operator()();
27  virtual void init( const std::string& filepath ) = 0;
28  virtual void flush() = 0;
29  virtual void flush_file() = 0;
30 
31 protected:
32  std::unique_ptr<fmt::Writer> _writer;
33 };
34 
36 {
37 public:
39  virtual ~FMTStreamWriter(){};
40  virtual void init( const std::string& ) POL_OVERRIDE{}; // argument is not used
41  virtual void flush() POL_OVERRIDE{};
42  virtual void flush_file() POL_OVERRIDE{};
43 };
44 
46 {
47 public:
49  OFStreamWriter( std::ofstream* stream );
50  virtual ~OFStreamWriter();
51  virtual void init( const std::string& filepath ) POL_OVERRIDE;
52  virtual void flush() POL_OVERRIDE;
53  virtual void flush_file() POL_OVERRIDE;
54 
55 private:
56  std::ofstream* _stream;
57 #if 0
59 #endif
60  std::string _stream_name;
61 };
62 
64 {
65 public:
66  OStreamWriter();
67  OStreamWriter( std::ostream* stream );
68  virtual ~OStreamWriter();
69  virtual void init( const std::string& filepath ) POL_OVERRIDE;
70  virtual void flush() POL_OVERRIDE;
71  virtual void flush_file() POL_OVERRIDE;
72 
73 private:
74  std::ostream* _stream;
75 };
76 
77 
79 {
80  typedef std::unique_ptr<fmt::Writer> WriterPtr;
82 
83 public:
85  ThreadedOFStreamWriter( std::ofstream* stream );
86  virtual ~ThreadedOFStreamWriter();
87  virtual void init( const std::string& filepath ) POL_OVERRIDE;
88  virtual void flush() POL_OVERRIDE;
89  virtual void flush_file() POL_OVERRIDE;
90 
91 private:
92  void start_worker();
93  std::ofstream* _stream;
94  writer_queue _msg_queue;
95  std::thread _writethread;
96  std::list<WriterPtr> _writers_hold;
97  std::string _stream_name;
98 };
99 }
100 }
101 #endif // CLIB_STREAMSAVER_H
#define POL_OVERRIDE
STL namespace.
std::unique_ptr< fmt::Writer > _writer
Definition: streamsaver.h:32
fmt::Writer & operator()()
Definition: streamsaver.cpp:18
std::unique_ptr< fmt::Writer > WriterPtr
Definition: streamsaver.h:80
virtual void flush_file() POL_OVERRIDE
Definition: streamsaver.h:42
virtual void init(const std::string &filepath)=0
virtual void flush_file()=0
virtual void flush() POL_OVERRIDE
Definition: streamsaver.h:41
std::ostream * _stream
Definition: streamsaver.h:74
StreamWriter()
BaseClass implements only writer operator logic.
Definition: streamsaver.cpp:15
message_queue< WriterPtr > writer_queue
Definition: streamsaver.h:81
virtual void flush()=0
std::ofstream * _stream
Definition: streamsaver.h:56
virtual void init(const std::string &) POL_OVERRIDE
Definition: streamsaver.h:40
std::chrono::microseconds time_mu
Definition: timer.h:55
Definition: berror.cpp:12