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

Last change on this file since 13452 was 11942, checked in by tbretz, 13 years ago
Added append option to OpenLogFile
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/// Stream manipulators to change the color of a WindowLog stream
12enum WindowLogColor
13{
14 kDefault = 0, ///< Set default colors
15 kRed = 1, ///< Set color Red
16 kGreen = 2, ///< Set color Green
17 kYellow = 3, ///< Set color Yellow
18 kBlue = 4, ///< Set color Blue
19 kMagenta = 5, ///< Set color Magenta
20 kCyan = 6, ///< Set color Cyan
21 kWhite = 7, ///< Set color White
22};
23
24/// Stream manipulators to change the attributes of a WindowLog stream
25enum WindowLogAttrs
26{
27 kReset = -1, ///< Reset all attributes
28 kNormal = A_NORMAL, ///< Set attribute Normal
29 kHighlight = A_STANDOUT, ///< Set attribute Highlight
30 kUnderline = A_UNDERLINE, ///< Set attribute Underline
31 kReverse = A_REVERSE, ///< Set attribute Reverse
32 kBlink = A_BLINK, ///< Set attribute Blink
33 kDim = A_DIM, ///< Set attribute Dim
34 kBold = A_BOLD, ///< Set attribute Bold
35 kProtect = A_PROTECT, ///< Set attribute Protect
36 kInvisible = A_INVIS, ///< Set attribute Invisible
37 kAltCharset = A_ALTCHARSET, ///< Set attribute Alternative charset
38};
39/*
40enum WindowLogManip
41{
42 kLogOn = 1,
43 kLogOff = 2,
44 kNullOn = 3,
45 kNullOff = 4,
46};
47*/
48class WindowLog : public std::streambuf, public std::ostream
49{
50 friend std::ostream &operator<<(std::ostream &lout, WindowLogColor m);
51 friend std::ostream &operator<<(std::ostream &lout, WindowLogAttrs m);
52 //friend std::ostream &operator<<(std::ostream &lout, WindowLogManip m);
53private:
54 static const int fgBufferSize = 160;
55
56 char fBuffer; ///
57 char fBase[fgBufferSize+1]; /// Buffer to store the data in
58 char *fPPtr; /// Pointer to present position in buffer
59 const char *fEPtr; /// Pointer to end of buffer
60
61 WINDOW *fWindow; /// Pointer to an ncurses Window
62
63 std::vector<char> fBacklog; /// Backlog storage
64 std::map<int, int> fAttributes; /// Storage for attributes (backlog)
65
66 std::ofstream fLogFile; /// Stream for redirection to a log-file
67
68 bool fIsNull; /// Switch to toggle off physical output to the screen
69 bool fEnableBacklog; /// Switch to toggle storage in the backlog on or off
70
71 std::mutex fMuxBacklog; /// Mutex securing backlog access
72 std::mutex fMuxFile; /// Mutex securing file access
73 std::mutex fMuxCout; /// Mutex securing output to cout
74 std::mutex fMuxWindow; /// Mutex securing output to fWindow
75
76 static std::string GetAnsiAttr(int m);
77
78 void AddAttr(int m);
79 void AddColor(int m);
80
81 void WriteBuffer();
82
83 int sync();
84 int overflow(int i); // i=EOF means not a real overflow
85
86public:
87 // --------------------------------------------------------------------------
88 //
89 //! Default constructor which initializes the streamer and sets the device
90 //! which is used for the output
91 //!
92 //! Switch on backlog
93 //! Switch on screen output
94 //
95 WindowLog() : std::ostream(this), fPPtr(fBase), fEPtr(fBase+fgBufferSize), fWindow(0), fIsNull(false), fEnableBacklog(true)
96 {
97 fLogFile.rdbuf()->pubsetbuf(0,0); // Switch off buffering
98 setp(&fBuffer, &fBuffer+1);
99 *this << '\0';
100 }
101 WindowLog(WindowLog const& log) : std::ios(), std::streambuf(), std::ostream((std::streambuf*)&log), fWindow(log.fWindow), fIsNull(false), fEnableBacklog(true)
102 {
103 fLogFile.rdbuf()->pubsetbuf(0,0); // Switch off buffering
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.