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
initial changes for file creation
  • Loading branch information
Annie Smith committed Jul 17, 2023
commit 8ba6fe16507a5cab21a8c34685fec96796c2607b
30 changes: 18 additions & 12 deletions src/api/cpp/GravityNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <zmq.h>
#include <iostream>
#include <stdio.h>
#include <thread>
#include <assert.h>
#ifdef WIN32
Expand Down Expand Up @@ -594,7 +595,6 @@ void GravityNode::configSpdLoggers()
console_proxy_for_app->set_level(app_console_level);
app_sink_list.push_back(console_proxy_for_app);


string filename = getStringParam("LogDirectory", ".") + file_separator + componentID + ".log";
auto sharedFileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename);

Expand Down Expand Up @@ -638,6 +638,7 @@ void GravityNode::configSpdLoggers()

// Set the ApplicationLogger as the default
spdlog::set_default_logger(app_logger);
std::cout<< " DONE SETTING LOGGERS \n";
}

GravityReturnCode GravityNode::init()
Expand Down Expand Up @@ -1000,6 +1001,17 @@ GravityReturnCode GravityNode::init(std::string componentID)
logger->warn("Gravity.ini specifies both Domain and URL. Using URL.");
}

std::cout<<"About to configure subscriber for " << componentID;
if(componentID != "ServiceDirectory")
{
// Register subscriber to allow for dynamic logging changes
GravityReturnCode ret = registerSpdlogDynamicConfiguration();
if (ret!=GravityReturnCodes::SUCCESS)
{
logger->error(" {}'s Dynamic SpdLog Subscriber not registered ( code: {})", componentID, ret);
}
}

initLock.Unlock();

