add tmp buffer to store messages until the log file is ready
This commit is contained in:
32
src/log.cpp
32
src/log.cpp
@@ -74,35 +74,51 @@ void LogRemote::net_close()
|
||||
}
|
||||
void LogRemote::file_init()
|
||||
{
|
||||
std::lock_guard<std::mutex> _lock(m_mutex);
|
||||
if (!m_logfile.is_open())
|
||||
m_logfile.open(App::I->data_path + "/panopainter-log.txt");
|
||||
if (m_logfile.is_open() && !m_tmp.empty())
|
||||
{
|
||||
for (auto const& s : m_tmp)
|
||||
m_logfile.write(s.data(), s.size());
|
||||
m_logfile.flush();
|
||||
m_tmp.clear();
|
||||
}
|
||||
}
|
||||
void LogRemote::file_close()
|
||||
{
|
||||
std::lock_guard<std::mutex> _lock(m_mutex);
|
||||
if (!m_logfile.is_open())
|
||||
m_logfile.close();
|
||||
}
|
||||
void LogRemote::log(const char* format, ...)
|
||||
{
|
||||
std::lock_guard<std::mutex> _lock(m_mutex);
|
||||
static char buffer[4096];
|
||||
va_list arglist;
|
||||
va_start(arglist, format);
|
||||
int n = vsnprintf(buffer, sizeof(buffer), format, arglist);
|
||||
va_end(arglist);
|
||||
m_mq.Post(std::string(buffer, n));
|
||||
auto line = std::string(buffer, n) + "\n";
|
||||
if (m_logfile.is_open())
|
||||
{
|
||||
auto line = std::string(buffer, n) + "\n";
|
||||
m_logfile.write(line.data(), line.size());
|
||||
m_logfile.flush();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_tmp.push_back(line);
|
||||
}
|
||||
#if _WIN32
|
||||
auto line = "DBG: " + std::string(buffer, n) + "\n";
|
||||
OutputDebugStringA(line.c_str());
|
||||
auto line_console = "DBG: " + line;
|
||||
OutputDebugStringA(line_console.c_str());
|
||||
#endif
|
||||
}
|
||||
void LogRemote::log(const wchar_t* format, ...)
|
||||
{
|
||||
std::lock_guard<std::mutex> _lock(m_mutex);
|
||||
|
||||
static wchar_t buffer[4096];
|
||||
va_list arglist;
|
||||
va_start(arglist, format);
|
||||
@@ -125,15 +141,19 @@ void LogRemote::log(const wchar_t* format, ...)
|
||||
//std::string converted_str = converter.to_bytes(string_to_convert);
|
||||
|
||||
m_mq.Post(std::string(converted));
|
||||
auto line = converted + "\n";
|
||||
if (m_logfile.is_open())
|
||||
{
|
||||
auto line = converted + "\n";
|
||||
m_logfile.write(line.data(), line.size());
|
||||
m_logfile.flush();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_tmp.push_back(line);
|
||||
}
|
||||
#if _WIN32
|
||||
auto line = L"DBG: " + std::wstring(buffer, n) + L"\n";
|
||||
OutputDebugStringW(line.c_str());
|
||||
auto line_console = L"DBG: " + std::wstring(buffer, n) + L"\n";
|
||||
OutputDebugStringW(line_console.c_str());
|
||||
#endif
|
||||
}
|
||||
LogRemote::~LogRemote()
|
||||
|
||||
Reference in New Issue
Block a user