mirror of
https://github.com/davidallendj/gdpm.git
synced 2025-12-20 03:27:02 -07:00
Recfactored and simplified more code
- Added function to convert color string to ansi color string - Added `trim` and `join` utility functions - Added initial plugin test case - Implemented `config get` command to see config properties - Improved logging functionality and removed duplicate logging functions - Removed unused functions - Fixed more styling issues - Fixed some CLI commands not working correctly - Fixed CLI documentation format - Fixed some error handling issues
This commit is contained in:
parent
e48c54aa40
commit
02a4e879a8
21 changed files with 541 additions and 384 deletions
145
include/log.hpp
145
include/log.hpp
|
|
@ -1,10 +1,12 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "clipp.h"
|
||||
#include "utils.hpp"
|
||||
#include "colors.hpp"
|
||||
#include "types.hpp"
|
||||
#include <format>
|
||||
#include <bitset>
|
||||
// #include <fmt/core.h>
|
||||
|
||||
#if __cplusplus > 201703L
|
||||
|
|
@ -21,22 +23,51 @@ TODO: Write log information to file
|
|||
namespace gdpm::log
|
||||
{
|
||||
|
||||
enum level{
|
||||
NONE = 0,
|
||||
INFO,
|
||||
WARNING,
|
||||
DEBUG,
|
||||
ERROR
|
||||
enum level_e : int{
|
||||
NONE = 0,
|
||||
INFO = 1,
|
||||
WARNING = 2,
|
||||
DEBUG = 3,
|
||||
ERROR = 4
|
||||
};
|
||||
|
||||
struct context {
|
||||
int level;
|
||||
string prefix;
|
||||
string path;
|
||||
bool print_to_stdout;
|
||||
bool print_to_stderr;
|
||||
enum flag_opt {
|
||||
PRINT_STDOUT = 0b00000001,
|
||||
PRINT_STDERR = 0b00000010
|
||||
};
|
||||
|
||||
static int level = INFO;
|
||||
static string prefix = "";
|
||||
static string suffix = "";
|
||||
static string path = "";
|
||||
static std::bitset<8> flags = PRINT_STDOUT | PRINT_STDERR;
|
||||
static bool print_to_stdout;
|
||||
static bool print_to_stderr;
|
||||
|
||||
inline constexpr level_e to_level(int l){
|
||||
return static_cast<level_e>(l);
|
||||
}
|
||||
|
||||
inline constexpr int to_int(const level_e& l){
|
||||
return static_cast<int>(l);
|
||||
}
|
||||
|
||||
inline constexpr void set_flag(uint8_t flag, bool value){
|
||||
(value) ? flags.set(flag) : flags.reset(flag);
|
||||
}
|
||||
|
||||
inline constexpr bool get_flag(uint8_t flag){
|
||||
return flags.test(flag);
|
||||
}
|
||||
|
||||
inline constexpr void set_prefix_if(const std::string& v, bool predicate = !prefix.empty()){
|
||||
prefix = (predicate) ? v : prefix;
|
||||
}
|
||||
|
||||
inline constexpr void set_suffix_if(const std::string& v, bool predicate = !suffix.empty()){
|
||||
suffix = (predicate) ? v : suffix;
|
||||
}
|
||||
|
||||
static void vlog(fmt::string_view format, fmt::format_args args){
|
||||
fmt::vprint(format, args);
|
||||
}
|
||||
|
|
@ -47,30 +78,13 @@ namespace gdpm::log
|
|||
|
||||
template <typename S, typename...Args>
|
||||
static constexpr void info(const S& format, Args&&...args){
|
||||
if(log::level < to_int(log::INFO))
|
||||
return;
|
||||
#if GDPM_LOG_LEVEL > NONE
|
||||
set_prefix_if(fmt::format("[INFO {}] ", utils::timestamp()));
|
||||
set_suffix_if("\n");
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_INFO "[INFO {}] {}\n" GDPM_COLOR_LOG_RESET, utils::timestamp(), format),
|
||||
// fmt::make_format_args<Args...>(args...)
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename S, typename...Args>
|
||||
static constexpr void info(const S& prefix, const S& format, Args&&...args){
|
||||
#if GDPM_LOG_LEVEL > INFO
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_INFO + prefix + GDPM_COLOR_LOG_RESET, format),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename S, typename...Args>
|
||||
static constexpr void info(const context& context, const S& format, Args&&...args){
|
||||
#if GDPM_LOG_LEVEL > INFO
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_INFO + context.prefix + GDPM_COLOR_LOG_RESET, format),
|
||||
fmt::format(GDPM_COLOR_LOG_INFO "{}{}{}" GDPM_COLOR_LOG_RESET, prefix, format, suffix),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
|
|
@ -78,10 +92,13 @@ namespace gdpm::log
|
|||
|
||||
template <typename S, typename...Args>
|
||||
static constexpr void info_n(const S& format, Args&&...args){
|
||||
if(log::level < to_int(log::INFO))
|
||||
return;
|
||||
#if GDPM_LOG_LEVEL > INFO
|
||||
set_prefix_if(fmt::format("[INFO {}] ", utils::timestamp()));
|
||||
set_suffix_if("");
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_INFO "[INFO {}] {}" GDPM_COLOR_LOG_RESET, utils::timestamp(), format),
|
||||
// fmt::make_format_args<Args...>(args...)
|
||||
fmt::format(GDPM_COLOR_LOG_INFO "{}{}{}" GDPM_COLOR_LOG_RESET, prefix, format, suffix),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
|
|
@ -89,30 +106,13 @@ namespace gdpm::log
|
|||
|
||||
template <typename S, typename...Args>
|
||||
static constexpr void error(const S& format, Args&&...args){
|
||||
if(log::level < to_int(log::ERROR))
|
||||
return;
|
||||
#if GDPM_LOG_LEVEL > ERROR
|
||||
set_prefix_if(std::format("[ERROR {}] ", utils::timestamp()));
|
||||
set_suffix_if("\n");
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_ERROR "[ERROR {}] {}\n" GDPM_COLOR_LOG_RESET, utils::timestamp(), format),
|
||||
// fmt::make_format_args<Args...>(args...)
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename S, typename...Args>
|
||||
static constexpr void error(const S& prefix, const S& format, Args&&...args){
|
||||
#if GDPM_LOG_LEVEL > ERROR
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_ERROR + prefix + GDPM_COLOR_LOG_RESET, format),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename S, typename...Args>
|
||||
static constexpr void error(const context& context, const S& format, Args&&...args){
|
||||
#if GDPM_LOG_LEVEL > ERROR
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_ERROR + context.prefix + GDPM_COLOR_LOG_RESET, format),
|
||||
fmt::format(GDPM_COLOR_LOG_ERROR "{}{}{}" GDPM_COLOR_LOG_RESET, prefix, format, suffix),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
|
|
@ -120,30 +120,13 @@ namespace gdpm::log
|
|||
|
||||
template <typename S, typename...Args>
|
||||
static constexpr void debug(const S& format, Args&&...args){
|
||||
if(log::level < to_int(log::DEBUG))
|
||||
return;
|
||||
#if GDPM_LOG_LEVEL > DEBUG
|
||||
set_prefix_if(std::format("[DEBUG {}] ", utils::timestamp()));
|
||||
set_suffix_if("\n");
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_DEBUG "[DEBUG {}] {}\n" GDPM_COLOR_LOG_RESET, utils::timestamp(), format),
|
||||
// fmt::make_format_args<Args...>(args...)
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename S, typename...Args>
|
||||
static constexpr void debug(const S& prefix, const S& format, Args&&...args){
|
||||
#if GDPM_LOG_LEVEL > DEBUG
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_DEBUG + prefix + GDPM_COLOR_LOG_RESET, format),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename S, typename...Args>
|
||||
static constexpr void debug(const context& context, const S& format, Args&&...args){
|
||||
#if GDPM_LOG_LEVEL > DEBUG
|
||||
vlog(
|
||||
fmt::format(GDPM_COLOR_LOG_DEBUG + context.prefix + GDPM_COLOR_LOG_RESET, format),
|
||||
fmt::format(GDPM_COLOR_LOG_DEBUG "{}{}{}" GDPM_COLOR_LOG_RESET, prefix, format, suffix),
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
#endif
|
||||
|
|
@ -153,7 +136,6 @@ namespace gdpm::log
|
|||
static constexpr void print(const S& format, Args&&...args){
|
||||
vlog(
|
||||
fmt::format("{}", format),
|
||||
// fmt::make_format_args<Args...>(args...)
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
}
|
||||
|
|
@ -162,7 +144,6 @@ namespace gdpm::log
|
|||
static constexpr void println(const S& format, Args&&...args){
|
||||
vlog(
|
||||
fmt::format("{}\n", format),
|
||||
// fmt::make_format_args<Args...>(args...)
|
||||
fmt::make_format_args(args...)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue