Fraxinus  17.12-rc4
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxLogFileWatcherThread.h"
34 #include "cxLogFileWatcher.h"
35 #include "cxLogger.h"
36 #include <QtGlobal>
37 #include <iostream>
38 #include "boost/bind.hpp"
39 #include "boost/shared_ptr.hpp"
40 #include <QString>
41 #include <QMutex>
42 #include <QSound>
43 #include <QDir>
44 #include <QTextStream>
45 #include <QTimer>
46 #include "cxTypeConversions.h"
47 #include "cxDefinitionStrings.h"
48 #include "cxTime.h"
49 #include "cxMessageListener.h"
50 #include "cxLogFile.h"
51 
53 #include "cxTime.h"
54 
55 namespace cx
56 {
57 
59  LogThread(parent)
60 {
61  qRegisterMetaType<Message>("Message");
62 
63  connect(&mWatcher, &QFileSystemWatcher::directoryChanged, this, &LogFileWatcherThread::onDirectoryChanged);
64  connect(&mWatcher, &QFileSystemWatcher::fileChanged, this, &LogFileWatcherThread::onFileChanged);
65 }
66 
68 {
69 }
70 
71 void LogFileWatcherThread::onDirectoryChanged(const QString& path)
72 {
73  QDir info(mLogPath);
74  QStringList nameFilters;
75  nameFilters << "org.custusx.*";
76  QStringList current = info.entryList(nameFilters, QDir::Files);
77  current.removeAll("org.custusx.log.all.txt");
78  current.sort();
79 
80  if (current==mInitializedFiles)
81  return;
82 
83  if (!mWatcher.files().isEmpty())
84  mWatcher.removePaths(mWatcher.files());
85 
86  mInitializedFiles = current;
87  std::multimap<QDateTime, Message> messages;
88  for (int i=0; i<mInitializedFiles.size(); ++i)
89  {
90  QString filename = info.absoluteFilePath(mInitializedFiles[i]);
91  mWatcher.addPath(filename);
92  std::vector<Message> msg = this->readMessages(filename);
93  for (unsigned i=0; i<msg.size(); ++i)
94  messages.insert(std::make_pair(msg[i].mTimeStamp, msg[i]));
95  this->onFileChanged(filename);
96  }
97 
98  for (std::multimap<QDateTime, Message>::iterator i=messages.begin(); i!=messages.end(); ++i)
99  this->processMessage(i->second);
100 }
101 
102 void LogFileWatcherThread::onFileChanged(const QString& path)
103 {
104 // if (!mFiles.count(path))
105 // mFiles[path] = LogFile::fromFilename(path);
106 
107 // std::vector<Message> messages = mFiles[path].readMessages();
108  std::vector<Message> messages = this->readMessages(path);
109  for (unsigned i=0; i<messages.size(); ++i)
110  this->processMessage(messages[i]);
111 }
112 
113 std::vector<Message> LogFileWatcherThread::readMessages(const QString& path)
114 {
115  if (!mFiles.count(path))
116  mFiles[path] = LogFile::fromFilename(path);
117 
118  std::vector<Message> messages = mFiles[path].readMessages();
119  return messages;
120 }
121 
122 void LogFileWatcherThread::executeSetLoggingFolder(QString absoluteLoggingFolderPath)
123 {
124  if (mLogPath == absoluteLoggingFolderPath)
125  return;
126 
127  mLogPath = absoluteLoggingFolderPath;
128 
129  mInitializedFiles.clear();
130  mRepository->clearQueue();
131 
132  mWatcher.addPath(mLogPath);
133 
134  if (!mWatcher.directories().isEmpty())
135  mWatcher.removePaths(mWatcher.directories());
136  if (!mWatcher.files().isEmpty())
137  mWatcher.removePaths(mWatcher.files());
138  this->onDirectoryChanged(mLogPath);
139 }
140 
141 
142 } //End namespace cx
LogFileWatcherThread(QObject *parent=NULL)
MessageRepositoryPtr mRepository
Definition: cxLogThread.h:126
static LogFile fromFilename(QString filename)
Definition: cxLogFile.cpp:65
void processMessage(Message msg)
Namespace for all CustusX production code.