mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 23:28:46 +02:00
「🏗️」 wip: Added logger class
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */
|
/* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/03/19 17:35:09 by adjoly ### ########.fr */
|
/* Updated: 2025/03/20 09:27:58 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,6 +14,10 @@
|
|||||||
|
|
||||||
#include "cppeleven.hpp"
|
#include "cppeleven.hpp"
|
||||||
#include "node/ANode.hpp"
|
#include "node/ANode.hpp"
|
||||||
|
#include "node/Array.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
|
#include <cstdlib>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -23,14 +27,130 @@
|
|||||||
namespace webserv {
|
namespace webserv {
|
||||||
namespace config {
|
namespace config {
|
||||||
|
|
||||||
|
int32_t parseSize(std::string size) {
|
||||||
|
if (size[size.size()] == 'M')
|
||||||
|
return std::atoi(size.c_str()) * 1024 * 1024;
|
||||||
|
if (size[size.size()] == 'K')
|
||||||
|
return std::atoi(size.c_str()) * 1024;
|
||||||
|
if (isalpha(size[size.size()]))
|
||||||
|
return std::atoi(size.c_str());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
class Route {
|
class Route {
|
||||||
public:
|
public:
|
||||||
Route(std::map<std::string, toml::ANode *> *node) {
|
Route(std::map<std::string, toml::ANode *> *node) {
|
||||||
if (node == not_nullptr)
|
if (node == not_nullptr)
|
||||||
throw std::runtime_error("location table does not exist");
|
throw std::runtime_error("location table does not exist");
|
||||||
std::map<std::string, toml::ANode *> *errorPagesTable = (*node)["error_pages"]->getTable();
|
|
||||||
if (errorPagesTable == not_nullptr)
|
_methods[0] = false;
|
||||||
throw std::runtime_error("error_pages not present");
|
_methods[1] = false;
|
||||||
|
_methods[2] = false;
|
||||||
|
|
||||||
|
std::map<std::string, toml::ANode *>::iterator it;
|
||||||
|
|
||||||
|
it = (*node).find("redirect");
|
||||||
|
if (it != node->end()) {
|
||||||
|
toml::ANode *redirNode = it->second;
|
||||||
|
if (redirNode->type() == toml::STRING) {
|
||||||
|
_redirect = true;
|
||||||
|
_root = *(std::string *)redirNode->getValue();
|
||||||
|
_cookies = false;
|
||||||
|
_uploads = false;
|
||||||
|
_dirlist = false;
|
||||||
|
_cgi = not_nullptr;
|
||||||
|
_methods[0] = false;
|
||||||
|
_methods[1] = false;
|
||||||
|
_methods[2] = false;
|
||||||
|
_err_pages = not_nullptr;
|
||||||
|
_upRoot = not_nullptr;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it = (*node).find("uploads");
|
||||||
|
if (it != node->end()) {
|
||||||
|
toml::ANode *uploadsNode = it->second;
|
||||||
|
if (uploadsNode->type() == toml::BOOL)
|
||||||
|
_uploads = *(bool *)uploadsNode->getValue();
|
||||||
|
}
|
||||||
|
it = (*node).find("dirlist");
|
||||||
|
if (it != node->end()) {
|
||||||
|
toml::ANode *dirlistNode = it->second;
|
||||||
|
if (dirlistNode->type() == toml::BOOL)
|
||||||
|
_dirlist = *(bool *)dirlistNode->getValue();
|
||||||
|
}
|
||||||
|
it = (*node).find("cookies");
|
||||||
|
if (it != node->end()) {
|
||||||
|
toml::ANode *cookiesNode = it->second;
|
||||||
|
if (cookiesNode->type() == toml::BOOL)
|
||||||
|
_cookies = *(bool *)cookiesNode->getValue();
|
||||||
|
}
|
||||||
|
it = (*node).find("root");
|
||||||
|
if (it != node->end()) {
|
||||||
|
toml::ANode *rootNode = it->second;
|
||||||
|
if (rootNode->type() == toml::STRING)
|
||||||
|
_root = *(std::string *)rootNode->getValue();
|
||||||
|
}
|
||||||
|
it = (*node).find("client_max_body_size");
|
||||||
|
if (it != node->end()) {
|
||||||
|
toml::ANode *rootNode = it->second;
|
||||||
|
if (rootNode->type() == toml::STRING) {
|
||||||
|
int32_t maxBody =
|
||||||
|
parseSize(*(std::string *)rootNode->getValue());
|
||||||
|
if (maxBody != 0 && maxBody != -1)
|
||||||
|
_max_body = maxBody;
|
||||||
|
else {
|
||||||
|
std::cerr << "root size is not correctttt"
|
||||||
|
<< std::endl; /// change that later TODO
|
||||||
|
_max_body = 10485760;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it = (*node).find("methods");
|
||||||
|
if (it != node->end()) {
|
||||||
|
toml::ANode *methodsNode = it->second;
|
||||||
|
if (methodsNode->type() == toml::ARRAY) {
|
||||||
|
std::vector<toml::ANode *>::iterator methods =
|
||||||
|
methodsNode->getArray()->begin();
|
||||||
|
for (; methods != methodsNode->getArray()->end(); methods++) {
|
||||||
|
if ((*methods)->type() == toml::STRING) {
|
||||||
|
std::string method =
|
||||||
|
*(std::string *)(*methods)->getValue();
|
||||||
|
if (method == "GET")
|
||||||
|
_methods[0] = true;
|
||||||
|
else if (method == "POST")
|
||||||
|
_methods[1] = true;
|
||||||
|
else if (method == "DELETE")
|
||||||
|
_methods[2] = true;
|
||||||
|
else
|
||||||
|
throw std::runtime_error("da fuk that not a valid "
|
||||||
|
"methods bro"); // replace
|
||||||
|
// with
|
||||||
|
// warning
|
||||||
|
// log
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"error why did you put something elle than a "
|
||||||
|
"string"); // replace with log func
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it = (*node).find("error_pages");
|
||||||
|
if (it != node->end()) {
|
||||||
|
toml::ANode *errorPagesNode = it->second;
|
||||||
|
// for loop on it and add to error pages and atoi the name of the
|
||||||
|
// page(need to be a deep copy) TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
it = (*node).find("cgi");
|
||||||
|
if (it != node->end()) {
|
||||||
|
toml::ANode *cgiNode = it->second;
|
||||||
|
// for loop on it and add to cgi (need to be a deep copy) TODO
|
||||||
|
}
|
||||||
}
|
}
|
||||||
~Route(void) {}
|
~Route(void) {}
|
||||||
|
|
||||||
@ -40,12 +160,15 @@ class Route {
|
|||||||
bool _cookies;
|
bool _cookies;
|
||||||
bool _uploads;
|
bool _uploads;
|
||||||
bool _redirect;
|
bool _redirect;
|
||||||
int32_t _maxBody;
|
|
||||||
|
int32_t _max_body;
|
||||||
|
|
||||||
std::string _root;
|
std::string _root;
|
||||||
std::string _upRoot;
|
std::string _upRoot;
|
||||||
std::map<std::string, std::string> *_cgi;
|
std::map<std::string, std::string> *_cgi;
|
||||||
std::vector<std::string> *_methods;
|
|
||||||
std::map<int, std::string> *_errPages;
|
bool _methods[3]; // 1: GET, 2: POST, 3: DELETE
|
||||||
|
std::map<int, std::string> *_err_pages;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace config
|
} // namespace config
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */
|
/* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/03/19 16:54:35 by adjoly ### ########.fr */
|
/* Updated: 2025/03/19 23:09:56 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -23,6 +23,8 @@ class Server {
|
|||||||
private:
|
private:
|
||||||
Route *_default;
|
Route *_default;
|
||||||
std::vector<Route *> *_routes;
|
std::vector<Route *> *_routes;
|
||||||
|
std::string host;
|
||||||
|
std::vector<std::string> server_names;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace config
|
} // namespace config
|
||||||
|
82
includes/log.hpp
Normal file
82
includes/log.hpp
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* log.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2025/03/20 14:42:26 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace webserv {
|
||||||
|
class Logger {
|
||||||
|
public:
|
||||||
|
Logger(std::string fileName) {
|
||||||
|
if (fileName.empty())
|
||||||
|
_ttyOnly = true;
|
||||||
|
else {
|
||||||
|
_file.open(fileName.c_str(), std::ios::app);
|
||||||
|
_ttyOnly = false;
|
||||||
|
}
|
||||||
|
if (!_file.is_open()) {
|
||||||
|
throw std::runtime_error("could not open fileeee"); // TODO change that shit but i dont know what to put other than a htrow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~Logger(void) { _file.close(); }
|
||||||
|
|
||||||
|
void info(std::string &msg) {
|
||||||
|
std::stringstream ss = printPogitMsg("✏️", "webserv", "info", msg);
|
||||||
|
std::cerr << ss << std::endl;
|
||||||
|
if (!_ttyOnly) {
|
||||||
|
_file << ss << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void warn(std::string &msg) {
|
||||||
|
std::stringstream ss = printPogitMsg("🔨", "webserv", "warning", msg);
|
||||||
|
std::cerr << ss << std::endl;
|
||||||
|
if (!_ttyOnly) {
|
||||||
|
_file << ss << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
void error(std::string &msg) {
|
||||||
|
std::stringstream ss = printPogitMsg("🚧", "webserv", "error", msg);
|
||||||
|
std::cerr << ss << std::endl;
|
||||||
|
if (!_ttyOnly) {
|
||||||
|
_file << ss << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
private:
|
||||||
|
std::stringstream printPogitMsg(std::string emoji, std::string type, std::string what, std::string msg) {
|
||||||
|
std::stringstream os;
|
||||||
|
#ifdef tty
|
||||||
|
if (what.empty())
|
||||||
|
os << type << ":" << msg;
|
||||||
|
else
|
||||||
|
os << type << "(" << what << "):" << msg;
|
||||||
|
#else
|
||||||
|
if (what.empty())
|
||||||
|
os << "「" << emoji << "」" << type << ":" << msg;
|
||||||
|
else
|
||||||
|
os << "「" << emoji << "」" << type << "(" << what << "):" << msg;
|
||||||
|
#endif
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
bool _ttyOnly;
|
||||||
|
std::ofstream _file;
|
||||||
|
};
|
||||||
|
|
||||||
|
}; // namespace webserv
|
Reference in New Issue
Block a user