-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
591 lines (534 loc) · 22.9 KB
/
Copy pathmainwindow.cpp
File metadata and controls
591 lines (534 loc) · 22.9 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// mainwindow.cpp
#include "mainwindow.h"
#include "processworker.h"
#include <QApplication>
#include <QIcon>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFormLayout>
#include <QGroupBox>
#include <QHeaderView>
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include <QDateTime>
#include <algorithm>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// --- Basic Window Setup ---
this->setWindowTitle("Memory Analyzer");
this->setMinimumSize(800, 600);
this->setWindowIcon(QIcon(":/cpu.svg"));
// --- Create Central Widget and Main Layout ---
QWidget* centralWidget = new QWidget(this);
QHBoxLayout* mainLayout = new QHBoxLayout(centralWidget);
this->setCentralWidget(centralWidget);
// --- Create and Configure Sidebar and StackedWidget ---
m_sidebar = new QListWidget();
m_mainStack = new QStackedWidget();
m_sidebar->setMaximumWidth(220);
m_sidebar->setIconSize(QSize(24, 24));
mainLayout->addWidget(m_sidebar);
mainLayout->addWidget(m_mainStack);
// --- Populate Sidebar Menu with Icons ---
m_sidebar->addItem(new QListWidgetItem(QIcon(":/cpu.svg"), "System Overview"));
m_sidebar->addItem(new QListWidgetItem(QIcon(":/monitor.svg"), "Real-time Process Monitor"));
m_sidebar->addItem(new QListWidgetItem(QIcon(":/search.svg"), "Process Inspector"));
m_sidebar->addItem(new QListWidgetItem(QIcon(":/alert-triangle.svg"), "Threshold Alert"));
m_sidebar->addItem(new QListWidgetItem(QIcon(":/save.svg"), "Save Report"));
m_sidebar->addItem(new QListWidgetItem(QIcon(":/search.svg"), "Top N Processes"));
m_sidebar->addItem(new QListWidgetItem(QIcon(":/save.svg"), "Track Memory Usage"));
m_sidebar->setCurrentRow(0);
// --- Create and add ALL feature pages to the StackedWidget ---
m_mainStack->addWidget(createSystemOverviewPage());
m_mainStack->addWidget(createRealTimeMonitorPage());
m_mainStack->addWidget(createProcessInspectorPage());
m_mainStack->addWidget(createThresholdAlertPage());
m_mainStack->addWidget(createSaveReportPage());
m_mainStack->addWidget(createTopNPage());
m_mainStack->addWidget(createTrackMemoryPage());
// --- Connect Signals and Slots ---
connect(m_sidebar, &QListWidget::currentRowChanged, m_mainStack, &QStackedWidget::setCurrentIndex);
connect(m_pidGetInfoButton, &QPushButton::clicked, this, &MainWindow::onGetInfoButtonClicked);
connect(m_pidCompareButton, &QPushButton::clicked, this, &MainWindow::onCompareButtonClicked);
connect(m_setAlertButton, &QPushButton::clicked, this, &MainWindow::onSetAlertButtonClicked);
connect(m_saveReportButton, &QPushButton::clicked, this, &MainWindow::onSaveReportButtonClicked);
connect(m_searchLineEdit, &QLineEdit::textChanged, this, &MainWindow::onSearchTextChanged);
connect(m_topNButton, &QPushButton::clicked, this, &MainWindow::onGetTopNClicked);
connect(m_startLoggingButton, &QPushButton::clicked, this, &MainWindow::onStartLoggingClicked);
// --- Register Custom Type and Start Worker Thread ---
qRegisterMetaType<AppData>("AppData");
workerThread = new QThread();
worker = new ProcessWorker();
worker->moveToThread(workerThread);
connect(workerThread, &QThread::started, worker, &ProcessWorker::startWork);
connect(worker, &ProcessWorker::resultReady, this, &MainWindow::handleResults);
connect(worker, &ProcessWorker::thresholdExceeded, this, &MainWindow::handleThresholdAlert);
workerThread->start();
m_loggingTimer = new QTimer(this);
connect(m_loggingTimer, &QTimer::timeout, this, &MainWindow::performLog);
m_logContent = "";
}
MainWindow::~MainWindow()
{
workerThread->quit();
workerThread->wait();
m_loggingTimer->stop();
}
QWidget* MainWindow::createSystemOverviewPage()
{
QWidget* page = new QWidget();
QVBoxLayout* pageLayout = new QVBoxLayout(page);
pageLayout->setContentsMargins(10, 10, 10, 10);
pageLayout->setSpacing(15);
QGroupBox* cpuGroup = new QGroupBox("CPU Information");
QFormLayout* cpuLayout = new QFormLayout(cpuGroup);
m_cpuModelLabel = new QLabel("retrieving...");
m_cpuCoresThreadsLabel = new QLabel("retrieving...");
m_cpuL1CacheLabel = new QLabel("retrieving...");
m_cpuL2CacheLabel = new QLabel("retrieving...");
m_cpuL3CacheLabel = new QLabel("retrieving...");
cpuLayout->addRow("Model:", m_cpuModelLabel);
cpuLayout->addRow("Cores / Threads:", m_cpuCoresThreadsLabel);
cpuLayout->addRow("L1 Cache:", m_cpuL1CacheLabel);
cpuLayout->addRow("L2 Cache:", m_cpuL2CacheLabel);
cpuLayout->addRow("L3 Cache:", m_cpuL3CacheLabel);
QGroupBox* memGroup = new QGroupBox("Memory (RAM) Information");
QFormLayout* memLayout = new QFormLayout(memGroup);
m_totalMemoryLabel = new QLabel("retrieving...");
m_availableMemoryLabel = new QLabel("retrieving...");
m_memoryTypeLabel = new QLabel("retrieving...");
m_memorySpeedLabel = new QLabel("retrieving...");
m_memorySlotsLabel = new QLabel("retrieving...");
memLayout->addRow("Total Installed:", m_totalMemoryLabel);
memLayout->addRow("Currently Available:", m_availableMemoryLabel);
memLayout->addRow("Type:", m_memoryTypeLabel);
memLayout->addRow("Speed:", m_memorySpeedLabel);
memLayout->addRow("Slots:", m_memorySlotsLabel);
QGroupBox* graphicsGroup = new QGroupBox("Graphics Controllers");
QVBoxLayout* graphicsLayout = new QVBoxLayout(graphicsGroup);
m_gpuListLabel = new QLabel("retrieving...");
m_gpuListLabel->setWordWrap(true);
graphicsLayout->addWidget(m_gpuListLabel);
pageLayout->addWidget(cpuGroup);
pageLayout->addWidget(memGroup);
pageLayout->addWidget(graphicsGroup);
pageLayout->addStretch();
return page;
}
QWidget* MainWindow::createRealTimeMonitorPage()
{
QWidget* page = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(page);
m_searchLineEdit = new QLineEdit();
m_searchLineEdit->setPlaceholderText("Filter by process name...");
m_searchLineEdit->setClearButtonEnabled(true);
layout->addWidget(m_searchLineEdit);
m_processTableWidget = new QTableWidget();
layout->addWidget(m_processTableWidget);
m_processTableWidget->setColumnCount(3);
m_processTableWidget->setHorizontalHeaderLabels({"Process Name", "PID", "Memory Usage"});
m_processTableWidget->horizontalHeader()->setStretchLastSection(true);
m_processTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_processTableWidget->setAlternatingRowColors(true);
m_processTableWidget->setSortingEnabled(true);
return page;
}
QWidget* MainWindow::createProcessInspectorPage()
{
QWidget* page = new QWidget();
QVBoxLayout* mainVLayout = new QVBoxLayout(page);
QGroupBox* infoGroup = new QGroupBox("Get Info for a Single Process");
QFormLayout* infoLayout = new QFormLayout(infoGroup);
m_pidLineEdit = new QLineEdit();
m_pidLineEdit->setPlaceholderText("Enter PID");
m_pidGetInfoButton = new QPushButton("Get Info");
m_pidResultLabel = new QLabel("Result will be shown here.");
infoLayout->addRow(m_pidLineEdit, m_pidGetInfoButton);
infoLayout->addRow(m_pidResultLabel);
QGroupBox* compareGroup = new QGroupBox("Compare Two Processes");
QFormLayout* compareLayout = new QFormLayout(compareGroup);
m_pid1LineEdit = new QLineEdit();
m_pid1LineEdit->setPlaceholderText("Enter first PID");
m_pid2LineEdit = new QLineEdit();
m_pid2LineEdit->setPlaceholderText("Enter second PID");
m_pidCompareButton = new QPushButton("Compare");
m_compareResultLabel = new QLabel("Comparison result will be shown here.");
compareLayout->addRow("Process 1 PID:", m_pid1LineEdit);
compareLayout->addRow("Process 2 PID:", m_pid2LineEdit);
compareLayout->addWidget(m_pidCompareButton);
compareLayout->addRow(m_compareResultLabel);
mainVLayout->addWidget(infoGroup);
mainVLayout->addWidget(compareGroup);
mainVLayout->addStretch();
return page;
}
QWidget* MainWindow::createThresholdAlertPage()
{
QWidget* page = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(page);
QFormLayout* form = new QFormLayout();
m_thresholdSpinBox = new QSpinBox();
m_thresholdSpinBox->setRange(0, 100);
m_thresholdSpinBox->setValue(80);
m_thresholdSpinBox->setSuffix("%");
form->addRow("Memory Usage Threshold:", m_thresholdSpinBox);
layout->addLayout(form);
m_setAlertButton = new QPushButton("Set Alert");
layout->addWidget(m_setAlertButton);
m_alertStatusLabel = new QLabel("No threshold set.");
layout->addWidget(m_alertStatusLabel);
layout->addStretch();
return page;
}
QWidget* MainWindow::createSaveReportPage()
{
QWidget* page = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(page);
m_saveReportButton = new QPushButton("Save Current Report");
layout->addWidget(m_saveReportButton);
m_reportStatusLabel = new QLabel("Click to save a report.");
layout->addWidget(m_reportStatusLabel);
layout->addStretch();
return page;
}
QWidget* MainWindow::createTopNPage()
{
QWidget* page = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(page);
QHBoxLayout* controlsLayout = new QHBoxLayout();
m_topNSpinBox = new QSpinBox();
m_topNSpinBox->setRange(1, 200);
m_topNSpinBox->setValue(10);
m_topNSpinBox->setPrefix("Show Top ");
m_topNButton = new QPushButton("Get Processes");
controlsLayout->addWidget(new QLabel("Show Top N Processes:"));
controlsLayout->addWidget(m_topNSpinBox);
controlsLayout->addWidget(m_topNButton);
controlsLayout->addStretch();
layout->addLayout(controlsLayout);
m_topNTableWidget = new QTableWidget();
layout->addWidget(m_topNTableWidget);
m_topNTableWidget->setColumnCount(3);
m_topNTableWidget->setHorizontalHeaderLabels({"Process Name", "PID", "Memory Usage"});
m_topNTableWidget->horizontalHeader()->setStretchLastSection(true);
m_topNTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
return page;
}
QWidget* MainWindow::createTrackMemoryPage()
{
QWidget* page = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(page);
QFormLayout* form = new QFormLayout();
QHBoxLayout* intervalLayout = new QHBoxLayout();
m_intervalValueSpinBox = new QSpinBox();
m_intervalValueSpinBox->setRange(1, 9999);
m_intervalValueSpinBox->setValue(10);
m_intervalUnitComboBox = new QComboBox();
m_intervalUnitComboBox->addItems({"seconds", "minutes", "hours"});
intervalLayout->addWidget(m_intervalValueSpinBox);
intervalLayout->addWidget(m_intervalUnitComboBox);
form->addRow("Logging Interval:", intervalLayout);
QHBoxLayout* durationLayout = new QHBoxLayout();
m_durationValueSpinBox = new QSpinBox();
m_durationValueSpinBox->setRange(1, 9999);
m_durationValueSpinBox->setValue(5);
m_durationUnitComboBox = new QComboBox();
m_durationUnitComboBox->addItems({"seconds", "minutes", "hours"});
durationLayout->addWidget(m_durationValueSpinBox);
durationLayout->addWidget(m_durationUnitComboBox);
form->addRow("Total Duration:", durationLayout);
QGroupBox* group = new QGroupBox("Processes to Log");
QVBoxLayout* groupLayout = new QVBoxLayout(group);
m_allRadio = new QRadioButton("All Processes");
m_specificRadio = new QRadioButton("Specific Processes");
groupLayout->addWidget(m_allRadio);
groupLayout->addWidget(m_specificRadio);
m_allRadio->setChecked(true);
m_pidsLineEdit = new QLineEdit();
m_pidsLineEdit->setPlaceholderText("Enter PIDs separated by commas");
m_pidsLineEdit->setEnabled(false);
connect(m_specificRadio, &QRadioButton::toggled, m_pidsLineEdit, &QLineEdit::setEnabled);
groupLayout->addWidget(m_pidsLineEdit);
form->addRow(group);
layout->addLayout(form);
m_startLoggingButton = new QPushButton("Start Logging");
layout->addWidget(m_startLoggingButton);
m_loggingStatusLabel = new QLabel("Status: Idle");
layout->addWidget(m_loggingStatusLabel);
layout->addStretch();
return page;
}
void MainWindow::onSearchTextChanged(const QString &text)
{
m_currentFilter = text;
handleResults(lastData);
}
void MainWindow::handleResults(const AppData &data)
{
lastData = data;
QString memStr;
m_cpuModelLabel->setText(data.cpuModel);
m_cpuCoresThreadsLabel->setText(QString("%1 Cores / %2 Threads").arg(data.cpuCores, data.cpuThreads));
m_cpuL1CacheLabel->setText(data.cpuL1Cache);
m_cpuL2CacheLabel->setText(data.cpuL2Cache);
m_cpuL3CacheLabel->setText(data.cpuL3Cache);
formatMemory(memStr, data.memTotal);
m_totalMemoryLabel->setText(memStr);
formatMemory(memStr, data.memAvailable);
m_availableMemoryLabel->setText(memStr);
m_memoryTypeLabel->setText(data.memoryType);
m_memorySpeedLabel->setText(data.memorySpeed);
m_memorySlotsLabel->setText(data.memorySlots);
m_gpuListLabel->setText(data.gpuModels.join("\n"));
QList<ProcessInfo> filteredList;
if (m_currentFilter.isEmpty()) {
filteredList = data.processes;
} else {
for (const auto& process : data.processes) {
if (process.name.contains(m_currentFilter, Qt::CaseInsensitive)) {
filteredList.append(process);
}
}
}
m_processTableWidget->setSortingEnabled(false);
m_processTableWidget->setRowCount(filteredList.count());
for(int i = 0; i < filteredList.count(); ++i) {
const auto& process = filteredList.at(i);
formatMemory(memStr, process.memory);
QTableWidgetItem *nameItem = new QTableWidgetItem(process.name);
QTableWidgetItem *pidItem = new QTableWidgetItem(QString::number(process.pid));
QTableWidgetItem *memItem = new QTableWidgetItem(memStr);
m_processTableWidget->setItem(i, 0, nameItem);
m_processTableWidget->setItem(i, 1, pidItem);
m_processTableWidget->setItem(i, 2, memItem);
}
m_processTableWidget->setSortingEnabled(true);
}
void MainWindow::handleThresholdAlert(const QString& message)
{
if (!alertActive && currentThreshold != -1) {
QMessageBox msgBox;
msgBox.setWindowTitle("Memory Alert");
msgBox.setText(message);
m_ignoreButton = msgBox.addButton("OK", QMessageBox::ActionRole);
msgBox.exec();
if (msgBox.clickedButton() == m_ignoreButton) {
alertActive = true;
}
}
}
void MainWindow::onIgnoreAlert()
{
alertActive = true;
}
void MainWindow::onGetInfoButtonClicked()
{
pid_t pid = m_pidLineEdit->text().toInt();
ProcessWorker tempWorker;
long mem_kb = tempWorker.getVmRssFromPid(pid);
QString result;
if (mem_kb != -1) {
QString memStr;
formatMemory(memStr, mem_kb);
result = QString("PID %1 is using %2.").arg(pid).arg(memStr);
} else {
result = QString("Could not find process with PID %1.").arg(pid);
}
m_pidResultLabel->setText(result);
}
void MainWindow::onCompareButtonClicked()
{
pid_t pid1 = m_pid1LineEdit->text().toInt();
pid_t pid2 = m_pid2LineEdit->text().toInt();
ProcessWorker tempWorker;
long mem1 = tempWorker.getVmRssFromPid(pid1);
long mem2 = tempWorker.getVmRssFromPid(pid2);
QString result;
if(mem1 == -1 || mem2 == -1) {
result = "Could not find one or both PIDs.";
} else {
QString memStr1, memStr2, diffStr;
formatMemory(memStr1, mem1);
formatMemory(memStr2, mem2);
result = QString("PID %1: %2 | PID %3: %4. ").arg(pid1).arg(memStr1).arg(pid2).arg(memStr2);
if (mem1 > mem2) {
formatMemory(diffStr, mem1 - mem2);
result += QString("PID %1 uses %2 more.").arg(pid1).arg(diffStr);
} else if (mem2 > mem1) {
formatMemory(diffStr, mem2 - mem1);
result += QString("PID %1 uses %2 more.").arg(pid2).arg(diffStr);
} else {
result += "They use the same amount.";
}
}
m_compareResultLabel->setText(result);
}
void MainWindow::onSetAlertButtonClicked()
{
int threshold = m_thresholdSpinBox->value();
worker->setThreshold(threshold);
m_alertStatusLabel->setText(QString("Alert threshold set to %1%").arg(threshold));
currentThreshold = threshold; // Update current threshold
alertActive = false; // Reset alert status when threshold changes
}
void MainWindow::onSaveReportButtonClicked()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save Report", "memory_report.txt", "Text Files (*.txt)");
if (fileName.isEmpty()) return;
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(this, "Error", "Could not save file.");
return;
}
QTextStream out(&file);
QString memStr;
out << "--- System Memory Report ---\n";
formatMemory(memStr, lastData.memTotal);
out << "Total Memory: " << memStr << "\n";
formatMemory(memStr, lastData.memAvailable);
out << "Available Memory: " << memStr << "\n";
out << "Memory Type: " << lastData.memoryType << "\n";
out << "Memory Speed: " << lastData.memorySpeed << "\n";
out << "\n--- All Running Processes ---\n";
out << QString("%1; %2; %3\n").arg("Name", -30).arg("PID", -10).arg("Memory");
out << "--------------------------------------------------------------\n";
for(const auto& process : lastData.processes) {
formatMemory(memStr, process.memory);
out << QString("%1; %2; %3\n").arg(process.name, -30).arg(process.pid, -10).arg(memStr);
}
file.close();
m_reportStatusLabel->setText(QString("Report saved to %1").arg(fileName));
}
void MainWindow::onGetTopNClicked()
{
int n = m_topNSpinBox->value();
int rowCount = qMin(n, lastData.processes.size());
m_topNTableWidget->setRowCount(rowCount);
QString memStr;
for(int i = 0; i < rowCount; ++i) {
const auto& process = lastData.processes.at(i);
formatMemory(memStr, process.memory);
QTableWidgetItem *nameItem = new QTableWidgetItem(process.name);
QTableWidgetItem *pidItem = new QTableWidgetItem(QString::number(process.pid));
QTableWidgetItem *memItem = new QTableWidgetItem(memStr);
m_topNTableWidget->setItem(i, 0, nameItem);
m_topNTableWidget->setItem(i, 1, pidItem);
m_topNTableWidget->setItem(i, 2, memItem);
}
}
void MainWindow::onStartLoggingClicked()
{
if (m_loggingTimer->isActive()) {
m_loggingStatusLabel->setText("Logging already in progress.");
return;
}
int intervalValue = m_intervalValueSpinBox->value();
QString intervalUnit = m_intervalUnitComboBox->currentText();
int interval = intervalValue * (intervalUnit == "minutes" ? 60 : intervalUnit == "hours" ? 3600 : 1);
int durationValue = m_durationValueSpinBox->value();
QString durationUnit = m_durationUnitComboBox->currentText();
int duration_sec = durationValue * (durationUnit == "minutes" ? 60 : durationUnit == "hours" ? 3600 : 1);
if (duration_sec < interval) {
QMessageBox::warning(this, "Error", "Duration too short.");
return;
}
m_totalLogs = duration_sec / interval;
m_logCount = 0;
if (m_specificRadio->isChecked()) {
QString pidsText = m_pidsLineEdit->text();
if (pidsText.isEmpty()) {
QMessageBox::warning(this, "Error", "Enter PIDs.");
return;
}
m_specificPids.clear();
for (QString pidStr : pidsText.split(',')) {
bool ok;
pid_t pid = pidStr.trimmed().toInt(&ok);
if (ok) m_specificPids.append(pid);
}
if (m_specificPids.isEmpty()) {
QMessageBox::warning(this, "Error", "Invalid PIDs.");
return;
}
} else {
m_specificPids.clear();
}
QString dateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss");
m_logContent = QString("Memory Usage Log - Started at %1\n").arg(dateTime);
m_logContent += QString("Interval: %1 %2\n").arg(intervalValue).arg(intervalUnit);
m_logContent += QString("Duration: %1 %2\n").arg(durationValue).arg(durationUnit);
if (m_specificPids.isEmpty()) {
m_logContent += "Logging: All Processes\n\n";
} else {
m_logContent += QString("Logging: Specific PIDs - %1\n\n").arg(m_pidsLineEdit->text());
}
performLog();
m_loggingTimer->start(interval * 1000);
m_loggingStatusLabel->setText("Logging in progress...");
}
void MainWindow::performLog()
{
QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
m_logContent += QString("--- Log at %1 ---\n").arg(timestamp);
QString memStr;
formatMemory(memStr, lastData.memTotal);
m_logContent += QString("Total Memory: %1\n").arg(memStr);
formatMemory(memStr, lastData.memAvailable);
m_logContent += QString("Available Memory: %1\n\n").arg(memStr);
m_logContent += "Processes:\n";
m_logContent += QString("%1; %2; %3\n").arg("Name", -30).arg("PID", -10).arg("Memory");
if (m_specificPids.isEmpty()) {
for (const auto& process : lastData.processes) {
formatMemory(memStr, process.memory);
m_logContent += QString("%1; %2; %3\n").arg(process.name, -30).arg(process.pid, -10).arg(memStr);
}
} else {
for (pid_t pid : m_specificPids) {
auto it = std::find_if(lastData.processes.begin(), lastData.processes.end(),
[pid](const ProcessInfo& p) { return p.pid == pid; });
if (it != lastData.processes.end()) {
formatMemory(memStr, it->memory);
m_logContent += QString("%1; %2; %3\n").arg(it->name, -30).arg(it->pid, -10).arg(memStr);
} else {
m_logContent += QString("PID %1 not found.\n").arg(pid);
}
}
}
m_logContent += "\n";
m_logCount++;
if (m_logCount >= m_totalLogs) {
m_loggingTimer->stop();
m_logContent += "Logging completed.\n";
QString dateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss");
QString defaultFileName = QString("memory_report_%1.txt").arg(dateTime);
QString fileName = QFileDialog::getSaveFileName(this, "Save Log Report", defaultFileName, "Text Files (*.txt)");
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << m_logContent;
file.close();
m_loggingStatusLabel->setText(QString("Log saved to %1").arg(fileName));
} else {
m_loggingStatusLabel->setText("Failed to save log.");
}
} else {
m_loggingStatusLabel->setText("Logging completed, but not saved.");
}
m_logContent = "";
}
}
void MainWindow::formatMemory(QString& buffer, long kilobytes)
{
if (kilobytes < 0) {
buffer = "N/A";
} else if (kilobytes < 1024) {
buffer = QString("%1 KB").arg(kilobytes);
} else if (kilobytes < 1024 * 1024) {
buffer = QString::asprintf("%.2f MB", kilobytes / 1024.0);
} else {
buffer = QString::asprintf("%.2f GB", kilobytes / (1024.0 * 1024.0));
}
}