Skip to content

Commit

Permalink
chore(debug): remove printFrame and embed it in Debug::log
Browse files Browse the repository at this point in the history
  • Loading branch information
552020 committed May 23, 2024
1 parent 8c917ca commit c029abb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
28 changes: 22 additions & 6 deletions src/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,35 @@ void Debug::removeLevel(Debug::Level level)
debugLevel = static_cast<Debug::Level>(debugLevel & ~level);
}

void Debug::log(const std::string &message, Debug::Level paramLevel, const std::string &color)
void printFrame(const std::string &message, const std::string &color, bool blinking)
{
std::string blink = blinking ? BLINKING : "";
std::cout << color << blink;
std::cout << std::setw(10) << ' ' << "***************************" << std::endl;
std::cout << std::setw(10) << ' ' << "* " << message << " *" << std::endl;
std::cout << std::setw(10) << ' ' << "***************************" << std::endl;
std::cout << RESET;
}

void Debug::log(const std::string &message, Debug::Level paramLevel, const std::string &color, bool bliking, bool frame)
{
if (!debugEnabled)
{
return;
}
if (debugLevel & paramLevel)
{
std::cout << color << message << "\033[0m" << std::endl;
std::string blink = bliking ? BLINKING : "";
if (frame)
{
printFrame(message, color, bliking);
}
else
std::cout << color << blink << message << RESET << std::endl;
}
}

void Debug::log(const std::string &message, Debug::Level paramLevel)
{
log(message, paramLevel, RESET);
}
// void Debug::log(const std::string &message, Debug::Level paramLevel)
// {
// log(message, paramLevel, RESET);
// }
9 changes: 7 additions & 2 deletions src/Debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define DEBUG_HPP

#include <iostream>
#include <iomanip>
#include <string>

class Debug
Expand Down Expand Up @@ -33,8 +34,12 @@ class Debug
static void setLevel(Level level);
static void addLevel(Level level);
static void removeLevel(Level leve);
static void log(const std::string &message, Debug::Level paramLevel);
static void log(const std::string &message, Debug::Level paramLevel, const std::string &color);
// static void log(const std::string &message, Debug::Level paramLevel);
static void log(const std::string &message,
Debug::Level paramLevel,
const std::string &color = "\033[0m",
bool bliking = false,
bool frame = false);
};

#endif
10 changes: 4 additions & 6 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void Server::startPollEventLoop()
<< " Waiting for new connection or Polling +++++++++++++++" << RESET << std::endl;
int ret = poll(_FDs.data(), _FDs.size(), timeout);
pollCounter++;
// printFrame("POLL EVENT DETECTED", true);
Debug::log("Poll event detected", Debug::SERVER, YELLOW, false, true);
// printConnections("AFTER POLL", _FDs, _connections, true);
if (ret > 0)
{
Expand Down Expand Up @@ -476,10 +476,8 @@ void Server::handleConnection(Connection &conn, size_t &i)
HTTPRequest &request = _connections[i].getRequest();
HTTPResponse &response = _connections[i].getResponse();

// printFrame("CLIENT SOCKET EVENT", true);
std::cout << "\033[1;36m"
<< "Entering handleConnection"
<< "\033[0m" << std::endl;
Debug::log("Client socket event", Debug::SERVER, CYAN, false, true);
Debug::log("Entering handleConnection", Debug::SERVER);

conn.setHasReadSocket(false);
std::cout << "Has finished reading: " << conn.getHasFinishedReading() << std::endl;
Expand Down Expand Up @@ -728,7 +726,7 @@ void Server::addServerSocketsPollFdToVectors()
void Server::acceptNewConnection(Connection &conn)
{

printFrame("SERVER SOCKET EVENT", true);
Debug::log("SERVER SOCKET EVENT", Debug::SERVER, CYAN, false, true);
struct sockaddr_storage clientAddress;
socklen_t ClientAddrLen = sizeof(clientAddress);
Debug::log("New connection detected", Debug::SERVER);
Expand Down
10 changes: 0 additions & 10 deletions src/server_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@ void printVariablesHeadersBody(const HTTPRequest &obj)
std::cout << c << std::endl;
}

void printFrame(std::string frame, bool value)
{
if (!value)
return;
std::cout << ORANGE << BLINKING;
std::cout << std::setw(10) << ' ' << "***************************" << std::endl;
std::cout << std::setw(10) << ' ' << "* " << frame << " *" << std::endl;
std::cout << std::setw(10) << ' ' << "***************************" << std::endl;
std::cout << RESET;
}

void printConnections(const std::string &location,
const std::vector<pollfd> &_FDs,
Expand Down
1 change: 0 additions & 1 deletion src/server_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@ void printConnections(const std::string &location,
const std::vector<pollfd> &_FDs,
const std::vector<Connection> &_connections,
bool value);
void printFrame(std::string frame, bool value = false);

#endif // SERVER_UTILS_HPP

0 comments on commit c029abb

Please sign in to comment.