Skip to content

Commit

Permalink
Merge pull request #60 from nacos-group/develop
Browse files Browse the repository at this point in the history
bugfix #58
  • Loading branch information
TTTTTAAAAAKKKKEEEENNNN authored Mar 19, 2021
2 parents 5e7c4e8 + d1f61aa commit b08f0b9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
5 changes: 5 additions & 0 deletions include/factory/NacosServiceFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define NACOS_SDK_CPP_NACOSSERVICEFACTORY_H

#include "INacosServiceFactory.h"
#include "src/thread/Mutex.h"
namespace nacos{

class AppConfigManager;
Expand All @@ -18,6 +19,10 @@ class NacosServiceFactory : public INacosServiceFactory {
bool configIsSet;
bool propsIsSet;

Mutex logSysInitLock;
static volatile bool logSystemInitialized;
void initializeRuntimeLogSettings(AppConfigManager *_appConfigManager);

void checkConfig() throw(InvalidFactoryConfigException);
AppConfigManager *buildConfigManager(ObjectConfigData *objectConfigData);

Expand Down
27 changes: 25 additions & 2 deletions src/factory/NacosServiceFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
//Unlike Java, in cpp, there's no container, no spring to do the ORM job, so I have to handle it myself
namespace nacos{

volatile bool NacosServiceFactory::logSystemInitialized = false;

void buildSecurityManagerAndHttpDelegate(ObjectConfigData *objectConfigData) {
AppConfigManager *appConfigManager = objectConfigData->_appConfigManager;
if (appConfigManager->nacosAuthEnabled()) {
Expand Down Expand Up @@ -53,6 +55,24 @@ AppConfigManager *NacosServiceFactory::buildConfigManager(ObjectConfigData *obje
return appConfigManager;
}

void NacosServiceFactory::initializeRuntimeLogSettings(AppConfigManager *_appConfigManager) {
if (logSystemInitialized) {
return;
}

{
LockGuard __lockLogSystem(logSysInitLock);

if (logSystemInitialized) {
return;
}

logSystemInitialized = true;
Properties copiedProps = _appConfigManager->getAllConfig();
Logger::applyLogSettings(copiedProps);
}
}

//FIXME:Memory leak at initializing stage, e.g.:
//when a HttpDelegate is allocated in CreateConfigService, after that an EXCEPTION is thrown during the initialization of ServerListManager
//the resource for HttpDelegate is never released
Expand All @@ -63,7 +83,8 @@ NamingService *NacosServiceFactory::CreateNamingService() throw(NacosException)
objectConfigData->name = "config";
NacosString encoding = "UTF-8";

buildConfigManager(objectConfigData);
AppConfigManager *appConfigManager = buildConfigManager(objectConfigData);
initializeRuntimeLogSettings(appConfigManager);

//Create http client
IHttpCli *httpCli= new HTTPCli();
Expand Down Expand Up @@ -107,6 +128,7 @@ ConfigService *NacosServiceFactory::CreateConfigService() throw(NacosException)
objectConfigData->name = "name";

AppConfigManager *appConfigManager = buildConfigManager(objectConfigData);
initializeRuntimeLogSettings(appConfigManager);

//Create http client
IHttpCli *httpCli = NULL;
Expand Down Expand Up @@ -139,7 +161,8 @@ NamingMaintainService *NacosServiceFactory::CreateNamingMaintainService() throw(
objectConfigData->name = "config";
NacosString encoding = "UTF-8";

buildConfigManager(objectConfigData);
AppConfigManager *appConfigManager = buildConfigManager(objectConfigData);
initializeRuntimeLogSettings(appConfigManager);

//Create http client
IHttpCli *httpCli= new HTTPCli();
Expand Down
13 changes: 11 additions & 2 deletions src/log/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,18 @@ void Logger::deInit() {
void Logger::initializeLogSystem() {
Properties props;

//if we failed to read log settings
props = ConfigParserUtils::parseConfigFile(DirUtils::getCwd() + ConfigConstant::FILE_SEPARATOR + ConfigConstant::DEFAULT_CONFIG_FILE);
try {
props = ConfigParserUtils::parseConfigFile(DirUtils::getCwd() + ConfigConstant::FILE_SEPARATOR + ConfigConstant::DEFAULT_CONFIG_FILE);
} catch (IOException &e) {
//if we failed to read log settings
//use default settings as backup
}

applyLogSettings(props);
}


void Logger::applyLogSettings(Properties &props) {
if (!props.contains(PropertyKeyConst::LOG_PATH)) {
NacosString homedir = DirUtils::getHome();
Logger::setBaseDir(homedir + ConfigConstant::FILE_SEPARATOR + "nacos" + ConfigConstant::FILE_SEPARATOR + "logs");
Expand Down
19 changes: 19 additions & 0 deletions src/log/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>
#include "NacosString.h"
#include "src/thread/Mutex.h"
#include "Properties.h"

#define DETAILED_DEBUG_INFO

Expand Down Expand Up @@ -34,6 +35,23 @@ enum LOG_LEVEL {
NONE
};

/**
* Logger
* Author: Liu, Hanyu
* This piece of code is a little bit weird, I have to admit
* Actually there are 2 stages which need logging:
* 1. The nacos-client is up but no client service is created, need to log issues when creating client service object
* 2. client service is created, now need to log issue for the client service
*
* stage #1 is called the bootstrap stage
* stage #2 is called the runtime stage
*
* On stage #1, the log will be written to:
* homedir(~)/nacos/logs/nacos-sdk-cpp.log
*
* On stage #2, the path can be configured AT MOST ONCE
* The path will be implicitly configured when the first client service object is created, so if you don't specify it, the default setting(same as #1) will be used
*/
class Logger {
private:
static LOG_LEVEL _CUR_SYS_LOG_LEVEL;
Expand All @@ -51,6 +69,7 @@ class Logger {

public:

static void applyLogSettings(Properties &props);
static void setRotateTime(int64_t rotateTime);
static void setBaseDir(const NacosString &baseDir);
static void setLogLevel(LOG_LEVEL level);
Expand Down

0 comments on commit b08f0b9

Please sign in to comment.