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

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