Skip to content

Nick #14

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
Prev Previous commit
Next Next commit
REBASE 3/5
  • Loading branch information
ncc14003 committed Mar 16, 2020
commit d43c958b25a1c9975cf828de6858ae7c580f3147
4 changes: 3 additions & 1 deletion bfs/include/Logger.hpp
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
#include <time.h>
#include <chrono>
#include <ctime>
#include <windows.h>

using namespace std;

@@ -23,11 +24,12 @@ extern class Logger {
~Logger();
void writeLog(string msg, int msg_level);
static Logger* getLogger(Logger* logger);
string getPath();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these functions can be private I think

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This getPath function should be static

private:
int logging_Level;
//auto now;

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of this space

};


static Logger* logger = logger->getLogger(logger);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of this global, if we need to use the logger in the code, we can just call getLogger(). Instead, add it as a private static pointer in the class initialized to null at first. The first time getLogger is called, this private static variable will be null, so initialize it. Every other call to getLogger (after the first) will just return the pointer and NOT reinitialize it

#endif
24 changes: 16 additions & 8 deletions bfs/src/comms/AlgorithmServer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

#include "AlgorithmServer.hpp"
#include <Logger.h>
#include "Logger.hpp"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be included in the algorithm server hpp file not the cpp file.



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");
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of this printf

>>>>>>> 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());
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of this printf

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");
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of this printf

}
}
@@ -172,9 +176,13 @@ vector<Attribute> 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;
5 changes: 4 additions & 1 deletion bfs/src/comms/DataSyncThread.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//#include <Logger.h>
#include "DataSyncThread.hpp"
//include<Logger.h>
#include "Logger.hpp"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, do not include things in cpp files. It should be included in the hpp file corresponding to this cpp file.



//extern Logger* logger = logger->getLogger(logger);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of this and all other commented out code

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;
17 changes: 12 additions & 5 deletions bfs/src/logging/Logger.cpp
Original file line number Diff line number Diff line change
@@ -5,16 +5,23 @@ Logger::Logger(int l_Level) {
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix spacing

char buffer [80];
strftime (buffer,80,"%Y-%m-%d %H:%M:%S",now);
strftime (buffer,80,"%Y-%m-%d-%Hhr%Mm%Ss",now);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the spacing between commas in the function call here

std::string str(buffer);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get rid of all the std:: in this file because you have "using namespace std" in your header

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() {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the indenting in this function and get rid of the commented out code. Also, can't this function be static?

char buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH); // Get current working directory
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still didn't fix the indenting here

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;