source: trunk/FACT++/src/WindowLog.h@ 20086

Last change on this file since 20086 was 19064, checked in by tbretz, 6 years ago
Some minor changes to make it compatible with newer sets of headers.
File size: 4.6 KB
Line 
1#ifndef FACT_WindowLog
2#define FACT_WindowLog
3
4#include <map>
5#include <mutex>
6#include <vector>
7#include <fstream>
8#include <functional> // std::bind
9
10#include <ncurses.h> // A_NORMAL etc
11
12#include "../externals/Queue.h"
13
14#include "WindowLogColor.h"
15
16/// Stream manipulators to change the attributes of a WindowLog stream
17enum WindowLogAttrs
18{
19 kReset = -1, ///< Reset all attributes
20 kNormal = A_NORMAL, ///< Set attribute Normal
21 kHighlight = A_STANDOUT, ///< Set attribute Highlight
22 kUnderline = A_UNDERLINE, ///< Set attribute Underline
23 kReverse = A_REVERSE, ///< Set attribute Reverse
24 kBlink = A_BLINK, ///< Set attribute Blink
25 kDim = A_DIM, ///< Set attribute Dim
26 kBold = A_BOLD, ///< Set attribute Bold
27 kProtect = A_PROTECT, ///< Set attribute Protect
28 kInvisible = A_INVIS, ///< Set attribute Invisible
29 kAltCharset = A_ALTCHARSET, ///< Set attribute Alternative charset
30};
31/*
32enum WindowLogManip
33{
34 kLogOn = 1,
35 kLogOff = 2,
36 kNullOn = 3,
37 kNullOff = 4,
38};
39*/
40class WindowLog : public std::streambuf, public std::ostream
41{
42 friend std::ostream &operator<<(std::ostream &lout, WindowLogColor m);
43 friend std::ostream &operator<<(std::ostream &lout, WindowLogAttrs m);
44 //friend std::ostream &operator<<(std::ostream &lout, WindowLogManip m);
45private:
46 static const int fgBufferSize = 160;
47
48 char fBuffer; ///
49 char fBase[fgBufferSize+1]; /// Buffer to store the data in
50 char *fPPtr; /// Pointer to present position in buffer
51 const char *fEPtr; /// Pointer to end of buffer
52
53 WINDOW *fWindow; /// Pointer to an ncurses Window
54
55 std::vector<char> fBacklog; /// Backlog storage
56 std::map<int, int> fAttributes; /// Storage for attributes (backlog)
57
58 std::ofstream fLogFile; /// Stream for redirection to a log-file
59
60 bool fIsNull; /// Switch to toggle off physical output to the screen
61 bool fEnableBacklog; /// Switch to toggle storage in the backlog on or off
62
63 std::mutex fMuxBacklog; /// Mutex securing backlog access
64 std::mutex fMuxFile; /// Mutex securing file access
65 std::mutex fMuxCout; /// Mutex securing output to cout
66 std::mutex fMuxWindow; /// Mutex securing output to fWindow
67
68 Queue<std::string> fQueueFile;
69
70 static std::string GetAnsiAttr(int m);
71
72 void AddAttr(int m);
73 void AddColor(int m);
74
75 bool WriteFile(const std::string &);
76 void WriteBuffer();
77
78 int sync();
79 int overflow(int i); // i=EOF means not a real overflow
80
81public:
82 // --------------------------------------------------------------------------
83 //
84 //! Default constructor which initializes the streamer and sets the device
85 //! which is used for the output
86 //!
87 //! Switch on backlog
88 //! Switch on screen output
89 //
90 WindowLog() : std::ostream(this), fPPtr(fBase), fEPtr(fBase+fgBufferSize), fWindow(0), fIsNull(false), fEnableBacklog(true),
91 fQueueFile(std::bind(&WindowLog::WriteFile, this, std::placeholders::_1))
92 {
93 //fLogFile.rdbuf()->pubsetbuf(0,0); // Switch off buffering
94 setp(&fBuffer, &fBuffer+1);
95 *this << '\0';
96 }
97 WindowLog(WindowLog const& log) : std::ios(), std::streambuf(), std::ostream((std::streambuf*)&log), fWindow(log.fWindow), fIsNull(false), fEnableBacklog(true),
98 fQueueFile(std::bind(&WindowLog::WriteFile, this, std::placeholders::_1))
99 {
100 //fLogFile.rdbuf()->pubsetbuf(0,0); // Switch off buffering
101 }
102 ~WindowLog()
103 {
104 fQueueFile.wait(false);
105 }
106
107 /// Redirect the output to an ncurses WINDOW instead of cout
108 void SetWindow(WINDOW *w) { fWindow=w; }
109
110 /// Open a log-file
111 bool OpenLogFile(const std::string &filename, bool append=false);
112
113 /// Close a log-file
114 void CloseLogFile();
115
116 /// Display backlog
117 void Display(bool empty=false);
118
119 /// Empty backlog
120 void EmptyBacklog();
121
122 /// Get the current size of the backlog in bytes
123 size_t GetSizeBacklog() const { return fBacklog.size(); }
124 std::string GetSizeStr() const;
125
126 /// Switch on or off any physical output to the screen (cout or fWindow)
127 void SetNullOutput(bool n=true) { fIsNull=n; }
128 bool GetNullOutput() const { return fIsNull; }
129
130 /// Switch on or off any storage in the backlog
131 void SetBacklog(bool n=true) { fEnableBacklog=n; }
132 bool GetBacklog() const { return fEnableBacklog; }
133};
134
135std::ostream &operator<<(std::ostream &lout, WindowLogColor m);
136std::ostream &operator<<(std::ostream &lout, WindowLogAttrs m);
137
138#endif
Note: See TracBrowser for help on using the repository browser.