From b3936ca2c62842dccdc8a9f119e7f45a09833011 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Thu, 20 Feb 2020 13:22:28 -0500 Subject: [PATCH 01/16] Test log --- bfs/include/Logger.h | 49 +++++++++++++++++-------------- bfs/src/comms/AlgorithmServer.cpp | 20 ++++++------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/bfs/include/Logger.h b/bfs/include/Logger.h index de4420c..685e949 100644 --- a/bfs/include/Logger.h +++ b/bfs/include/Logger.h @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include using namespace std; @@ -13,35 +16,37 @@ using namespace std; extern class Logger { public: - int logging_Level = 1; + ofstream log_file; string filename = "log.txt"; - + Logger(int l_Level, string fileName = "log.txt") { + this->logging_Level = l_Level; + this->filename = fileName; + //this->currentTime = time(NULL); + } + void write_log(string msg, int msg_level) { + string line; + ofstream log_file; + log_file.open(this->filename, std::ios_base::app); + if (this->logging_Level <= msg_level) { + //for (int i = 0; i < &msg.size; i++) + auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); + log_file << ctime(&now) << " " << msg << endl; + + } + log_file.close(); + } +private: + int logging_Level; + //auto now; }; -void start_log(string filename){ - ofstream log_file; - log_file.open(filename, std::ios_base::in | std::ios_base::trunc); - log_file << "Start log"; - log_file.close(); -} - -void write_log(string msg, int msg_level, string filename, Logger* logObject) { - string line; - ofstream log_file; - log_file.open(filename, std::ios_base::app); - if (logObject->logging_Level <= msg_level) { - //for (int i = 0; i < &msg.size; i++) - log_file << msg; - } - log_file.close(); -} -Logger* logger = NULL; -Logger* getLogger() { +//Logger* logger = NULL; +Logger* getLogger(Logger* logger) { if (logger == NULL) { - logger = new Logger(); + logger = new Logger(1); } return logger; diff --git a/bfs/src/comms/AlgorithmServer.cpp b/bfs/src/comms/AlgorithmServer.cpp index 3a81708..d3bf554 100644 --- a/bfs/src/comms/AlgorithmServer.cpp +++ b/bfs/src/comms/AlgorithmServer.cpp @@ -3,6 +3,7 @@ #include +extern Logger* logger = getLogger(logger); AlgorithmServer::AlgorithmServer(size_t numClients) { this->hThread = NULL; @@ -19,7 +20,6 @@ AlgorithmServer::~AlgorithmServer() void AlgorithmServer::serverThreadRuntime() { - //Logger* logger = getLogger(); int iResult; clientThreadsMutex = CreateMutex(NULL, false, NULL); @@ -68,7 +68,7 @@ void AlgorithmServer::serverThreadRuntime() iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - //write_log("listen failed with error\n", 1, logger->filename, logger); + logger->write_log("listen failed with error\n", 1); printf("listen failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); @@ -79,15 +79,15 @@ void AlgorithmServer::serverThreadRuntime() // Accept a client socket while (continueThread) { - write_log("Listening for clients...\n", 1, logger->filename, logger); + logger->write_log("Listening for clients...\n", 1); printf("Listening for clients...\n"); ClientSocket = accept(ListenSocket, NULL, NULL); - write_log("Hello new client!\n", 1, logger->filename, logger); + logger->write_log("Hello new client!\n", 1); printf("Hello new client!\n"); if (ClientSocket == INVALID_SOCKET) { - write_log("accept failed with error: ", 1, logger->filename, logger); - write_log(std::to_string(WSAGetLastError()), 1, logger->filename, logger); - write_log("\n", 1, logger->filename, logger); + logger->write_log("accept failed with error: ", 1); + logger->write_log(std::to_string(WSAGetLastError()), 1); + logger->write_log("\n", 1); printf("accept failed with error: %d\n", WSAGetLastError()); AcceptFailures++; if (AcceptFailures >= MAX_ACCEPT_FAILURES) @@ -124,7 +124,7 @@ void AlgorithmServer::serverThreadRuntime() unlockClientThreadsMutex(); } else { - write_log("Could not acquire mutex to add new client thread\n", 1, logger->filename, logger); + logger->write_log("Could not acquire mutex to add new client thread\n", 1); printf("Could not acquire mutex to add new client thread\n"); } } @@ -144,7 +144,7 @@ vector* AlgorithmServer::getAllIncomingAttributes() Polls DataSyncThreads for new updates Updates master storage accordingly */ - Logger* logger = getLogger(); + //Logger* logger = getLogger(); vector* newAttribs = new vector; if (lockClientThreadsMutex() == STATUS_WAIT_0) { @@ -156,7 +156,7 @@ vector* AlgorithmServer::getAllIncomingAttributes() unlockClientThreadsMutex(); } else { - write_log("Could not acquire mutex to add new client thread\n", 1, logger->filename, logger); + logger->write_log("Could not acquire mutex to add new client thread\n", 1); printf("Could not acquire mutex to get incoming attributes.\n"); } return newAttribs; From c0b68e9c33c9d5e10b20c0a0d6be27241d305458 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 9 Mar 2020 11:58:21 -0400 Subject: [PATCH 02/16] Logger changes --- bfs/include/Logger.hpp | 33 +++++++++++++++++++++++++++++++++ bfs/src/logging/Logger.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 bfs/include/Logger.hpp create mode 100644 bfs/src/logging/Logger.cpp diff --git a/bfs/include/Logger.hpp b/bfs/include/Logger.hpp new file mode 100644 index 0000000..60ea5b7 --- /dev/null +++ b/bfs/include/Logger.hpp @@ -0,0 +1,33 @@ +#ifndef LOGGER_HPP +#define LOGGER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + + +extern class Logger { +public: + ofstream logFile; + string filename; + string filePath; + Logger(int l_Level); + ~Logger(); + void writeLog(string msg, int msg_level); + static Logger* getLogger(Logger* logger); +private: + int logging_Level; + //auto now; + +}; + + +#endif diff --git a/bfs/src/logging/Logger.cpp b/bfs/src/logging/Logger.cpp new file mode 100644 index 0000000..ad4e261 --- /dev/null +++ b/bfs/src/logging/Logger.cpp @@ -0,0 +1,32 @@ +#include "Logger.hpp" + +Logger::Logger(int l_Level) { + this->logging_Level = l_Level; + time_t t = time(0); // get time now + struct tm * now = localtime( & t ); + char buffer [80]; + strftime (buffer,80,"%Y-%m-%d %H:%M:%S",now); + std::string str(buffer); + this->filename = str + ".log"; + //cout << filename << "\n"; + //this->filename = "log.txt"; +} + +void Logger::writeLog(string msg, int msg_level) { + string line; + this->logFile.open(this->filename, ios::in); + if (this->logging_Level <= msg_level) { + auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); + this->logFile << ctime(&now) << " " << msg << endl; + + } + this->logFile.close(); +} + +Logger* Logger::getLogger(Logger* logger) { + if (logger == NULL) { + logger = new Logger(1); + + } + return logger; +} \ No newline at end of file From 810ff1b9f787fc330a2549e5e48cb40a29ddb0f9 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 16 Mar 2020 12:26:42 -0400 Subject: [PATCH 03/16] Reupload logger changes --- bfs/include/Logger.hpp | 4 +++- bfs/src/comms/AlgorithmServer.cpp | 20 ++++++++++---------- bfs/src/comms/DataSyncThread.cpp | 14 ++++++++++++-- bfs/src/logging/Logger.cpp | 17 ++++++++++++----- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/bfs/include/Logger.hpp b/bfs/include/Logger.hpp index 60ea5b7..f4c2c26 100644 --- a/bfs/include/Logger.hpp +++ b/bfs/include/Logger.hpp @@ -9,6 +9,7 @@ #include #include #include +#include using namespace std; @@ -23,11 +24,12 @@ public: ~Logger(); void writeLog(string msg, int msg_level); static Logger* getLogger(Logger* logger); + string getPath(); private: int logging_Level; //auto now; }; - +static Logger* logger = logger->getLogger(logger); #endif diff --git a/bfs/src/comms/AlgorithmServer.cpp b/bfs/src/comms/AlgorithmServer.cpp index d3bf554..af7122f 100644 --- a/bfs/src/comms/AlgorithmServer.cpp +++ b/bfs/src/comms/AlgorithmServer.cpp @@ -1,9 +1,9 @@ #include "AlgorithmServer.hpp" -#include +#include "Logger.hpp" -extern Logger* logger = getLogger(logger); +//extern Logger* logger = logger->getLogger(logger); AlgorithmServer::AlgorithmServer(size_t numClients) { this->hThread = NULL; @@ -68,7 +68,7 @@ void AlgorithmServer::serverThreadRuntime() iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - logger->write_log("listen failed with error\n", 1); + logger->writeLog("listen failed with error\n", 1); printf("listen failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); @@ -79,15 +79,15 @@ void AlgorithmServer::serverThreadRuntime() // Accept a client socket while (continueThread) { - logger->write_log("Listening for clients...\n", 1); + logger->writeLog("Listening for clients...\n", 1); printf("Listening for clients...\n"); ClientSocket = accept(ListenSocket, NULL, NULL); - logger->write_log("Hello new client!\n", 1); + logger->writeLog("Hello new client!\n", 1); printf("Hello new client!\n"); if (ClientSocket == INVALID_SOCKET) { - logger->write_log("accept failed with error: ", 1); - logger->write_log(std::to_string(WSAGetLastError()), 1); - logger->write_log("\n", 1); + logger->writeLog("accept failed with error: ", 1); + logger->writeLog(std::to_string(WSAGetLastError()), 1); + logger->writeLog("\n", 1); printf("accept failed with error: %d\n", WSAGetLastError()); AcceptFailures++; if (AcceptFailures >= MAX_ACCEPT_FAILURES) @@ -124,7 +124,7 @@ void AlgorithmServer::serverThreadRuntime() unlockClientThreadsMutex(); } else { - logger->write_log("Could not acquire mutex to add new client thread\n", 1); + logger->writeLog("Could not acquire mutex to add new client thread\n", 1); printf("Could not acquire mutex to add new client thread\n"); } } @@ -156,7 +156,7 @@ vector* AlgorithmServer::getAllIncomingAttributes() unlockClientThreadsMutex(); } else { - logger->write_log("Could not acquire mutex to add new client thread\n", 1); + logger->writeLog("Could not acquire mutex to add new client thread\n", 1); printf("Could not acquire mutex to get incoming attributes.\n"); } return newAttribs; diff --git a/bfs/src/comms/DataSyncThread.cpp b/bfs/src/comms/DataSyncThread.cpp index a999136..dd7ffc7 100644 --- a/bfs/src/comms/DataSyncThread.cpp +++ b/bfs/src/comms/DataSyncThread.cpp @@ -1,7 +1,9 @@ //#include #include "DataSyncThread.hpp" -//include +#include "Logger.hpp" + +//extern Logger* logger = logger->getLogger(logger); void DataSyncThread::threadRuntime() { threadRunning = true; @@ -14,8 +16,8 @@ void DataSyncThread::threadRuntime() while (continueThread && iResult > 0) { - //write_log("Waiting to receive bytes\n", 1, logger->filename, logger); printf("Waiting to receive bytes\n"); + logger->writeLog("Waiting to receive bytes\n", 1); if (!readyToReceive()) continue; @@ -141,6 +143,10 @@ void DataSyncThread::startComms() //write_log(std::to_string(incomingAttributes), 1, logger->filename, logger); printf("Start comms: %d\n", incomingAttributes); + char buffer[100]; + snprintf(buffer, 100, "Start comms: %d\n", incomingAttributes); + string str(buffer); + logger->writeLog(str, 1); if (!threadRunning) { threadRunning = false; @@ -246,7 +252,11 @@ std::vector *DataSyncThread::getIncomingAttributes() ReleaseMutex(attribMutex); } else { + char buffer[100]; printf("2 Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); + snprintf(buffer, 100, "2 Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); + string str(buffer); + logger->writeLog(str, 1); } return newVector; diff --git a/bfs/src/logging/Logger.cpp b/bfs/src/logging/Logger.cpp index ad4e261..657e25d 100644 --- a/bfs/src/logging/Logger.cpp +++ b/bfs/src/logging/Logger.cpp @@ -5,16 +5,23 @@ Logger::Logger(int l_Level) { time_t t = time(0); // get time now struct tm * now = localtime( & t ); char buffer [80]; - strftime (buffer,80,"%Y-%m-%d %H:%M:%S",now); + strftime (buffer,80,"%Y-%m-%d-%Hhr%Mm%Ss",now); std::string str(buffer); this->filename = str + ".log"; - //cout << filename << "\n"; - //this->filename = "log.txt"; + this->filePath = getPath() + this->filename; // Sets location and name of log file +} + +string Logger::getPath() { + char buffer[MAX_PATH]; + GetModuleFileName(NULL, buffer, MAX_PATH); // Get current working directory + string::size_type pos = string(buffer).find_last_of("\\/"); + string::size_type pos2 = string(buffer).substr(0, pos).find_last_of( "\\/" ); + string logPath = string(buffer).substr(0, pos2) + "\\logs\\"; + return logPath; } void Logger::writeLog(string msg, int msg_level) { - string line; - this->logFile.open(this->filename, ios::in); + this->logFile.open(this->filePath, ios::app); if (this->logging_Level <= msg_level) { auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); this->logFile << ctime(&now) << " " << msg << endl; From daa4ff980b92ad9ac6cdefdeb2a9d56046254180 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 16 Mar 2020 12:28:22 -0400 Subject: [PATCH 04/16] Removing old logger file --- bfs/include/Logger.h | 56 -------------------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 bfs/include/Logger.h diff --git a/bfs/include/Logger.h b/bfs/include/Logger.h deleted file mode 100644 index 685e949..0000000 --- a/bfs/include/Logger.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef LOGGER_H -#define LOGGER_H - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - - - -extern class Logger { -public: - ofstream log_file; - string filename = "log.txt"; - Logger(int l_Level, string fileName = "log.txt") { - this->logging_Level = l_Level; - this->filename = fileName; - //this->currentTime = time(NULL); - } - void write_log(string msg, int msg_level) { - string line; - ofstream log_file; - log_file.open(this->filename, std::ios_base::app); - if (this->logging_Level <= msg_level) { - //for (int i = 0; i < &msg.size; i++) - auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); - log_file << ctime(&now) << " " << msg << endl; - - } - log_file.close(); - } -private: - int logging_Level; - //auto now; - -}; - - - -//Logger* logger = NULL; -Logger* getLogger(Logger* logger) { - if (logger == NULL) { - logger = new Logger(1); - - } - return logger; -} - -//Logger* logger = getLogger(); -#endif From e0b4037389860f35e34eb1aafec3311e75ae367d Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 16 Mar 2020 12:35:11 -0400 Subject: [PATCH 05/16] CMake --- bfs/CMakeSettings.json | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/bfs/CMakeSettings.json b/bfs/CMakeSettings.json index 53e51a3..08bf11d 100644 --- a/bfs/CMakeSettings.json +++ b/bfs/CMakeSettings.json @@ -10,13 +10,7 @@ "cmakeCommandArgs": "", "buildCommandArgs": "-v", "ctestCommandArgs": "", - "variables": [ - { - "name": "CMAKE_INSTALL_PREFIX", - "value": "C:/Users/Greg/Documents/git/bfs/breadcrumbs/install/basic_build", - "type": "PATH" - } - ] + "variables": [] } ] } \ No newline at end of file From f8c705d77f56c81f340a05a860f17ad44a0dad05 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 16 Mar 2020 13:41:17 -0400 Subject: [PATCH 06/16] Rebase part 1 --- bfs/include/Logger.h | 49 +++++++++++++++++-------------- bfs/src/comms/AlgorithmServer.cpp | 29 ++++++++++++++---- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/bfs/include/Logger.h b/bfs/include/Logger.h index de4420c..685e949 100644 --- a/bfs/include/Logger.h +++ b/bfs/include/Logger.h @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include using namespace std; @@ -13,35 +16,37 @@ using namespace std; extern class Logger { public: - int logging_Level = 1; + ofstream log_file; string filename = "log.txt"; - + Logger(int l_Level, string fileName = "log.txt") { + this->logging_Level = l_Level; + this->filename = fileName; + //this->currentTime = time(NULL); + } + void write_log(string msg, int msg_level) { + string line; + ofstream log_file; + log_file.open(this->filename, std::ios_base::app); + if (this->logging_Level <= msg_level) { + //for (int i = 0; i < &msg.size; i++) + auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); + log_file << ctime(&now) << " " << msg << endl; + + } + log_file.close(); + } +private: + int logging_Level; + //auto now; }; -void start_log(string filename){ - ofstream log_file; - log_file.open(filename, std::ios_base::in | std::ios_base::trunc); - log_file << "Start log"; - log_file.close(); -} - -void write_log(string msg, int msg_level, string filename, Logger* logObject) { - string line; - ofstream log_file; - log_file.open(filename, std::ios_base::app); - if (logObject->logging_Level <= msg_level) { - //for (int i = 0; i < &msg.size; i++) - log_file << msg; - } - log_file.close(); -} -Logger* logger = NULL; -Logger* getLogger() { +//Logger* logger = NULL; +Logger* getLogger(Logger* logger) { if (logger == NULL) { - logger = new Logger(); + logger = new Logger(1); } return logger; diff --git a/bfs/src/comms/AlgorithmServer.cpp b/bfs/src/comms/AlgorithmServer.cpp index 3521eb4..851fbb6 100644 --- a/bfs/src/comms/AlgorithmServer.cpp +++ b/bfs/src/comms/AlgorithmServer.cpp @@ -3,6 +3,7 @@ #include +extern Logger* logger = getLogger(logger); AlgorithmServer::AlgorithmServer(size_t numClients) { this->hThread = NULL; @@ -19,7 +20,6 @@ AlgorithmServer::~AlgorithmServer() void AlgorithmServer::serverThreadRuntime() { - //Logger* logger = getLogger(); int iResult; clientThreadsMutex = CreateMutex(NULL, false, NULL); @@ -68,7 +68,7 @@ void AlgorithmServer::serverThreadRuntime() iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - //write_log("listen failed with error\n", 1, logger->filename, logger); + logger->write_log("listen failed with error\n", 1); printf("listen failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); @@ -79,15 +79,23 @@ void AlgorithmServer::serverThreadRuntime() // Accept a client socket while (continueThread) { +<<<<<<< HEAD write_log("Listening for clients...\n", 1, logger->filename, logger); fprintf(stderr, "Listening for clients...\n"); ClientSocket = accept(ListenSocket, NULL, NULL); write_log("Hello new client!\n", 1, logger->filename, logger); fprintf(stderr, "Hello new client!\n"); +======= + logger->write_log("Listening for clients...\n", 1); + printf("Listening for clients...\n"); + ClientSocket = accept(ListenSocket, NULL, NULL); + logger->write_log("Hello new client!\n", 1); + printf("Hello new client!\n"); +>>>>>>> Test log if (ClientSocket == INVALID_SOCKET) { - write_log("accept failed with error: ", 1, logger->filename, logger); - write_log(std::to_string(WSAGetLastError()), 1, logger->filename, logger); - write_log("\n", 1, logger->filename, logger); + logger->write_log("accept failed with error: ", 1); + logger->write_log(std::to_string(WSAGetLastError()), 1); + logger->write_log("\n", 1); printf("accept failed with error: %d\n", WSAGetLastError()); AcceptFailures++; if (AcceptFailures >= MAX_ACCEPT_FAILURES) @@ -126,7 +134,7 @@ void AlgorithmServer::serverThreadRuntime() unlockClientThreadsMutex(); } else { - write_log("Could not acquire mutex to add new client thread\n", 1, logger->filename, logger); + logger->write_log("Could not acquire mutex to add new client thread\n", 1); printf("Could not acquire mutex to add new client thread\n"); } } @@ -146,8 +154,13 @@ vector AlgorithmServer::getAllIncomingAttributes() Polls DataSyncThreads for new updates Updates master storage accordingly */ +<<<<<<< HEAD Logger* logger = getLogger(); vector newAttribs; +======= + //Logger* logger = getLogger(); + vector* newAttribs = new vector; +>>>>>>> Test log if (lockClientThreadsMutex() == STATUS_WAIT_0) { for (DataSyncThread* dst : clientThreads) @@ -158,6 +171,10 @@ vector AlgorithmServer::getAllIncomingAttributes() unlockClientThreadsMutex(); } else { +<<<<<<< HEAD +======= + logger->write_log("Could not acquire mutex to add new client thread\n", 1); +>>>>>>> Test log printf("Could not acquire mutex to get incoming attributes.\n"); } return newAttribs; From 7f6854ff146c06bf5fc7a1d4faad3c31e1654634 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 9 Mar 2020 11:58:21 -0400 Subject: [PATCH 07/16] Logger changes --- bfs/include/Logger.hpp | 33 +++++++++++++++++++++++++++++++++ bfs/src/logging/Logger.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 bfs/include/Logger.hpp create mode 100644 bfs/src/logging/Logger.cpp diff --git a/bfs/include/Logger.hpp b/bfs/include/Logger.hpp new file mode 100644 index 0000000..60ea5b7 --- /dev/null +++ b/bfs/include/Logger.hpp @@ -0,0 +1,33 @@ +#ifndef LOGGER_HPP +#define LOGGER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + + +extern class Logger { +public: + ofstream logFile; + string filename; + string filePath; + Logger(int l_Level); + ~Logger(); + void writeLog(string msg, int msg_level); + static Logger* getLogger(Logger* logger); +private: + int logging_Level; + //auto now; + +}; + + +#endif diff --git a/bfs/src/logging/Logger.cpp b/bfs/src/logging/Logger.cpp new file mode 100644 index 0000000..ad4e261 --- /dev/null +++ b/bfs/src/logging/Logger.cpp @@ -0,0 +1,32 @@ +#include "Logger.hpp" + +Logger::Logger(int l_Level) { + this->logging_Level = l_Level; + time_t t = time(0); // get time now + struct tm * now = localtime( & t ); + char buffer [80]; + strftime (buffer,80,"%Y-%m-%d %H:%M:%S",now); + std::string str(buffer); + this->filename = str + ".log"; + //cout << filename << "\n"; + //this->filename = "log.txt"; +} + +void Logger::writeLog(string msg, int msg_level) { + string line; + this->logFile.open(this->filename, ios::in); + if (this->logging_Level <= msg_level) { + auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); + this->logFile << ctime(&now) << " " << msg << endl; + + } + this->logFile.close(); +} + +Logger* Logger::getLogger(Logger* logger) { + if (logger == NULL) { + logger = new Logger(1); + + } + return logger; +} \ No newline at end of file From d43c958b25a1c9975cf828de6858ae7c580f3147 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 16 Mar 2020 14:55:17 -0400 Subject: [PATCH 08/16] REBASE 3/5 --- bfs/include/Logger.hpp | 4 +++- bfs/src/comms/AlgorithmServer.cpp | 24 ++++++++++++++++-------- bfs/src/comms/DataSyncThread.cpp | 5 ++++- bfs/src/logging/Logger.cpp | 17 ++++++++++++----- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/bfs/include/Logger.hpp b/bfs/include/Logger.hpp index 60ea5b7..f4c2c26 100644 --- a/bfs/include/Logger.hpp +++ b/bfs/include/Logger.hpp @@ -9,6 +9,7 @@ #include #include #include +#include using namespace std; @@ -23,11 +24,12 @@ public: ~Logger(); void writeLog(string msg, int msg_level); static Logger* getLogger(Logger* logger); + string getPath(); private: int logging_Level; //auto now; }; - +static Logger* logger = logger->getLogger(logger); #endif diff --git a/bfs/src/comms/AlgorithmServer.cpp b/bfs/src/comms/AlgorithmServer.cpp index 851fbb6..a27961d 100644 --- a/bfs/src/comms/AlgorithmServer.cpp +++ b/bfs/src/comms/AlgorithmServer.cpp @@ -1,9 +1,9 @@ #include "AlgorithmServer.hpp" -#include +#include "Logger.hpp" -extern Logger* logger = getLogger(logger); +//extern Logger* logger = logger->getLogger(logger); AlgorithmServer::AlgorithmServer(size_t numClients) { this->hThread = NULL; @@ -68,7 +68,7 @@ void AlgorithmServer::serverThreadRuntime() iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - logger->write_log("listen failed with error\n", 1); + logger->writeLog("listen failed with error\n", 1); printf("listen failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); @@ -79,6 +79,7 @@ void AlgorithmServer::serverThreadRuntime() // Accept a client socket while (continueThread) { +<<<<<<< HEAD <<<<<<< HEAD write_log("Listening for clients...\n", 1, logger->filename, logger); fprintf(stderr, "Listening for clients...\n"); @@ -87,15 +88,18 @@ void AlgorithmServer::serverThreadRuntime() fprintf(stderr, "Hello new client!\n"); ======= logger->write_log("Listening for clients...\n", 1); +======= + logger->writeLog("Listening for clients...\n", 1); +>>>>>>> Reupload logger changes printf("Listening for clients...\n"); ClientSocket = accept(ListenSocket, NULL, NULL); - logger->write_log("Hello new client!\n", 1); + logger->writeLog("Hello new client!\n", 1); printf("Hello new client!\n"); >>>>>>> Test log if (ClientSocket == INVALID_SOCKET) { - logger->write_log("accept failed with error: ", 1); - logger->write_log(std::to_string(WSAGetLastError()), 1); - logger->write_log("\n", 1); + logger->writeLog("accept failed with error: ", 1); + logger->writeLog(std::to_string(WSAGetLastError()), 1); + logger->writeLog("\n", 1); printf("accept failed with error: %d\n", WSAGetLastError()); AcceptFailures++; if (AcceptFailures >= MAX_ACCEPT_FAILURES) @@ -134,7 +138,7 @@ void AlgorithmServer::serverThreadRuntime() unlockClientThreadsMutex(); } else { - logger->write_log("Could not acquire mutex to add new client thread\n", 1); + logger->writeLog("Could not acquire mutex to add new client thread\n", 1); printf("Could not acquire mutex to add new client thread\n"); } } @@ -172,9 +176,13 @@ vector AlgorithmServer::getAllIncomingAttributes() } else { <<<<<<< HEAD +<<<<<<< HEAD ======= logger->write_log("Could not acquire mutex to add new client thread\n", 1); >>>>>>> Test log +======= + logger->writeLog("Could not acquire mutex to add new client thread\n", 1); +>>>>>>> Reupload logger changes printf("Could not acquire mutex to get incoming attributes.\n"); } return newAttribs; diff --git a/bfs/src/comms/DataSyncThread.cpp b/bfs/src/comms/DataSyncThread.cpp index 9d22dec..ea88052 100644 --- a/bfs/src/comms/DataSyncThread.cpp +++ b/bfs/src/comms/DataSyncThread.cpp @@ -1,7 +1,9 @@ //#include #include "DataSyncThread.hpp" -//include +#include "Logger.hpp" + +//extern Logger* logger = logger->getLogger(logger); void DataSyncThread::threadRuntime() { threadRunning = true; @@ -155,6 +157,7 @@ void DataSyncThread::startComms() //write_log("Start comms: ", 1, logger->filename, logger); //write_log(std::to_string(incomingAttributes), 1, logger->filename, logger); + if (!threadRunning) { threadRunning = false; diff --git a/bfs/src/logging/Logger.cpp b/bfs/src/logging/Logger.cpp index ad4e261..657e25d 100644 --- a/bfs/src/logging/Logger.cpp +++ b/bfs/src/logging/Logger.cpp @@ -5,16 +5,23 @@ Logger::Logger(int l_Level) { time_t t = time(0); // get time now struct tm * now = localtime( & t ); char buffer [80]; - strftime (buffer,80,"%Y-%m-%d %H:%M:%S",now); + strftime (buffer,80,"%Y-%m-%d-%Hhr%Mm%Ss",now); std::string str(buffer); this->filename = str + ".log"; - //cout << filename << "\n"; - //this->filename = "log.txt"; + this->filePath = getPath() + this->filename; // Sets location and name of log file +} + +string Logger::getPath() { + char buffer[MAX_PATH]; + GetModuleFileName(NULL, buffer, MAX_PATH); // Get current working directory + string::size_type pos = string(buffer).find_last_of("\\/"); + string::size_type pos2 = string(buffer).substr(0, pos).find_last_of( "\\/" ); + string logPath = string(buffer).substr(0, pos2) + "\\logs\\"; + return logPath; } void Logger::writeLog(string msg, int msg_level) { - string line; - this->logFile.open(this->filename, ios::in); + this->logFile.open(this->filePath, ios::app); if (this->logging_Level <= msg_level) { auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); this->logFile << ctime(&now) << " " << msg << endl; From ba311d5a38962d8dbf7e40dafd76d5e8c00fe566 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 16 Mar 2020 12:28:22 -0400 Subject: [PATCH 09/16] Removing old logger file --- bfs/include/Logger.h | 56 -------------------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 bfs/include/Logger.h diff --git a/bfs/include/Logger.h b/bfs/include/Logger.h deleted file mode 100644 index 685e949..0000000 --- a/bfs/include/Logger.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef LOGGER_H -#define LOGGER_H - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - - - -extern class Logger { -public: - ofstream log_file; - string filename = "log.txt"; - Logger(int l_Level, string fileName = "log.txt") { - this->logging_Level = l_Level; - this->filename = fileName; - //this->currentTime = time(NULL); - } - void write_log(string msg, int msg_level) { - string line; - ofstream log_file; - log_file.open(this->filename, std::ios_base::app); - if (this->logging_Level <= msg_level) { - //for (int i = 0; i < &msg.size; i++) - auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); - log_file << ctime(&now) << " " << msg << endl; - - } - log_file.close(); - } -private: - int logging_Level; - //auto now; - -}; - - - -//Logger* logger = NULL; -Logger* getLogger(Logger* logger) { - if (logger == NULL) { - logger = new Logger(1); - - } - return logger; -} - -//Logger* logger = getLogger(); -#endif From cabb7d40946d8fb30c73e02a44626910dffb543d Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 16 Mar 2020 12:35:11 -0400 Subject: [PATCH 10/16] CMake --- bfs/CMakeSettings.json | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/bfs/CMakeSettings.json b/bfs/CMakeSettings.json index 53e51a3..08bf11d 100644 --- a/bfs/CMakeSettings.json +++ b/bfs/CMakeSettings.json @@ -10,13 +10,7 @@ "cmakeCommandArgs": "", "buildCommandArgs": "-v", "ctestCommandArgs": "", - "variables": [ - { - "name": "CMAKE_INSTALL_PREFIX", - "value": "C:/Users/Greg/Documents/git/bfs/breadcrumbs/install/basic_build", - "type": "PATH" - } - ] + "variables": [] } ] } \ No newline at end of file From 9de731e79f8c6e134710c459c97f1e4df88a50a4 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 13 Apr 2020 20:21:45 -0400 Subject: [PATCH 11/16] Logger fixes --- bfs/src/comms/AlgorithmServer.cpp | 5 +++-- bfs/src/comms/DataSyncThread.cpp | 19 +++---------------- bfs/src/logging/Logger.cpp | 2 ++ 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/bfs/src/comms/AlgorithmServer.cpp b/bfs/src/comms/AlgorithmServer.cpp index 50899ed..4f493b3 100644 --- a/bfs/src/comms/AlgorithmServer.cpp +++ b/bfs/src/comms/AlgorithmServer.cpp @@ -3,7 +3,7 @@ #include "Logger.hpp" -extern Logger* logger = logger->getLogger(logger); +//extern Logger* logger = logger->getLogger(logger); AlgorithmServer::AlgorithmServer(size_t numClients) { this->hThread = NULL; @@ -80,6 +80,7 @@ void AlgorithmServer::serverThreadRuntime() while (continueThread) { logger->writeLog("Listening for clients...\n"); + logger->writeLog("%s", logger->filePath); printf("Listening for clients...\n"); ClientSocket = accept(ListenSocket, NULL, NULL); logger->writeLog("Hello new client!\n"); @@ -145,7 +146,7 @@ vector AlgorithmServer::getAllIncomingAttributes() Updates master storage accordingly */ vector newAttribs; - vector* newAttribs = new vector; + //vector* newAttribs = new vector; if (lockClientThreadsMutex() == STATUS_WAIT_0) { for (DataSyncThread* dst : clientThreads) diff --git a/bfs/src/comms/DataSyncThread.cpp b/bfs/src/comms/DataSyncThread.cpp index ccdf9ea..3522ff5 100644 --- a/bfs/src/comms/DataSyncThread.cpp +++ b/bfs/src/comms/DataSyncThread.cpp @@ -46,6 +46,7 @@ void DataSyncThread::threadRuntime() // : AttributeUpdate(, ) cout << this << ": AttributeUpdate(" << attr.getKey() << ", " << attr.getLength() << ")" << endl; + logger->writeLog(": AttributeUpdate(%s, %zu)\n", attr.getKey(), attr.getLength()); // Storing the attrib update addIncomingAttribute(attr); @@ -159,15 +160,8 @@ void DataSyncThread::startComms() //write_log("Start comms: ", 1, logger->filename, logger); //write_log(std::to_string(incomingAttributes), 1, logger->filename, logger); -<<<<<<< HEAD - -======= printf("Start comms: %d\n", incomingAttributes); - char buffer[100]; - snprintf(buffer, 100, "Start comms: %d\n", incomingAttributes); - string str(buffer); - logger->writeLog(str, 1); ->>>>>>> e0b4037389860f35e34eb1aafec3311e75ae367d + logger->writeLog("Start comms: %d\n", incomingAttributes); if (!threadRunning) { threadRunning = false; @@ -273,15 +267,8 @@ vector DataSyncThread::getIncomingAttributes() ReleaseMutex(attribMutex); } else { -<<<<<<< HEAD printf("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); -======= - char buffer[100]; - printf("2 Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); - snprintf(buffer, 100, "2 Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); - string str(buffer); - logger->writeLog(str, 1); ->>>>>>> e0b4037389860f35e34eb1aafec3311e75ae367d + logger->writeLog("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); } return newAttribVector; diff --git a/bfs/src/logging/Logger.cpp b/bfs/src/logging/Logger.cpp index d51b29e..88559ff 100644 --- a/bfs/src/logging/Logger.cpp +++ b/bfs/src/logging/Logger.cpp @@ -17,6 +17,7 @@ string Logger::getPath() { string::size_type pos = string(buffer).find_last_of("\\/"); string::size_type pos2 = string(buffer).substr(0, pos).find_last_of( "\\/" ); string logPath = string(buffer).substr(0, pos2) + "\\logs\\"; + //cout << logPath; return logPath; } @@ -32,6 +33,7 @@ void Logger::writeLog(const char* fmt, ...) { //<< " " << msg << endl; //} +// fprintf(this->fp, this->filePath.c_str); fclose(this->fp); } From 6f9b08ee13ca5a1709e18988f1930a8d6055bf2f Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Wed, 29 Apr 2020 12:17:56 -0400 Subject: [PATCH 12/16] Logging polishing --- bfs/include/Logger.hpp | 11 ++++------- bfs/src/comms/AlgorithmServer.cpp | 26 ++++++++++---------------- bfs/src/comms/DataSyncThread.cpp | 22 +++++++--------------- bfs/src/logging/Logger.cpp | 21 ++++++--------------- 4 files changed, 27 insertions(+), 53 deletions(-) diff --git a/bfs/include/Logger.hpp b/bfs/include/Logger.hpp index b9f7e51..475c92e 100644 --- a/bfs/include/Logger.hpp +++ b/bfs/include/Logger.hpp @@ -13,21 +13,18 @@ using namespace std; - - extern class Logger { public: - string filename; - string filePath; - FILE* fp; Logger(int l_Level); ~Logger(); - void writeLog(const char* fmt, ...); + void log(const char* fmt, ...); static Logger* getLogger(Logger* logger); string getPath(); private: int logging_Level; - //auto now; + string filename; + string filePath; + FILE* fp; }; diff --git a/bfs/src/comms/AlgorithmServer.cpp b/bfs/src/comms/AlgorithmServer.cpp index 4f493b3..54e3095 100644 --- a/bfs/src/comms/AlgorithmServer.cpp +++ b/bfs/src/comms/AlgorithmServer.cpp @@ -40,7 +40,7 @@ void AlgorithmServer::serverThreadRuntime() // Resolve the server address and port iResult = getaddrinfo(NULL, ALGORITHM_SERVER_PORT, &hints, &result); if (iResult != 0) { - printf("getaddrinfo failed with error: %d\n", iResult); + logger->log("getaddrinfo failed with error: %d\n", iResult); WSACleanup(); return; } @@ -48,7 +48,7 @@ void AlgorithmServer::serverThreadRuntime() // Create a SOCKET for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { - printf("socket failed with error: %ld\n", WSAGetLastError()); + logger->log("socket failed with error: %ld\n", WSAGetLastError()); freeaddrinfo(result); WSACleanup(); return; @@ -58,7 +58,7 @@ void AlgorithmServer::serverThreadRuntime() // Setup the TCP listening socket iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); if (iResult == SOCKET_ERROR) { - printf("bind failed with error: %d\n", WSAGetLastError()); + logger->log("bind failed with error\n", WSAGetLastError()); freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); @@ -68,8 +68,7 @@ void AlgorithmServer::serverThreadRuntime() iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - logger->writeLog("listen failed with error\n"); - printf("listen failed with error: %d\n", WSAGetLastError()); + logger->log("listen failed with error\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return; @@ -79,15 +78,11 @@ void AlgorithmServer::serverThreadRuntime() // Accept a client socket while (continueThread) { - logger->writeLog("Listening for clients...\n"); - logger->writeLog("%s", logger->filePath); - printf("Listening for clients...\n"); + logger->log("Listening for clients...\n"); ClientSocket = accept(ListenSocket, NULL, NULL); - logger->writeLog("Hello new client!\n"); - printf("Hello new client!\n"); + logger->log("Hello new client!\n"); if (ClientSocket == INVALID_SOCKET) { - logger->writeLog("accept failed with error: %d\n", WSAGetLastError()); - printf("accept failed with error: %d\n", WSAGetLastError()); + logger->log("accept failed with error: %d\n", WSAGetLastError()); AcceptFailures++; if (AcceptFailures >= MAX_ACCEPT_FAILURES) break; @@ -120,13 +115,12 @@ void AlgorithmServer::serverThreadRuntime() client->startComms(); } else { - printf("Client attempted connection when (%d) clients are already connected", numClients); + logger->log("Client attempted connection when (%d) clients are already connected", numClients); } unlockClientThreadsMutex(); } else { - logger->writeLog("Could not acquire mutex to add new client thread\n"); - printf("Could not acquire mutex to add new client thread\n"); + logger->log("Could not acquire mutex to add new client thread\n"); } } @@ -157,7 +151,7 @@ vector AlgorithmServer::getAllIncomingAttributes() unlockClientThreadsMutex(); } else { - logger->writeLog("Could not acquire mutex to add new client thread\n"); + logger->log("Could not acquire mutex to add new client thread\n"); } return newAttribs; } diff --git a/bfs/src/comms/DataSyncThread.cpp b/bfs/src/comms/DataSyncThread.cpp index 3522ff5..b8dfbb6 100644 --- a/bfs/src/comms/DataSyncThread.cpp +++ b/bfs/src/comms/DataSyncThread.cpp @@ -1,9 +1,6 @@ -//#include #include "DataSyncThread.hpp" #include "Logger.hpp" - -//extern Logger* logger = logger->getLogger(logger); void DataSyncThread::threadRuntime() { threadRunning = true; @@ -16,8 +13,7 @@ void DataSyncThread::threadRuntime() while (continueThread && iResult > 0) { - printf("Waiting to receive bytes\n"); - logger->writeLog("Waiting to receive bytes\n"); + logger->log("Waiting to receive bytes\n"); if (!readyToReceive()) continue; @@ -45,8 +41,7 @@ void DataSyncThread::threadRuntime() Attribute attr(bAttr, value); // : AttributeUpdate(, ) - cout << this << ": AttributeUpdate(" << attr.getKey() << ", " << attr.getLength() << ")" << endl; - logger->writeLog(": AttributeUpdate(%s, %zu)\n", attr.getKey(), attr.getLength()); + logger->log(": AttributeUpdate(%s, %zu)\n", attr.getKey(), attr.getLength()); // Storing the attrib update addIncomingAttribute(attr); @@ -103,11 +98,12 @@ int DataSyncThread::recvBytes(void* buffer, size_t numBytes) iResult = recv(sock, ((char*) buffer) + bytesRecved, numBytes - bytesRecved, 0); if (iResult < 0) { - printf("recv failed with error: %d\n", WSAGetLastError()); + logger->log("recv failed with error: %d\n", WSAGetLastError()); return iResult; } else if (iResult == 0) { printf("recv returned 0, connection closed.\n"); + logger->log("recv returned 0, connection closed.\n"); return iResult; } else @@ -127,7 +123,7 @@ int DataSyncThread::sendBytes(char* buffer, size_t numBytes) iResult = send(sock, buffer + bytesSent, numBytes - bytesSent, 0); if (iResult < 0) { - printf("send failed with error: %d\n", WSAGetLastError()); + logger->log("send failed with error: %d\n", WSAGetLastError()); return iResult; } else if (iResult == 0) { @@ -157,11 +153,7 @@ void DataSyncThread::startComms() /* Initializes the communication thread */ - //write_log("Start comms: ", 1, logger->filename, logger); - //write_log(std::to_string(incomingAttributes), 1, logger->filename, logger); - - printf("Start comms: %d\n", incomingAttributes); - logger->writeLog("Start comms: %d\n", incomingAttributes); + logger->log("Start comms: %d\n", incomingAttributes); if (!threadRunning) { threadRunning = false; @@ -268,7 +260,7 @@ vector DataSyncThread::getIncomingAttributes() } else { printf("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); - logger->writeLog("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); + logger->log("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); } return newAttribVector; diff --git a/bfs/src/logging/Logger.cpp b/bfs/src/logging/Logger.cpp index 88559ff..2a1807f 100644 --- a/bfs/src/logging/Logger.cpp +++ b/bfs/src/logging/Logger.cpp @@ -3,10 +3,10 @@ Logger::Logger(int l_Level) { this->logging_Level = l_Level; time_t t = time(0); // get time now - struct tm * now = localtime( & t ); + struct tm *now = localtime( & t ); char buffer [80]; - strftime (buffer,80,"%Y-%m-%d-%Hhr%Mm%Ss",now); - std::string str(buffer); + strftime (buffer, 80, "%Y-%m-%d-%Hhr%Mm%Ss", now); + string str(buffer); this->filename = str + ".log"; this->filePath = getPath() + this->filename; // Sets location and name of log file } @@ -17,23 +17,14 @@ string Logger::getPath() { string::size_type pos = string(buffer).find_last_of("\\/"); string::size_type pos2 = string(buffer).substr(0, pos).find_last_of( "\\/" ); string logPath = string(buffer).substr(0, pos2) + "\\logs\\"; - //cout << logPath; return logPath; } -void Logger::writeLog(const char* fmt, ...) { +void Logger::log(const char* fmt, ...) { this->fp = fopen(this->filePath.c_str(), "a"); - //if (this->logging_Level <= msg_level) { auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); - //this->logFile << ctime(&now); - fprintf(this->fp, ctime(&now)); - fprintf(this->fp, "\n"); - fprintf(this->fp, fmt); - fprintf(this->fp, "\n"); - //<< " " << msg << endl; - - //} -// fprintf(this->fp, this->filePath.c_str); + cout << ctime(&now) << "\n" << fmt << "\n"; + fprintf(this->fp, ctime(&now), "\n", fmt, "\n"); fclose(this->fp); } From afceba1f73007b1b1c3980d5470230d478da40cc Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Fri, 8 May 2020 12:29:21 -0400 Subject: [PATCH 13/16] CMake revert --- bfs/CMakeSettings.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bfs/CMakeSettings.json b/bfs/CMakeSettings.json index 08bf11d..53e51a3 100644 --- a/bfs/CMakeSettings.json +++ b/bfs/CMakeSettings.json @@ -10,7 +10,13 @@ "cmakeCommandArgs": "", "buildCommandArgs": "-v", "ctestCommandArgs": "", - "variables": [] + "variables": [ + { + "name": "CMAKE_INSTALL_PREFIX", + "value": "C:/Users/Greg/Documents/git/bfs/breadcrumbs/install/basic_build", + "type": "PATH" + } + ] } ] } \ No newline at end of file From edc74e07fe2283c1f9e85059cf5b9c61d008db00 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Fri, 8 May 2020 12:30:54 -0400 Subject: [PATCH 14/16] Final edits --- bfs/include/Logger.hpp | 7 ++++--- bfs/src/comms/AlgorithmServer.cpp | 22 +++++++++++----------- bfs/src/comms/DataSyncThread.cpp | 15 ++++++++------- bfs/src/logging/Logger.cpp | 16 +++++++++------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/bfs/include/Logger.hpp b/bfs/include/Logger.hpp index 475c92e..c799ee3 100644 --- a/bfs/include/Logger.hpp +++ b/bfs/include/Logger.hpp @@ -18,9 +18,10 @@ public: Logger(int l_Level); ~Logger(); void log(const char* fmt, ...); - static Logger* getLogger(Logger* logger); - string getPath(); + Logger* getLogger(); + static string getPath(); private: + static Logger* logger; int logging_Level; string filename; string filePath; @@ -28,5 +29,5 @@ private: }; -static Logger* logger = logger->getLogger(logger); +//static Logger* logger = logger->getLogger(logger); #endif diff --git a/bfs/src/comms/AlgorithmServer.cpp b/bfs/src/comms/AlgorithmServer.cpp index 54e3095..e1dd506 100644 --- a/bfs/src/comms/AlgorithmServer.cpp +++ b/bfs/src/comms/AlgorithmServer.cpp @@ -3,7 +3,7 @@ #include "Logger.hpp" -//extern Logger* logger = logger->getLogger(logger); +Logger* server_Logger = server_Logger->getLogger(); AlgorithmServer::AlgorithmServer(size_t numClients) { this->hThread = NULL; @@ -40,7 +40,7 @@ void AlgorithmServer::serverThreadRuntime() // Resolve the server address and port iResult = getaddrinfo(NULL, ALGORITHM_SERVER_PORT, &hints, &result); if (iResult != 0) { - logger->log("getaddrinfo failed with error: %d\n", iResult); + server_Logger->log("getaddrinfo failed with error: %d\n", iResult); WSACleanup(); return; } @@ -48,7 +48,7 @@ void AlgorithmServer::serverThreadRuntime() // Create a SOCKET for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { - logger->log("socket failed with error: %ld\n", WSAGetLastError()); + server_Logger->log("socket failed with error: %ld\n", WSAGetLastError()); freeaddrinfo(result); WSACleanup(); return; @@ -58,7 +58,7 @@ void AlgorithmServer::serverThreadRuntime() // Setup the TCP listening socket iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); if (iResult == SOCKET_ERROR) { - logger->log("bind failed with error\n", WSAGetLastError()); + server_Logger->log("bind failed with error\n", WSAGetLastError()); freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); @@ -68,7 +68,7 @@ void AlgorithmServer::serverThreadRuntime() iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - logger->log("listen failed with error\n", WSAGetLastError()); + server_Logger->log("listen failed with error\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return; @@ -78,11 +78,11 @@ void AlgorithmServer::serverThreadRuntime() // Accept a client socket while (continueThread) { - logger->log("Listening for clients...\n"); + server_Logger->log("Listening for clients...\n"); ClientSocket = accept(ListenSocket, NULL, NULL); - logger->log("Hello new client!\n"); + server_Logger->log("Hello new client!\n"); if (ClientSocket == INVALID_SOCKET) { - logger->log("accept failed with error: %d\n", WSAGetLastError()); + server_Logger->log("accept failed with error: %d\n", WSAGetLastError()); AcceptFailures++; if (AcceptFailures >= MAX_ACCEPT_FAILURES) break; @@ -115,12 +115,12 @@ void AlgorithmServer::serverThreadRuntime() client->startComms(); } else { - logger->log("Client attempted connection when (%d) clients are already connected", numClients); + server_Logger->log("Client attempted connection when (%d) clients are already connected", numClients); } unlockClientThreadsMutex(); } else { - logger->log("Could not acquire mutex to add new client thread\n"); + server_Logger->log("Could not acquire mutex to add new client thread\n"); } } @@ -151,7 +151,7 @@ vector AlgorithmServer::getAllIncomingAttributes() unlockClientThreadsMutex(); } else { - logger->log("Could not acquire mutex to add new client thread\n"); + server_Logger->log("Could not acquire mutex to add new client thread\n"); } return newAttribs; } diff --git a/bfs/src/comms/DataSyncThread.cpp b/bfs/src/comms/DataSyncThread.cpp index b8dfbb6..57ba5fd 100644 --- a/bfs/src/comms/DataSyncThread.cpp +++ b/bfs/src/comms/DataSyncThread.cpp @@ -1,6 +1,7 @@ #include "DataSyncThread.hpp" #include "Logger.hpp" +Logger* thread_Logger = thread_Logger->getLogger(); void DataSyncThread::threadRuntime() { threadRunning = true; @@ -13,7 +14,7 @@ void DataSyncThread::threadRuntime() while (continueThread && iResult > 0) { - logger->log("Waiting to receive bytes\n"); + thread_Logger->log("Waiting to receive bytes\n"); if (!readyToReceive()) continue; @@ -41,7 +42,7 @@ void DataSyncThread::threadRuntime() Attribute attr(bAttr, value); // : AttributeUpdate(, ) - logger->log(": AttributeUpdate(%s, %zu)\n", attr.getKey(), attr.getLength()); + thread_Logger->log(": AttributeUpdate(%s, %zu)\n", attr.getKey(), attr.getLength()); // Storing the attrib update addIncomingAttribute(attr); @@ -98,12 +99,12 @@ int DataSyncThread::recvBytes(void* buffer, size_t numBytes) iResult = recv(sock, ((char*) buffer) + bytesRecved, numBytes - bytesRecved, 0); if (iResult < 0) { - logger->log("recv failed with error: %d\n", WSAGetLastError()); + thread_Logger->log("recv failed with error: %d\n", WSAGetLastError()); return iResult; } else if (iResult == 0) { printf("recv returned 0, connection closed.\n"); - logger->log("recv returned 0, connection closed.\n"); + thread_Logger->log("recv returned 0, connection closed.\n"); return iResult; } else @@ -123,7 +124,7 @@ int DataSyncThread::sendBytes(char* buffer, size_t numBytes) iResult = send(sock, buffer + bytesSent, numBytes - bytesSent, 0); if (iResult < 0) { - logger->log("send failed with error: %d\n", WSAGetLastError()); + thread_Logger->log("send failed with error: %d\n", WSAGetLastError()); return iResult; } else if (iResult == 0) { @@ -153,7 +154,7 @@ void DataSyncThread::startComms() /* Initializes the communication thread */ - logger->log("Start comms: %d\n", incomingAttributes); + thread_Logger->log("Start comms: %d\n", incomingAttributes); if (!threadRunning) { threadRunning = false; @@ -260,7 +261,7 @@ vector DataSyncThread::getIncomingAttributes() } else { printf("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); - logger->log("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); + thread_Logger->log("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); } return newAttribVector; diff --git a/bfs/src/logging/Logger.cpp b/bfs/src/logging/Logger.cpp index 2a1807f..1d60e61 100644 --- a/bfs/src/logging/Logger.cpp +++ b/bfs/src/logging/Logger.cpp @@ -1,5 +1,7 @@ #include "Logger.hpp" +Logger* Logger::logger = NULL; + Logger::Logger(int l_Level) { this->logging_Level = l_Level; time_t t = time(0); // get time now @@ -13,24 +15,24 @@ Logger::Logger(int l_Level) { string Logger::getPath() { char buffer[MAX_PATH]; - GetModuleFileName(NULL, buffer, MAX_PATH); // Get current working directory - string::size_type pos = string(buffer).find_last_of("\\/"); + GetModuleFileName(NULL, buffer, MAX_PATH); // Get current working directory + string::size_type pos = string(buffer).find_last_of("\\/"); string::size_type pos2 = string(buffer).substr(0, pos).find_last_of( "\\/" ); string logPath = string(buffer).substr(0, pos2) + "\\logs\\"; - return logPath; + return logPath; } void Logger::log(const char* fmt, ...) { this->fp = fopen(this->filePath.c_str(), "a"); auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); cout << ctime(&now) << "\n" << fmt << "\n"; - fprintf(this->fp, ctime(&now), "\n", fmt, "\n"); + fprintf(this->fp, "%s\n%s\n", ctime(&now), fmt); fclose(this->fp); } -Logger* Logger::getLogger(Logger* logger) { - if (logger == NULL) { - logger = new Logger(1); +Logger* Logger::getLogger() { + if (this->logger == NULL) { + this->logger = new Logger(1); } return logger; From 2d08d00e7ccc2739a58a8f869f5f88421d9d6f66 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Fri, 8 May 2020 20:24:26 -0400 Subject: [PATCH 15/16] Final edits --- bfs/include/AlgorithmServer.hpp | 1 + bfs/include/DataSyncThread.hpp | 1 + bfs/include/Logger.hpp | 6 ++---- bfs/src/comms/AlgorithmServer.cpp | 25 ++++++++++++------------- bfs/src/comms/DataSyncThread.cpp | 22 +++++++++++++--------- bfs/src/logging/Logger.cpp | 12 ++++++------ 6 files changed, 35 insertions(+), 32 deletions(-) diff --git a/bfs/include/AlgorithmServer.hpp b/bfs/include/AlgorithmServer.hpp index 4928c52..b0bae90 100644 --- a/bfs/include/AlgorithmServer.hpp +++ b/bfs/include/AlgorithmServer.hpp @@ -16,6 +16,7 @@ #include "CMakeConfig.h" #include "DataSyncThread.hpp" +#include "Logger.hpp" #define MAX_ACCEPT_FAILURES 5 diff --git a/bfs/include/DataSyncThread.hpp b/bfs/include/DataSyncThread.hpp index 09a3ceb..060d83e 100644 --- a/bfs/include/DataSyncThread.hpp +++ b/bfs/include/DataSyncThread.hpp @@ -14,6 +14,7 @@ #include "Attribute.hpp" #include "CMakeConfig.h" +#include "Logger.hpp" // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib #pragma comment (lib, "Ws2_32.lib") diff --git a/bfs/include/Logger.hpp b/bfs/include/Logger.hpp index c799ee3..cd4d731 100644 --- a/bfs/include/Logger.hpp +++ b/bfs/include/Logger.hpp @@ -18,7 +18,7 @@ public: Logger(int l_Level); ~Logger(); void log(const char* fmt, ...); - Logger* getLogger(); + static Logger* getLogger(); static string getPath(); private: static Logger* logger; @@ -26,8 +26,6 @@ private: string filename; string filePath; FILE* fp; - + const char* time_Format = "%Y-%m-%d-%Hhr%Mm%Ss"; }; - -//static Logger* logger = logger->getLogger(logger); #endif diff --git a/bfs/src/comms/AlgorithmServer.cpp b/bfs/src/comms/AlgorithmServer.cpp index e1dd506..c5fe097 100644 --- a/bfs/src/comms/AlgorithmServer.cpp +++ b/bfs/src/comms/AlgorithmServer.cpp @@ -1,9 +1,7 @@ #include "AlgorithmServer.hpp" -#include "Logger.hpp" -Logger* server_Logger = server_Logger->getLogger(); AlgorithmServer::AlgorithmServer(size_t numClients) { this->hThread = NULL; @@ -20,6 +18,7 @@ AlgorithmServer::~AlgorithmServer() void AlgorithmServer::serverThreadRuntime() { + Logger* logger = logger->getLogger(); int iResult; clientThreadsMutex = CreateMutex(NULL, false, NULL); @@ -40,7 +39,7 @@ void AlgorithmServer::serverThreadRuntime() // Resolve the server address and port iResult = getaddrinfo(NULL, ALGORITHM_SERVER_PORT, &hints, &result); if (iResult != 0) { - server_Logger->log("getaddrinfo failed with error: %d\n", iResult); + logger->log("getaddrinfo failed with error: %d\n", iResult); WSACleanup(); return; } @@ -48,7 +47,7 @@ void AlgorithmServer::serverThreadRuntime() // Create a SOCKET for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { - server_Logger->log("socket failed with error: %ld\n", WSAGetLastError()); + logger->log("socket failed with error: %ld\n", WSAGetLastError()); freeaddrinfo(result); WSACleanup(); return; @@ -58,7 +57,7 @@ void AlgorithmServer::serverThreadRuntime() // Setup the TCP listening socket iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); if (iResult == SOCKET_ERROR) { - server_Logger->log("bind failed with error\n", WSAGetLastError()); + logger->log("bind failed with error\n", WSAGetLastError()); freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); @@ -68,7 +67,7 @@ void AlgorithmServer::serverThreadRuntime() iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - server_Logger->log("listen failed with error\n", WSAGetLastError()); + logger->log("listen failed with error\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return; @@ -78,11 +77,11 @@ void AlgorithmServer::serverThreadRuntime() // Accept a client socket while (continueThread) { - server_Logger->log("Listening for clients...\n"); + logger->log("Listening for clients...\n"); ClientSocket = accept(ListenSocket, NULL, NULL); - server_Logger->log("Hello new client!\n"); + logger->log("Hello new client!\n"); if (ClientSocket == INVALID_SOCKET) { - server_Logger->log("accept failed with error: %d\n", WSAGetLastError()); + logger->log("accept failed with error: %d\n", WSAGetLastError()); AcceptFailures++; if (AcceptFailures >= MAX_ACCEPT_FAILURES) break; @@ -115,12 +114,12 @@ void AlgorithmServer::serverThreadRuntime() client->startComms(); } else { - server_Logger->log("Client attempted connection when (%d) clients are already connected", numClients); + logger->log("Client attempted connection when (%d) clients are already connected", numClients); } unlockClientThreadsMutex(); } else { - server_Logger->log("Could not acquire mutex to add new client thread\n"); + logger->log("Could not acquire mutex to add new client thread\n"); } } @@ -139,8 +138,8 @@ vector AlgorithmServer::getAllIncomingAttributes() Polls DataSyncThreads for new updates Updates master storage accordingly */ + Logger* logger = logger->getLogger(); vector newAttribs; - //vector* newAttribs = new vector; if (lockClientThreadsMutex() == STATUS_WAIT_0) { for (DataSyncThread* dst : clientThreads) @@ -151,7 +150,7 @@ vector AlgorithmServer::getAllIncomingAttributes() unlockClientThreadsMutex(); } else { - server_Logger->log("Could not acquire mutex to add new client thread\n"); + logger->log("Could not acquire mutex to add new client thread\n"); } return newAttribs; } diff --git a/bfs/src/comms/DataSyncThread.cpp b/bfs/src/comms/DataSyncThread.cpp index 57ba5fd..7a60649 100644 --- a/bfs/src/comms/DataSyncThread.cpp +++ b/bfs/src/comms/DataSyncThread.cpp @@ -1,9 +1,9 @@ #include "DataSyncThread.hpp" -#include "Logger.hpp" -Logger* thread_Logger = thread_Logger->getLogger(); + void DataSyncThread::threadRuntime() { + Logger* logger = logger->getLogger(); threadRunning = true; cout << "Starting DataSyncThread: " << this << endl; @@ -14,7 +14,7 @@ void DataSyncThread::threadRuntime() while (continueThread && iResult > 0) { - thread_Logger->log("Waiting to receive bytes\n"); + logger->log("Waiting to receive bytes\n"); if (!readyToReceive()) continue; @@ -42,7 +42,7 @@ void DataSyncThread::threadRuntime() Attribute attr(bAttr, value); // : AttributeUpdate(, ) - thread_Logger->log(": AttributeUpdate(%s, %zu)\n", attr.getKey(), attr.getLength()); + logger->log(": AttributeUpdate(%s, %zu)\n", attr.getKey(), attr.getLength()); // Storing the attrib update addIncomingAttribute(attr); @@ -91,6 +91,7 @@ bool DataSyncThread::readyToReceive(int interval) // Garentees receiving the given number of bytes int DataSyncThread::recvBytes(void* buffer, size_t numBytes) { + Logger* logger = logger->getLogger(); size_t bytesRecved = 0; int iResult; @@ -99,12 +100,12 @@ int DataSyncThread::recvBytes(void* buffer, size_t numBytes) iResult = recv(sock, ((char*) buffer) + bytesRecved, numBytes - bytesRecved, 0); if (iResult < 0) { - thread_Logger->log("recv failed with error: %d\n", WSAGetLastError()); + logger->log("recv failed with error: %d\n", WSAGetLastError()); return iResult; } else if (iResult == 0) { printf("recv returned 0, connection closed.\n"); - thread_Logger->log("recv returned 0, connection closed.\n"); + logger->log("recv returned 0, connection closed.\n"); return iResult; } else @@ -116,6 +117,7 @@ int DataSyncThread::recvBytes(void* buffer, size_t numBytes) // Garentees sending the given number of bytes int DataSyncThread::sendBytes(char* buffer, size_t numBytes) { + Logger* logger = logger->getLogger(); size_t bytesSent = 0; int iResult; @@ -124,7 +126,7 @@ int DataSyncThread::sendBytes(char* buffer, size_t numBytes) iResult = send(sock, buffer + bytesSent, numBytes - bytesSent, 0); if (iResult < 0) { - thread_Logger->log("send failed with error: %d\n", WSAGetLastError()); + logger->log("send failed with error: %d\n", WSAGetLastError()); return iResult; } else if (iResult == 0) { @@ -154,7 +156,8 @@ void DataSyncThread::startComms() /* Initializes the communication thread */ - thread_Logger->log("Start comms: %d\n", incomingAttributes); + Logger* logger = logger->getLogger(); + logger->log("Start comms: %d\n", incomingAttributes); if (!threadRunning) { threadRunning = false; @@ -247,6 +250,7 @@ int DataSyncThread::connectToAlgorithm(char* serverName) vector DataSyncThread::getIncomingAttributes() { + Logger* logger = logger->getLogger(); vector newAttribVector; if (! areIncomingAttributesAvailable()) return newAttribVector; @@ -261,7 +265,7 @@ vector DataSyncThread::getIncomingAttributes() } else { printf("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); - thread_Logger->log("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); + logger->log("Failed to acquire mutex, wait returned %x, error: %d\n", result, GetLastError()); } return newAttribVector; diff --git a/bfs/src/logging/Logger.cpp b/bfs/src/logging/Logger.cpp index 1d60e61..50c7026 100644 --- a/bfs/src/logging/Logger.cpp +++ b/bfs/src/logging/Logger.cpp @@ -1,13 +1,14 @@ #include "Logger.hpp" Logger* Logger::logger = NULL; +//string Logger::filename = ""; Logger::Logger(int l_Level) { this->logging_Level = l_Level; time_t t = time(0); // get time now - struct tm *now = localtime( & t ); + struct tm *now = localtime(&t); char buffer [80]; - strftime (buffer, 80, "%Y-%m-%d-%Hhr%Mm%Ss", now); + strftime (buffer, 80, this->time_Format, now); string str(buffer); this->filename = str + ".log"; this->filePath = getPath() + this->filename; // Sets location and name of log file @@ -24,16 +25,15 @@ string Logger::getPath() { void Logger::log(const char* fmt, ...) { this->fp = fopen(this->filePath.c_str(), "a"); - auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); + auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); // long long __time64_t cout << ctime(&now) << "\n" << fmt << "\n"; fprintf(this->fp, "%s\n%s\n", ctime(&now), fmt); fclose(this->fp); } Logger* Logger::getLogger() { - if (this->logger == NULL) { - this->logger = new Logger(1); - + if (logger == NULL) { + logger = new Logger(1); } return logger; } \ No newline at end of file From b1393ff48b7a979748d27544d234067a458b2834 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Fri, 8 May 2020 20:29:38 -0400 Subject: [PATCH 16/16] Final edits --- bfs/include/Logger.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bfs/include/Logger.hpp b/bfs/include/Logger.hpp index cd4d731..371a7f9 100644 --- a/bfs/include/Logger.hpp +++ b/bfs/include/Logger.hpp @@ -13,7 +13,7 @@ using namespace std; -extern class Logger { +class Logger { public: Logger(int l_Level); ~Logger();