File: CamiTKLogger.cpp

package info (click to toggle)
camitk 6.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 389,496 kB
  • sloc: cpp: 103,476; sh: 2,448; python: 1,618; xml: 984; makefile: 128; perl: 84; sed: 20
file content (375 lines) | stat: -rw-r--r-- 14,056 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2025 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/

// -- Core stuff
#include "CamiTKLogger.h"
#include "Action.h"
#include "Component.h"
#include "Viewer.h"
#include "Application.h"
#include "MainWindow.h"
#include "ActionExtension.h"
#include "ComponentExtension.h"
#include "ViewerExtension.h"
#include "Log.h"

#include <iostream>

#include <QDateTime>
#include <QMessageBox>
#include <QTextStream>
#include <QFileInfo>
#include <QMutex>

namespace camitk {

// --------------------- Constructor ---------------------
CamiTKLogger::CamiTKLogger() :
    logFileDirectory{QDir(QDir::tempPath() + "/CamiTK")},
    logStartTime{QDateTime::currentDateTime()} {
    level = InterfaceLogger::WARNING;
    messageBoxLevel = InterfaceLogger::NONE;
    logToStdOut = true;
    displayDebugInformation = false;
    displayTimeStampInformation = true;
    logToFile = false;
    logFile = nullptr;
    logStream = nullptr;
}

// --------------------- Destructor ---------------------
CamiTKLogger::~CamiTKLogger() {
    closeLogFile();
    log("Log finished", TRACE, __FILE__, Q_FUNC_INFO, __LINE__);
}

// --------------------- getLogLevel ---------------------
CamiTKLogger::LogLevel CamiTKLogger::getLogLevel() {
    return level;
}

// --------------------- setLogLevel ---------------------
void CamiTKLogger::setLogLevel(camitk::InterfaceLogger::LogLevel level) {
    if (level != this->level) {
        this->level = level;
        log("Log level changed to: " + Log::getLevelAsString(level), TRACE, __FILE__, Q_FUNC_INFO, __LINE__);
    }
}

// --------------------- setLogToStandardOutput ---------------------
void CamiTKLogger::setLogToStandardOutput(bool logToStdOut) {
    if (logToStdOut != this->logToStdOut) {
        this->logToStdOut = logToStdOut;
        log("Logging to standard output: " + QString((logToStdOut ? "true" : "false")), TRACE, __FILE__, Q_FUNC_INFO, __LINE__);
    }
}

// --------------------- getLogToStandardOutput ---------------------
bool CamiTKLogger::getLogToStandardOutput() {
    return logToStdOut;
}

// --------------------- setLogToFile ---------------------
bool CamiTKLogger::setLogToFile(bool logToFile) {
    if (logToFile != this->logToFile) {
        log("Logging to file: " + QString((logToFile ? "true" : "false")), TRACE, __FILE__, Q_FUNC_INFO, __LINE__);

        //-- if logger was already writing into a log file, close it properly before opening a new one...
        if (logToFile) {
            openLogFile();
        }
        else {
            closeLogFile();
        }
    }
    return this->logToFile;
}

// --------------------- setLogFileDirectory ---------------------
bool CamiTKLogger::setLogFileDirectory(QDir directoryName, bool moveExistingLogFile) {
    if (logToFile) {
        // if the directory is the same as the given one, nothing to do
        if (directoryName != logFileDirectory.path()) {
            QFileInfo oldLogFile(logFile->fileName());
            closeLogFile();
            logFileDirectory = directoryName;
            if (!moveExistingLogFile) {
                // a new file is going to be created with a new name
                logStartTime = QDateTime::currentDateTime();
            }
            openLogFile(moveExistingLogFile, oldLogFile);
        }
    }
    else {
        logFileDirectory = directoryName;
    }
    return logToFile;
}

// --------------------- getLogToFile ---------------------
bool CamiTKLogger::getLogToFile() {
    return logToFile;
}

// --------------------- openLogFile ---------------------
bool CamiTKLogger::openLogFile(bool moveFile, QFileInfo fileToMove) {
    if (logToFile) {
        return logToFile;
    }

    //-- Check the write status of logFileDirectory
    if (!logFileDirectory.exists()) {
        // check if it can be created
        if (!logFileDirectory.mkpath(logFileDirectory.path())) {
            log("Cannot create directory \"" + logFileDirectory.path() + "\"", ERROR, __FILE__, Q_FUNC_INFO, __LINE__);
            return logToFile;
        }
    }

    //-- Move or create the name of the log file with time stamp
    // Replace ":" by "-" as windows has sometimes difficilties with ":" in filename
    QString logFileName = "log-" + logStartTime.toString(Qt::ISODate).replace(":", "-") + ".camitklog";

    //-- move file if required
    if (moveFile) {
        QFile::rename(fileToMove.absoluteFilePath(), logFileDirectory.path() + "/" + logFileName);
    }

    //-- open the file
    logFile = new QFile(logFileDirectory.path() + "/" + logFileName);

    //-- check that we can create a file in logFileDirectory
    if (!logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
        log("Cannot open \"" + logFile->fileName() + "\" for logging", ERROR, __FILE__, Q_FUNC_INFO, __LINE__);
        logToFile = false;
        delete logFile;
        logFile = nullptr;
    }
    else {
        logToFile = true;
        logStream = new QTextStream(logFile);
        log("Log in file \"" + logFile->fileName() + "\" started", TRACE, __FILE__, Q_FUNC_INFO, __LINE__);
    }
    return logToFile;

}

// --------------------- closeLogFile ---------------------
void CamiTKLogger::closeLogFile() {
    if (logFile != nullptr) {
        log("Log in file \"" + logFile->fileName() + "\" finished", TRACE, __FILE__, Q_FUNC_INFO, __LINE__);
        logStream->flush();
        if (logStream->status() != QTextStream::Ok) {
            log("Error closing log output stream. Log output stream status is: " + QString::number(logStream->status()), ((logStream->status() == QTextStream::WriteFailed) ? ERROR : WARNING), __FILE__, Q_FUNC_INFO, __LINE__);
        }
        delete logStream;
        logStream = nullptr;
        logFile->close();
        delete logFile;
        logFile = nullptr;
    }
    logToFile = false;
}

// --------------------- getLogFileInfo ---------------------
QFileInfo CamiTKLogger::getLogFileInfo() {
    if (logToFile) {
        return QFileInfo(logFile->fileName());
    }
    else {
        return QFileInfo();
    }
}

// --------------------- setMessageBoxLevel ---------------------
void CamiTKLogger::setMessageBoxLevel(InterfaceLogger::LogLevel level) {
    if (messageBoxLevel != level) {
        messageBoxLevel = level;
        log("Message box level changed to: " + Log::getLevelAsString(level), TRACE, __FILE__, Q_FUNC_INFO, __LINE__);
    }
}

// --------------------- getMessageBoxLevel ---------------------
InterfaceLogger::LogLevel CamiTKLogger::getMessageBoxLevel() {
    return messageBoxLevel;
}

// --------------------- setDebugInformation ---------------------
void CamiTKLogger::setDebugInformation(bool showDebugInformation) {
    if (showDebugInformation != displayDebugInformation) {
        displayDebugInformation = showDebugInformation;
        log("Logging debug information: " + QString((displayDebugInformation ? "true" : "false")), TRACE, __FILE__, Q_FUNC_INFO, __LINE__);
    }
}

// --------------------- getDebugInformation ---------------------
bool CamiTKLogger::getDebugInformation() {
    return displayDebugInformation;
}

// --------------------- setTimeStampInformation ---------------------
void CamiTKLogger::setTimeStampInformation(bool showTimeStamp) {
    if (showTimeStamp != displayTimeStampInformation) {
        displayTimeStampInformation = showTimeStamp;
        log("Logging time stamp: " + QString((displayTimeStampInformation ? "true" : "false")), TRACE, __FILE__, Q_FUNC_INFO, __LINE__);
    }
}

// --------------------- getTimeStampInformation ---------------------
bool CamiTKLogger::getTimeStampInformation() {
    return displayTimeStampInformation;
}

// --------------------- log ---------------------
QString CamiTKLogger::log(const QString msg, const LogLevel level, char const* fileName, char const* methodName, int lineNumber, const QObject* sender) {
    // To ensure thread safety of the logger
    // see http://www.kdab.com/wp-content/uploads/stories/slides/Day2/KaiKoehne_Qt%20Logging%20Framework%2016_9_0.pdf page 22
    static QMutex mutex;

    QString message;
    if (level <= this->level && mutex.tryLock(500)) {
        QString userMsg = msg;
        message = buildLogMessage(userMsg, level, fileName, methodName, lineNumber, sender);
        if (logToStdOut) {
            std::cout << message.toStdString() << std::endl;
            std::cout.flush();
        }
        if (logToFile) {
            (*logStream) << message << Qt::endl;
            logStream->flush();
            if (logStream->status() != QTextStream::Ok) {
                std::cerr << buildLogMessage("Error closing log output stream. Log output stream status is: " + QString::number(logStream->status()), ((logStream->status() == QTextStream::WriteFailed) ? ERROR : WARNING), fileName, methodName, lineNumber, sender).toStdString() << std::endl;
                std::cerr.flush();
            }
        }
        if (level <= messageBoxLevel) {
            QMessageBox::Icon msgBoxIcon;
            switch (level) {
                case WARNING:
                    msgBoxIcon = QMessageBox::Warning;
                    break;
                case ERROR:
                    msgBoxIcon = QMessageBox::Critical;
                    break;
                case TRACE:
                case INFO:
                default:
                    msgBoxIcon = QMessageBox::Information;
                    break;
            }

            // if log was sent in the middle of an operation, restore cursor before displaying the dialog
            QApplication::restoreOverrideCursor();

            QMessageBox msgBox(msgBoxIcon, Log::getLevelAsString(level), getCamiTKAPIInformation(sender) + msg, QMessageBox::Close);
            msgBox.setEscapeButton(QMessageBox::Close);
            msgBox.setDetailedText(message);
            msgBox.exec();
        }
        mutex.unlock();
    }
    // ⚠ WARNING : if the mutex is not unlocked after 500ms, the log message sent by another thread will be lost...

    return message;
}


// --------------------- buildLogMessage ---------------------
QString CamiTKLogger::buildLogMessage(QString message, LogLevel level, char const* fileName, char const* methodName, int lineNumber, const QObject* sender) {
    QString logMessage;

    if (displayTimeStampInformation) {
        logMessage = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz") + " ";
    }

    logMessage += "[" + Log::getLevelAsString(level).leftJustified(7, ' ') + "] ";

    if (displayDebugInformation) {
        logMessage += "[" + QString(methodName) + "@" + QFileInfo(QString(fileName)).baseName() + "." + QFileInfo(QString(fileName)).completeSuffix() + ":" + QString::number(lineNumber) + "] ";
    }

    // specific CamiTK API processing
    logMessage += getCamiTKAPIInformation(sender);

    logMessage += message;

    return logMessage;
}

// --------------------- getCamiTKAPIInformation ---------------------
QString CamiTKLogger::getCamiTKAPIInformation(const QObject* sender) {
    // sender is an action
    const auto* camitkAction = dynamic_cast<const camitk::Action*>(sender);
    if (camitkAction != nullptr) {
        return "Action \"" + camitkAction->getName() + "\" - ";
    }

    // sender is a component
    const auto* camitkComponent = dynamic_cast<const camitk::Component*>(sender);
    if (camitkComponent != nullptr) {
        return QString(camitkComponent->metaObject()->className()) + " \"" + camitkComponent->getName() + "\" - ";
    }

    // sender is a ActionExtension
    auto* nonConstSender = const_cast<QObject*>(sender); // beware!
    auto* camitkActionExt = dynamic_cast<camitk::ActionExtension*>(nonConstSender);
    if (camitkActionExt != nullptr) {
        return QString("Action Extension \"") + camitkActionExt->getName() + "\" - ";
    }

    // sender is a ComponentExtension
    const auto* camitkComponentExt = dynamic_cast<const camitk::ComponentExtension*>(sender);
    if (camitkComponentExt != nullptr) {
        return QString("Component Extension \"") + camitkComponentExt->getName() + "\" - ";
    }

    // sender is a ComponentExtension
    auto* camitkViewerExt = dynamic_cast<camitk::ViewerExtension*>(nonConstSender);
    if (camitkViewerExt != nullptr) {
        return QString("Viewer Extension \"") + camitkViewerExt->getName() + "\" - ";
    }

    // sender is an application
    const auto* camitkApplication = dynamic_cast<const camitk::Application*>(sender);
    if (camitkApplication != nullptr) {
        return QString("Application \"") + camitkApplication->getName() + "\" - ";
    }

    // sender is a main window
    const auto* camitkMainWindow = dynamic_cast<const camitk::MainWindow*>(sender);
    if (camitkMainWindow != nullptr) {
        return QString("Main Window \"") + camitkMainWindow->getName() + "\" - ";
    }

    // sender is a viewer
    const auto* camitkViewer = dynamic_cast<const camitk::Viewer*>(sender);
    if (camitkViewer != nullptr) {
        return QString("Viewer \"") + camitkViewer->objectName() + "\" - ";
    }

    return "";
}

} // end namespace