add tmp buffer to store messages until the log file is ready

This commit is contained in:
2019-07-13 16:48:05 +02:00
parent 0f208ced60
commit d7386cdfa9
2 changed files with 29 additions and 6 deletions

View File

@@ -74,35 +74,51 @@ void LogRemote::net_close()
} }
void LogRemote::file_init() void LogRemote::file_init()
{ {
std::lock_guard<std::mutex> _lock(m_mutex);
if (!m_logfile.is_open()) if (!m_logfile.is_open())
m_logfile.open(App::I->data_path + "/panopainter-log.txt"); 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() void LogRemote::file_close()
{ {
std::lock_guard<std::mutex> _lock(m_mutex);
if (!m_logfile.is_open()) if (!m_logfile.is_open())
m_logfile.close(); m_logfile.close();
} }
void LogRemote::log(const char* format, ...) void LogRemote::log(const char* format, ...)
{ {
std::lock_guard<std::mutex> _lock(m_mutex);
static char buffer[4096]; static char buffer[4096];
va_list arglist; va_list arglist;
va_start(arglist, format); va_start(arglist, format);
int n = vsnprintf(buffer, sizeof(buffer), format, arglist); int n = vsnprintf(buffer, sizeof(buffer), format, arglist);
va_end(arglist); va_end(arglist);
m_mq.Post(std::string(buffer, n)); m_mq.Post(std::string(buffer, n));
auto line = std::string(buffer, n) + "\n";
if (m_logfile.is_open()) if (m_logfile.is_open())
{ {
auto line = std::string(buffer, n) + "\n";
m_logfile.write(line.data(), line.size()); m_logfile.write(line.data(), line.size());
m_logfile.flush(); m_logfile.flush();
} }
else
{
m_tmp.push_back(line);
}
#if _WIN32 #if _WIN32
auto line = "DBG: " + std::string(buffer, n) + "\n"; auto line_console = "DBG: " + line;
OutputDebugStringA(line.c_str()); OutputDebugStringA(line_console.c_str());
#endif #endif
} }
void LogRemote::log(const wchar_t* format, ...) void LogRemote::log(const wchar_t* format, ...)
{ {
std::lock_guard<std::mutex> _lock(m_mutex);
static wchar_t buffer[4096]; static wchar_t buffer[4096];
va_list arglist; va_list arglist;
va_start(arglist, format); 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); //std::string converted_str = converter.to_bytes(string_to_convert);
m_mq.Post(std::string(converted)); m_mq.Post(std::string(converted));
auto line = converted + "\n";
if (m_logfile.is_open()) if (m_logfile.is_open())
{ {
auto line = converted + "\n";
m_logfile.write(line.data(), line.size()); m_logfile.write(line.data(), line.size());
m_logfile.flush(); m_logfile.flush();
} }
else
{
m_tmp.push_back(line);
}
#if _WIN32 #if _WIN32
auto line = L"DBG: " + std::wstring(buffer, n) + L"\n"; auto line_console = L"DBG: " + std::wstring(buffer, n) + L"\n";
OutputDebugStringW(line.c_str()); OutputDebugStringW(line_console.c_str());
#endif #endif
} }
LogRemote::~LogRemote() LogRemote::~LogRemote()

View File

@@ -17,7 +17,10 @@ public:
bool m_running = false; bool m_running = false;
bool m_error = false; bool m_error = false;
std::thread m_thread; std::thread m_thread;
std::mutex m_mutex;
BlockingQueue<std::string> m_mq; BlockingQueue<std::string> m_mq;
// Store messages until the file is open
std::vector<std::string> m_tmp;
CURL *curl = nullptr; CURL *curl = nullptr;
CURLcode res; CURLcode res;
std::string readBuffer; std::string readBuffer;