Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Functions
pano_log.cpp File Reference
#include <Windows.h>
#include <sstream>
#include <iostream>
#include "service_constants.h"

Go to the source code of this file.

Functions

bool EnsureDirectoryExists (const std::wstring &path)
 
void WriteToLogFile (const std::string &message)
 

Function Documentation

◆ EnsureDirectoryExists()

bool EnsureDirectoryExists ( const std::wstring &  path)

Definition at line 6 of file pano_log.cpp.

6 {
7 DWORD attributes = GetFileAttributesW(path.c_str());
8 if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
9 return true; // Directory already exists
10 }
11
12 // Recursively create parent directories
13 size_t pos = path.find_last_of(L"\\/");
14 if (pos != std::wstring::npos) {
15 if (!EnsureDirectoryExists(path.substr(0, pos))) {
16 return false;
17 }
18 }
19
20 // Create the directory
21 if (!CreateDirectoryW(path.c_str(), NULL)) {
22 DWORD error = GetLastError();
23 if (error != ERROR_ALREADY_EXISTS) {
24 std::wcerr << L"Failed to create directory: " << path << L". Error code: " << error << std::endl;
25 return false;
26 }
27 }
28
29 return true;
30}
unsigned long DWORD
Definition inject.h:2
bool EnsureDirectoryExists(const std::wstring &path)
Definition pano_log.cpp:6

References EnsureDirectoryExists().

Referenced by EnsureDirectoryExists(), and WriteToLogFile().

◆ WriteToLogFile()

void WriteToLogFile ( const std::string &  message)

Definition at line 32 of file pano_log.cpp.

32 {
33 static DWORD currentFileNumber = 0;
34 static HANDLE hFile = INVALID_HANDLE_VALUE;
35
36 std::wstring fullPath;
37 DWORD bytesWritten;
38
39 // Ensure the log directory exists
41 std::wcerr << L"Failed to create log directory" << std::endl;
42 return;
43 }
44
45 if (hFile == INVALID_HANDLE_VALUE || GetFileSize(hFile, NULL) >= MAX_FILE_SIZE) {
46 if (hFile != INVALID_HANDLE_VALUE) {
47 CloseHandle(hFile);
48 currentFileNumber++;
49 }
50
51 std::wostringstream oss;
52 oss << LOG_FOLDER << BASE_FILENAME;
53 if (currentFileNumber > 0) {
54 oss << currentFileNumber;
55 }
56 oss << FILE_EXTENSION;
57 fullPath = oss.str();
58
59 hFile = CreateFileW(
60 fullPath.c_str(),
61 FILE_APPEND_DATA, // Changed from GENERIC_WRITE to FILE_APPEND_DATA
62 FILE_SHARE_READ,
63 NULL,
64 OPEN_ALWAYS, // Changed from CREATE_ALWAYS to OPEN_ALWAYS
65 FILE_ATTRIBUTE_NORMAL,
66 NULL
67 );
68
69 if (hFile == INVALID_HANDLE_VALUE) {
70 std::wcerr << L"Failed to open log file: " << fullPath << std::endl;
71 return;
72 }
73
74 // Move file pointer to the end of the file
75 SetFilePointer(hFile, 0, NULL, FILE_END);
76 }
77
78 if (!WriteFile(hFile, message.c_str(), message.length() * sizeof(char), &bytesWritten, NULL)) {
79 std::wcerr << L"Failed to write to log file" << std::endl;
80 }
81}
#define BASE_FILENAME
#define LOG_FOLDER
#define MAX_FILE_SIZE
#define FILE_EXTENSION

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

Referenced by DisplayEventInfo().