findwindow/PrintHelper.cpp

191 lines
7.6 KiB
C++

/*
* MIT License
*
* Copyright (c) 2025 Vanessa T. <nessa@neko-tools.de>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <format>
#include "PrintHelper.h"
#include "Utility.h"
std::ostream &operator<<(std::ostream &os, const InlinePrint<Process> &process) {
auto width = process.printWidth;
if (width < 80) width = 80;
auto imagePathWidth = width - 36 - 10 - 10 - 10 - 4;
auto formatString = "{:^8}|{:<" + std::to_string(imagePathWidth) + "}|{:>10}|{:>10}|{:>10}";
std::string peakWorkingSetSize;
std::string workingSetSize;
std::string pageFileUsage;
try {
auto ramUsage = process->ramUsage();
peakWorkingSetSize = formatBytes<std::string>(ramUsage.peakWorkingSetSize);
workingSetSize = formatBytes<std::string>(ramUsage.workingSetSize);
pageFileUsage = formatBytes<std::string>(ramUsage.pageFileUsage);
} catch (std::exception &e) {
peakWorkingSetSize = "N/A";
workingSetSize = "N/A";
pageFileUsage = "N/A";
}
std::wstring imagePath;
try {
imagePath = process->imagePath();
if (imagePath.length() > imagePathWidth) {
auto start = imagePath.length() - (imagePathWidth - 13);
imagePath = imagePath.substr(0, 10) + L"..." + imagePath.substr(start, imagePathWidth - 13);
}
} catch (std::exception &e) {
imagePath = L"N/A";
}
os << std::vformat(formatString, std::make_format_args(
process->processId(),
wstringToString(imagePath),
peakWorkingSetSize,
workingSetSize,
pageFileUsage
));
return os;
}
std::wostream &operator<<(std::wostream &os, const InlinePrint<Process> &process) {
auto width = process.printWidth;
if (width < 80) width = 80;
auto imagePathWidth = width - 36 - 10 - 10 - 10 - 4;
auto formatString = L"{:^8}|{:<" + std::to_wstring(imagePathWidth) + L"}|{:>10}|{:>10}|{:>10}";
std::wstring peakWorkingSetSize;
std::wstring workingSetSize;
std::wstring pageFileUsage;
try {
auto ramUsage = process->ramUsage();
peakWorkingSetSize = formatBytes<std::wstring>(ramUsage.peakWorkingSetSize);
workingSetSize = formatBytes<std::wstring>(ramUsage.workingSetSize);
pageFileUsage = formatBytes<std::wstring>(ramUsage.pageFileUsage);
} catch (std::exception &e) {
peakWorkingSetSize = L"N/A";
workingSetSize = L"N/A";
pageFileUsage = L"N/A";
}
std::wstring imagePath;
try {
imagePath = process->imagePath();
if (imagePath.length() > imagePathWidth) {
auto start = imagePath.length() - (imagePathWidth - 13);
imagePath = imagePath.substr(0, 10) + L"..." + imagePath.substr(start, imagePathWidth - 13);
}
} catch (std::exception &e) {
imagePath = L"N/A";
}
os << std::vformat(formatString, std::make_wformat_args(
process->processId(),
imagePath,
peakWorkingSetSize,
workingSetSize,
pageFileUsage
));
return os;
}
std::ostream &operator<<(std::ostream &os, const InlineHeader<Process> &process) {
auto width = process.printWidth;
if (width < 80) width = 80;
auto imagePathWidth = width - 36 - 10 - 10 - 10 - 4;
auto formatString = "{:^8}|{:<" + std::to_string(imagePathWidth) + "}|{:>10}|{:>10}|{:>10}";
os << std::vformat(formatString, std::make_format_args(
"PID",
"Image Path",
"reserved",
"use",
"swap"
)) << std::endl;
os << "--------+"
<< std::string(imagePathWidth, '-')
<< "+----------+----------+----------";
return os;
}
std::wostream &operator<<(std::wostream &os, const InlineHeader<Process> &process) {
auto width = process.printWidth;
if (width < 80) width = 80;
auto imagePathWidth = width - 36 - 10 - 10 - 10 - 4;
auto formatString = L"{:^8}|{:<" + std::to_wstring(imagePathWidth) + L"}|{:>10}|{:>10}|{:>10}";
os << std::vformat(formatString, std::make_wformat_args(
L"PID",
L"Image Path",
L"reserved",
L"use",
L"swap"
)) << std::endl;
os << L"--------+"
<< std::wstring(imagePathWidth, L'-')
<< L"+----------+----------+----------";
return os;
}
std::ostream &operator<<(std::ostream &os, const Process::MemoryUsage &memUsage) {
os << "Memory Usage Details:\n"
<< " Page Faults: " << memUsage.pageFaults << "\n"
<< " Peak Working Set Size: " << formatBytes<std::string>(memUsage.peakWorkingSetSize) << "\n"
<< " Working Set Size: " << formatBytes<std::string>(memUsage.workingSetSize) << "\n"
<< " Page File Usage: " << formatBytes<std::string>(memUsage.pageFileUsage) << "\n"
<< " Peak Page File Usage: " << formatBytes<std::string>(memUsage.peakPageFileUsage) << "\n"
<< " Private Usage: " << formatBytes<std::string>(memUsage.privateUsage) << "\n"
<< " Pool Usages:\n"
<< " Paged Pool Peak: " << formatBytes<std::string>(memUsage.quotaPeakPagedPoolUsage) << "\n"
<< " Paged Pool Current: " << formatBytes<std::string>(memUsage.quotaPagedPoolUsage) << "\n"
<< " Non-Paged Pool Peak: " << formatBytes<std::string>(memUsage.quotaPeakNonPagedPoolUsage) << "\n"
<< " Non-Paged Pool Current:" << formatBytes<std::string>(memUsage.quotaNonPagedPoolUsage);
return os;
}
std::wostream &operator<<(std::wostream &os, const Process::MemoryUsage &memUsage) {
os << L"Memory Usage Details:\n"
<< L" Page Faults: " << memUsage.pageFaults << L"\n"
<< L" Peak Working Set Size: " << formatBytes<std::wstring>(memUsage.peakWorkingSetSize) << L"\n"
<< L" Working Set Size: " << formatBytes<std::wstring>(memUsage.workingSetSize) << L"\n"
<< L" Page File Usage: " << formatBytes<std::wstring>(memUsage.pageFileUsage) << L"\n"
<< L" Peak Page File Usage: " << formatBytes<std::wstring>(memUsage.peakPageFileUsage) << L"\n"
<< L" Private Usage: " << formatBytes<std::wstring>(memUsage.privateUsage) << L"\n"
<< L" Pool Usages:\n"
<< L" Paged Pool Peak: " << formatBytes<std::wstring>(memUsage.quotaPeakPagedPoolUsage) << L"\n"
<< L" Paged Pool Current: " << formatBytes<std::wstring>(memUsage.quotaPagedPoolUsage) << L"\n"
<< L" Non-Paged Pool Peak: " << formatBytes<std::wstring>(memUsage.quotaPeakNonPagedPoolUsage) << L"\n"
<< L" Non-Paged Pool Current:" << formatBytes<std::wstring>(memUsage.quotaNonPagedPoolUsage);
return os;
}