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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SRCS = $(SRCS_DIR)/main.cpp \
$(SRCS_DIR)/core/Server.cpp \
$(SRCS_DIR)/core/Client.cpp \
$(SRCS_DIR)/core/Channel.cpp \
$(SRCS_DIR)/core/Logger.cpp \
$(SRCS_DIR)/network/PollSocketManager.cpp \
$(SRCS_DIR)/network/MessageBuffer.cpp \
$(SRCS_DIR)/commands/ACommand.cpp \
Expand Down
4 changes: 2 additions & 2 deletions incs/core/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class Config
private:
int m_port;
std::string m_passwd;
Config(int port, std::string password);
Config(int port, const std::string& password);

public:
int getPort() const;
const std::string& getPassword() const;
static Config checkArgs(int argc, char** argv);
};
};
69 changes: 69 additions & 0 deletions incs/core/Logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once

#include <ostream>
#include <sstream>

class Logger
{
public:
enum Level
{
DEBUG = 0,
NOTICE = 1,
INFO = 2,
WARNING = 3,
ERROR = 4,
CRITICAL = 5
};

private:
Level m_minLevel;
std::ostream* m_output;
std::ostringstream m_buffer;
Level m_currentLevel;
bool m_lineStarted;

Logger();
~Logger();
Logger(const Logger&);
Logger& operator=(const Logger&);

std::string getLevelString(Level level) const;
std::string getColorCode(Level level) const;
void flush();

public:
static Logger& getInstance();

void setMinLevel(Level level);
void setOutput(std::ostream& output);

Logger& log(Level level);
Logger& debug();
Logger& notice();
Logger& info();
Logger& warning();
Logger& error();
Logger& critical();

template < typename T > Logger& operator<<(const T& value)
{
if (!m_lineStarted)
{
m_buffer << "[ " << getColorCode(m_currentLevel) << getLevelString(m_currentLevel)
<< getColorCode((Level)-1) << " ] ";
m_lineStarted = true;
}
m_buffer << value;
return *this;
}

Logger& operator<<(std::ostream& (*manip)(std::ostream&));
};

#define LOG_DEBUG Logger::getInstance().debug()
#define LOG_NOTICE Logger::getInstance().notice()
#define LOG_INFO Logger::getInstance().info()
#define LOG_WARNING Logger::getInstance().warning()
#define LOG_ERROR Logger::getInstance().error()
#define LOG_CRITICAL Logger::getInstance().critical()
9 changes: 9 additions & 0 deletions incs/core/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
#include "core/IServer.hpp"
#include "network/ISocketManager.hpp"

#define RESET "\033[0m"
#define RED "\033[91m"
#define GREEN "\033[92m"
#define YELLOW "\033[93m"
#define BLUE "\033[94m"
#define MAGENTA "\033[95m"
#define CYAN "\033[96m"
#define WHITE "\033[97m"

