source: trunk/FACT++/src/WindowLog.cc@ 11575

Last change on this file since 11575 was 10498, checked in by tbretz, 13 years ago
Use stringstreams to replace Form not strings and +=
File size: 10.0 KB
Line 
1// **************************************************************************
2/** @class WindowLog
3
4@brief A C++ ostream to an ncurses window supporting attributes and colors
5
6@section References
7
8 - <A HREF="http://www.gnu.org/software/ncurses">GNU Ncurses</A>
9
10@todo
11 improve docu
12
13
14**/
15// **************************************************************************
16#include "WindowLog.h"
17
18#include <sstream>
19#include <iostream>
20#include <algorithm>
21
22#include <curses.h>
23
24#include "tools.h"
25
26using namespace std;
27
28// --------------------------------------------------------------------------
29//
30//! Delete the contents of the backlog
31//
32void WindowLog::EmptyBacklog()
33{
34 fMuxBacklog.lock();
35 fBacklog.clear();
36 fMuxBacklog.unlock();
37}
38
39// --------------------------------------------------------------------------
40//
41//! Display the backlog. If fWindow is NULL then it is flushed to cout
42//! otherwise to the window (including the colors and attributes)
43//!
44//! Access to cout, the backlog and the window is encapsulated in mutices.
45//
46void WindowLog::Display(bool empty)
47{
48 if (!fWindow)
49 {
50 fMuxBacklog.lock();
51 fMuxCout.lock();
52
53 for (unsigned int i=0; i<fBacklog.size(); i++)
54 cout << fBacklog[i];
55 cout.flush();
56
57 if (empty)
58 fBacklog.clear();
59
60 fMuxCout.unlock();
61 fMuxBacklog.unlock();
62 return;
63 }
64
65 int w, h;
66 getmaxyx(fWindow, h, w);
67
68 fMuxBacklog.lock();
69 fMuxWindow.lock();
70 vector<char>::iterator p0 = fBacklog.begin();
71
72 int lines = 0;
73 int x = 0;
74
75 for (unsigned int i=0; i<fBacklog.size(); i++)
76 {
77 if (fAttributes.find(i)!=fAttributes.end())
78 fAttributes[i]==-1 ? wattrset(fWindow, 0) : wattron(fWindow, fAttributes[i]);
79
80 if (fBacklog[i]=='\n')
81 {
82 // The attribute is added to the backlog in WriteBuffer
83 //wattrset(fWindow, 0);
84 lines += x/w + 1;
85 x=0;
86 }
87 wprintw(fWindow, "%c", fBacklog[i]);
88 x++;
89 }
90
91 if (empty)
92 fBacklog.clear();
93
94 fMuxWindow.unlock();
95 fMuxBacklog.unlock();
96
97 lines += x/w;
98}
99
100// --------------------------------------------------------------------------
101//
102//! Open a log-file into which any of the following output is written. If a
103//! log-file is alraedy open it is closed.
104//! before the new file is opened or an old one closed the current buffer
105//! is flushed.
106//!
107//! @param filename
108//! The filename of the file to open
109//!
110//! @returns
111//! Whether the log-file stream is open or not
112//
113bool WindowLog::OpenLogFile(const string &filename)
114{
115 fMuxFile.lock();
116 flush();
117
118 if (fLogFile.is_open())
119 fLogFile.close();
120
121 fLogFile.open(filename);
122 fMuxFile.unlock();
123
124 return fLogFile.is_open();
125}
126
127// --------------------------------------------------------------------------
128//
129//! Close an open log-file
130//
131void WindowLog::CloseLogFile()
132{
133 fMuxFile.lock();
134 fLogFile.close();
135 fMuxFile.unlock();
136}
137
138// --------------------------------------------------------------------------
139//
140//! This is the function which writes the stream physically to a device.
141//! If you want to add a new device this must be done here.
142//!
143//! If the backlog is enabled, the contents are put into the backlog.
144//! if fWindow is NULL the contents are flushed to cout, otherwise
145//! to the window defined by fWindow.
146//!
147//! In addition the contents are flushed to the log-file if open.
148//! If fIsNull is true any output on the screen (cout or fWindow) is
149//! suppressed.
150//!
151//! @todo
152//! Truncate the backlog
153//
154void WindowLog::WriteBuffer()
155{
156 // Store the number of characters in the buffer which should be flushed
157 const int len = fPPtr - fBase;
158
159 // restart writing to the buffer at its first char
160 fPPtr = fBase;
161
162 // If the is nothing to output, we are done
163 if (len<=0)
164 return;
165
166 // FIXME: Truncate backlog!
167
168 // If fWindow is set, output everything to the window, otherwise
169 // to cout
170 const string sout = string(fBase, len);
171
172 if (!fIsNull)
173 {
174 if (fWindow)
175 {
176 fMuxWindow.lock();
177 if (!fIsNull)
178 wprintw(fWindow, "%s", sout.c_str());
179 // If the stream got flushed due to a line break
180 // reset all attributes
181 if (fBase[len-1]=='\n')
182 wattrset(fWindow, 0);
183 fMuxWindow.unlock();
184 }
185 else
186 {
187 fMuxCout.lock();
188 cout << sout;
189 // If the stream got flushed due to a line break
190 // reset all attributes
191 if (fBase[len-1]=='\n')
192 cout << "\033[0m";
193 cout.flush();
194 fMuxCout.unlock();
195 }
196 }
197
198 // Add the buffer to the backlog
199 if (fEnableBacklog)
200 {
201 fMuxBacklog.lock();
202 fBacklog.insert(fBacklog.end(), fBase, fBase+len);
203
204 // If the stream got flushed due to a line break
205 // add the reset of all attributes to the backlog
206 if (fBase[len-1]=='\n')
207 {
208 if (!fWindow)
209 {
210 const char *reset = "\033[0m";
211 fBacklog.insert(fBacklog.end(), reset, reset+4);
212
213 }
214 else
215 fAttributes[fBacklog.size()] = -1;
216 }
217 fMuxBacklog.unlock();
218 }
219
220 // Output everything also to the log-file
221 fMuxFile.lock();
222 fLogFile << sout;
223 fLogFile.flush();
224 fMuxFile.unlock();
225
226 // If we are flushing because of an EOL, we reset also all attributes
227}
228
229// --------------------------------------------------------------------------
230//
231//! This is called to flush the buffer of the streaming devices
232//
233int WindowLog::sync()
234{
235 WriteBuffer();
236 return 0;
237}
238
239// --------------------------------------------------------------------------
240//
241//! This function comes from streambuf and should output the buffer to
242//! the device (flush, endl) or handle a buffer overflow (too many chars)
243//! If a real overflow happens i contains the next chars which doesn't
244//! fit into the buffer anymore.If the buffer is not really filled,
245//! i is EOF(-1).
246//
247int WindowLog::overflow(int i) // i=EOF means not a real overflow
248{
249 *fPPtr++ = (char)i;
250
251 if (fPPtr == fEPtr)
252 WriteBuffer();
253
254 return 0;
255}
256
257// --------------------------------------------------------------------------
258//
259//! Returns the size of the backlog buffer as string.
260//!
261//! @returns
262//! Size of the backlog as a string, e.g. "1k"
263//
264string WindowLog::GetSizeStr() const
265{
266 int s = GetSizeBacklog()/1000;
267 if (s==0)
268 return "0";
269
270 char u = 'k';
271 if (s>999)
272 {
273 s/=1000;
274 u = 'M';
275 }
276
277 ostringstream str;
278 str << s << u;
279 return str.str();
280}
281
282// --------------------------------------------------------------------------
283//
284//! @returns
285//! the ANSI code corresponding to the attributes
286//
287string WindowLog::GetAnsiAttr(int m)
288{
289 if (m==kReset || m==kDefault)
290 return "\033[0m";
291
292 string rc;
293
294 if ((m&COLOR_PAIR(kRed) )==COLOR_PAIR(kRed) ) rc += "\033[31m";
295 if ((m&COLOR_PAIR(kGreen) )==COLOR_PAIR(kGreen) ) rc += "\033[32m";
296 if ((m&COLOR_PAIR(kYellow) )==COLOR_PAIR(kYellow) ) rc += "\033[33m";
297 if ((m&COLOR_PAIR(kBlue) )==COLOR_PAIR(kBlue) ) rc += "\033[34m";
298 if ((m&COLOR_PAIR(kMagenta))==COLOR_PAIR(kMagenta)) rc += "\033[35m";
299 if ((m&COLOR_PAIR(kCyan) )==COLOR_PAIR(kCyan) ) rc += "\033[36m";
300 if ((m&COLOR_PAIR(kWhite) )==COLOR_PAIR(kWhite) ) rc += "\033[0m\033[1m";
301
302 if ((m&kBold )==kBold ) rc += "\033[1m";
303 if ((m&kDim )==kDim ) rc += "\033[2m";
304 if ((m&kUnderline)==kUnderline) rc += "\033[4m";
305 if ((m&kBlink )==kBlink ) rc += "\033[5m";
306
307 return rc;
308}
309
310// --------------------------------------------------------------------------
311//
312//! Add color to the stream according to the attribute. If fWindow is not
313//! set this is an ANSI color code, otherwise the window's output
314//! attributes are set.
315//! It is also added to the backlog. Access to the backlog is encapsulated
316//! into its mutex.
317//
318void WindowLog::AddColor(int m)
319{
320 const int col = COLOR_PAIR(m);
321
322 if (!fWindow)
323 // We don't have to flush here, because the attributes are simply
324 // part of the stream
325 *this << GetAnsiAttr(col);
326 else
327 {
328 // Before we change the attributes we have to flush the screen
329 // otherwise we would have to buffer them until we flush the
330 // contents
331 flush();
332 wattron(fWindow, col);
333 }
334
335 fMuxBacklog.lock();
336 fAttributes[fBacklog.size()] |= col;
337 fMuxBacklog.unlock();
338}
339
340// --------------------------------------------------------------------------
341//
342//! Add attributes to the stream according to the attribute. If fWindow is
343//! not set this is an ANSI code, otherwise the window's output
344//! attributes are set.
345//! It is also added to the backlog. Access to the backlog is encapsulated
346//! into its mutex.
347//
348void WindowLog::AddAttr(int m)
349{
350 if (!fWindow)
351 // We don't have to flush here, because the attributes are simply
352 // part of the stream
353 *this << GetAnsiAttr(m);
354 else
355 {
356 // Before we change the attributes we have to flush the screen
357 // otherwise we would have to buffer them until we flush the
358 // contents
359 flush();
360 m==kReset ? wattrset(fWindow, 0) : wattron(fWindow, m);
361 }
362
363 fMuxBacklog.lock();
364 m==kReset ?
365 fAttributes[fBacklog.size()] = -1 :
366 fAttributes[fBacklog.size()] |= m;
367 fMuxBacklog.unlock();
368}
369
370// --------------------------------------------------------------------------
371//
372//!
373//
374std::ostream &operator<<(std::ostream &lout, WindowLogColor m)
375{
376 WindowLog *log=dynamic_cast<WindowLog*>(lout.rdbuf());
377 if (log)
378 log->AddColor(m);
379 return lout;
380}
381
382// --------------------------------------------------------------------------
383//
384//!
385//
386std::ostream &operator<<(std::ostream &lout, WindowLogAttrs m)
387{
388 WindowLog *log=dynamic_cast<WindowLog*>(lout.rdbuf());
389 if (log)
390 log->AddAttr(m);
391 return lout;
392}
Note: See TracBrowser for help on using the repository browser.