Skip to content

Commit

Permalink
[Add] Enable/Disable LogLevel/LogChannel for all streams
Browse files Browse the repository at this point in the history
  • Loading branch information
sqex-rmaxime committed Mar 31, 2024
1 parent f485ab6 commit 34dd27e
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Include/SA/Logger/Log/LogChannelFilter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ namespace SA
* \param[in] _offset Current offset to check (internal use only).
*/
bool IsChannelEnabled(const std::wstring& _chanName, size_t _offset = 0u) const;

/**
* Enable/Disable a channel.
* Only the channel itself is enabled/disabled (not the parent channels).
*
* \param[in] _chanName Channel name to enable/disable.
* \param[in] _bEnabled Whether to enable or disable the channel.
*/
void SetChannelEnabled(const std::wstring& _chanName, bool _bEnabled);
};
}

Expand Down
35 changes: 34 additions & 1 deletion Include/SA/Logger/LoggerBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,53 @@ namespace SA
template <typename StreamT>
bool DestroyStream(StreamT& _stream, bool _bFlush = true);


/**
* Destroy all streams.
*
* \param _bFlush Should flush stream before destroy.
*/
void ClearStreams(bool _bFlush = true);
virtual void ClearStreams(bool _bFlush = true);


/**
* \brief Force logger to flush all streams.
*/
virtual void Flush();


/**
* \brief \e Enable \b LogLevel for all registered streams.
*
* \param[in] _level Level to enable.
*/
virtual void EnableLogLevel(LogLevel _level);

/**
* \brief \e Disable \b LogLevel for all registered streams.
*
* \param[in] _level Level to disable.
*/
virtual void DisableLogLevel(LogLevel _level);


/**
* \brief \e Enable \b LogChannel for all registered streams.
*
* \param[in] _channel Channel to enable.
*/
virtual void EnableLogChannel(const std::wstring& _channel);

/**
* \brief \e Disable \b LogChannel for all registered streams.
*
* \param[in] _channel Channel to disable.
*/
virtual void DisableLogChannel(const std::wstring& _channel);

//}


//{ Frame Num

/// Increment current registered frame number.
Expand Down
17 changes: 17 additions & 0 deletions Include/SA/Logger/LoggerThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,26 @@ namespace SA

void Log(SA::Log _log) override final;

//{ Streams

void ClearStreams(bool _bFlush = true) override final;


void Flush() override final;


void EnableLogLevel(LogLevel _level) override final;

void DisableLogLevel(LogLevel _level) override final;


void EnableLogChannel(const std::wstring& _channel) override final;

void DisableLogChannel(const std::wstring& _channel) override final;

//}


//{ Frame Num

/// Increment current registered frame number.
Expand Down
5 changes: 5 additions & 0 deletions Source/SA/Logger/Log/LogChannelFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ namespace SA

return IsSingleChannelEnabled(_chanName.substr(0u, fIndex)) && IsChannelEnabled(_chanName, fIndex + 1);
}

void LogChannelFilter::SetChannelEnabled(const std::wstring& _chanName, bool _bEnabled)
{
mChannels[_chanName] = _bEnabled;
}
}
29 changes: 29 additions & 0 deletions Source/SA/Logger/LoggerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace SA
(*it)->ProcessLog(_log, _bForce);
}


void LoggerBase::RegisterStream(ALogStream* _stream)
{
mStreams.push_back(_stream);
Expand All @@ -41,6 +42,7 @@ namespace SA
return false;
}


void LoggerBase::ClearStreams(bool _bFlush)
{
for (auto it = mStreams.begin(); it != mStreams.end(); ++it)
Expand All @@ -54,11 +56,38 @@ namespace SA
mStreams.clear();
}


void LoggerBase::Flush()
{
for (auto it = mStreams.begin(); it != mStreams.end(); ++it)
(*it)->Flush();
}


void LoggerBase::EnableLogLevel(LogLevel _level)
{
for (auto it = mStreams.begin(); it != mStreams.end(); ++it)
(*it)->levelFlags |= _level;
}

void LoggerBase::DisableLogLevel(LogLevel _level)
{
for (auto it = mStreams.begin(); it != mStreams.end(); ++it)
(*it)->levelFlags &= ~_level;
}


void LoggerBase::EnableLogChannel(const std::wstring& _channel)
{
for (auto it = mStreams.begin(); it != mStreams.end(); ++it)
(*it)->channelFilter.SetChannelEnabled(_channel, true);
}

void LoggerBase::DisableLogChannel(const std::wstring& _channel)
{
for (auto it = mStreams.begin(); it != mStreams.end(); ++it)
(*it)->channelFilter.SetChannelEnabled(_channel, false);
}

//}
}
41 changes: 41 additions & 0 deletions Source/SA/Logger/LoggerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace SA
LoggerBase::ProcessLog(_log, _bForce);
}


void LoggerThread::RegisterStream(ALogStream* _stream)
{
std::lock_guard lk(mStreamMutex);
Expand All @@ -63,6 +64,15 @@ namespace SA
return LoggerBase::UnregisterStream(_stream);
}


void LoggerThread::ClearStreams(bool _bFlush)
{
std::lock_guard lk(mStreamMutex);

LoggerBase::ClearStreams(_bFlush);
}


void LoggerThread::Flush()
{
while(!mRingBuffer.IsEmpty())
Expand All @@ -74,8 +84,39 @@ namespace SA
LoggerBase::Flush();
}


void LoggerThread::EnableLogLevel(LogLevel _level)
{
std::lock_guard lk(mStreamMutex);

LoggerBase::EnableLogLevel(_level);
}

void LoggerThread::DisableLogLevel(LogLevel _level)
{
std::lock_guard lk(mStreamMutex);

LoggerBase::DisableLogLevel(_level);
}


void LoggerThread::EnableLogChannel(const std::wstring& _channel)
{
std::lock_guard lk(mStreamMutex);

LoggerBase::EnableLogChannel(_channel);
}

void LoggerThread::DisableLogChannel(const std::wstring& _channel)
{
std::lock_guard lk(mStreamMutex);

LoggerBase::DisableLogChannel(_channel);
}

//}


//{ Frame Num

/// Increment current registered frame number.
Expand Down

0 comments on commit 34dd27e

Please sign in to comment.