Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Made a config subscriber to be used in gravityNode
  • Loading branch information
Annie Smith committed Jul 7, 2023
commit c3b2c4d28dd8192f626f71724af2e50d65f97321
77 changes: 3 additions & 74 deletions src/api/cpp/GravityNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
#include "protobuf/ServiceDirectoryUnregistrationPB.pb.h"
#include "protobuf/ServiceDirectoryBroadcastPB.pb.h"
#include "protobuf/GravityConfigParamPB.pb.h"
#include "protobuf/GravitySpdLogConfigPB.pb.h"

#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/dist_sink.h"
Expand Down Expand Up @@ -642,6 +641,9 @@ void GravityNode::configSpdLoggers()
// Set the ApplicationLogger as the default
spdlog::set_default_logger(app_logger);
}

// Set up the subscriber for any reconfiguration messages
this->subscribe("GravitySpdLogConfig", slcs)
}

GravityReturnCode GravityNode::init()
Expand Down Expand Up @@ -2500,15 +2502,6 @@ GravityReturnCode GravityNode::unregisterHeartbeatListener(string componentID, s
return GravityReturnCodes::SUCCESS;
}


GravityReturnCode GravityNode::registerSpdLogConfigListener()
{
// Subscribe to the spdlogconfig publisher
this-> subscribe();

// Listener thread ?
}

GravityReturnCode GravityNode::registerRelay(string dataProductID, const GravitySubscriber& subscriber, bool localOnly, GravityTransportType transportType)
{
return registerRelay(dataProductID, subscriber, localOnly, transportType, false);
Expand Down Expand Up @@ -2797,70 +2790,6 @@ bool GravityNode::getBoolParam(std::string key, bool default_value) {
}
}

bool GravityNode::reconfigSpdLoggers(GravitySpdLogConfigPB spdLogConfigPB)
{
bool isCompID = false;
// Check if it is one of the specified components
if (spdLogConfigPB.has_componentID())
{
for (auto compID : spdLogConfigPB.componentID())
{
if (compID == componentID)
{
isCompID = true;
break;
}
}
}
// All components subscribed should be updated
else{
isCompID = true;
}

if (isCompID){

// Get the gravity and application loggers
auto gravityLogger = spdlog::get("GravityLogger");
auto gravityApplicationLogger = spdlog::get("GravityApplicationLogger");

string loggerID = {"console","file", "network"};

if (spdLogConfigPB.has_spdlog_component());
{
for(auto logConfig : spdLogConfigPB.spdlog_component());
{
// Choose logger
auto logger;
// Set max number of loggers (2 for gravity, 3 for app)
int max = 2;
if (logConfig.isAppLogger())
{
logger = gravityApplicationLogger;
max = 3;
}
else
{
logger = gravityLogger;
}

// Choose sink and set level
for (int i = 0 ; i < max; i++)
{
if(loggerID[i] == logConfig.loggerID())
{
logger->sink()[i] -> set_level(logConfig.log_level());
break;
}
}

}
}
}


return false;
}

std::string GravityNode::getComponentID()
{
return componentID;
Expand Down
3 changes: 3 additions & 0 deletions src/api/cpp/GravityNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "GravitySemaphore.h"
#include "GravityServiceProvider.h"
#include "GravitySubscriptionMonitor.h"
#include "GravitySpdLogConfigSubscriber.h"
#include "Utility.h"
#include "CommUtil.h"
//#include "PublishSink.h"
Expand Down Expand Up @@ -211,6 +212,8 @@ class GravityNode

bool defaultCacheLastSentDataprodut;
bool defaultReceiveLastSentDataproduct;

SpdLogConfigSubscriber slcs;

std::thread subscriptionManagerThread;

Expand Down
84 changes: 84 additions & 0 deletions src/api/cpp/GravitySpdLogConfigSubscriber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "GravitySpdLogConfigSubscriber.h"

#include "spdlog/spdlog.h"

#include "protobuf/GravitySpdLogConfigPB.pb.h"


namespace gravity {
//init function setting the componentID
void SpdLogConfigSubscriber::subscriptionFilled(const std::vector< std::shared_ptr<GravityDataProduct> >& dataProducts)
{
for(std::vector< std::shared_ptr<GravityDataProduct> >::const_iterator i = dataProducts.begin(); i != dataProducts.end(); i++)
{
//Get the protobuf object from the message
GravitySpdLogConfigPB spdLogConfigPB;
(*i)->populateMessage(spdLogConfigPB);
// Reconfigure the loggers based on this
reconfigSpdLoggers(spdLogConfigPB);
}
}

// Determines whether or not to reconfigure this componenet based on the component ID
void SpdLogConfigSubscriber::reconfigSpdLoggers(GravitySpdLogConfigPB spdLogConfigPB)
{
bool isCompID = false;
// Check if it is one of the specified components
if (spdLogConfigPB.has_componentID())
{
for (auto compID : spdLogConfigPB.componentID())
{
if (compID == componentID)
{
isCompID = true;
break;
}
}
}
// All components subscribed should be updated
else
{
isCompID = true;
}

// Update valid component
if (isCompID){
// Get the gravity and application loggers
auto gravityLogger = spdlog::get("GravityLogger");
auto gravityApplicationLogger = spdlog::get("GravityApplicationLogger");

string loggerID = {"console","file", "network"};

if (spdLogConfigPB.has_spdlog_component());
{
for(SpdLogComponentInfoPB logConfig : spdLogConfigPB.spdlog_component());
{
// Choose logger and max number
// of loggers (2 for gravity, 3 for app)
auto logger;
int max;
if (logConfig.isAppLogger())
{
logger = gravityApplicationLogger;
max = 3;
}
else
{
logger = gravityLogger;
max = 2;
}

// Choose sink and set level
for (int i = 0 ; i < max; i++)
{
if(loggerID[i] == logConfig.loggerID())
{
logger->sink()[i] -> set_level(logConfig.log_level());
break;
}
}
}
}
}
}
}
18 changes: 18 additions & 0 deletions src/api/cpp/GravitySpdLogConfigSubscriber.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <GravitySubscriber.h>
#include <Utility.h>

#include "protobuf/GravitySpdLogConfigPB.pb.h"


namespace gravity {
//Declare class for receiving published messages about the log.
class SpdLogConfigSubscriber: public GravitySubscriber
{
private:
string componentID;
void reconfigSpdLoggers(GravitySpdLogConfigPB spdLogConfigPB);
public:
void subscriptionFilled(const std::vector< std::shared_ptr<GravityDataProduct> >& dataProducts);
};
}