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

Last change on this file since 17689 was 17229, checked in by tbretz, 11 years ago
Use Queue from externals instead of src.
File size: 5.0 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/// Stream manipulators to change the color of a WindowLog stream
14enum WindowLogColor
15{
16 kDefault = 0, ///< Set default colors
17 kRed = 1, ///< Set color Red
18 kGreen = 2, ///< Set color Green
19 kYellow = 3, ///< Set color Yellow
20 kBlue = 4, ///< Set color Blue
21 kMagenta = 5, ///< Set color Magenta
22 kCyan = 6, ///< Set color Cyan
23 kWhite = 7, ///< Set color White
24};
25
26/// Stream manipulators to change the attributes of a WindowLog stream
27enum WindowLogAttrs
28{
29 kReset = -1, ///< Reset all attributes
30 kNormal = A_NORMAL, ///< Set attribute Normal
31 kHighlight = A_STANDOUT, ///< Set attribute Highlight
32 kUnderline = A_UNDERLINE, ///< Set attribute Underline
33 kReverse = A_REVERSE, ///< Set attribute Reverse
34 kBlink = A_BLINK, ///< Set attribute Blink
35 kDim = A_DIM, ///< Set attribute Dim
36 kBold = A_BOLD, ///< Set attribute Bold
37 kProtect = A_PROTECT, ///< Set attribute Protect
38 kInvisible = A_INVIS, ///< Set attribute Invisible
39 kAltCharset = A_ALTCHARSET, ///< Set attribute Alternative charset
40};
41/*
42enum WindowLogManip
43{
44 kLogOn = 1,
45 kLogOff = 2,
46 kNullOn = 3,
47 kNullOff = 4,
48};
49*/
50class 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);
55private:
56 static const int fgBufferSize = 160;
57
58 char fBuffer; ///
59 char fBase[fgBufferSize+1]; /// Buffer to store the data in
60 char *fPPtr; /// Pointer to present position in buffer
61 const char *fEPtr; /// Pointer to end of buffer
62
63 WINDOW *fWindow; /// Pointer to an ncurses Window
64
65 std::vector<char> fBacklog; /// Backlog storage
66 std::map<int, int> fAttributes; /// Storage for attributes (backlog)
67
68 std::ofstream fLogFile; /// Stream for redirection to a log-file
69
70 bool fIsNull; /// Switch to toggle off physical output to the screen
71 bool fEnableBacklog; /// Switch to toggle storage in the backlog on or off
72
73 std::mutex fMuxBacklog; /// Mutex securing backlog access
74 std::mutex fMuxFile; /// Mutex securing file access
75 std::mutex fMuxCout; /// Mutex securing output to cout
76 std::mutex fMuxWindow; /// Mutex securing output to fWindow
77
78 Queue<std::string> fQueueFile;
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
91public:
92 // --------------------------------------------------------------------------
93 //
94 //! Default constructor which initializes the streamer and sets the device
95 //! which is used for the output
96 //!
97 //! Switch on backlog
98 //! Switch on screen output
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 }
112 ~WindowLog()
113 {
114 fQueueFile.wait(false);
115 }
116
117 /// Redirect the output to an ncurses WINDOW instead of cout
118 void SetWindow(WINDOW *w) { fWindow=w; }
119
120 /// Open a log-file
121 bool OpenLogFile(const std::string &filename, bool append=false);
122
123 /// Close a log-file
124 void CloseLogFile();
125
126 /// Display backlog
127 void Display(bool empty=false);
128
129 /// Empty backlog
130 void EmptyBacklog();
131
132 /// Get the current size of the backlog in bytes
133 size_t GetSizeBacklog() const { return fBacklog.size(); }
134 std::string GetSizeStr() const;
135
136 /// Switch on or off any physical output to the screen (cout or fWindow)
137 void SetNullOutput(bool n=true) { fIsNull=n; }
138 bool GetNullOutput() const { return fIsNull; }
139
140 /// Switch on or off any storage in the backlog
141 void SetBacklog(bool n=true) { fEnableBacklog=n; }
142 bool GetBacklog() const { return fEnableBacklog; }
143};
144
145std::ostream &operator<<(std::ostream &lout, WindowLogColor m);
146std::ostream &operator<<(std::ostream &lout, WindowLogAttrs m);
147
148#endif
Note: See TracBrowser for help on using the repository browser.