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.h b/bfs/include/Logger.h deleted file mode 100644 index de4420c..0000000 --- a/bfs/include/Logger.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef LOGGER_H -#define LOGGER_H - -#include -#include -#include -#include -#include - -using namespace std; - - - -extern class Logger { -public: - int logging_Level = 1; - string filename = "log.txt"; - - -}; - -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() { - if (logger == NULL) { - logger = new Logger(); - - } - return logger; -} - -//Logger* logger = getLogger(); -#endif diff --git a/bfs/include/Logger.hpp b/bfs/include/Logger.hpp new file mode 100644 index 0000000..371a7f9 --- /dev/null +++ b/bfs/include/Logger.hpp @@ -0,0 +1,31 @@ +#ifndef LOGGER_HPP +#define LOGGER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +class Logger { +public: + Logger(int l_Level); + ~Logger(); + void log(const char* fmt, ...); + static Logger* getLogger(); + static string getPath(); +private: + static Logger* logger; + int logging_Level; + string filename; + string filePath; + FILE* fp; + const char* time_Format = "%Y-%m-%d-%Hhr%Mm%Ss"; +}; +#endif diff --git a/bfs/scripts/buildbfs.pyc b/bfs/scripts/buildbfs.pyc new file mode 100644 index 0000000..26df4a0 Binary files /dev/null and b/bfs/scripts/buildbfs.pyc differ diff --git a/bfs/scripts/log.txt b/bfs/scripts/log.txt deleted file mode 100644 index 408b766..0000000 --- a/bfs/scripts/log.txt +++ /dev/null @@ -1,337 +0,0 @@ -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Could not acquire mutex to add new client thread -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... -Hello new client! -Listening for clients... diff --git a/bfs/scripts/startbfs.pyc b/bfs/scripts/startbfs.pyc new file mode 100644 index 0000000..545f785 Binary files /dev/null and b/bfs/scripts/startbfs.pyc differ diff --git a/bfs/src/comms/AlgorithmServer.cpp b/bfs/src/comms/AlgorithmServer.cpp index 3521eb4..c5fe097 100644 --- a/bfs/src/comms/AlgorithmServer.cpp +++ b/bfs/src/comms/AlgorithmServer.cpp @@ -1,6 +1,5 @@ #include "AlgorithmServer.hpp" -#include AlgorithmServer::AlgorithmServer(size_t numClients) @@ -19,7 +18,7 @@ AlgorithmServer::~AlgorithmServer() void AlgorithmServer::serverThreadRuntime() { - //Logger* logger = getLogger(); + 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) { - printf("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) { - printf("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) { - printf("bind failed with error: %d\n", WSAGetLastError()); + logger->log("bind failed with error\n", WSAGetLastError()); freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); @@ -68,8 +67,7 @@ void AlgorithmServer::serverThreadRuntime() iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - //write_log("listen failed with error\n", 1, logger->filename, logger); - printf("listen failed with error: %d\n", WSAGetLastError()); + logger->log("listen failed with error\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return; @@ -79,16 +77,11 @@ void AlgorithmServer::serverThreadRuntime() // Accept a client socket while (continueThread) { - write_log("Listening for clients...\n", 1, logger->filename, logger); - fprintf(stderr, "Listening for clients...\n"); + logger->log("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->log("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); - printf("accept failed with error: %d\n", WSAGetLastError()); + logger->log("accept failed with error: %d\n", WSAGetLastError()); AcceptFailures++; if (AcceptFailures >= MAX_ACCEPT_FAILURES) break; @@ -121,13 +114,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 { - write_log("Could not acquire mutex to add new client thread\n", 1, logger->filename, logger); - printf("Could not acquire mutex to add new client thread\n"); + logger->log("Could not acquire mutex to add new client thread\n"); } } @@ -146,7 +138,7 @@ vector AlgorithmServer::getAllIncomingAttributes() Polls DataSyncThreads for new updates Updates master storage accordingly */ - Logger* logger = getLogger(); + Logger* logger = logger->getLogger(); vector newAttribs; if (lockClientThreadsMutex() == STATUS_WAIT_0) { @@ -158,7 +150,7 @@ vector AlgorithmServer::getAllIncomingAttributes() unlockClientThreadsMutex(); } else { - printf("Could not acquire mutex to get incoming attributes.\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 9d22dec..7a60649 100644 --- a/bfs/src/comms/DataSyncThread.cpp +++ b/bfs/src/comms/DataSyncThread.cpp @@ -1,9 +1,9 @@ -//#include #include "DataSyncThread.hpp" -//include + void DataSyncThread::threadRuntime() { + Logger* logger = logger->getLogger(); threadRunning = true; cout << "Starting DataSyncThread: " << this << endl; @@ -14,6 +14,7 @@ void DataSyncThread::threadRuntime() while (continueThread && iResult > 0) { + logger->log("Waiting to receive bytes\n"); if (!readyToReceive()) continue; @@ -41,7 +42,7 @@ void DataSyncThread::threadRuntime() Attribute attr(bAttr, value); // : AttributeUpdate(, ) - cout << this << ": AttributeUpdate(" << attr.getKey() << ", " << attr.getLength() << ")" << endl; + logger->log(": AttributeUpdate(%s, %zu)\n", attr.getKey(), attr.getLength()); // Storing the attrib update addIncomingAttribute(attr); @@ -90,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; @@ -98,11 +100,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 @@ -114,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; @@ -122,7 +126,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) { @@ -152,9 +156,8 @@ 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); - + 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,6 +265,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()); } return newAttribVector; diff --git a/bfs/src/logging/Logger.cpp b/bfs/src/logging/Logger.cpp new file mode 100644 index 0000000..50c7026 --- /dev/null +++ b/bfs/src/logging/Logger.cpp @@ -0,0 +1,39 @@ +#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); + char buffer [80]; + 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 +} + +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::log(const char* fmt, ...) { + this->fp = fopen(this->filePath.c_str(), "a"); + 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 (logger == NULL) { + logger = new Logger(1); + } + return logger; +} \ No newline at end of file