Skip to content

Commit 935ae03

Browse files
committed
set mutex for logging manager
1 parent a579f19 commit 935ae03

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

source/QSLogLib/LoggingManager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LoggingManager::LoggingManager()
1919
{
2020
mIsDisabled = false;
2121
}
22+
2223
LoggingManager::~LoggingManager()
2324
{
2425
// Delete all LoggingDevices.
@@ -158,7 +159,11 @@ void LoggingManager::WriteMessage(const std::string& fileName,
158159
AbstractLoggingDevice* _device = *iter;
159160
if(_device->IsEnabled())
160161
{
162+
mMutex.lock(); // lock the mutex
163+
161164
_device->WriteMessage(_message);
165+
166+
mMutex.unlock(); // unlock the mutex
162167
}
163168
}
164169

source/QSLogLib/LoggingManager.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#ifndef _SLOGLIB_LOGGINGMANAGER_H_
1111
#define _SLOGLIB_LOGGINGMANAGER_H_
1212

13-
// #include "SLogLib/Config.h"
13+
#include <QMutex>
14+
1415
#include "QSLogLib/AddToCallStack.h"
1516
#include "QSLogLib/Devices/AbstractLoggingDevice.h"
1617

@@ -37,8 +38,7 @@ class LoggingManager
3738
static LoggingManager _singleton;
3839
return _singleton;
3940
}
40-
// TODO: If it uses singleton value, how about thread-safe mechanism?
41-
41+
4242
// Add a new logging device. The device is owned by the logging manager and is automatically
4343
// deleted when LoggingManager is destructed.
4444
void AddDevice(AbstractLoggingDevice* device);
@@ -93,6 +93,8 @@ class LoggingManager
9393

9494
// If true then disable logging.
9595
bool mIsDisabled;
96+
97+
QMutex mMutex;
9698
};
9799

98100
}; // End namespace SLogLib.

0 commit comments

Comments
 (0)