Skip to content

Commit d359a64

Browse files
iboukhssStokesAsselborn
authored andcommitted
feat: add macros for logging and debugging
1 parent 2de7f06 commit d359a64

File tree

11 files changed

+163
-25
lines changed

11 files changed

+163
-25
lines changed

.clang-format

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,25 @@ SpaceAfterCStyleCast: true
99
IndentAccessModifiers: false
1010
AccessModifierOffset: -4
1111

12-
#AllowShortCaseLabelsOnASingleLine: true
1312
AllowShortFunctionsOnASingleLine: InlineOnly
1413
BreakConstructorInitializers: BeforeColon
1514
PackConstructorInitializers: Never
1615

16+
AllowShortCaseLabelsOnASingleLine: true
17+
AlignConsecutiveShortCaseStatements:
18+
Enabled: true
19+
20+
# Fails on ubuntu-latest
21+
#AlignEscapedNewlines: LeftWithLastLine
22+
23+
AlignEscapedNewlines: DontAlign
24+
AlignConsecutiveMacros:
25+
Enabled: true
26+
27+
# Fails on ubuntu-latest
28+
#BreakTemplateDeclarations: Yes
29+
AlwaysBreakTemplateDeclarations: true
30+
1731
SortIncludes: true
1832
IncludeBlocks: Regroup
1933
IncludeCategories:

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ SRCS = $(addprefix $(SRC_DIR)/, \
3232
http/http_response.hpp \
3333
router/router.cpp \
3434
router/router.hpp \
35+
util/log_message.cpp \
36+
util/log_message.hpp \
3537
util/str_split.cpp \
3638
util/str_trim.cpp \
3739
util/string.hpp \

src/core/client.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "core/client.hpp"
22

3+
#include "util/log_message.hpp"
34
#include "util/syscall_error.hpp"
45

56
#include <fcntl.h>
@@ -14,7 +15,7 @@ Client::Client(uint64_t id, int fd, const sockaddr_in& addr)
1415
handler_(NULL)
1516
{
1617
set_nonblocking();
17-
std::cout << "Client constructor called" << std::endl;
18+
LOG(DEBUG) << "Client constructor called";
1819
}
1920

2021
Client::~Client()
@@ -25,7 +26,7 @@ Client::~Client()
2526
if (handler_) {
2627
delete handler_;
2728
}
28-
std::cout << "Client destructor called" << std::endl;
29+
LOG(DEBUG) << "Client destructor called";
2930
}
3031

3132
void Client::set_nonblocking()

src/core/server.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "core/signals.hpp"
55
#include "http/http_parser.hpp"
66
#include "router/router.hpp"
7+
#include "util/log_message.hpp"
78
#include "util/syscall_error.hpp"
89

910
#include <errno.h>
@@ -85,7 +86,7 @@ void Server::close_connection(Client& conn)
8586

8687
std::map<uint64_t, Client*>::iterator it = clients_.find(client_id);
8788
if (it == clients_.end()) {
88-
std::cerr << "[WARNING] Attempted to remove inexistant client id" << std::endl;
89+
LOG(WARN) << "Attempted to remove inexistand client id";
8990
return;
9091
}
9192

@@ -97,7 +98,7 @@ Client& Server::get_client(uint64_t id)
9798
{
9899
std::map<uint64_t, Client*>::iterator it = clients_.find(id);
99100
if (it == clients_.end()) {
100-
throw std::runtime_error("[FATAL] Attempted to retrieve inexistant client id");
101+
throw std::runtime_error("Attempted to retrieve inexistant client id");
101102
}
102103
return *(it->second);
103104
}
@@ -121,7 +122,7 @@ void Server::accept_connection()
121122
ev.data.u64 = client_id;
122123
epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, client_fd, &ev);
123124

124-
std::cout << "* Connection established, client_id = " << client_id << std::endl;
125+
LOG(INFO) << "Client #" << client_id << " connected";
125126
}
126127

127128
void Server::run()
@@ -135,8 +136,7 @@ void Server::run()
135136
// nothing happened.
136137
n_events = 0;
137138
break;
138-
default:
139-
throw UnrecoverableError("epoll_wait", errno);
139+
default: throw UnrecoverableError("epoll_wait", errno);
140140
}
141141
}
142142

@@ -193,7 +193,7 @@ void Server::handle_events(Client& conn, uint32_t events)
193193
return;
194194
}
195195

196-
std::cout << "* Received data from socket" << std::endl;
196+
LOG(DEBUG) << "Received " << bytes_received << " bytes from socket";
197197
print_raw_data(tmp);
198198

199199
// Feed data to the parser

src/core/server_defaults.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef SERVER_DEFAULTS_HPP_
22
#define SERVER_DEFAULTS_HPP_
33

4-
#define WEBSERV_DEFAULT_PORT 8080
5-
#define WEBSERV_DEFAULT_BACKLOG 128
4+
#define WEBSERV_DEFAULT_PORT 8080
5+
#define WEBSERV_DEFAULT_BACKLOG 128
66
#define WEBSERV_DEFAULT_MAX_BODY_SIZE (1 << 20) // 1MB
77

88
#endif // SERVER_DEFAULTS_HPP_

