Skip to content

Commit

Permalink
Merge pull request #216 from usdot-fhwa-OPS/205-messageloggerplugin_f…
Browse files Browse the repository at this point in the history
…ixes

Issue-205: Fix MessageLoggerPlugin logfile generation
  • Loading branch information
SaikrishnaBairamoni authored Jul 12, 2021
2 parents 7450468 + 3cc0efb commit af67e81
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 107 deletions.
37 changes: 19 additions & 18 deletions configuration/amd64/mysql/localhost.sql

Large diffs are not rendered by default.

51 changes: 27 additions & 24 deletions configuration/arm64/mysql/localhost.sql

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/v2i-hub/CARMACloudPlugin/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name":"CARMACloud",
"description":"CARMA cloud plugin for making websocket connection with CARMA cloud .",
"version":"3.0.0",
"version":"@PROJECT_VERSION@",
"exeLocation":"/bin/CARMACloudPlugin",
"coreIpAddr":"127.0.0.1",
"corePort":24601,
Expand Down
19 changes: 7 additions & 12 deletions src/v2i-hub/MessageLoggerPlugin/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name":"MessageLoggerPlugin",
"description":"Listens for J2735 messages and logs them in a file in JSON format.",
"version":"5.0",
"version":"@PROJECT_VERSION@",
"exeLocation":"/bin/MessageLoggerPlugin",
"coreIpAddr":"127.0.0.1",
"corePort":24601,
Expand All @@ -14,20 +14,15 @@
"default":"100",
"description":"Maximum size of the BSM log file in mb."
},
{
"key":"File Location",
"default":"/var/log/tmx",
"description":"The location where the log files are stored."
},
{
"key":"Messagetype",
"default":"BSM",
"description":"The types of messages to be logged."
},
{
"key":"Filename",
"default":"BSMLog",
"default":"bsmTx",
"description":"Default name of the BSM log file."
},
{
"key":"File Location",
"default":"/var/log/tmx",
"description":"The location where the log files are stored. DO NOT edit while using docker deployment of V2X-Hub!"
}


