Skip to content

Commit

Permalink
feat(Flakkari): implement Logger class to handle different level of m…
Browse files Browse the repository at this point in the history
…essage
  • Loading branch information
MasterLaplace committed Dec 9, 2023
1 parent 41f7a95 commit 9a937c9
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 29 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,6 @@ FodyWeavers.xsd

# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
Expand Down Expand Up @@ -569,3 +567,9 @@ _deps
*.exe
*.out
*.app
flakkari
r-type_server

Docs/Flakkari/
build/
.Test/
60 changes: 49 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
# Flakkari/CMakeLists.txt
cmake_minimum_required(VERSION 3.26.4)

project(R-Type_Server)

add_executable(r-type_server
# Add all your source and headers files:
set(SOURCES
Flakkari/core.cpp
)

# Set the output directory for the server executable
set_target_properties(r-type_server PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
set(HEADERS
Flakkari/FlakkariLogger.hpp
)

# Custom target for cleaning server build artifacts
add_custom_target(clean-server
COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/bin/r-type_server
COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/_deps/r-type_server
COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/r-type_server/CMakeCache.txt
COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/r-type_server/CMakeFiles
)
# Separate Build Artifacts:
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

# Compiler Standards:
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Compiler Warnings:
if(MSVC)
add_compile_options(/W4)
elseif(WIN32)
add_compile_options(-Wall -Wextra -pedantic)
else()
add_compile_options(-Wall -Wextra -pedantic)
endif()

# Create the executable
add_executable(r-type_server ${SOURCES} ${HEADERS})

# Include Directories:
target_include_directories(r-type_server PRIVATE ${CMAKE_SOURCE_DIR}/Flakkari)

# Documentation: sudo apt-get install graphviz
find_package(Doxygen)
if(DOXYGEN_FOUND)
add_custom_target(doc
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_SOURCE_DIR}/Doxyfile.cfg
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM)
endif()

# Versioning:
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 0)
set(PROJECT_VERSION_PATCH 0)

configure_file(${CMAKE_SOURCE_DIR}/config.h.in ${CMAKE_BINARY_DIR}/Flakkari/config.h)

# Install Targets:
install(TARGETS r-type_server DESTINATION bin)
install(FILES ${CMAKE_BINARY_DIR}/config.h DESTINATION include)
15 changes: 0 additions & 15 deletions Flakkari/CMakeLists.txt

This file was deleted.

217 changes: 217 additions & 0 deletions Flakkari/FlakkariLogger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/*
** EPITECH PROJECT, 2023
** Flakkari
** File description:
** Flakkari::Logger
*/

#ifndef FLAKKARI_LOGGER_HPP_
#define FLAKKARI_LOGGER_HPP_

#define LOG_INFO 0
#define LOG_LOG 1
#define LOG_DEBUG 2
#define LOG_WARNING 3
#define LOG_ERROR 4
#define LOG_FATAL 5

#include <iostream>
#include <string>
#include <chrono>
#include <ctime>

#define FLAKKARI_LOG(level, message) Flakkari::Logger::log(level, message, __FILE__, __LINE__)
#define FLAKKARI_LOG_INFO(message) FLAKKARI_LOG(LOG_INFO, message)
#define FLAKKARI_LOG_LOG(message) FLAKKARI_LOG(LOG_LOG, message)
#define FLAKKARI_LOG_DEBUG(message) FLAKKARI_LOG(LOG_DEBUG, message)
#define FLAKKARI_LOG_WARNING(message) FLAKKARI_LOG(LOG_WARNING, message)
#define FLAKKARI_LOG_ERROR(message) FLAKKARI_LOG(LOG_ERROR, message)
#define FLAKKARI_LOG_FATAL(message) FLAKKARI_LOG(LOG_FATAL, message)

#define COLOR_RESET "\033[0m"
#define COLOR_RED "\033[31m"
#define COLOR_GREEN "\033[32m"
#define COLOR_YELLOW "\033[33m"
#define COLOR_BLUE "\033[34m"
#define COLOR_MAGENTA "\033[35m"
#define COLOR_CYAN "\033[36m"
#define COLOR_WHITE "\033[37m"
#define COLOR_ORANGE "\033[38;5;208m"
#define COLOR_BRIGHT_RED "\033[91m"
#define COLOR_BRIGHT_GREEN "\033[92m"
#define COLOR_BRIGHT_YELLOW "\033[93m"
#define COLOR_BRIGHT_BLUE "\033[94m"
#define COLOR_BRIGHT_MAGENTA "\033[95m"
#define COLOR_BRIGHT_CYAN "\033[96m"
#define COLOR_BRIGHT_WHITE "\033[97m"

namespace Flakkari {
class Logger {
public:
static const std::string get_current_time() noexcept;
static void log(int level, std::string message, std::string file, int line);
static void log(int level, std::string message);
static void log(int level, std::string message, std::string file);
static void log(int level, std::string message, int line);
};
} // namespace Flakkari

#endif /* !FLAKKARI_LOGGER_HPP_ */

