Fraxinus  17.12
An IGT application
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 #include <iostream>
37 
38 namespace cx
39 {
40 
41 
46 class MyStreamBuf: public std::basic_streambuf<char, std::char_traits<char> >
47 {
48 public:
49  MyStreamBuf(MESSAGE_LEVEL level) :
50  mEnabledRedirect(true), mOrig(NULL), mMessageLevel(level)
51  {
52  }
53  void setOriginal(std::streambuf* orig)
54  {
55  mOrig = orig;
56  }
57  virtual int_type overflow(int_type meta = traits_type::eof())
58  {
59  char single = traits_type::to_char_type(meta);
60  if (mOrig) // send to original stream as well
61  {
62  QMutexLocker sentry(&mOrigMutex);
63  mOrig->sputc(single);
64  }
65 
66  if (mEnabledRedirect)
67  {
68  if (single == '\n')
69  {
70  QMutexLocker sentry(&mMutex);
71  QString buffer = qstring_cast(mBuffer);
72  mBuffer.clear();
73  sentry.unlock();
74 
75  Message msg(buffer, mMessageLevel);
76 // msg.mChannel = qstring_cast(mMessageLevel);
77  msg.mChannel = "stdout";
78  if (isValidMessage(buffer))
79  reporter()->sendMessage(msg);
80  }
81  else
82  {
83  QMutexLocker sentry(&mMutex);
84  mBuffer += single;
85  }
86  }
87  return traits_type::not_eof(meta);
88  }
89  void setEnableRedirect(bool on)
90  {
91  mEnabledRedirect = on;
92  }
93 
94  //this is threadsafe fix...
95  void sendUnredirected(const QString& sequence)
96  {
97  QMutexLocker sentry(&mOrigMutex);
98  mOrig->sputn(sequence.toLatin1(), sequence.size());
99  }
100 
101  bool isValidMessage(QString message)
102  {
103  //Some tests fail when something is written in std::err, and VKT writes warnings here.
104  //Temporary fix of failing tests:
105  //Remove VTK Warnings about deprecated classes vtkVolumeTextureMapper3D and vtkOpenGLVolumeTextureMapper3D
106  //VTK writes several lines for each warning
107  if (message.contains("vtkVolumeTextureMapper3D") || message.contains("vtkOpenGLVolumeTextureMapper3D"))
108  {
109 // std::cout << "Found VTK deprecated message. Removing this for now." << std::endl;
110  return false;
111  }
112  else if (message.isEmpty())
113  return false;
114 
115  return true;
116  }
117 
118 private:
119  bool mEnabledRedirect;
120  QString mBuffer;
121  std::streambuf* mOrig;
122  MESSAGE_LEVEL mMessageLevel;
123  QMutex mMutex;
124  QMutex mOrigMutex;
125 };
126 
127 
128 
129 SingleStreamerImpl::SingleStreamerImpl(std::ostream& str, MESSAGE_LEVEL level) :
130  mStream(str)
131 {
132  StreamBuf.reset(new MyStreamBuf(level));
133  OrigBuf = mStream.rdbuf(StreamBuf.get());
134  StreamBuf->setOriginal(OrigBuf);
135 }
136 
138 {
139  mStream.rdbuf(OrigBuf);
140 }
141 
142 void SingleStreamerImpl::sendUnredirected(const QString& sequence)
143 {
144  StreamBuf->sendUnredirected(sequence);
145 }
146 
147 
148 
149 
150 } //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
bool isValidMessage(QString message)
Namespace for all CustusX production code.