Skip to content

Commit

Permalink
支持Log Appender运行时设置
Browse files Browse the repository at this point in the history
  • Loading branch information
lailongwei committed Sep 3, 2024
1 parent b983a70 commit c8b2fa3
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 34 deletions.
9 changes: 8 additions & 1 deletion llbc/include/llbc/core/log/BaseLogAppender.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ class LLBC_BaseLogAppender : public LLBC_ILogAppender
*/
virtual int GetLogLevel() const;

/**
* Set log level.
* @param[in] logLevel - the log level, End means disable appender output.
* @return int - return 0 if success, otherwise return -1.
*/
virtual int SetLogLevel(int logLevel);

/**
* Get current appender's token chain.
* @return LLBC_LogTokenChain * - the log token chain.
Expand Down Expand Up @@ -85,7 +92,7 @@ class LLBC_BaseLogAppender : public LLBC_ILogAppender
virtual void Flush();

private:
int _level;
volatile int _logLevel;
LLBC_LogTokenChain *_chain;
LLBC_ILogAppender *_next;

Expand Down
17 changes: 16 additions & 1 deletion llbc/include/llbc/core/log/BaseLogAppenderInl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,26 @@

#pragma once

#include "llbc/core/log/LogLevel.h"

__LLBC_NS_BEGIN

inline int LLBC_BaseLogAppender::GetLogLevel() const
{
return _level;
return _logLevel;
}

inline int LLBC_BaseLogAppender::SetLogLevel(int logLevel)
{
if (!LLBC_LogLevel::IsValid(logLevel) && logLevel != LLBC_LogLevel::End)
{
LLBC_SetLastError(LLBC_ERROR_INVALID);
return LLBC_FAILED;
}

_logLevel = logLevel;

return LLBC_OK;
}

inline LLBC_LogTokenChain *LLBC_BaseLogAppender::GetTokenChain() const
Expand Down
15 changes: 14 additions & 1 deletion llbc/include/llbc/core/log/ILogAppender.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,20 @@ class LLBC_LogAppenderType

End
};

/**
* Check givin appender type is valid or not.
* @return bool - reutrn true if valid, otherwise return false.
*/
static constexpr bool IsValid(int appenderType) { return appenderType >= Begin && appenderType < End; }
};

