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