forked from sskodje/ScreenRecorderLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cpp
More file actions
46 lines (40 loc) · 1.08 KB
/
Log.cpp
File metadata and controls
46 lines (40 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "Log.h"
#include <mutex>
static std::mutex g_Mtx{};
void _log(PCWSTR format, ...)
{
g_Mtx.lock();
wchar_t buffer[LOG_BUFFER_SIZE];
va_list args;
va_start(args, format);
vswprintf_s(buffer, LOG_BUFFER_SIZE, format, args);
if (!logFilePath.empty()) {
std::wofstream logFile(logFilePath, std::ios_base::app | std::ios_base::out);
if (logFile.is_open())
{
logFile << buffer;
logFile.close();
}
else {
OutputDebugStringW(L"Error opening log file for write");
}
}
else {
OutputDebugStringW(buffer);
}
va_end(args);
g_Mtx.unlock();
}
std::wstring GetTimestamp() {
// get a precise timestamp as a string
const auto now = std::chrono::system_clock::now();
const auto nowAsTimeT = std::chrono::system_clock::to_time_t(now);
tm localTime;
localtime_s(&localTime, &nowAsTimeT);
const auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
std::wstringstream nowSs;
nowSs
<< std::put_time(&localTime, L"%Y-%m-%d %H:%M:%S")
<< '.' << std::setfill(L'0') << std::setw(3) << nowMs.count();
return nowSs.str();
}