/* * MIT License * * Copyright (c) 2025 Vanessa T. * * 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 #include "PrintHelper.h" #include "Utility.h" std::ostream &operator<<(std::ostream &os, const InlinePrint &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(ramUsage.peakWorkingSetSize); workingSetSize = formatBytes(ramUsage.workingSetSize); pageFileUsage = formatBytes(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) { 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(ramUsage.peakWorkingSetSize); workingSetSize = formatBytes(ramUsage.workingSetSize); pageFileUsage = formatBytes(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) { 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) { 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(memUsage.peakWorkingSetSize) << "\n" << " Working Set Size: " << formatBytes(memUsage.workingSetSize) << "\n" << " Page File Usage: " << formatBytes(memUsage.pageFileUsage) << "\n" << " Peak Page File Usage: " << formatBytes(memUsage.peakPageFileUsage) << "\n" << " Private Usage: " << formatBytes(memUsage.privateUsage) << "\n" << " Pool Usages:\n" << " Paged Pool Peak: " << formatBytes(memUsage.quotaPeakPagedPoolUsage) << "\n" << " Paged Pool Current: " << formatBytes(memUsage.quotaPagedPoolUsage) << "\n" << " Non-Paged Pool Peak: " << formatBytes(memUsage.quotaPeakNonPagedPoolUsage) << "\n" << " Non-Paged Pool Current:" << formatBytes(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(memUsage.peakWorkingSetSize) << L"\n" << L" Working Set Size: " << formatBytes(memUsage.workingSetSize) << L"\n" << L" Page File Usage: " << formatBytes(memUsage.pageFileUsage) << L"\n" << L" Peak Page File Usage: " << formatBytes(memUsage.peakPageFileUsage) << L"\n" << L" Private Usage: " << formatBytes(memUsage.privateUsage) << L"\n" << L" Pool Usages:\n" << L" Paged Pool Peak: " << formatBytes(memUsage.quotaPeakPagedPoolUsage) << L"\n" << L" Paged Pool Current: " << formatBytes(memUsage.quotaPagedPoolUsage) << L"\n" << L" Non-Paged Pool Peak: " << formatBytes(memUsage.quotaPeakNonPagedPoolUsage) << L"\n" << L" Non-Paged Pool Current:" << formatBytes(memUsage.quotaNonPagedPoolUsage); return os; }