Expand Down
64 changes: 33 additions & 31 deletions src/v2i-hub/MessageLoggerPlugin/src/MessageLoggerPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,10 @@ MessageLoggerPlugin::MessageLoggerPlugin(string name): PluginClient(name)
{
PLOG(logDEBUG)<< "In MessageLoggerPlugin Constructor";
// The log level can be changed from the default here.
//FILELog::ReportingLevel() = FILELog::FromString("DEBUG");
// FILELog::ReportingLevel() = FILELog::FromString("DEBUG");

// Critical section
std::lock_guard<mutex> lock(_cfgLock);
GetConfigValue("File Location", _fileDirectory);
GetConfigValue("File Size In MB", _maxFilesizeInMB);
GetConfigValue("Messagetype", _cvmsgtype);
GetConfigValue("Filename", _filename);
_curFilename = _fileDirectory + "/" + _filename + ".json";
_curFilenamebin = _fileDirectory + "/" + _filename + ".bin";
_curFilenamesize = _curFilenamebin;

OpenMSGLogFile();
// Add a message filter and handler for each message this plugin wants to receive.
//AddMessageFilter<DecodedBsmMessage>(this, &BsmLoggerPlugin::HandleDecodedBsmMessage);
AddMessageFilter < BsmMessage > (this, &MessageLoggerPlugin::HandleBasicSafetyMessage);

// Subscribe to all messages specified by the filters above.
Expand Down Expand Up @@ -83,15 +72,14 @@ void MessageLoggerPlugin::UpdateConfigSettings()
__frequency_mon.check();

std::lock_guard<mutex> lock(_cfgLock);
GetConfigValue("File Location", _fileDirectory);
GetConfigValue("File Location", _fileDirectory);
GetConfigValue("File Size In MB", _maxFilesizeInMB);
GetConfigValue("Messagetype", _cvmsgtype);
GetConfigValue("Filename", _filename);
std::string oldFilename = _curFilename;
std::string oldFilenamebin = _curFilenamebin;
_curFilename = _fileDirectory + "/" + _filename + ".json";
_curFilenamebin = _fileDirectory + "/" + _filename + ".bin";

_curFilenamesize = _curFilenamebin;
if (_curFilename.compare (oldFilename) !=0 )
{
_logFile.close();
Expand Down Expand Up @@ -124,7 +112,6 @@ void MessageLoggerPlugin::OnStateChange(IvpPluginState state)
if (state == IvpPluginState_registered)
{
UpdateConfigSettings();
//SetStatus("ReceivedMaps", 0);
}
}

Expand All @@ -138,7 +125,8 @@ void MessageLoggerPlugin::OnStateChange(IvpPluginState state)
*/
void MessageLoggerPlugin::HandleBasicSafetyMessage(BsmMessage &msg,
routeable_message &routeableMsg) {

// check size of the log file and open new one if needed
CheckMSGLogFileSizeAndRename();
char *BsmOut;
cJSON *BsmRoot, *BsmMessageContent, *_BsmMessageContent;

Expand Down Expand Up @@ -409,26 +397,42 @@ void MessageLoggerPlugin::HandleBasicSafetyMessage(BsmMessage &msg,


/**
* Opens a new log file in the directory specified of specified name for logging BSM messages and
* inserts a header row with names of fields that will be logged when data is received. If a log file
* with the same name already exists before opening a new file, it's renamed with current timestamp suffix.
* Opens a new log file in the directory specified of specified name for logging BSM messages. Once the
* current binary logfile size reaches the configurable maxSize this file is closed, renamed by the current
* time and date and moved to a /ode/ directory where it can be sent to an ODE using the filewatchscript.sh.
*/
void MessageLoggerPlugin::OpenMSGLogFile()
{
PLOG(logDEBUG) << "Message Log File: " << _curFilename << std::endl;;
//rename logfile if one already exists
std::string newFilename = _fileDirectory + "/" + _filename + GetCurDateTimeStr() + ".json";
std::string newbinFilename = _fileDirectory + "/" + _filename + GetCurDateTimeStr() + ".bin";
if ( !boost::filesystem::exists( _fileDirectory + "/json/") ){
boost::filesystem::create_directory( _fileDirectory + "/json/");
}
if ( !boost::filesystem::exists( _fileDirectory + "/ode/") ){
boost::filesystem::create_directory( _fileDirectory + "/ode/");
}
std::string newFilename = _fileDirectory + "/json/" + _filename + GetCurDateTimeStr() + ".json";
std::string newbinFilename = _fileDirectory + "/ode/" + _filename + GetCurDateTimeStr() + ".bin";
std::string _newFilename = newbinFilename.c_str();
std::rename(_curFilename.c_str(), newFilename.c_str());
std::rename(_curFilenamebin.c_str(), newbinFilename.c_str());
_logFile.open(_curFilename);
_logFilebin.open(_curFilenamebin, std::ios::out | std::ios::binary | std::ios::app);
int error;
if ( boost::filesystem::exists( _curFilenamebin.c_str() ) ) {
error = std::rename(_curFilename.c_str(), newFilename.c_str() );
if ( error != 0 ) {
FILE_LOG(logERROR) << "Failed to mv " << _curFilename.c_str() << " to " << newFilename.c_str() << std::endl;
}

error = std::rename(_curFilenamebin.c_str(), newbinFilename.c_str() );
if ( error != 0 ) {
FILE_LOG(logERROR) << "Failed to mv " << _curFilenamebin.c_str() << " to " << newbinFilename.c_str() << std::endl;
}
}
_logFile.open(_curFilename);
_logFilebin.open(_curFilenamebin, std::ios::out | std::ios::binary | std::ios::app);
if (!_logFile.is_open())
std::cerr << "Could not open log : " << strerror(errno) << std::endl;
else
{
_logFile << "Message Log file" << GetCurDateTimeStr() << endl;
_logFile << "Message JSON Logs" << endl;

}
}
Expand All @@ -437,7 +441,7 @@ void MessageLoggerPlugin::OpenMSGLogFile()
* Checks the size of the logfile and opens a new_fileDirectory file if it's size is greater
* than the max size specified.
*/
void MessageLoggerPlugin::CheckMSGLogFileSizeAndRename(bool createNewFile)
void MessageLoggerPlugin::CheckMSGLogFileSizeAndRename()
{
if (_logFile.is_open())
{
Expand All @@ -448,7 +452,6 @@ void MessageLoggerPlugin::CheckMSGLogFileSizeAndRename(bool createNewFile)
int curFilesizeInMB = _logFilesize/1048576;
if (curFilesizeInMB >= _maxFilesizeInMB)
{
createNewFile = true;
_logFile.close();
_logFilebin.close();
OpenMSGLogFile();
Expand Down Expand Up @@ -483,8 +486,7 @@ int MessageLoggerPlugin::Main()

this_thread::sleep_for(chrono::milliseconds(1000));

// check size of the log file and open new one if needed
CheckMSGLogFileSizeAndRename(true);

}

PLOG(logDEBUG) << "MessageLoggerPlugin terminating gracefully.";
Expand Down
6 changes: 3 additions & 3 deletions src/v2i-hub/MessageLoggerPlugin/src/MessageLoggerPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@
#include <chrono>
#include <thread>
#include <../../../tmx/TmxApi/tmx/json/cJSON.h>
//#include <DecodedBsmMessage.h>
#include <tmx/messages/IvpJ2735.h>
#include <tmx/j2735_messages/BasicSafetyMessage.hpp>
#include <BasicSafetyMessage.h>
#include <tmx/messages/auto_message.hpp>
#include <boost/filesystem.hpp>


using namespace std;
using namespace tmx;
using namespace tmx::utils;
using namespace tmx::messages;
using namespace boost::filesystem;


namespace MessageLoggerPlugin
Expand Down Expand Up @@ -78,12 +79,11 @@ class MessageLoggerPlugin: public PluginClient
DATA_MONITOR(_frequency); // Declares the

void OpenMSGLogFile();
void CheckMSGLogFileSizeAndRename(bool createNewFile=false);
void CheckMSGLogFileSizeAndRename();
std::string GetCurDateTimeStr();

std::ofstream _logFile;
std::ofstream _logFilebin;
std::string _cvmsgtype;
std::string _filename, _fileDirectory;
std::string _curFilename;
std::string _curFilenamebin;
Expand Down
2 changes: 1 addition & 1 deletion src/v2i-hub/MobilityOperationPlugin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"type":"J2735",
"subtype":"TMSG03-P",
"description":"..."
"description":"In development"
}
],
"configuration":[
Expand Down
2 changes: 1 addition & 1 deletion src/v2i-hub/ODELoggerPlugin/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name":"ODELoggerPlugin",
"description":"Listens for J2735 messages and realtime forwards them to ODE.",
"version":"5.0",
"version":"@PROJECT_VERSION@",
"exeLocation":"/bin/ODELoggerPlugin",
"coreIpAddr":"127.0.0.1",
"corePort":24601,
Expand Down
10 changes: 5 additions & 5 deletions src/v2i-hub/SPaTLoggerPlugin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
"default":"100",
"description":"Maximum size of the SPaT log file in mb."
},
{
"key":"File Location",
"default":"/var/log/tmx",
"description":"The location where the log files are stored."
},
{
"key":"Filename",
"default":"spatTx",
"description":"Default name of the SPaT log file."
},
{
"key":"File Location",
"default":"/var/log/tmx",
"description":"The location where the log files are stored. DO NOT edit while using docker deployment of V2X-Hub!"
}


Expand Down
25 changes: 14 additions & 11 deletions src/v2i-hub/SPaTLoggerPlugin/src/SPaTLoggerPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ void SPaTLoggerPlugin::HandleSpatMessage(SpatMessage &msg, routeable_message &ro


/**
* Opens a new log file in the directory specified of specified name for logging SPaT messages and
* inserts a header row with names of fields that will be logged when data is received. If a log file
* with the same name already exists before opening a new file, it's renamed with current timestamp suffix.
* Opens a new log file in the directory specified of specified name for logging SPaT messages. Once the
* current binary logfile size reaches the configurable maxSize this file is closed, renamed by the current
* time and date and moved to a /ode/ directory where it can be sent to an ODE using the filewatchscript.sh.
*/
void SPaTLoggerPlugin::OpenSPaTLogFile()
{
Expand All @@ -288,14 +288,17 @@ void SPaTLoggerPlugin::OpenSPaTLogFile()
std::string newbinFilename = _fileDirectory + "/ode/" + _filename + GetCurDateTimeStr() + ".bin";

int error;
error = std::rename(_curFilename.c_str(), newFilename.c_str() );
if ( error != 0 ) {
FILE_LOG(logERROR) << "Failed to mv " << _curFilename.c_str() << " to " << newFilename.c_str() << std::endl;
}
error = std::rename(_curFilenamebin.c_str(), newbinFilename.c_str() );
if ( error != 0 ) {
FILE_LOG(logERROR) << "Failed to mv " << _curFilenamebin.c_str() << " to " << newbinFilename.c_str() << std::endl;
}
if ( boost::filesystem::exists( _curFilenamebin.c_str() ) ) {
error = std::rename(_curFilename.c_str(), newFilename.c_str() );
if ( error != 0 ) {
FILE_LOG(logERROR) << "Failed to mv " << _curFilename.c_str() << " to " << newFilename.c_str() << std::endl;
}

error = std::rename(_curFilenamebin.c_str(), newbinFilename.c_str() );
if ( error != 0 ) {
FILE_LOG(logERROR) << "Failed to mv " << _curFilenamebin.c_str() << " to " << newbinFilename.c_str() << std::endl;
}
}
_logFile.open(_curFilename);
_logFilebin.open(_curFilenamebin, std::ios::out | std::ios::binary | std::ios::app);
if (!_logFile.is_open())
Expand Down

0 comments on commit af67e81

Please sign in to comment.