-
Notifications
You must be signed in to change notification settings - Fork 1
/
MessageLogger.cpp
55 lines (42 loc) · 1.14 KB
/
MessageLogger.cpp
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
//-----------------------------------------------------------
// Copyright (C) 2021 Piotr (Peter) Beben <pdbcas2@gmail.com>
// See LICENSE included with this distribution.
#include "MessageLogger.h"
#include <QPlainTextEdit>
#include <QRecursiveMutex>
#include <QScrollBar>
MessageLogger::MessageLogger(QObject *parent) :
QObject(parent)
{
}
MessageLogger::~MessageLogger()
{
}
void MessageLogger::logMessage(const QString& text, int append) {
QMutexLocker locker(&m_recMutex);
switch (append) {
case 2:
emit logTextAppend(text);
++m_lastPos;
break;
case 1:
emit logTextInsert(text);
break;
case 0:
emit logTextReplace(text);
break;
}
}
void MessageLogger::logProgress(
const QString& msgPrefix, size_t i, size_t n, int infreq,
size_t& threshold, size_t& lastPos) {
float percent = (100.0f*i)/n;
if (percent >= threshold) {
QMutexLocker locker(&m_recMutex);
bool append = threshold > 0 ? (lastPos < m_lastPos) : true;
logMessage(msgPrefix + ": " + QString::number(int(percent)) + "%...",
append ? 2 : 0);
threshold += infreq;
lastPos = m_lastPos;
}
}