Skip to content

Commit

Permalink
listener-observer implementation for config changes
Browse files Browse the repository at this point in the history
logger listens to minseverity updates
  • Loading branch information
Alex Epifanov committed Dec 24, 2017
1 parent bbf72b2 commit 99f650e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
13 changes: 12 additions & 1 deletion platform/shared/common/RhoConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,16 @@ bool RhoSettings::getBool(const char* szName){

void RhoSettings::setString(const char* szName, const String& str, boolean bSaveToFile){
m_mapValues[szName] = str;
notifyListeners( szName, str );

if ( bSaveToFile )
saveToFile(szName);
}

void RhoSettings::setInt(const char* szName, int nVal, boolean bSaveToFile){
m_mapValues[szName] = common::convertToStringA(nVal);
const String sVal = common::convertToStringA(nVal);
m_mapValues[szName] = sVal;
notifyListeners( szName, sVal );

if ( bSaveToFile )
saveToFile(szName);
Expand All @@ -263,6 +266,14 @@ void RhoSettings::removeProperty(const char* szName, boolean bSaveToFile)
saveToFile( szName, true );
}

void RhoSettings::notifyListeners( const String& name, const String& newVal )
{
for ( int i = 0; i < m_listeners.size(); ++i )
{
m_listeners[i]->onSettingUpdated( name, newVal );
}
}

}
}

Expand Down
15 changes: 15 additions & 0 deletions platform/shared/common/RhoConf.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ namespace common{
#define RHOVERSION "Version"
#define RHODBVERSION "DBVersion"

class IRhoSettingsListener {
public:
virtual void onSettingUpdated( const String& name, const String& newVal ) = 0;
};

class RhoSettings{

Vector<IRhoSettingsListener*> m_listeners;

String m_strConfFilePath, m_strAppConfFilePath, m_strAppConfUserPath;
Hashtable<String,String> m_mapValues;
Hashtable<String,String> m_mapChangedValues;
Expand Down Expand Up @@ -70,6 +78,10 @@ class RhoSettings{

HashtablePtr<String,Vector<String>* >& getConflicts(){ return m_mapConflictedValues;}
void conflictsResolved();

void addListener( IRhoSettingsListener* l ) { m_listeners.push_back(l); }
void removeListener( IRhoSettingsListener* l ) { m_listeners.removeElement(l); }

protected:
void saveChangesToString(String& strData);
void loadFromString(const char* szSettings, Hashtable<String,String>& mapValues);
Expand All @@ -80,6 +92,9 @@ class RhoSettings{
void saveToFile(const char* szName, boolean bRemove = false );
void readChanges();
void checkConflicts();

private:
void notifyListeners( const String& name, const String& newVal );
};

extern RhoSettings g_RhoSettings;
Expand Down
9 changes: 9 additions & 0 deletions platform/shared/logging/RhoLogConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ void LogSettings::loadFromConf(rho::common::RhoSettings& oRhoConf)
int milliseconds = oRhoConf.getInt("LogMemPeriod");
setCollectMemoryInfoInterval(milliseconds);
}

oRhoConf.addListener( this );

m_bIsInitialized = true;
}

Expand Down Expand Up @@ -387,6 +390,12 @@ void LogSettings::internalSinkLogMessage( String& strMsg ){
}
}

void LogSettings::onSettingUpdated( const String& name, const String& newVal ) {
if ( strcasecmp(name.c_str(),"minseverity") == 0 ) {
setMinSeverity( atoi(newVal.c_str()) );
}
}

bool LogSettings::isCategoryEnabled(const LogCategory& cat)const{
//TODO: Optimize categories search : add map
common::CMutexLock oLock(m_CatLock);
Expand Down
6 changes: 4 additions & 2 deletions platform/shared/logging/RhoLogConf.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
#include "common/RhoMutexLock.h"
#include "common/RhoTime.h"
#include "common/RhoThread.h"
#include "common/RhoConf.h"

namespace rho {

namespace common{ class RhoSettings; }
class LogCategory;

struct ILogSink{
Expand All @@ -54,7 +54,7 @@ class IMemoryInfoCollector
virtual String collect() = 0;
};

class LogSettings{
class LogSettings : public common::IRhoSettingsListener {

class MemoryInfoCollectorThread : public common::CRhoThread
{
Expand Down Expand Up @@ -183,6 +183,8 @@ class LogSettings{

private:
void internalSinkLogMessage( String& strMsg );
virtual void onSettingUpdated( const String& name, const String& newVal );

};

extern LogSettings g_LogSettings;
Expand Down

0 comments on commit 99f650e

Please sign in to comment.