return ret;
Expand Down Expand Up @@ -1593,7 +1605,6 @@ GravityReturnCode GravityNode::ServiceDirectoryDataProductLookup(std::string dat

// Send request to service directory
GravityReturnCode ret = sendRequestToServiceDirectory(request, response);

if (ret == GravityReturnCodes::SUCCESS)
{
ComponentDataLookupResponsePB pb;
Expand Down Expand Up @@ -1669,7 +1680,7 @@ GravityReturnCode GravityNode::subscribeInternal(string dataProductID, const Gra

GravityReturnCode ret;
ret = ServiceDirectoryDataProductLookup(dataProductID, publisherInfoPBs, domain);
if(ret != GravityReturnCodes::SUCCESS) {
if(ret != GravityReturnCodes::SUCCESS) {
return ret;
}

Expand Down Expand Up @@ -2495,18 +2506,13 @@ GravityReturnCode GravityNode::unregisterHeartbeatListener(string componentID, s
return GravityReturnCodes::SUCCESS;
}

GravityReturnCode GravityNode::registerSpdlogConfiguration()
GravityReturnCode GravityNode::registerSpdlogDynamicConfiguration()
{
// Set up the subscriber for any reconfiguration messages
const std::string dataProductID = "GravitySpdLogConfig";
spdLogConfigSub.init(componentID);
GravityReturnCode ret = GravityReturnCodes::SUCCESS;
ret = this->subscribe(dataProductID, spdLogConfigSub);
if (ret!=GravityReturnCodes::SUCCESS)
{
spdlog::error("Unsuccessfully registered data product {}", ret);
}
return ret;
spdLogConfigSub.init(componentID, getStringParam("LogDirectory", ".") + file_separator + componentID + ".log");
sleep(700); // wait to see if domain changing
return this->subscribe(dataProductID, spdLogConfigSub);
}

GravityReturnCode GravityNode::registerRelay(string dataProductID, const GravitySubscriber& subscriber, bool localOnly, GravityTransportType transportType)
Expand Down
7 changes: 3 additions & 4 deletions src/api/cpp/GravityNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,11 @@ class GravityNode
GRAVITY_API GravityReturnCode unregisterHeartbeatListener(std::string componentID, std::string domain = "");


/**
* Sets up the ability to reconfigure spdlog levels
* potentially add in params??
/**
* Unregisters a callback for when we get a heartbeat from another component.
* \return success flag
*/
GRAVITY_API GravityReturnCode registerSpdlogConfiguration();
GRAVITY_API GravityReturnCode registerSpdlogDynamicConfiguration();

/**
* Register a Relay that will act as a pass-through for the given dataProductID. It will be a publisher and subscriber
Expand Down
28 changes: 27 additions & 1 deletion src/api/cpp/GravitySpdLogConfigSubscriber.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include <memory>
#include <iostream>
#include <stdio.h>
#include "GravitySpdLogConfigSubscriber.h"
#include "spdlog/spdlog.h"
#include "protobuf/GravitySpdLogConfigPB.pb.h"


namespace gravity {
SpdLogConfigSubscriber::SpdLogConfigSubscriber(){}
void SpdLogConfigSubscriber::init(std::string compID)
void SpdLogConfigSubscriber::init(std::string compID,std::string fname)
{
componentID = compID;
filename = fname;
}
void SpdLogConfigSubscriber::subscriptionFilled(const std::vector< std::shared_ptr<GravityDataProduct> >& dataProducts)
{
Expand All @@ -23,6 +25,27 @@ namespace gravity {
}
}

/*
void checkFile(GravitySpdLogConfigPB_LoggerType lt,GravitySpdLogConfigPB_LoggerLevel ll,std::string filename)
{
bool fileLogger = (lt == GravitySpdLogConfigPB_LoggerType_GravityFileLogger)
|| (lt == GravitySpdLogConfigPB_LoggerType_ApplicationFileLogger);
bool levelOff = ll == GravitySpdLogConfigPB_LoggerLevel_off;
if(fileLogger && !levelOff)
{
FILE *pfile = fopen(filename.c_str(),"a");
if (pfile==NULL)
{
spdlog::error("Could not open/create file");
}
else
{
fclose(pfile);
}
}
}
*/

// Determines if correct ID and reconfigures accordingly
void SpdLogConfigSubscriber::reconfigSpdLoggers(GravitySpdLogConfigPB spdLogConfigPB)
{
Expand All @@ -32,6 +55,9 @@ namespace gravity {
return;
}

// Create a file if necessary
//checkFile(spdLogConfigPB.logger_id(),spdLogConfigPB.logger_level(), filename);

if (spdLogConfigPB.logger_id() == GravitySpdLogConfigPB_LoggerType_GravityConsoleLogger || spdLogConfigPB.logger_id() == GravitySpdLogConfigPB_LoggerType_GravityFileLogger)
{
auto log = spdlog::get("GravityLogger");
Expand Down
3 changes: 2 additions & 1 deletion src/api/cpp/GravitySpdLogConfigSubscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace gravity {
{
private:
std::string componentID;
std::string filename;
void reconfigSpdLoggers(GravitySpdLogConfigPB spdLogConfigPB);
public:
SpdLogConfigSubscriber();
void init(std::string compID);
void init(std::string compID,std::string fname);
void subscriptionFilled(const std::vector< std::shared_ptr<GravityDataProduct> >& dataProducts);

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,15 @@ int main()
exit(1);
}

// Register the subscriber for spdlog regconfiguration
// Set up a subscriber to the Application Publisher Logger
gn.registerSpdlogConfiguration();
SimpleSubscriber simpleSubscriber;
gn.subscribe(gravity::constants::GRAVITY_LOGGER_DPID, simpleSubscriber);

std::cout<<"Begin testing GravityLogger\n";

// Change SimpleGravityComponentID1's File to trace level
// for gravity and trace level for the console
sendConfigMessage(gn, dataProductID,"SimpleGravityComponentID1", GravitySpdLogConfigPB_LoggerType_GravityFileLogger, GravitySpdLogConfigPB_LoggerLevel_trace);
sendConfigMessage(gn, dataProductID,"", GravitySpdLogConfigPB_LoggerType_GravityFileLogger, GravitySpdLogConfigPB_LoggerLevel_trace);
sendConfigMessage(gn, dataProductID,"SimpleGravityComponentID1", GravitySpdLogConfigPB_LoggerType_GravityConsoleLogger, GravitySpdLogConfigPB_LoggerLevel_trace);

logAllAppLevels(); // nothing prints
Expand Down