I'm new to C++, and I wanted to add a way to log in a fancy way. As such, I created a small bit of code to do so, but I'm wondering how I can improve my code, especially the fact that DebugColors consists of two files, and regarding my use of templates, as I don't think the adder method(s) of the Debug struct is the best way to concatenate strings.
Additionally, I'm unsure if I should be using a struct in order to contain the methods, or if I should use a namespace for it, as I only want to be able to access the public methods from outside the struct.
DebugColors.hpp
#ifndef DEBUG_COLORS
#define DEBUG_COLORS
#include <string>
namespace DebugColors {
extern std::string reset;
extern std::string red;
extern std::string green;
extern std::string yellow;
extern std::string blue;
}
#endif
DebugColors.cpp
#include "DebugColors.hpp"
namespace DebugColors {
std::string reset = "\x1b[0m";
std::string red = "\x1b[31m";
std::string green = "\x1b[32m";
std::string yellow = "\x1b[33m";
std::string blue = "\x1b[34m";
}
Debug.hpp
#ifndef DEBUG_HPP
#define DEBUG_HPP
#include <string>
#include <iostream>
#include "DebugColors.hpp"
struct Debug {
private:
template<typename T>
static T adder(T v) {
return v;
}
template<typename T, typename ...Args>
static T adder(T first, Args ...args) {
return first + " " + adder<std::string>(args...);
}
template<typename ...T>
static void LogMessage(T&... args) {
std::string message = adder<std::string>(args...);
std::cout << message << std::endl;
}
public:
template<typename ...T>
static void Log(T&... args) {
LogMessage(args...);
}
template<typename ...T>
static void Info(T&... args) {
LogMessage(DebugColors::blue, "[INFO]", args..., DebugColors::reset);
}
template<typename ...T>
static void Warning(T&... args) {
LogMessage(DebugColors::yellow, "[WARN]", args..., DebugColors::reset);
}
template<typename ...T>
static void Error(T&... args) {
LogMessage(DebugColors::red, "[ERROR]", args..., DebugColors::reset);
}
template<typename ...T>
static void Success(T&... args) {
LogMessage(DebugColors::green, "[SUCCESS]", args..., DebugColors::reset);
}
};
#endif
```