src/handler/static_file_handler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "handler/static_file_handler.hpp"
22

33
#include "http/http_response.hpp"
4+
#include "util/log_message.hpp"
45
#include "util/syscall_error.hpp"
56

67
#include <errno.h>
@@ -22,7 +23,7 @@ StaticFileHandler::StaticFileHandler(const std::string& path)
2223
HttpResponse res;
2324
res.http_version = "HTTP/1.1";
2425

25-
std::cout << "[DEBUG] Trying to open file: " << path << std::endl;
26+
LOG(DEBUG) << "Trying to open file: " << path;
2627

2728
if (stat(path.c_str(), &file_stat) != 0 || !S_ISREG(file_stat.st_mode)) {
2829
res.code = HttpResponse::kNotFound;

src/http/http_response.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22

33
#include <sstream>
44

5-
const char* HttpResponse::reason_phrase(Status code) const
5+
const char* HttpResponse::reason_phrase(HttpResponse::Status code) const
66
{
7-
/* clang-format off */
87
switch (code) {
9-
case kOk: return "OK";
10-
case kBadRequest: return "Bad Request";
11-
case kForbidden: return "Forbidden";
12-
case kNotFound: return "Not Found";
13-
case kMethodNotAllowed: return "Method Not Allowed";
8+
case kOk: return "OK";
9+
case kBadRequest: return "Bad Request";
10+
case kForbidden: return "Forbidden";
11+
case kNotFound: return "Not Found";
12+
case kMethodNotAllowed: return "Method Not Allowed";
1413
case kInternalServerError: return "Internal Server Error";
15-
case kNotImplemented: return "Not Implemented";
16-
default: return "Unknown";
14+
case kNotImplemented: return "Not Implemented";
1715
}
18-
/* clang-format on */
1916
}
2017

2118
std::string HttpResponse::to_string() const

src/http/http_response.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ struct HttpResponse {
1717
};
1818

1919
std::string http_version;
20-
Status code;
20+
HttpResponse::Status code;
2121
std::map<std::string, std::string> headers;
2222
std::string body;
2323

2424
std::string to_string() const;
2525

2626
private:
27-
const char* reason_phrase(Status code) const;
27+
const char* reason_phrase(HttpResponse::Status code) const;
2828
};
2929

3030
#endif // HTTP_HTTP_RESPONSE_HPP_

src/router/router.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "config/server_config.hpp"
44
#include "handler/static_file_handler.hpp"
55
#include "http/http_request.hpp"
6+
#include "util/log_message.hpp"
67

78
#include <sys/stat.h>
89

@@ -58,7 +59,6 @@ std::string Router::build_request_path(const HttpRequest& request, const Locatio
5859
{
5960
std::string full_path = config_.root;
6061
std::string folder = static_cast<std::string>(best_match->path);
61-
// std::cout << "root = " << full_path << std::endl;
6262
std::string file =
6363
request.path.substr(best_match->path.size(), request.path.size() - best_match->path.size());
6464
if (full_path[full_path.size() - 1] != '/')

src/util/log_message.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "util/log_message.hpp"
2+
3+
#include <cstdio>
4+
#include <cstdlib>
5+
6+
LogMessage::LogMessage(LogMessage::Level level)
7+
: level_(level)
8+
{
9+
}
10+
11+
LogMessage::~LogMessage()
12+
{
13+
print_message();
14+
15+
if (level_ == kLevelFatal)
16+
std::abort();
17+
}
18+
19+
void LogMessage::print_message()
20+
{
21+
/* clang-format off */
22+
fprintf(stderr, "%s[%s]%s %s\n",
23+
color_string(get_color()),
24+
level_string(level_),
25+
color_string(kColorReset),
26+
stream_.str().c_str());
27+
/* clang-format on */
28+
}
29+
30+
LogMessage::Color LogMessage::get_color() const
31+
{
32+
switch (level_) {
33+
case kLevelDebug: return kColorBlue;
34+
case kLevelInfo: return kColorReset;
35+
case kLevelWarning: return kColorYellow;
36+
case kLevelError: return kColorRed;
37+
case kLevelFatal: return kColorRed;
38+
}
39+
40+
NOTREACHED();
41+
return kColorReset;
42+
}
43+
44+
const char* LogMessage::level_string(LogMessage::Level level)
45+
{
46+
switch (level) {
47+
case kLevelDebug: return "DEBUG";
48+
case kLevelInfo: return "INFO";
49+
case kLevelWarning: return "WARN";
50+
case kLevelError: return "ERROR";
51+
case kLevelFatal: return "FATAL";
52+
}
53+
54+
NOTREACHED();
55+
return "UNKNOWN";
56+
}
57+
58+
const char* LogMessage::color_string(LogMessage::Color color)
59+
{
60+
switch (color) {
61+
case kColorReset: return "\033[0m";
62+
case kColorRed: return "\033[31m";
63+
case kColorGreen: return "\033[32m";
64+
case kColorYellow: return "\033[33m";
65+
case kColorBlue: return "\033[34m";
66+
}
67+
68+
NOTREACHED();
69+
return "";
70+
}

0 commit comments

Comments
 (0)