#include "guiconsole_define.h" #include #include #include #include std::string GenerateTimeStr() { SYSTEMTIME st; GetLocalTime(&st); CSimpleString timeStr = CSimpleString::Format("[%04d-%02d-%02d %02d:%02d:%02d]", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); return timeStr.GetData(); } #ifdef RVC_OS_WIN #include #include #include #include #include #include #include #include #include // memInfo ½á¹¹Ìå struct memInfo { ULONGLONG TotalMemory; ULONGLONG UsedMemory; double UsedMemoryPerc; ULONGLONG TotalPageMemory; ULONGLONG UsedPageMemory; double UsedPageMemoryPerc; ULONGLONG UpTime; // Constructor to initialize the fields memInfo() : TotalMemory(0), UsedMemory(0), UsedMemoryPerc(0.0), TotalPageMemory(0), UsedPageMemory(0), UsedPageMemoryPerc(0.0), UpTime(0) {} }; static ULONGLONG SubtractTimes(const FILETIME* A, const FILETIME* B) { LARGE_INTEGER lA, lB; lA.LowPart = A->dwLowDateTime; lA.HighPart = A->dwHighDateTime; lB.LowPart = B->dwLowDateTime; lB.HighPart = B->dwHighDateTime; return lA.QuadPart - lB.QuadPart; } void PollMemInfo(memInfo& info) { MEMORYSTATUSEX MemoryInfo; MemoryInfo.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&MemoryInfo); info.TotalMemory = TO_MB(MemoryInfo.ullTotalPhys); info.UsedMemory = TO_MB(MemoryInfo.ullTotalPhys - MemoryInfo.ullAvailPhys); info.UsedMemoryPerc = (double)info.UsedMemory / (double)info.TotalMemory; info.TotalPageMemory = TO_MB(MemoryInfo.ullTotalPageFile); info.UsedPageMemory = TO_MB(MemoryInfo.ullTotalPageFile - MemoryInfo.ullAvailPageFile); info.UsedPageMemoryPerc = (double)info.UsedPageMemory / (double)info.TotalPageMemory; info.UpTime = GetTickCount64(); } std::pair PollProcessList(long UpdateTime, system_monitor_status& curStatus) { HANDLE Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); if (!Snapshot) return std::make_pair(0, ""); PROCESSENTRY32 Entry; Entry.dwSize = sizeof(Entry); BOOL Status = Process32First(Snapshot, &Entry); if (!Status) return std::make_pair(0, ""); memInfo info; PollMemInfo(info); HANDLE curHandle = NULL; // curProcess Handle for (; Status; Status = Process32Next(Snapshot, &Entry)) { processStatus Process; Process.ID = Entry.th32ProcessID; if (Process.ID == 0) continue; Process.HandleCount = Entry.cntThreads; //Process.BasePriority = Entry.pcPriClassBase; //Process.ParentPID = Entry.th32ParentProcessID; Process.processName = Entry.szExeFile; _tcsncpy_s(Process.UserName, UNLEN, _T("SYSTEM"), UNLEN); curHandle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, Entry.th32ProcessID); if (curHandle) { PROCESS_MEMORY_COUNTERS ProcMemCounters; if (GetProcessMemoryInfo(curHandle, &ProcMemCounters, sizeof(ProcMemCounters))) { Process.UsedMemory = (unsigned __int64)ProcMemCounters.WorkingSetSize; Process.PercentMemory = (double)TO_MB(Process.UsedMemory) / (double)info.TotalMemory; } HANDLE ProcessTokenHandle; if (OpenProcessToken(curHandle, TOKEN_READ, &ProcessTokenHandle)) { DWORD ReturnLength; GetTokenInformation(ProcessTokenHandle, TokenUser, 0, 0, &ReturnLength); if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { PTOKEN_USER TokenUserStruct = (PTOKEN_USER)malloc(ReturnLength); if (GetTokenInformation(ProcessTokenHandle, TokenUser, TokenUserStruct, ReturnLength, &ReturnLength)) { SID_NAME_USE NameUse; DWORD NameLength = UNLEN; TCHAR DomainName[MAX_PATH]; DWORD DomainLength = MAX_PATH; LookupAccountSid(0, TokenUserStruct->User.Sid, Process.UserName, &NameLength, DomainName, &DomainLength, &NameUse); Process.UserName[9] = 0; } free(TokenUserStruct); } CloseHandle(ProcessTokenHandle); } } curStatus.processList.push_back(Process); } CloseHandle(Snapshot); //get total system information DWORD NewProcessCount = curStatus.processList.size(); typedef struct system_times { FILETIME IdleTime, KernelTime, UserTime; } system_times; system_times PrevSysTimes; GetSystemTimes(&PrevSysTimes.IdleTime, &PrevSysTimes.KernelTime, &PrevSysTimes.UserTime); typedef struct process_times { FILETIME CreationTime, ExitTime, KernelTime, UserTime; } process_times; process_times* ProcessTimes = (process_times*)malloc(NewProcessCount * sizeof(*ProcessTimes)); /* for (DWORD ProcIndex = 0; ProcIndex < NewProcessCount; ProcIndex++) { processStatus* Process = &curStatus.processList[ProcIndex]; process_times* ProcessTime = &ProcessTimes[ProcIndex]; if (curHandle) { GetProcessTimes(curHandle, &ProcessTime->CreationTime, &ProcessTime->ExitTime, &ProcessTime->KernelTime, &ProcessTime->UserTime); IO_COUNTERS IoCounters; if (GetProcessIoCounters(curHandle, &IoCounters)) { Process->DiskOperationsPrev = IoCounters.ReadTransferCount + IoCounters.WriteTransferCount; } } } */ Sleep(UpdateTime); system_times SysTimes; GetSystemTimes(&SysTimes.IdleTime, &SysTimes.KernelTime, &SysTimes.UserTime); ULONGLONG SysKernelDiff = SubtractTimes(&SysTimes.KernelTime, &PrevSysTimes.KernelTime); ULONGLONG SysUserDiff = SubtractTimes(&SysTimes.UserTime, &PrevSysTimes.UserTime); ULONGLONG SysIdleDiff = SubtractTimes(&SysTimes.IdleTime, &PrevSysTimes.IdleTime); int RunningProcessCount = 0; for (DWORD ProcIndex = 0; ProcIndex < NewProcessCount; ProcIndex++) { process_times ProcessTime = { 0 }; process_times* PrevProcessTime = &ProcessTimes[ProcIndex]; processStatus* Process = &curStatus.processList[ProcIndex]; if (curHandle) { GetProcessTimes(curHandle, &ProcessTime.CreationTime, &ProcessTime.ExitTime, &ProcessTime.KernelTime, &ProcessTime.UserTime); ULONGLONG ProcKernelDiff = SubtractTimes(&ProcessTime.KernelTime, &PrevProcessTime->KernelTime); ULONGLONG ProcUserDiff = SubtractTimes(&ProcessTime.UserTime, &PrevProcessTime->UserTime); ULONGLONG TotalSys = SysKernelDiff + SysUserDiff; ULONGLONG TotalProc = ProcKernelDiff + ProcUserDiff; if (TotalSys > 0) { Process->PercentProcessorTime = (double)((100.0 * (double)TotalProc) / (double)TotalSys); if (Process->PercentProcessorTime >= 0.01) { RunningProcessCount++; } } FILETIME SysTime; GetSystemTimeAsFileTime(&SysTime); //Process->UpTime = SubtractTimes(&SysTime, &ProcessTime.CreationTime) / 10000; /* get disk information IO_COUNTERS IoCounters; if (GetProcessIoCounters(curHandle, &IoCounters)) { Process->DiskOperations = IoCounters.ReadTransferCount + IoCounters.WriteTransferCount; Process->DiskUsage = (DWORD)((Process->DiskOperations - Process->DiskOperationsPrev) * (1000 / UpdateTime)); } */ CloseHandle(curHandle); curHandle = NULL; } } free(ProcessTimes); /* Since we have the values already we can compute CPU usage too */ ULONGLONG SysTime = SysKernelDiff + SysUserDiff; if (SysTime > 0) { double Percentage = (double)(SysTime - SysIdleDiff) / (double)SysTime; curStatus.total_cpu = min(Percentage, 1.0); } curStatus.TotalMemory = info.TotalMemory; curStatus.UsedMemory = info.UsedMemory; curStatus.UsedMemoryPerc = info.UsedMemoryPerc; curStatus.TotalPageMemory = info.TotalPageMemory; curStatus.UsedPageMemory = info.UsedPageMemory; curStatus.UsedPageMemoryPerc = info.UsedPageMemoryPerc; curStatus.total_uptime = info.UpTime; return std::make_pair(0, ""); } #else #include "linux_system_monitor/system.h" std::pair PollProcessList(long UpdateTime, system_monitor_status& curStatus) { System system; auto processArr = system.Processes(); curStatus.processList.clear(); for (auto it : processArr) { processStatus cur; cur.ID = it.Pid(); cur.processName = it.name(); char buffer[20]; std::sprintf(buffer, "%.2f", it.CpuUtilization()); cur.PercentProcessorTime = std::atof(buffer); cur.UsedMemory = std::stoi(it.Ram()); cur.HandleCount = it.fdHandles(); curStatus.processList.push_back(cur); } return std::make_pair(0, ""); } #endif static const std::unordered_map logTypeEnumToString = { {LogTypeEnum::Log_Ignore, "Ignore"}, {LogTypeEnum::Log_Event, "Event"}, {LogTypeEnum::Log_Warning, "Warning"}, {LogTypeEnum::Log_Error, "Error"}, {LogTypeEnum::Log_Debug, "Debug"}, {LogTypeEnum::Log_Fatal, "Fatal"}, {LogTypeEnum::Log_Notify, "Notify"} }; static const std::unordered_map stringToLogTypeEnum = { {"Ignore", LogTypeEnum::Log_Ignore}, {"Event", LogTypeEnum::Log_Event}, {"Warning", LogTypeEnum::Log_Warning}, {"Error", LogTypeEnum::Log_Error}, {"Debug", LogTypeEnum::Log_Debug}, {"Fatal", LogTypeEnum::Log_Fatal}, {"Notify", LogTypeEnum::Log_Notify} }; std::string LogTypeEnumToString(LogTypeEnum logType) { auto it = logTypeEnumToString.find(logType); if (it != logTypeEnumToString.end()) { return it->second; } else { return "Unknown"; } } LogTypeEnum StringToLogTypeEnum(const std::string& logTypeStr) { auto it = stringToLogTypeEnum.find(logTypeStr); if (it != stringToLogTypeEnum.end()) { return it->second; } else return LogTypeEnum::Log_Warning; } void startProcess(std::string cmdline, bool isWait) { }