Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
pano_log.h File Reference
#include <Windows.h>
#include <string>

Go to the source code of this file.

Functions

void WriteToLogFile (const std::string &message)
 Appends a line to the event log: LOG_FOLDER\pano.log, continuing in pano1.log, pano2.log, ... once a file reaches MAX_FILE_SIZE. On the first write it continues in the newest file already on disk, so a restart neither appends to a full file nor reuses old ones.
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), N for baseN.ext. Other names, such as base.old.ext or baseX.ext, are ignored.

Function Documentation

◆ NewestLogFileNumber()

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), N for baseN.ext. Other names, such as base.old.ext or baseX.ext, are ignored.

Parameters
folderThe folder, ending in a backslash
baseThe file name before the number, e.g. L"pano"
extThe extension including the dot, e.g. L".log"

Definition at line 33 of file pano_log.cpp.

33 {
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}

Referenced by LogFolder::Newest(), TEST_F(), and WriteToLogFile().

◆ WriteToLogFile()

void WriteToLogFile ( const std::string & message)

Appends a line to the event log: LOG_FOLDER\pano.log, continuing in pano1.log, pano2.log, ... once a file reaches MAX_FILE_SIZE. On the first write it continues in the newest file already on disk, so a restart neither appends to a full file nor reuses old ones.

Parameters
messageThe line, including its newline

Definition at line 72 of file pano_log.cpp.

72 {
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
#define BASE_FILENAME
#define LOG_FOLDER
#define MAX_FILE_SIZE
#define FILE_EXTENSION

References BASE_FILENAME, EnsureDirectoryExists(), FILE_EXTENSION, LOG_FOLDER, MAX_FILE_SIZE, and NewestLogFileNumber().

Referenced by RecordEvent().