mirror of
https://github.com/davidallendj/gdpm.git
synced 2025-12-19 19:17:01 -07:00
Fix fmt 10 breaking logging and changed printing format
This commit is contained in:
parent
5bdc605f0b
commit
431be7914c
10 changed files with 114 additions and 130 deletions
|
|
@ -3,52 +3,51 @@
|
|||
#include "colors.hpp"
|
||||
#include <string>
|
||||
|
||||
// #define GDPM_ENABLE_COLORS 1
|
||||
#if GDPM_ENABLE_COLORS == 1
|
||||
#define GDPM_COLOR_BLACK "\033[0;30m"
|
||||
#define GDPM_COLOR_BLUE "\033[0;34m"
|
||||
#define GDPM_COLOR_GREEN "\033[0;32m"
|
||||
#define GDPM_COLOR_CYAN "\033[0;36m"
|
||||
#define GDPM_COLOR_RED "\033[0;31m"
|
||||
#define GDPM_COLOR_PURPLE "\033[0;35m"
|
||||
#define GDPM_COLOR_BROWN "\033[0;33m"
|
||||
#define GDPM_COLOR_GRAY "\033[0;37m"
|
||||
#define GDPM_COLOR_DARK_GRAY "\033[0;30m"
|
||||
#define GDPM_COLOR_LIGHT_BLUE "\033[0;34m"
|
||||
#define GDPM_COLOR_LIGHT_GREEN "\033[0;32m"
|
||||
#define GDPM_COLOR_LIGHT_CYAN "\033[0;36m"
|
||||
#define GDPM_COLOR_LIGHT_RED "\033[0;31m"
|
||||
#define GDPM_COLOR_LIGHT_PURPLE "\033[0;35m"
|
||||
#define GDPM_COLOR_YELLOW "\033[0;33m"
|
||||
#define GDPM_COLOR_WHITE "\033[0;37m"
|
||||
#define GDPM_COLOR_RESET GDPM_COLOR_WHITE
|
||||
constexpr const char *GDPM_COLOR_BLACK = "\033[0;30m";
|
||||
constexpr const char *GDPM_COLOR_BLUE = "\033[0;34m";
|
||||
constexpr const char *GDPM_COLOR_GREEN = "\033[0;32m";
|
||||
constexpr const char *GDPM_COLOR_CYAN = "\033[0;36m";
|
||||
constexpr const char *GDPM_COLOR_RED = "\033[0;31m";
|
||||
constexpr const char *GDPM_COLOR_PURPLE = "\033[0;35m";
|
||||
constexpr const char *GDPM_COLOR_BROWN = "\033[0;33m";
|
||||
constexpr const char *GDPM_COLOR_GRAY = "\033[0;37m";
|
||||
constexpr const char *GDPM_COLOR_DARK_GRAY = "\033[0;30m";
|
||||
constexpr const char *GDPM_COLOR_LIGHT_BLUE = "\033[0;34m";
|
||||
constexpr const char *GDPM_COLOR_LIGHT_GREEN = "\033[0;32m";
|
||||
constexpr const char *GDPM_COLOR_LIGHT_CYAN = "\033[0;36m";
|
||||
constexpr const char *GDPM_COLOR_LIGHT_RED = "\033[0;31m";
|
||||
constexpr const char *GDPM_COLOR_LIGHT_PURPLE = "\033[0;35m";
|
||||
constexpr const char *GDPM_COLOR_YELLOW = "\033[0;33m";
|
||||
constexpr const char *GDPM_COLOR_WHITE = "\033[0;37m";
|
||||
constexpr const char *GDPM_COLOR_RESET = GDPM_COLOR_WHITE;
|
||||
|
||||
#else
|
||||
#define GDPM_COLOR_BLACK ""
|
||||
#define GDPM_COLOR_BLUE ""
|
||||
#define GDPM_COLOR_GREEN ""
|
||||
#define GDPM_COLOR_CYAN ""
|
||||
#define GDPM_COLOR_RED ""
|
||||
#define GDPM_COLOR_PURPLE ""
|
||||
#define GDPM_COLOR_BROWN ""
|
||||
#define GDPM_COLOR_GRAY ""
|
||||
#define GDPM_COLOR_DARK_GRAY ""
|
||||
#define GDPM_COLOR_LIGHT_BLUE ""
|
||||
#define GDPM_COLOR_LIGHT_GREEN ""
|
||||
#define GDPM_COLOR_LIGHT_CYAN ""
|
||||
#define GDPM_COLOR_LIGHT_RED ""
|
||||
#define GDPM_COLOR_LIGHT_PURPLE ""
|
||||
#define GDPM_COLOR_YELLOW ""
|
||||
#define GDPM_COLOR_WHITE ""
|
||||
#define GDPM_COLOR_RESET GDPM_COLOR_WHITE
|
||||
|
||||
|
||||
constexpr const char *GDPM_COLOR_BLACK = "";
|
||||
constexpr const char *GDPM_COLOR_BLUE = "";
|
||||
constexpr const char *GDPM_COLOR_GREEN = "";
|
||||
constexpr const char *GDPM_COLOR_CYAN = "";
|
||||
constexpr const char *GDPM_COLOR_RED = "";
|
||||
constexpr const char *GDPM_COLOR_PURPLE = "";
|
||||
constexpr const char *GDPM_COLOR_BROWN = "";
|
||||
constexpr const char *GDPM_COLOR_GRAY = "";
|
||||
constexpr const char *GDPM_COLOR_DARK_GRAY = "";
|
||||
constexpr const char *GDPM_COLOR_LIGHT_BLUE = "";
|
||||
constexpr const char *GDPM_COLOR_LIGHT_GREEN = "";
|
||||
constexpr const char *GDPM_COLOR_LIGHT_CYAN = "";
|
||||
constexpr const char *GDPM_COLOR_LIGHT_RED = "";
|
||||
constexpr const char *GDPM_COLOR_LIGHT_PURPLE = "";
|
||||
constexpr const char *GDPM_COLOR_YELLOW = "";
|
||||
constexpr const char *GDPM_COLOR_WHITE = "";
|
||||
constexpr const char *GDPM_COLOR_RESET = GDPM_COLOR_WHITE;
|
||||
#endif
|
||||
|
||||
#define GDPM_COLOR_LOG_RESET GDPM_COLOR_WHITE
|
||||
#define GDPM_COLOR_LOG_INFO GDPM_COLOR_LOG_RESET
|
||||
#define GDPM_COLOR_LOG_ERROR GDPM_COLOR_RED
|
||||
#define GDPM_COLOR_LOG_DEBUG GDPM_COLOR_YELLOW
|
||||
#define GDPM_COLOR_LOG_WARNING GDPM_COLOR_YELLOW
|
||||
constexpr const char *GDPM_COLOR_LOG_RESET = GDPM_COLOR_WHITE;
|
||||
constexpr const char *GDPM_COLOR_LOG_INFO = GDPM_COLOR_LOG_RESET;
|
||||
constexpr const char *GDPM_COLOR_LOG_ERROR = GDPM_COLOR_RED;
|
||||
constexpr const char *GDPM_COLOR_LOG_DEBUG = GDPM_COLOR_YELLOW;
|
||||
constexpr const char *GDPM_COLOR_LOG_WARNING = GDPM_COLOR_YELLOW;
|
||||
|
||||
namespace gdpm::color{
|
||||
inline std::string from_string(const std::string& color_name){
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
namespace gdpm::constants::error{
|
||||
|
||||
enum {
|
||||
enum class ec{
|
||||
NONE = 0,
|
||||
UNKNOWN,
|
||||
UNKNOWN_COMMAND,
|
||||
|
|
@ -75,36 +75,40 @@ namespace gdpm::constants::error{
|
|||
};
|
||||
|
||||
|
||||
inline string get_message(int error_code) {
|
||||
inline string get_message(ec error_code) {
|
||||
string message{};
|
||||
try{ message = messages[error_code]; }
|
||||
try{ message = messages[(int)error_code]; }
|
||||
catch(const std::bad_alloc& e){
|
||||
log::error("No default message for error code.");
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T to(ec c){ return fmt::underlying_t<T>(c); }
|
||||
};
|
||||
|
||||
namespace gdpm{
|
||||
namespace ec = constants::error;
|
||||
using ec = constants::error::ec;
|
||||
namespace ce = constants::error;
|
||||
class error {
|
||||
public:
|
||||
constexpr explicit error(int code = 0, const string& message = "{default}"):
|
||||
constexpr explicit error(ec code = ec::NONE, const string& message = "{default}"):
|
||||
m_code(code),
|
||||
m_message(utils::replace_all(message, "{default}", ec::get_message(code)))
|
||||
m_message(utils::replace_all(message, "{default}", ce::get_message(code)))
|
||||
{}
|
||||
|
||||
void set_code(int code) { m_code = code; }
|
||||
void set_code(ec code) { m_code = code; }
|
||||
void set_message(const string& message) { m_message = message; }
|
||||
|
||||
int get_code() const { return m_code; }
|
||||
ec get_code() const { return m_code; }
|
||||
string get_message() const { return m_message; }
|
||||
bool has_occurred() const { return m_code != 0; }
|
||||
bool has_occurred() const { return (int)m_code != 0; }
|
||||
|
||||
bool operator()(){ return has_occurred(); }
|
||||
|
||||
private:
|
||||
int m_code;
|
||||
ec m_code;
|
||||
string m_message;
|
||||
};
|
||||
|
||||
|
|
@ -115,7 +119,7 @@ namespace gdpm{
|
|||
set_prefix_if(std::format("[ERROR {}] ", utils::timestamp()), true);
|
||||
set_suffix_if("\n");
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_ERROR "{}{}\n" GDPM_COLOR_LOG_RESET, prefix.contents, e.get_message()),
|
||||
fmt::format("{}{}{}{}\n", GDPM_COLOR_LOG_ERROR, prefix.contents, e.get_message(), GDPM_COLOR_LOG_RESET),
|
||||
fmt::make_format_args(prefix.contents, e.get_message())
|
||||
);
|
||||
#endif
|
||||
|
|
@ -130,7 +134,7 @@ namespace gdpm{
|
|||
return e;
|
||||
}
|
||||
|
||||
static constexpr gdpm::error error_rc(int code, const string& message = "{default}"){
|
||||
static constexpr gdpm::error error_rc(ec code, const string& message = "{default}"){
|
||||
return error_rc(gdpm::error(code, message));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ namespace gdpm::log
|
|||
set_prefix_if(fmt::format( get_info_prefix(), utils::timestamp()), true);
|
||||
set_suffix_if("\n");
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_INFO "{}{}{}" GDPM_COLOR_LOG_RESET, prefix.contents, format, suffix),
|
||||
fmt::format("{}{}{}{}{}", GDPM_COLOR_LOG_INFO, prefix.contents, format, suffix, GDPM_COLOR_LOG_RESET),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
|
|
@ -108,7 +108,7 @@ namespace gdpm::log
|
|||
set_prefix_if(fmt::format(get_info_prefix(), utils::timestamp()), true);
|
||||
set_suffix_if("", true);
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_INFO "{}{}{}" GDPM_COLOR_LOG_RESET, prefix.contents, format, suffix),
|
||||
fmt::format("{}{}{}{}{}", GDPM_COLOR_LOG_INFO, prefix.contents, format, suffix, GDPM_COLOR_LOG_RESET),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
|
|
@ -122,7 +122,7 @@ namespace gdpm::log
|
|||
set_prefix_if(std::format(get_error_prefix(), utils::timestamp()), true);
|
||||
set_suffix_if("\n");
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_ERROR "{}{}{}" GDPM_COLOR_LOG_RESET, prefix.contents, format, suffix),
|
||||
std::format("{}{}{}{}{}", GDPM_COLOR_LOG_ERROR, prefix.contents, format, suffix, GDPM_COLOR_LOG_RESET),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
|
|
@ -136,7 +136,7 @@ namespace gdpm::log
|
|||
set_prefix_if(std::format(get_debug_prefix(), utils::timestamp()), true);
|
||||
set_suffix_if("\n");
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_DEBUG "{}{}{}" GDPM_COLOR_LOG_RESET, prefix.contents, format, suffix),
|
||||
std::format("{}{}{}", GDPM_COLOR_LOG_DEBUG, prefix.contents, format, suffix, GDPM_COLOR_LOG_RESET),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
|
|
@ -150,7 +150,7 @@ namespace gdpm::log
|
|||
set_prefix_if(std::format(get_warning_prefix(), utils::timestamp()), true);
|
||||
set_suffix_if("\n");
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_WARNING "{}{}{}" GDPM_COLOR_LOG_RESET, prefix.contents, format, suffix),
|
||||
std::format("{}{}{}", GDPM_COLOR_LOG_WARNING, prefix.contents, format, suffix, GDPM_COLOR_LOG_RESET),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
|
|
@ -160,7 +160,7 @@ namespace gdpm::log
|
|||
template <typename S, typename...Args>
|
||||
static constexpr void print(const S& format, Args&&...args){
|
||||
vlog(
|
||||
fmt::format("{}", format),
|
||||
std::format("{}", format),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
}
|
||||
|
|
@ -168,7 +168,7 @@ namespace gdpm::log
|
|||
template <typename S = string, typename...Args>
|
||||
static constexpr void println(const S& format, Args&&...args){
|
||||
vlog(
|
||||
fmt::format("{}\n", format),
|
||||
std::format("{}\n", format),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue