Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions include/log/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Logger.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgreau <lgreau@student.42heilbronn.de> +#+ +:+ +#+ */
/* By: flfische <flfische@student.42heilbronn. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/09 14:14:03 by flfische #+# #+# */
/* Updated: 2024/10/15 16:21:42 by lgreau ### ########.fr */
/* Updated: 2025/01/17 09:39:23 by flfische ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,17 +19,23 @@

#include "ft_iomanip.hpp"

#define TRACE_COLOR PURPLE
#define DEBUG_COLOR BLUE
#define INFO_COLOR CYAN
#define WARN_COLOR YELLOW
#define ERROR_COLOR RED

#define TRACE_PREFIX TRACE_COLOR "[TRACE]" RESET_COLOR
#define DEBUG_PREFIX DEBUG_COLOR "[DEBUG]" RESET_COLOR
#define INFO_PREFIX INFO_COLOR "[INFO]" RESET_COLOR
#define WARN_PREFIX WARN_COLOR "[WARN]" RESET_COLOR
#define ERROR_PREFIX ERROR_COLOR "[ERROR]" RESET_COLOR

enum LogLevel { DEBUG, INFO, WARN, ERROR };
#ifndef LOG_LEVEL
#define LOG_LEVEL DEBUG
#endif

enum LogLevel { TRACE, DEBUG, INFO, WARN, ERROR };

class Logger {
public:
Expand All @@ -52,6 +58,7 @@ class Logger {

std::ostream& operator<<(std::ostream& os, LogLevel level);

#define LOG_TRACE(msg) Logger::getInstance().log(msg, TRACE)
#define LOG_DEBUG(msg) Logger::getInstance().log(msg, DEBUG)
#define LOG_INFO(msg) Logger::getInstance().log(msg, INFO)
#define LOG_WARN(msg) Logger::getInstance().log(msg, WARN)
Expand Down
7 changes: 3 additions & 4 deletions src/Server/ClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ bool ClientConnection::_receiveHeader() {
return false;
}

LOG_DEBUG(_log("Header content: \n" + std::string(_headerBuffer.begin(), _headerBuffer.end())));
LOG_TRACE(_log("Header content: \n" + std::string(_headerBuffer.begin(), _headerBuffer.end())));
LOG_DEBUG(_log("Header buffer size after read: " + std::to_string(_headerBuffer.size())));

std::vector<char> header;
Expand Down Expand Up @@ -435,15 +435,14 @@ bool ClientConnection::_parseHttpRequestHeader(const std::string& header) {

void ClientConnection::sendResponse() {
if (_status != Status::READY_TO_SEND) {
// LOG_ERROR("Response is not ready to be sent");
return;
}
if (!_response.getStatus()) {
LOG_DEBUG(_log("Building response for request"));
_response = _requestHandler.handleRequest(_request);
}
LOG_INFO(_log("Sending response with status code: " + std::to_string(_response.getStatus())));
LOG_DEBUG(_log("Response: \n" + _response.toString()));
LOG_TRACE(_log("Response: \n" + _response.toString()));
if (!_sendDataToClient(_response.toString())) {
return;
}
Expand Down Expand Up @@ -508,7 +507,7 @@ std::optional<size_t> ClientConnection::_findHeaderEnd(const std::vector<char>&
bool ClientConnection::isDisconnected() const { return _disconnected; }

void ClientConnection::_logHeader() const {
LOG_DEBUG(_log("Request recieved:\n====================\n" + toString(_request) + "\n===================="));
LOG_TRACE(_log("Request recieved:\n====================\n" + toString(_request) + "\n===================="));
}

std::string ClientConnection::_log(const std::string& msg) const {
Expand Down
3 changes: 1 addition & 2 deletions src/http/RequestHandler/PostRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: flfische <flfische@student.42heilbronn. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/11 14:43:11 by flfische #+# #+# */
/* Updated: 2025/01/15 16:03:33 by flfische ### ########.fr */
/* Updated: 2025/01/16 20:26:16 by flfische ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -104,7 +104,6 @@ std::string buildpath(const std::string &path, const std::string &filename, cons
}

HttpResponse RequestHandler::handleFileUpload(const std::string &part, const std::string &contentDisposition) {
LOG_DEBUG("Handling file upload");
std::string filename;
std::size_t filenamePos = contentDisposition.find("filename=");
if (filenamePos != std::string::npos) {
Expand Down
8 changes: 3 additions & 5 deletions src/http/messages/HttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: flfische <flfische@student.42heilbronn. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 16:19:37 by flfische #+# #+# */
/* Updated: 2025/01/15 21:16:39 by flfische ### ########.fr */
/* Updated: 2025/01/17 09:28:25 by flfische ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -79,8 +79,6 @@ void HttpRequest::_validateRequestLine() const {
}

void HttpRequest::_validateHeaders() const {
// TODO: check if Host header is required
// TODO: check if other stuff is required
if (_method == "POST" && _bodyType == BodyType::NO_BODY) {
throw BadRequest();
}
Expand All @@ -93,10 +91,10 @@ void HttpRequest::_parseURI() {
}
LOG_DEBUG(" |- Location: " + _location);
if (size_t queryStart = _location.find_first_of('?'); queryStart != std::string::npos) {
LOG_DEBUG(" |- Query string found: " + _location);
LOG_TRACE(" |- Query string found: " + _location);
_queryString = _location.substr(queryStart + 1, _location.back());
_location = _location.substr(0, queryStart);
LOG_DEBUG(" |- Query string: " + _queryString);
LOG_TRACE(" |- Query string: " + _queryString);
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/log/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: flfische <flfische@student.42heilbronn. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/09 14:20:32 by flfische #+# #+# */
/* Updated: 2024/10/09 15:21:54 by flfische ### ########.fr */
/* Updated: 2025/01/17 09:29:31 by flfische ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -33,6 +33,8 @@ void Logger::setOutputToConsole() {
}

void Logger::log(const std::string& msg, const LogLevel level) {
if (level < LOG_LEVEL)
return;
const std::time_t now = std::time(nullptr);
if (outputToFile) {
logFile << std::left << std::put_time(std::localtime(&now), "%F %T") << " " << std::left << std::setw(20)
Expand All @@ -45,6 +47,9 @@ void Logger::log(const std::string& msg, const LogLevel level) {

std::ostream& operator<<(std::ostream& os, const LogLevel level) {
switch (level) {
case TRACE:
os << TRACE_PREFIX;
break;
case DEBUG:
os << DEBUG_PREFIX;
break;
Expand Down
Loading