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

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