class Server : public IServer
{
private:
Expand Down
4 changes: 4 additions & 0 deletions incs/protocol/Message.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include "commands/CommandType.hpp"
#include <ostream>
#include <string>
#include <vector>

struct Message
{
std::string m_prefix;
Expand All @@ -13,3 +15,5 @@ struct Message
Message();
bool isValid() const;
};

std::ostream& operator<<(std::ostream& stream, const Message& message);
35 changes: 26 additions & 9 deletions srcs/core/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
#include "Config.hpp"
#include <sstream>

#define PORT_MIN 1024
#define PORT_MAX 65535

std::string parsePasswd(char* argv)
{
if (!argv)
throw(std::runtime_error("Password must not be null\n."));
throw(std::runtime_error("Password must not be null"));
std::string passwd = argv;
if (passwd.empty() || (*argv == ' ' && strlen(argv) == 1))
throw(std::runtime_error("Password must not be empty\n."));
throw(std::runtime_error("Password must not be empty"));
return (passwd);
}

int parsePort(char* argv)
{
if (!argv || *argv == '\0')
throw(std::runtime_error("Port must not be empty\n."));
throw(std::runtime_error("Port must not be empty"));

errno = 0;
char* end = 0;
long port = std::strtol(argv, &end, 10);

if (errno != 0 || end == argv || *end != '\0')
throw(std::runtime_error("Port must be a valid number\n."));
if (port < 1 || port > 65535)
throw(std::runtime_error("Port must be between 1 and 65535."));
{
std::ostringstream oss;
oss << "Port must be a number.\nI know it's crazy, but there's nothing to listen to on "
<< argv << " :o";
throw(std::runtime_error(oss.str()));
}
if (port < PORT_MIN || port > PORT_MAX)
{
std::ostringstream oss;
oss << "Port must be " << PORT_MIN << " and " << PORT_MAX;
throw(std::runtime_error(oss.str()));
}
return (static_cast< int >(port));
}

Config Config::checkArgs(int argc, char** argv)
{
if (argc != 3)
throw(std::runtime_error("Number of arguments must be equal to 3\n."));
{
std::ostringstream oss;
oss << "Usage: " << argv[0] << " <port> <password>";
throw(std::runtime_error(oss.str()));
}

int port = parsePort(argv[1]);
std::string passwd = parsePasswd(argv[2]);
Expand All @@ -48,6 +65,6 @@ const std::string& Config::getPassword() const
return this->m_passwd;
}

Config::Config(int port, std::string password) : m_port(port), m_passwd(password)
Config::Config(int port, const std::string& password) : m_port(port), m_passwd(password)
{
}
}
124 changes: 124 additions & 0 deletions srcs/core/Logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "core/Logger.hpp"
#include <iostream>

Logger::Logger()
: m_minLevel(INFO), m_output(&std::cout), m_currentLevel(INFO), m_lineStarted(false)
{
}

Logger::~Logger()
{
flush();
}

Logger& Logger::getInstance()
{
static Logger instance;
return (instance);
}

void Logger::setMinLevel(Level level)
{
m_minLevel = level;
}

void Logger::setOutput(std::ostream& output)
{
m_output = &output;
}

Logger& Logger::log(Level level)
{
flush();
m_currentLevel = level;
m_lineStarted = false;
return (*this);
}

Logger& Logger::debug()
{
return log(DEBUG);
}
Logger& Logger::notice()
{
return log(NOTICE);
}
Logger& Logger::info()
{
return log(INFO);
}
Logger& Logger::warning()
{
return log(WARNING);
}
Logger& Logger::error()
{
return log(ERROR);
}
Logger& Logger::critical()
{
return log(CRITICAL);
}

std::string Logger::getLevelString(Level level) const
{
switch (level)
{
case DEBUG:
return "DEBUG";
case NOTICE:
return "NOTICE";
case INFO:
return "INFO";
case WARNING:
return "WARN";
case ERROR:
return "ERROR";
case CRITICAL:
return "CRIT";
default:
return "?????"; // if you got a better suggestion im all ears
}
}

std::string Logger::getColorCode(Level level) const
{
switch (level)
{
case DEBUG:
return "\033[36m"; // cyan
case NOTICE:
return "\033[96m"; // bright cyan
case INFO:
return "\033[32m"; // green
case WARNING:
return "\033[33m"; // yellow
case ERROR:
return "\033[31m"; // red
case CRITICAL:
return "\033[95m"; // bold Red
default:
return "\033[0m"; // reset
}
}

void Logger::flush()
{
if (m_lineStarted && m_currentLevel >= m_minLevel)
{
*m_output << m_buffer.str() << "\033[0m" << '\n';
m_output->flush();
}
m_buffer.str("");
m_buffer.clear();
m_lineStarted = false;
}

Logger& Logger::operator<<(std::ostream& (*manip)(std::ostream&))
{
if (manip == static_cast< std::ostream& (*)(std::ostream&) >(std::endl))
{
flush();
}
return *this;
}
Loading