File: TraceFile.cpp

package info (click to toggle)
wsjtx-improved 2.8.0%2B250314%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 81,496 kB
  • sloc: cpp: 104,468; f90: 48,130; python: 27,241; ansic: 13,367; fortran: 2,382; makefile: 197; sh: 133
file content (93 lines) | stat: -rwxr-xr-x 2,402 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "TraceFile.hpp"

#include <stdexcept>

#include <QDebug>
#include <QMutex>
#include <QMutexLocker>
#include <QString>
#include <QFile>
#include <QTextStream>

#include "pimpl_impl.hpp"

class TraceFile::impl
{
public:
  impl (QString const& trace_file_path);
  ~impl ();

  // no copying
  impl (impl const&) = delete;
  impl& operator = (impl const&) = delete;

private:
  // write Qt messages to the diagnostic log file
  static void message_handler (QtMsgType type, QMessageLogContext const& context, QString const& msg);

  QFile file_;
  QTextStream stream_;
  QTextStream * original_stream_;
  QtMessageHandler original_handler_;
  static QTextStream * current_stream_;
  static QMutex mutex_;
};

QTextStream * TraceFile::impl::current_stream_;
QMutex TraceFile::impl::mutex_;

// delegate to implementation class
TraceFile::TraceFile (QString const& trace_file_path)
  : m_ {trace_file_path}
{
}

TraceFile::~TraceFile ()
{
}


TraceFile::impl::impl (QString const& trace_file_path)
  : file_ {trace_file_path}
  , original_stream_ {current_stream_}
  , original_handler_ {nullptr}
{
  // if the log file is writeable; initialise diagnostic logging to it
  // for append and hook up the Qt global message handler
  if (file_.open (QFile::WriteOnly | QFile::Append | QFile::Text))
    {
      stream_.setDevice (&file_);
      current_stream_ = &stream_;
      original_handler_ = qInstallMessageHandler (message_handler);
    }
}

TraceFile::impl::~impl ()
{
  // unhook our message handler before the stream and file are destroyed
  if (original_handler_)
    {
      qInstallMessageHandler (original_handler_);
    }
  current_stream_ = original_stream_; // revert to prior stream
}

// write Qt messages to the diagnostic log file
void TraceFile::impl::message_handler (QtMsgType type, QMessageLogContext const& context, QString const& msg)
{
  Q_ASSERT_X (current_stream_, "TraceFile:message_handler", "no stream to write to");
  {
    QMutexLocker lock {&mutex_}; // thread safety - serialize writes to the trace file
    *current_stream_ << qFormatLogMessage (type, context, msg) <<
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
                 endl;
#else
                 Qt::endl;
#endif
  }

  if (QtFatalMsg == type)
    {
      throw std::runtime_error {"Fatal Qt Error"};
    }
}