CustusX  18.04
An IGT application
cxLogFileWatcherThread.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 
12 #include "cxLogFileWatcherThread.h"
13 #include "cxLogFileWatcher.h"
14 #include "cxLogger.h"
15 #include <QtGlobal>
16 #include <iostream>
17 #include "boost/bind.hpp"
18 #include "boost/shared_ptr.hpp"
19 #include <QString>
20 #include <QMutex>
21 #include <QSound>
22 #include <QDir>
23 #include <QTextStream>
24 #include <QTimer>
25 #include "cxTypeConversions.h"
26 #include "cxDefinitionStrings.h"
27 #include "cxTime.h"
28 #include "cxMessageListener.h"
29 #include "cxLogFile.h"
30 
32 #include "cxTime.h"
33 
34 namespace cx
35 {
36 
38  LogThread(parent)
39 {
40  qRegisterMetaType<Message>("Message");
41 
42  connect(&mWatcher, &QFileSystemWatcher::directoryChanged, this, &LogFileWatcherThread::onDirectoryChanged);
43  connect(&mWatcher, &QFileSystemWatcher::fileChanged, this, &LogFileWatcherThread::onFileChanged);
44 }
45 
47 {
48 }
49 
50 void LogFileWatcherThread::onDirectoryChanged(const QString& path)
51 {
52  QDir info(mLogPath);
53  QStringList nameFilters;
54  nameFilters << "org.custusx.*";
55  QStringList current = info.entryList(nameFilters, QDir::Files);
56  current.removeAll("org.custusx.log.all.txt");
57  current.sort();
58 
59  if (current==mInitializedFiles)
60  return;
61 
62  if (!mWatcher.files().isEmpty())
63  mWatcher.removePaths(mWatcher.files());
64 
65  mInitializedFiles = current;
66  std::multimap<QDateTime, Message> messages;
67  for (int i=0; i<mInitializedFiles.size(); ++i)
68  {
69  QString filename = info.absoluteFilePath(mInitializedFiles[i]);
70  mWatcher.addPath(filename);
71  std::vector<Message> msg = this->readMessages(filename);
72  for (unsigned i=0; i<msg.size(); ++i)
73  messages.insert(std::make_pair(msg[i].mTimeStamp, msg[i]));
74  this->onFileChanged(filename);
75  }
76 
77  for (std::multimap<QDateTime, Message>::iterator i=messages.begin(); i!=messages.end(); ++i)
78  this->processMessage(i->second);
79 }
80 
81 void LogFileWatcherThread::onFileChanged(const QString& path)
82 {
83 // if (!mFiles.count(path))
84 // mFiles[path] = LogFile::fromFilename(path);
85 
86 // std::vector<Message> messages = mFiles[path].readMessages();
87  std::vector<Message> messages = this->readMessages(path);
88  for (unsigned i=0; i<messages.size(); ++i)
89  this->processMessage(messages[i]);
90 }
91 
92 std::vector<Message> LogFileWatcherThread::readMessages(const QString& path)
93 {
94  if (!mFiles.count(path))
95  mFiles[path] = LogFile::fromFilename(path);
96 
97  std::vector<Message> messages = mFiles[path].readMessages();
98  return messages;
99 }
100 
101 void LogFileWatcherThread::executeSetLoggingFolder(QString absoluteLoggingFolderPath)
102 {
103  if (mLogPath == absoluteLoggingFolderPath)
104  return;
105 
106  mLogPath = absoluteLoggingFolderPath;
107 
108  mInitializedFiles.clear();
109  mRepository->clearQueue();
110 
111  mWatcher.addPath(mLogPath);
112 
113  if (!mWatcher.directories().isEmpty())
114  mWatcher.removePaths(mWatcher.directories());
115  if (!mWatcher.files().isEmpty())
116  mWatcher.removePaths(mWatcher.files());
117  this->onDirectoryChanged(mLogPath);
118 }
119 
120 
121 } //End namespace cx
LogFileWatcherThread(QObject *parent=NULL)
MessageRepositoryPtr mRepository
Definition: cxLogThread.h:105
static LogFile fromFilename(QString filename)
Definition: cxLogFile.cpp:44
void processMessage(Message msg)
Namespace for all CustusX production code.