Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
pano_log.cpp
Go to the documentation of this file.
1#include <Windows.h>
2#include <sstream>
3#include <iostream>
4#include <mutex>
5#include "service_constants.h"
6
7bool EnsureDirectoryExists(const std::wstring& path) {
8 DWORD attributes = GetFileAttributesW(path.c_str());
9 if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
10 return true; // Directory already exists
11 }
12
13 // Recursively create parent directories
14 size_t pos = path.find_last_of(L"\\/");
15 if (pos != std::wstring::npos) {
16 if (!EnsureDirectoryExists(path.substr(0, pos))) {
17 return false;
18 }
19 }
20
21 // Create the directory
22 if (!CreateDirectoryW(path.c_str(), NULL)) {
23 DWORD error = GetLastError();
24 if (error != ERROR_ALREADY_EXISTS) {
25 std::wcerr << L"Failed to create directory: " << path << L". Error code: " << error << std::endl;
26 return false;
27 }
28 }
29
30 return true;
31}
32
33DWORD NewestLogFileNumber(const std::wstring& folder, const std::wstring& base, const std::wstring& ext) {
34 DWORD newest = 0;
35 WIN32_FIND_DATAW found;
36 HANDLE search = FindFirstFileW((folder + base + L"*" + ext).c_str(), &found);
37 if (search == INVALID_HANDLE_VALUE) {
38 return 0;
39 }
40 do {
41 std::wstring name = found.cFileName;
42 // The wildcard also matches short (8.3) names and longer extensions; check exactly
43 if (name.size() <= base.size() + ext.size() ||
44 _wcsnicmp(name.c_str(), base.c_str(), base.size()) != 0 ||
45 _wcsicmp(name.c_str() + name.size() - ext.size(), ext.c_str()) != 0) {
46 continue;
47 }
48 std::wstring digits = name.substr(base.size(), name.size() - base.size() - ext.size());
49 if (digits.size() > 9 || digits.find_first_not_of(L"0123456789") != std::wstring::npos) {
50 continue;
51 }
52 DWORD number = (DWORD)std::stoul(digits);
53 if (number > newest) {
54 newest = number;
55 }
56 } while (FindNextFileW(search, &found));
57 FindClose(search);
58 return newest;
59}
60
61// LOG_FOLDER\pano.log for 0, otherwise LOG_FOLDER\panoN.log
62static std::wstring LogFilePath(DWORD number) {
63 std::wostringstream oss;
64 oss << LOG_FOLDER << BASE_FILENAME;
65 if (number > 0) {
66 oss << number;
67 }
68 oss << FILE_EXTENSION;
69 return oss.str();
70}
71
72void WriteToLogFile(const std::string& message) {
73 // Called from the driver event threads and the hook event consumer; the lock keeps lines
74 // whole and serializes rotating to the next file
75 static std::mutex lock;
76 std::lock_guard<std::mutex> guard(lock);
77
78 static DWORD currentFileNumber = 0;
79 static bool numberFound = false;
80 static HANDLE hFile = INVALID_HANDLE_VALUE;
81 // Size of the open file, kept here so each write does not have to query it
82 static ULONGLONG currentFileSize = 0;
83
84 DWORD bytesWritten = 0;
85
86 if (hFile != INVALID_HANDLE_VALUE && currentFileSize >= MAX_FILE_SIZE) {
87 CloseHandle(hFile);
88 hFile = INVALID_HANDLE_VALUE;
89 currentFileNumber++;
90 }
91
92 if (hFile == INVALID_HANDLE_VALUE) {
93 // Only checked when a file is opened: once one is open, its folder exists
95 std::wcerr << L"Failed to create log directory" << std::endl;
96 return;
97 }
98
99 // Continue in the newest file from an earlier run rather than starting over at pano.log,
100 // which would append to full files and leave the newest events in an old one
101 if (!numberFound) {
103 numberFound = true;
104 }
105
106 // The newest file may already be full; then the next number starts a new one
107 for (;;) {
108 std::wstring fullPath = LogFilePath(currentFileNumber);
109 hFile = CreateFileW(
110 fullPath.c_str(),
111 FILE_APPEND_DATA, // Every write goes to the end of the file
112 FILE_SHARE_READ,
113 NULL,
114 OPEN_ALWAYS,
115 FILE_ATTRIBUTE_NORMAL,
116 NULL
117 );
118
119 if (hFile == INVALID_HANDLE_VALUE) {
120 std::wcerr << L"Failed to open log file: " << fullPath << std::endl;
121 return;
122 }
123
124 LARGE_INTEGER size;
125 currentFileSize = GetFileSizeEx(hFile, &size) ? (ULONGLONG)size.QuadPart : 0;
126 if (currentFileSize < MAX_FILE_SIZE) {
127 break;
128 }
129 CloseHandle(hFile);
130 hFile = INVALID_HANDLE_VALUE;
131 currentFileNumber++;
132 }
133 }
134
135 if (!WriteFile(hFile, message.c_str(), message.length() * sizeof(char), &bytesWritten, NULL)) {
136 std::wcerr << L"Failed to write to log file" << std::endl;
137 }
138 currentFileSize += bytesWritten;
139}
bool EnsureDirectoryExists(const std::wstring &path)
Definition pano_log.cpp:7
DWORD NewestLogFileNumber(const std::wstring &folder, const std::wstring &base, const std::wstring &ext)
The number of the newest log file in a folder: 0 for base.ext (or when there is none),...
Definition pano_log.cpp:33
void WriteToLogFile(const std::string &message)
Appends a line to the event log: LOG_FOLDER\pano.log, continuing in pano1.log, pano2....
Definition pano_log.cpp:72
#define BASE_FILENAME
#define LOG_FOLDER
#define MAX_FILE_SIZE
#define FILE_EXTENSION