FACT++  1.0
WindowLog.h
Go to the documentation of this file.
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 
15 {
16  kDefault = 0,
17  kRed = 1,
18  kGreen = 2,
19  kYellow = 3,
20  kBlue = 4,
21  kMagenta = 5,
22  kCyan = 6,
23  kWhite = 7,
24 };
25 
28 {
29  kReset = -1,
30  kNormal = A_NORMAL,
31  kHighlight = A_STANDOUT,
32  kUnderline = A_UNDERLINE,
33  kReverse = A_REVERSE,
34  kBlink = A_BLINK,
35  kDim = A_DIM,
36  kBold = A_BOLD,
37  kProtect = A_PROTECT,
38  kInvisible = A_INVIS,
39  kAltCharset = A_ALTCHARSET,
40 };
41 /*
42 enum WindowLogManip
43 {
44  kLogOn = 1,
45  kLogOff = 2,
46  kNullOn = 3,
47  kNullOff = 4,
48 };
49 */
50 class WindowLog : public std::streambuf, public std::ostream
51 {
52  friend std::ostream &operator<<(std::ostream &lout, WindowLogColor m);
53  friend std::ostream &operator<<(std::ostream &lout, WindowLogAttrs m);
54  //friend std::ostream &operator<<(std::ostream &lout, WindowLogManip m);
55 private:
56  static const int fgBufferSize = 160;
57 
58  char fBuffer;
59  char fBase[fgBufferSize+1];
60  char *fPPtr;
61  const char *fEPtr;
62 
64 
65  std::vector<char> fBacklog;
66  std::map<int, int> fAttributes;
67 
68  std::ofstream fLogFile;
69 
70  bool fIsNull;
72 
73  std::mutex fMuxBacklog;
74  std::mutex fMuxFile;
75  std::mutex fMuxCout;
76  std::mutex fMuxWindow;
77 
79 
80  static std::string GetAnsiAttr(int m);
81 
82  void AddAttr(int m);
83  void AddColor(int m);
84 
85  bool WriteFile(const std::string &);
86  void WriteBuffer();
87 
88  int sync();
89  int overflow(int i); // i=EOF means not a real overflow
90 
91 public:
92  // --------------------------------------------------------------------------
93  //
99  //
100  WindowLog() : std::ostream(this), fPPtr(fBase), fEPtr(fBase+fgBufferSize), fWindow(0), fIsNull(false), fEnableBacklog(true),
101  fQueueFile(std::bind(&WindowLog::WriteFile, this, std::placeholders::_1))
102  {
103  //fLogFile.rdbuf()->pubsetbuf(0,0); // Switch off buffering
104  setp(&fBuffer, &fBuffer+1);
105  *this << '\0';
106  }
107  WindowLog(WindowLog const& log) : std::ios(), std::streambuf(), std::ostream((std::streambuf*)&log), fWindow(log.fWindow), fIsNull(false), fEnableBacklog(true),
108  fQueueFile(bind(&WindowLog::WriteFile, this, std::placeholders::_1))
109  {
110  //fLogFile.rdbuf()->pubsetbuf(0,0); // Switch off buffering
111  }
113  {
114  fQueueFile.wait(false);
115  }
116 
118  void SetWindow(WINDOW *w) { fWindow=w; }
119 
121  bool OpenLogFile(const std::string &filename, bool append=false);
122 
124  void CloseLogFile();
125 
127  void Display(bool empty=false);
128 
130  void EmptyBacklog();
131 
133  size_t GetSizeBacklog() const { return fBacklog.size(); }
134  std::string GetSizeStr() const;
135 
137  void SetNullOutput(bool n=true) { fIsNull=n; }
138  bool GetNullOutput() const { return fIsNull; }
139 
141  void SetBacklog(bool n=true) { fEnableBacklog=n; }
142  bool GetBacklog() const { return fEnableBacklog; }
143 };
144 
145 std::ostream &operator<<(std::ostream &lout, WindowLogColor m);
146 std::ostream &operator<<(std::ostream &lout, WindowLogAttrs m);
147 
148 #endif
std::mutex fMuxFile
Mutex securing backlog access.
Definition: WindowLog.h:74
char fBuffer
Definition: WindowLog.h:58
Queue< std::string > fQueueFile
Mutex securing output to fWindow.
Definition: WindowLog.h:78
bool WriteFile(const std::string &)
Definition: WindowLog.cc:139
Set attribute Invisible.
Definition: WindowLog.h:38
std::vector< char > fBacklog
Pointer to an ncurses Window.
Definition: WindowLog.h:65
Set color Cyan.
Definition: WindowLog.h:22
Set attribute Reverse.
Definition: WindowLog.h:33
Set attribute Alternative charset.
Definition: WindowLog.h:39
bool fEnableBacklog
Switch to toggle off physical output to the screen.
Definition: WindowLog.h:71
Set color White.
Definition: WindowLog.h:23
static std::string GetAnsiAttr(int m)
Definition: WindowLog.cc:301
std::mutex fMuxCout
Mutex securing file access.
Definition: WindowLog.h:75
WindowLog(WindowLog const &log)
Definition: WindowLog.h:107
void SetNullOutput(bool n=true)
Switch on or off any physical output to the screen (cout or fWindow)
Definition: WindowLog.h:137
void CloseLogFile()
Close a log-file.
Definition: WindowLog.cc:132
int i
Definition: db_dim_client.c:21
Set color Green.
Definition: WindowLog.h:18
char * fPPtr
Buffer to store the data in.
Definition: WindowLog.h:60
A C++ ostream to an ncurses window supporting attributes and colors.
Definition: WindowLog.h:50
Set color Yellow.
Definition: WindowLog.h:19
Set attribute Blink.
Definition: WindowLog.h:34
Set color Red.
Definition: WindowLog.h:17
~WindowLog()
Definition: WindowLog.h:112
Set default colors.
Definition: WindowLog.h:16
STL namespace.
void EmptyBacklog()
Empty backlog.
Definition: WindowLog.cc:32
static const int fgBufferSize
Definition: WindowLog.h:56
WINDOW * fWindow
Pointer to end of buffer.
Definition: WindowLog.h:63
Set attribute Highlight.
Definition: WindowLog.h:31
friend std::ostream & operator<<(std::ostream &lout, WindowLogColor m)
Definition: WindowLog.cc:388
WindowLogColor
Stream manipulators to change the color of a WindowLog stream.
Definition: WindowLog.h:14
Set attribute Normal.
Definition: WindowLog.h:30
size_t GetSizeBacklog() const
Get the current size of the backlog in bytes.
Definition: WindowLog.h:133
Set attribute Underline.
Definition: WindowLog.h:32
int sync()
This is called to flush the buffer of the streaming devices.
Definition: WindowLog.cc:247
bool GetBacklog() const
Definition: WindowLog.h:142
std::string GetSizeStr() const
Definition: WindowLog.cc:278
void AddColor(int m)
Definition: WindowLog.cc:332
const char * fEPtr
Pointer to present position in buffer.
Definition: WindowLog.h:61
std::map< int, int > fAttributes
Backlog storage.
Definition: WindowLog.h:66
bool wait(bool abrt=false)
Definition: Queue.h:186
Set color Magenta.
Definition: WindowLog.h:21
std::mutex fMuxBacklog
Switch to toggle storage in the backlog on or off.
Definition: WindowLog.h:73
void AddAttr(int m)
Definition: WindowLog.cc:362
Set color Blue.
Definition: WindowLog.h:20
Set attribute Dim.
Definition: WindowLog.h:35
bool fIsNull
Stream for redirection to a log-file.
Definition: WindowLog.h:70
char fBase[fgBufferSize+1]
Definition: WindowLog.h:59
void SetWindow(WINDOW *w)
Redirect the output to an ncurses WINDOW instead of cout.
Definition: WindowLog.h:118
struct _win_st WINDOW
Definition: ReadlineWindow.h:6
std::mutex fMuxWindow
Mutex securing output to cout.
Definition: WindowLog.h:76
void WriteBuffer()
Definition: WindowLog.cc:165
std::ofstream fLogFile
Storage for attributes (backlog)
Definition: WindowLog.h:68
void Display(bool empty=false)
Display backlog.
Definition: WindowLog.cc:46
bool GetNullOutput() const
Definition: WindowLog.h:138
Set attribute Protect.
Definition: WindowLog.h:37
WindowLogAttrs
Stream manipulators to change the attributes of a WindowLog stream.
Definition: WindowLog.h:27
int overflow(int i)
Definition: WindowLog.cc:261
bool OpenLogFile(const std::string &filename, bool append=false)
Open a log-file.
Definition: WindowLog.cc:111
Reset all attributes.
Definition: WindowLog.h:29
void SetBacklog(bool n=true)
Switch on or off any storage in the backlog.
Definition: WindowLog.h:141
Set attribute Bold.
Definition: WindowLog.h:36