mirror of
https://github.com/etlegacy/Update-Installer.git
synced 2024-11-22 20:01:20 +00:00
Synchronize access to the log's output stream with a mutex and document the write() method as being thread-safe.
This commit is contained in:
parent
e2d3acf850
commit
9c260bc114
2 changed files with 9 additions and 0 deletions
|
@ -24,7 +24,9 @@ Log::~Log()
|
||||||
|
|
||||||
void Log::open(const std::string& path)
|
void Log::open(const std::string& path)
|
||||||
{
|
{
|
||||||
|
m_mutex.lock();
|
||||||
m_output.open(path.c_str(),std::ios_base::out | std::ios_base::app);
|
m_output.open(path.c_str(),std::ios_base::out | std::ios_base::app);
|
||||||
|
m_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::writeToStream(std::ostream& stream, Type type, const char* text)
|
void Log::writeToStream(std::ostream& stream, Type type, const char* text)
|
||||||
|
@ -46,10 +48,12 @@ void Log::writeToStream(std::ostream& stream, Type type, const char* text)
|
||||||
|
|
||||||
void Log::write(Type type, const char* text)
|
void Log::write(Type type, const char* text)
|
||||||
{
|
{
|
||||||
|
m_mutex.lock();
|
||||||
writeToStream(std::cerr,type,text);
|
writeToStream(std::cerr,type,text);
|
||||||
if (m_output.is_open())
|
if (m_output.is_open())
|
||||||
{
|
{
|
||||||
writeToStream(m_output,type,text);
|
writeToStream(m_output,type,text);
|
||||||
}
|
}
|
||||||
|
m_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
#include "tinythread.h"
|
||||||
|
|
||||||
class Log
|
class Log
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -18,7 +20,9 @@ class Log
|
||||||
|
|
||||||
void open(const std::string& path);
|
void open(const std::string& path);
|
||||||
|
|
||||||
|
/** Write @p text to the log. This method is thread-safe. */
|
||||||
void write(Type type, const std::string& text);
|
void write(Type type, const std::string& text);
|
||||||
|
/** Write @p text to the log. This method is thread-safe. */
|
||||||
void write(Type type, const char* text);
|
void write(Type type, const char* text);
|
||||||
|
|
||||||
static Log* instance();
|
static Log* instance();
|
||||||
|
@ -26,6 +30,7 @@ class Log
|
||||||
private:
|
private:
|
||||||
static void writeToStream(std::ostream& stream, Type type, const char* text);
|
static void writeToStream(std::ostream& stream, Type type, const char* text);
|
||||||
|
|
||||||
|
tthread::mutex m_mutex;
|
||||||
std::ofstream m_output;
|
std::ofstream m_output;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue