-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogBuffer.cpp
More file actions
142 lines (125 loc) · 3.06 KB
/
LogBuffer.cpp
File metadata and controls
142 lines (125 loc) · 3.06 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "LogBuffer.h"
#include "XPLMUtilities.h"
#include <algorithm>
#include <fstream>
#include <mutex>
#include <string>
namespace vr_image_client
{
namespace
{
std::string ConvertHfsPathToPosix(const std::string &hfs_path)
{
const auto first_colon = hfs_path.find(':');
if (first_colon == std::string::npos)
{
return hfs_path;
}
std::string volume = hfs_path.substr(0, first_colon);
std::string remainder = hfs_path.substr(first_colon + 1);
std::replace(remainder.begin(), remainder.end(), ':', '/');
std::string result("/Volumes/");
result.append(volume);
if (!remainder.empty() && remainder.front() != '/')
{
result.push_back('/');
}
result.append(remainder);
return result;
}
std::string ResolveLogFilePath()
{
char system_path[1024] = {0};
XPLMGetSystemPath(system_path);
std::string path(system_path);
const char *separator = XPLMGetDirectorySeparator();
char separator_char = (separator != nullptr && separator[0] != '\0') ? separator[0] : '/';
if (separator_char == ':' && path.find('/') == std::string::npos)
{
path = ConvertHfsPathToPosix(path);
separator_char = '/';
}
if (!path.empty() && path.back() != separator_char)
{
path.push_back(separator_char);
}
path.append("VR_Plugin.txt");
return path;
}
const std::string &GetLogFilePath()
{
static const std::string path = ResolveLogFilePath();
return path;
}
std::mutex &LogFileMutex()
{
static std::mutex mutex;
return mutex;
}
bool OpenLogFile(std::ofstream &file, std::ios::openmode mode)
{
const std::string &path = GetLogFilePath();
file.open(path, mode);
if (!file)
{
static bool reported = false;
if (!reported)
{
reported = true;
XPLMDebugString("[VR_ImageClient] Failed to open VR_Plugin.txt for logging.\n");
}
return false;
}
return true;
}
void AppendUnlocked(const std::string &message)
{
std::ofstream file;
if (!OpenLogFile(file, std::ios::out | std::ios::app))
{
if (!message.empty())
{
XPLMDebugString(message.c_str());
}
return;
}
file << message;
}
} // namespace
void LogBuffer::Push(const std::string &message)
{
std::lock_guard<std::mutex> lock(mutex_);
queue_.push_back(message);
}
void LogBuffer::Flush()
{
std::deque<std::string> drained;
{
std::lock_guard<std::mutex> lock(mutex_);
drained.swap(queue_);
}
for (const auto &line : drained)
{
WriteToPluginLog(line);
}
}
void InitializePluginLog()
{
std::lock_guard<std::mutex> lock(LogFileMutex());
std::ofstream file;
OpenLogFile(file, std::ios::out | std::ios::trunc);
}
void WriteToPluginLog(const std::string &message)
{
std::lock_guard<std::mutex> lock(LogFileMutex());
AppendUnlocked(message);
}
void WriteToPluginLog(const char *message)
{
if (message == nullptr)
{
return;
}
WriteToPluginLog(std::string(message));
}
} // namespace vr_image_client