#ifdef FLAKKARI_LOGGER_IMPLEMENTATION

const std::string Flakkari::Logger::get_current_time() noexcept
{
auto currentTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

char buffer[80];
std::strftime(buffer, sizeof(buffer), "[ %Y-%m-%d %H:%M:%S ]", std::localtime(&currentTime));

return std::string(buffer);
}

void Flakkari::Logger::log(int level, std::string message, std::string file, int line)
{
std::string color = COLOR_RESET;
std::string levelStr = "INFO";

switch (level) {
case LOG_INFO:
color = COLOR_CYAN;
levelStr = "INFO";
break;
case LOG_LOG:
color = COLOR_GREEN;
levelStr = "LOG";
break;
case LOG_DEBUG:
color = COLOR_MAGENTA;
levelStr = "DEBUG";
break;
case LOG_WARNING:
color = COLOR_YELLOW;
levelStr = "WARNING";
break;
case LOG_ERROR:
color = COLOR_ORANGE;
levelStr = "ERROR";
break;
case LOG_FATAL:
color = COLOR_BRIGHT_RED;
levelStr = "FATAL";
break;
}

std::cout << get_current_time();
std::cout << color << " [" << levelStr << "] " << message << " (" << file << ":" << line << ")" << COLOR_RESET << std::endl;
}

void Flakkari::Logger::log(int level, std::string message)
{
std::string color = COLOR_RESET;
std::string levelStr = "INFO";

switch (level) {
case LOG_INFO:
color = COLOR_CYAN;
levelStr = "INFO";
break;
case LOG_LOG:
color = COLOR_GREEN;
levelStr = "LOG";
break;
case LOG_DEBUG:
color = COLOR_MAGENTA;
levelStr = "DEBUG";
break;
case LOG_WARNING:
color = COLOR_YELLOW;
levelStr = "WARNING";
break;
case LOG_ERROR:
color = COLOR_ORANGE;
levelStr = "ERROR";
break;
case LOG_FATAL:
color = COLOR_BRIGHT_RED;
levelStr = "FATAL";
break;
}

std::cout << get_current_time();
std::cout << color << " [" << levelStr << "] " << message << COLOR_RESET << std::endl;
}

void Flakkari::Logger::log(int level, std::string message, std::string file)
{
std::string color = COLOR_RESET;
std::string levelStr = "INFO";

switch (level) {
case LOG_INFO:
color = COLOR_CYAN;
levelStr = "INFO";
break;
case LOG_LOG:
color = COLOR_GREEN;
levelStr = "LOG";
break;
case LOG_DEBUG:
color = COLOR_MAGENTA;
levelStr = "DEBUG";
break;
case LOG_WARNING:
color = COLOR_YELLOW;
levelStr = "WARNING";
break;
case LOG_ERROR:
color = COLOR_ORANGE;
levelStr = "ERROR";
break;
case LOG_FATAL:
color = COLOR_BRIGHT_RED;
levelStr = "FATAL";
break;
}

std::cout << get_current_time();
std::cout << color << " [" << levelStr << "] " << message << " (" << file << ")" << COLOR_RESET << std::endl;
}

void Flakkari::Logger::log(int level, std::string message, int line)
{
std::string color = COLOR_RESET;
std::string levelStr = "INFO";

switch (level) {
case LOG_INFO:
color = COLOR_CYAN;
levelStr = "INFO";
break;
case LOG_LOG:
color = COLOR_GREEN;
levelStr = "LOG";
break;
case LOG_DEBUG:
color = COLOR_MAGENTA;
levelStr = "DEBUG";
break;
case LOG_WARNING:
color = COLOR_YELLOW;
levelStr = "WARNING";
break;
case LOG_ERROR:
color = COLOR_ORANGE;
levelStr = "ERROR";
break;
case LOG_FATAL:
color = COLOR_BRIGHT_RED;
levelStr = "FATAL";
break;
}

std::cout << get_current_time();
std::cout << color << " [" << levelStr << "] " << message << " (" << line << ")" << COLOR_RESET << std::endl;
}

#endif /* !FLAKKARI_LOGGER_IMPLEMENTATION */
8 changes: 7 additions & 1 deletion Flakkari/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
*/

#include <iostream>
#include "FlakkariMessage.hpp"

using namespace std;

int main() {
cout << "Server: Hello, World!" << endl;
FLAKKARI_LOG_INFO("Server: Hello, World!");
FLAKKARI_LOG_LOG("Server: Hello, World!");
FLAKKARI_LOG_DEBUG("Server: Hello, World!");
FLAKKARI_LOG_WARNING("Server: Hello, World!");
FLAKKARI_LOG_ERROR("Server: Hello, World!");
FLAKKARI_LOG_FATAL("Server: Hello, World!");
return 0;
}
3 changes: 3 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define PROJECT_VERSION_MAJOR 0
#define PROJECT_VERSION_MINOR 0
#define PROJECT_VERSION_PATCH 1

0 comments on commit 9a937c9

Please sign in to comment.