feat(log): printStatus amb claudàtor acolorit al davant
This commit is contained in:
1 parent
7a1261202c
commit
b81a453b32
7 files changed
+112
-19
No files matched your search
+80
-10
@@ -1,7 +1,16 @@
|
||||
#include "utils/utils.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstdio>
|
||||
#endif
|
||||
|
||||
#include "utils/log.hpp"
|
||||
|
||||
@@ -9,14 +18,75 @@ auto getFileName(const std::string& path) -> std::string {
|
||||
return std::filesystem::path(path).filename().string();
|
||||
}
|
||||
|
||||
void printWithDots(const std::string& tag, const std::string& filename, const std::string& status) {
|
||||
// Reprodueix l'alineació històrica (amplada total ≈ 50). El logger anteposa
|
||||
// automàticament "tag: ", per això aquí només componem el cos.
|
||||
constexpr std::size_t TOTAL_WIDTH = 50;
|
||||
const std::size_t PREFIX_LEN = tag.size() + 2; // "{tag}: "
|
||||
const std::size_t USED = PREFIX_LEN + status.size();
|
||||
const std::size_t FIELD = (USED < TOTAL_WIDTH) ? (TOTAL_WIDTH - USED) : 0;
|
||||
std::string body = filename;
|
||||
if (body.size() < FIELD) { body.resize(FIELD, '.'); }
|
||||
Log::info(tag, "{}{}", body, status);
|
||||
namespace {
|
||||
|
||||
constexpr std::size_t BRACKET_INNER_WIDTH = 8;
|
||||
|
||||
auto defaultLabel(StatusKind kind) -> std::string_view {
|
||||
switch (kind) {
|
||||
case StatusKind::SUCCESS:
|
||||
return "LOADED";
|
||||
case StatusKind::INFO:
|
||||
return "INFO";
|
||||
case StatusKind::ERROR:
|
||||
return "ERROR";
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
auto colorCode(StatusKind kind) -> std::string_view {
|
||||
switch (kind) {
|
||||
case StatusKind::SUCCESS:
|
||||
return "\033[1;32m";
|
||||
case StatusKind::INFO:
|
||||
return "\033[1;34m";
|
||||
case StatusKind::ERROR:
|
||||
return "\033[1;31m";
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// Decideix una sola vegada per execució: stderr ha d'admetre ANSI?
|
||||
// A Windows només Git Bash (MSYSTEM) el suporta sense habilitar VT.
|
||||
auto useColor() -> bool {
|
||||
static const bool ENABLED = [] {
|
||||
#ifdef _WIN32
|
||||
return std::getenv("MSYSTEM") != nullptr;
|
||||
#else
|
||||
return isatty(fileno(stderr)) != 0;
|
||||
#endif
|
||||
}();
|
||||
return ENABLED;
|
||||
}
|
||||
|
||||
// Construeix "[ <padded label> ]" amb el contingut interior centrat dins
|
||||
// d'una amplada mínima de BRACKET_INNER_WIDTH. Si `with_color` és true,
|
||||
// només l'interior porta codis ANSI; els claudàtors queden sense color
|
||||
// (mateix patró que `mini`: `[<color>...<reset>]`).
|
||||
auto buildBracket(std::string_view label, StatusKind kind, bool with_color) -> std::string {
|
||||
const std::size_t INNER = std::max(BRACKET_INNER_WIDTH, label.size());
|
||||
const std::size_t PAD = INNER - label.size();
|
||||
const std::size_t LEFT = PAD / 2;
|
||||
const std::size_t RIGHT = PAD - LEFT;
|
||||
std::string out;
|
||||
out.push_back('[');
|
||||
if (with_color) { out.append(colorCode(kind)); }
|
||||
out.append(LEFT, ' ');
|
||||
out.append(label);
|
||||
out.append(RIGHT, ' ');
|
||||
if (with_color) { out.append("\033[0m"); }
|
||||
out.push_back(']');
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void printStatus(const std::string& tag,
|
||||
const std::string& filename,
|
||||
StatusKind kind,
|
||||
std::string_view label) {
|
||||
const std::string_view EFFECTIVE_LABEL = label.empty() ? defaultLabel(kind) : label;
|
||||
const std::string BRACKET = buildBracket(EFFECTIVE_LABEL, kind, useColor());
|
||||
const Log::Level LVL = (kind == StatusKind::ERROR) ? Log::Level::ERROR : Log::Level::INFO;
|
||||
Log::logPrefixed(LVL, tag, BRACKET, filename);
|
||||
}
|
||||
Reference in new issue
Block a user