CustusX  18.04
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 #include "cxReporter.h"
14 
15 #include <iostream>
16 
17 namespace cx
18 {
19 
20 
25 class MyStreamBuf: public std::basic_streambuf<char, std::char_traits<char> >
26 {
27 public:
28  MyStreamBuf(MESSAGE_LEVEL level) :
29  mEnabledRedirect(true), mOrig(NULL), mMessageLevel(level)
30  {
31  }
32  void setOriginal(std::streambuf* orig)
33  {
34  mOrig = orig;
35  }
36  virtual int_type overflow(int_type meta = traits_type::eof())
37  {
38  char single = traits_type::to_char_type(meta);
39  if (mOrig) // send to original stream as well
40  {
41  QMutexLocker sentry(&mOrigMutex);
42  mOrig->sputc(single);
43  }
44 
45  if (mEnabledRedirect)
46  {
47  if (single == '\n')
48  {
49  QMutexLocker sentry(&mMutex);
50  QString buffer = qstring_cast(mBuffer);
51  mBuffer.clear();
52  sentry.unlock();
53 
54  Message msg(buffer, mMessageLevel);
55 // msg.mChannel = qstring_cast(mMessageLevel);
56  msg.mChannel = "stdout";
57  reporter()->sendMessage(msg);
58  }
59  else
60  {
61  QMutexLocker sentry(&mMutex);
62  mBuffer += single;
63  }
64  }
65  return traits_type::not_eof(meta);
66  }
67  void setEnableRedirect(bool on)
68  {
69  mEnabledRedirect = on;
70  }
71 
72  //this is threadsafe fix...
73  void sendUnredirected(const QString& sequence)
74  {
75  QMutexLocker sentry(&mOrigMutex);
76  mOrig->sputn(sequence.toLatin1(), sequence.size());
77  }
78 
79 private:
80  bool mEnabledRedirect;
81  QString mBuffer;
82  std::streambuf* mOrig;
83  MESSAGE_LEVEL mMessageLevel;
84  QMutex mMutex;
85  QMutex mOrigMutex;
86 };
87 
88 
89 
90 SingleStreamerImpl::SingleStreamerImpl(std::ostream& str, MESSAGE_LEVEL level) :
91  mStream(str)
92 {
93  StreamBuf.reset(new MyStreamBuf(level));
94  OrigBuf = mStream.rdbuf(StreamBuf.get());
95  StreamBuf->setOriginal(OrigBuf);
96 }
97 
99 {
100  mStream.rdbuf(OrigBuf);
101 }
102 
103 void SingleStreamerImpl::sendUnredirected(const QString& sequence)
104 {
105  StreamBuf->sendUnredirected(sequence);
106 }
107 
108 
109 
110 
111 } //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:38
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:74
Namespace for all CustusX production code.