Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxLogIOStreamRedirecter.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 #include "cxReporter.h"
35 
36 namespace cx
37 {
38 
39 
44 class MyStreamBuf: public std::basic_streambuf<char, std::char_traits<char> >
45 {
46 public:
47  MyStreamBuf(MESSAGE_LEVEL level) :
48  mEnabledRedirect(true), mOrig(NULL), mMessageLevel(level)
49  {
50  }
51  void setOriginal(std::streambuf* orig)
52  {
53  mOrig = orig;
54  }
55  virtual int_type overflow(int_type meta = traits_type::eof())
56  {
57  char single = traits_type::to_char_type(meta);
58  if (mOrig) // send to original stream as well
59  {
60  QMutexLocker sentry(&mOrigMutex);
61  mOrig->sputc(single);
62  }
63 
64  if (mEnabledRedirect)
65  {
66  if (single == '\n')
67  {
68  QMutexLocker sentry(&mMutex);
69  QString buffer = qstring_cast(mBuffer);
70  mBuffer.clear();
71  sentry.unlock();
72 
73  Message msg(buffer, mMessageLevel);
74 // msg.mChannel = qstring_cast(mMessageLevel);
75  msg.mChannel = "stdout";
76  reporter()->sendMessage(msg);
77  }
78  else
79  {
80  QMutexLocker sentry(&mMutex);
81  mBuffer += single;
82  }
83  }
84  return traits_type::not_eof(meta);
85  }
86  void setEnableRedirect(bool on)
87  {
88  mEnabledRedirect = on;
89  }
90 
91  //this is threadsafe fix...
92  void sendUnredirected(const QString& sequence)
93  {
94  QMutexLocker sentry(&mOrigMutex);
95  mOrig->sputn(sequence.toLatin1(), sequence.size());
96  }
97 
98 private:
99  bool mEnabledRedirect;
100  QString mBuffer;
101  std::streambuf* mOrig;
102  MESSAGE_LEVEL mMessageLevel;
103  QMutex mMutex;
104  QMutex mOrigMutex;
105 };
106 
107 
108 
109 SingleStreamerImpl::SingleStreamerImpl(std::ostream& str, MESSAGE_LEVEL level) :
110  mStream(str)
111 {
112  StreamBuf.reset(new MyStreamBuf(level));
113  OrigBuf = mStream.rdbuf(StreamBuf.get());
114  StreamBuf->setOriginal(OrigBuf);
115 }
116 
118 {
119  mStream.rdbuf(OrigBuf);
120 }
121 
122 void SingleStreamerImpl::sendUnredirected(const QString& sequence)
123 {
124  StreamBuf->sendUnredirected(sequence);
125 }
126 
127 
128 
129 
130 } //End namespace cx
QString qstring_cast(const T &val)
SingleStreamerImpl(std::ostream &str, MESSAGE_LEVEL level)
void sendUnredirected(const QString &sequence)
ReporterPtr reporter()
Definition: cxReporter.cpp:59
void setEnableRedirect(bool on)
void setOriginal(std::streambuf *orig)
void sendUnredirected(const QString &sequence)
MyStreamBuf(MESSAGE_LEVEL level)
virtual int_type overflow(int_type meta=traits_type::eof())
QString mChannel
Definition: cxLogMessage.h:95