mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 18:58:46 +02:00
「✨」 feat: Finished parsing (should be working)
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
|
||||
/* Updated: 2025/03/20 14:55:09 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/03/25 17:50:45 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -21,37 +21,69 @@
|
||||
namespace webserv {
|
||||
class Logger {
|
||||
public:
|
||||
Logger(std::string fileName) {
|
||||
Logger(const std::string &fileName) : _fileName(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
|
||||
if (!_file.is_open() && !_ttyOnly) {
|
||||
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(); }
|
||||
Logger(const Logger &other) : _ttyOnly(other._ttyOnly) {
|
||||
if (!other._ttyOnly) {
|
||||
_file.open(other._fileName.c_str(), std::ios::app);
|
||||
if (!_file.is_open()) {
|
||||
throw std::runtime_error("Could not open file: " +
|
||||
other._fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void info(std::string msg) {
|
||||
std::stringstream ss = printPogitMsg("✏️", "webserv", "info", msg);
|
||||
// Copy assignment operator
|
||||
Logger &operator=(const Logger &other) {
|
||||
if (this != &other) {
|
||||
if (_file.is_open()) {
|
||||
_file.close();
|
||||
}
|
||||
_ttyOnly = other._ttyOnly;
|
||||
if (!other._ttyOnly) {
|
||||
_file.open(other._fileName.c_str(), std::ios::app);
|
||||
if (!_file.is_open()) {
|
||||
throw std::runtime_error("Could not open file: " +
|
||||
other._fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
~Logger(void) {
|
||||
if (_file.is_open())
|
||||
_file.close();
|
||||
}
|
||||
|
||||
void info(const std::string &msg) {
|
||||
std::string 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);
|
||||
void warn(const std::string &msg) {
|
||||
std::string 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);
|
||||
void error(const std::string &msg) {
|
||||
std::string ss = printPogitMsg("🚧", "webserv", "error", msg);
|
||||
std::cerr << ss << std::endl;
|
||||
if (!_ttyOnly) {
|
||||
_file << ss << std::endl;
|
||||
@ -60,7 +92,10 @@ class Logger {
|
||||
|
||||
protected:
|
||||
private:
|
||||
std::stringstream printPogitMsg(std::string emoji, std::string type, std::string what, std::string msg) {
|
||||
std::string printPogitMsg(const std::string &emoji,
|
||||
const std::string &type,
|
||||
const std::string &what,
|
||||
const std::string &msg) {
|
||||
std::stringstream os;
|
||||
#ifdef tty
|
||||
if (what.empty())
|
||||
@ -73,8 +108,10 @@ class Logger {
|
||||
else
|
||||
os << "「" << emoji << "」" << type << "(" << what << "):" << msg;
|
||||
#endif
|
||||
return os;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
std::string _fileName;
|
||||
bool _ttyOnly;
|
||||
std::ofstream _file;
|
||||
};
|
||||
|
Reference in New Issue
Block a user