/**
* The log apender init information structure encapsulation.
*/
struct LLBC_LogAppenderInitInfo
{
int level; // log level.
int logLevel; // log level.
LLBC_String pattern; // output pattern.
bool colourfulOutput; // colourful output flag.

Expand Down Expand Up @@ -120,6 +126,13 @@ class LLBC_ILogAppender
*/
virtual int GetLogLevel() const = 0;

/**
* Set log level.
* @param[in] logLevel - the log level, End means disable appender output.
* @return int - return 0 if success, otherwise return -1.
*/
virtual int SetLogLevel(int logLevel) = 0;

/**
* Get current appender's token chain.
* @return LLBC_LogTokenChain * - the log token chain.
Expand Down
5 changes: 3 additions & 2 deletions llbc/include/llbc/core/log/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ class LLBC_EXPORT LLBC_Logger

/**
* @brief Set log level.
* @param[in] logLevel - the log level(End will disabled logger).
* @param[in] appenderType - the log appender type.
* @param[in] logLevel - the log level(End will disabled logger).
* @return int - return 0 if success, otherwise return -1.
*/
int SetLogLevel(int logLevel);
int SetAppenderLogLevel(int appenderType, int logLevel);

/**
* Get logger take over option, only available on root logger.
Expand Down
13 changes: 0 additions & 13 deletions llbc/include/llbc/core/log/LoggerInl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ LLBC_FORCE_INLINE int LLBC_Logger::GetLogLevel() const
return _logLevel;
}

LLBC_FORCE_INLINE int LLBC_Logger::SetLogLevel(int logLevel)
{
if (!LLBC_LogLevel::IsValid(logLevel) && logLevel != LLBC_LogLevel::End)
{
LLBC_SetLastError(LLBC_ERROR_INVALID);
return LLBC_FAILED;
}

_logLevel = logLevel;

return LLBC_OK;
}

LLBC_FORCE_INLINE bool LLBC_Logger::IsAddTimestampInJsonLog() const
{
return _addTimestampInJsonLog;
Expand Down
6 changes: 3 additions & 3 deletions llbc/src/core/log/BaseLogAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
__LLBC_NS_BEGIN

LLBC_BaseLogAppender::LLBC_BaseLogAppender()
: _level(LLBC_LogLevel::End)
: _logLevel(LLBC_LogLevel::End)

, _chain(nullptr)
, _next(nullptr)
Expand All @@ -48,7 +48,7 @@ int LLBC_BaseLogAppender::Initialize(const LLBC_LogAppenderInitInfo &initInfo)
return LLBC_FAILED;
}

_level = initInfo.level;
_logLevel = initInfo.logLevel;

_chain = new LLBC_LogTokenChain;
if (_chain->Build(initInfo.pattern) != LLBC_OK)
Expand All @@ -63,7 +63,7 @@ int LLBC_BaseLogAppender::Initialize(const LLBC_LogAppenderInitInfo &initInfo)
void LLBC_BaseLogAppender::Finalize()
{
LLBC_XDelete(_chain);
_level = LLBC_LogLevel::End;
_logLevel = LLBC_LogLevel::End;
}

__LLBC_NS_END
53 changes: 51 additions & 2 deletions llbc/src/core/log/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ LLBC_Logger::LLBC_Logger()
, _flushInterval(LLBC_CFG_LOG_DEFAULT_LOG_FLUSH_INTERVAL)
, _appenders(nullptr)

, _objPool(true)
, _logDataTypedObjPool(*_objPool.GetTypedObjPool<LLBC_LogData>())
, _hookDelegs()
{
Expand Down Expand Up @@ -101,7 +102,7 @@ int LLBC_Logger::Initialize(const LLBC_LoggerConfigInfo *config, LLBC_LogRunnabl
if (_config->IsLogToConsole())
{
LLBC_LogAppenderInitInfo appenderInitInfo;
appenderInitInfo.level = _config->GetConsoleLogLevel();
appenderInitInfo.logLevel = _config->GetConsoleLogLevel();
appenderInitInfo.pattern = _config->GetConsolePattern();
appenderInitInfo.colourfulOutput = _config->IsColourfulOutput();

Expand All @@ -122,7 +123,7 @@ int LLBC_Logger::Initialize(const LLBC_LoggerConfigInfo *config, LLBC_LogRunnabl
if (_config->IsLogToFile())
{
LLBC_LogAppenderInitInfo appenderInitInfo;
appenderInitInfo.level = _config->GetFileLogLevel();
appenderInitInfo.logLevel = _config->GetFileLogLevel();
appenderInitInfo.pattern = _config->GetFilePattern();
appenderInitInfo.filePath = _config->GetLogFile();
appenderInitInfo.fileSuffix = _config->GetLogFileSuffix();
Expand Down Expand Up @@ -196,6 +197,54 @@ void LLBC_Logger::Finalize()
ClearNonRunnableMembers(false);
}

int LLBC_Logger::SetAppenderLogLevel(int appenderType, int logLevel)
{
// Argument check.
if (!LLBC_LogAppenderType::IsValid(appenderType))
{
LLBC_SetLastError(LLBC_ERROR_INVALID);
return LLBC_FAILED;
}
if (!LLBC_LogLevel::IsValid(logLevel) && logLevel != LLBC_LogLevel::End)
{
LLBC_SetLastError(LLBC_ERROR_INVALID);
return LLBC_FAILED;
}

// Logger inited check.
LLBC_LockGuard guard(_lock);
if (!IsInit())
{
LLBC_SetLastError(LLBC_ERROR_NOT_INIT);
return LLBC_FAILED;
}

// Find appender.
LLBC_ILogAppender *appender = _appenders;
while (appender && appender->GetType() != appenderType)
appender = appender->GetAppenderNext();

if (!appender)
{
LLBC_SetLastError(LLBC_ERROR_NOT_FOUND);
return LLBC_FAILED;
}

// Set appender log level.
appender->SetLogLevel(logLevel);

// Update logger log level.
_logLevel = LLBC_LogLevel::End;
appender = _appenders;
while (appender)
{
_logLevel = MIN(appender->GetLogLevel(), _logLevel);
appender = appender->GetAppenderNext();
}

return LLBC_OK;
}

bool LLBC_Logger::IsTakeOver() const
{
LLBC_LockGuard guard(_lock);
Expand Down
41 changes: 30 additions & 11 deletions testsuite/core/log/TestCase_Core_Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "core/log/TestCase_Core_Log.h"
#include <iomanip>

#include "llbc/core/log/ILogAppender.h"

TestCase_Core_Log::TestCase_Core_Log()
{
}
Expand Down Expand Up @@ -177,11 +179,14 @@ void TestCase_Core_Log::DoLogLevelSetTest()
testLogger->GetLoggerName().c_str(),
LLBC_LogLevel::GetLevelStr(testLogger->GetLogLevel()).c_str());

auto testOutput = [testLogger](int logLevel) {
testLogger->SetLogLevel(logLevel);
LLBC_PrintLn(">>>>>>>> logger: %s, level: %s <<<<<<<<<",
auto testOutput = [testLogger](int appenderType, int logLevel) {
const auto ret = testLogger->SetAppenderLogLevel(appenderType, logLevel);
LLBC_PrintLn(">>>>>>>> logger: %s, appender:%d, level: %s, ret:%d, error:%s <<<<<<<<<",
testLogger->GetLoggerName().c_str(),
LLBC_LogLevel::GetLevelStr(testLogger->GetLogLevel()).c_str());
appenderType,
LLBC_LogLevel::GetLevelStr(testLogger->GetLogLevel()).c_str(),
ret,
ret == LLBC_OK ? "Success" : LLBC_FormatLastError());
LLOG_DEBUG2("log_level_set_test", "This is a DEBUG log");
LLOG_TRACE2("log_level_set_test", "This is a TRACE log");
LLOG_INFO2("log_level_set_test", "This is a INFO log");
Expand All @@ -191,13 +196,27 @@ void TestCase_Core_Log::DoLogLevelSetTest()
LLBC_PrintLn(">>>>>>>> ****************** <<<<<<<<<");
};

testOutput(LLBC_LogLevel::Debug);
testOutput(LLBC_LogLevel::Trace);
testOutput(LLBC_LogLevel::Info);
testOutput(LLBC_LogLevel::Warn);
testOutput(LLBC_LogLevel::Error);
testOutput(LLBC_LogLevel::Fatal);
testOutput(LLBC_LogLevel::End);
testOutput(LLBC_LogAppenderType::Console, LLBC_LogLevel::Debug);
testOutput(LLBC_LogAppenderType::File, LLBC_LogLevel::Debug);
testOutput(LLBC_LogAppenderType::Network, LLBC_LogLevel::Debug);

testOutput(LLBC_LogAppenderType::Console, LLBC_LogLevel::Trace);
testOutput(LLBC_LogAppenderType::File, LLBC_LogLevel::Trace);

testOutput(LLBC_LogAppenderType::Console, LLBC_LogLevel::Info);
testOutput(LLBC_LogAppenderType::File, LLBC_LogLevel::Info);

testOutput(LLBC_LogAppenderType::Console, LLBC_LogLevel::Warn);
testOutput(LLBC_LogAppenderType::File, LLBC_LogLevel::Warn);

testOutput(LLBC_LogAppenderType::Console, LLBC_LogLevel::Error);
testOutput(LLBC_LogAppenderType::File, LLBC_LogLevel::Error);

testOutput(LLBC_LogAppenderType::Console, LLBC_LogLevel::Fatal);
testOutput(LLBC_LogAppenderType::File, LLBC_LogLevel::Fatal);

testOutput(LLBC_LogAppenderType::Console, LLBC_LogLevel::End);
testOutput(LLBC_LogAppenderType::File, LLBC_LogLevel::End);
}

void TestCase_Core_Log::DoJsonLogTest()
Expand Down

0 comments on commit c8b2fa3

Please sign in to comment.