summaryrefslogtreecommitdiff
path: root/TraceFile.cpp
diff options
context:
space:
mode:
authorIain R. Learmonth <irl@debian.org>2015-10-18 18:51:00 +0100
committerIain R. Learmonth <irl@debian.org>2015-10-18 18:51:00 +0100
commitd63dd8d081a6fcd0715cd13f26df59fb831769d2 (patch)
treeb9c1f6245147a310aac3762630bad389bbb57335 /TraceFile.cpp
parentc720a3d55c5ec832c29cb7ce50b6fb4b70c55b11 (diff)
Imported Upstream version 1.5.0upstream/1.5.0
Diffstat (limited to 'TraceFile.cpp')
-rw-r--r--TraceFile.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/TraceFile.cpp b/TraceFile.cpp
new file mode 100644
index 00000000..022a020d
--- /dev/null
+++ b/TraceFile.cpp
@@ -0,0 +1,118 @@
+#include "TraceFile.hpp"
+
+#include <stdexcept>
+
+#include <QDebug>
+#include <QString>
+#include <QFile>
+#include <QTextStream>
+#include <QMessageLogContext>
+#include <QDateTime>
+#include <QMutex>
+#include <QMutexLocker>
+
+#include "pimpl_impl.hpp"
+
+namespace
+{
+ QMutex lock;
+}
+
+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_;
+};
+
+QTextStream * TraceFile::impl::current_stream_;
+
+
+// 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)
+{
+ char const * severity;
+ switch (type)
+ {
+ case QtDebugMsg:
+ severity = "Debug";
+ break;
+
+ case QtWarningMsg:
+ severity = "Warning";
+ break;
+
+ case QtFatalMsg:
+ severity = "Fatal";
+ break;
+
+ default:
+ severity = "Critical";
+ break;
+ }
+
+ {
+ // guard against multiple threads with overlapping messages
+ QMutexLocker guard (&lock);
+ Q_ASSERT_X (current_stream_, "TraceFile:message_handler", "no stream to write to");
+ *current_stream_
+ << QDateTime::currentDateTimeUtc ().toString ()
+ << '(' << context.file << ':' << context.line /* << ", " << context.function */ << ')'
+ << severity << ": " << msg.trimmed () << endl;
+ }
+
+ if (QtFatalMsg == type)
+ {
+ throw std::runtime_error {"Fatal Qt Error"};
+ }
+}