8 DWORD attributes = GetFileAttributesW(path.c_str());
9 if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
14 size_t pos = path.find_last_of(L
"\\/");
15 if (pos != std::wstring::npos) {
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;
33DWORD
NewestLogFileNumber(
const std::wstring& folder,
const std::wstring& base,
const std::wstring& ext) {
35 WIN32_FIND_DATAW found;
36 HANDLE search = FindFirstFileW((folder + base + L
"*" + ext).c_str(), &found);
37 if (search == INVALID_HANDLE_VALUE) {
41 std::wstring name = found.cFileName;
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) {
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) {
52 DWORD number = (DWORD)std::stoul(digits);
53 if (number > newest) {
56 }
while (FindNextFileW(search, &found));
75 static std::mutex lock;
76 std::lock_guard<std::mutex> guard(lock);
78 static DWORD currentFileNumber = 0;
79 static bool numberFound =
false;
80 static HANDLE hFile = INVALID_HANDLE_VALUE;
82 static ULONGLONG currentFileSize = 0;
84 DWORD bytesWritten = 0;
86 if (hFile != INVALID_HANDLE_VALUE && currentFileSize >=
MAX_FILE_SIZE) {
88 hFile = INVALID_HANDLE_VALUE;
92 if (hFile == INVALID_HANDLE_VALUE) {
95 std::wcerr << L
"Failed to create log directory" << std::endl;
108 std::wstring fullPath = LogFilePath(currentFileNumber);
115 FILE_ATTRIBUTE_NORMAL,
119 if (hFile == INVALID_HANDLE_VALUE) {
120 std::wcerr << L
"Failed to open log file: " << fullPath << std::endl;
125 currentFileSize = GetFileSizeEx(hFile, &size) ? (ULONGLONG)size.QuadPart : 0;
130 hFile = INVALID_HANDLE_VALUE;
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;
138 currentFileSize += bytesWritten;
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),...
void WriteToLogFile(const std::string &message)
Appends a line to the event log: LOG_FOLDER\pano.log, continuing in pano1.log, pano2....