Skip to content

Commit

Permalink
Small logging fix (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeschmitt-tr authored Aug 12, 2024
1 parent a7dd912 commit 3bfe3ea
Showing 1 changed file with 57 additions and 55 deletions.
112 changes: 57 additions & 55 deletions src/xs_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,65 +55,67 @@ namespace logging

void log(logging::Level level, const char * fmt, ...)
{
if (level < _level) {
return;
}

std::string msg = "";
if (level >= _level) {
switch (level) {
case Level::DEBUG:
msg += GRN;
msg += "[DEBUG] ";
break;
case Level::INFO:
msg += OFF;
msg += "[INFO] ";
break;
case Level::WARN:
msg += YLW;
msg += "[WARN] ";
break;
case Level::ERROR:
msg += RED;
msg += "[ERROR] ";
break;
case Level::FATAL:
msg += RED;
msg += "[FATAL] ";
break;
default:
break;
}
switch (level) {
case Level::DEBUG:
msg += GRN;
msg += "[DEBUG] ";
break;
case Level::INFO:
msg += OFF;
msg += "[INFO] ";
break;
case Level::WARN:
msg += YLW;
msg += "[WARN] ";
break;
case Level::ERROR:
msg += RED;
msg += "[ERROR] ";
break;
case Level::FATAL:
msg += RED;
msg += "[FATAL] ";
break;
default:
break;
}

auto duration = std::chrono::system_clock::now().time_since_epoch();
double seconds = std::chrono::duration<double>(duration).count();

msg = msg +
" [" +
std::to_string(seconds) +
"] ";

va_list args;
va_start(args, fmt);
size_t size = 1024;
std::vector<char> dynamicbuf(size);
char * buf = &dynamicbuf[0];

va_list argsTmp;

while (1) {
va_copy(argsTmp, args);
int needed = vsnprintf(buf, size, fmt, argsTmp);
va_end(argsTmp);
if (needed < static_cast<int>(size) - 1 && needed >= 0) {
msg.append(buf, static_cast<size_t>(needed));
break;
}
size = needed >= 0 ? needed + 2 : size * 2;
dynamicbuf.resize(size);
buf = &dynamicbuf[0];
auto duration = std::chrono::system_clock::now().time_since_epoch();
double seconds = std::chrono::duration<double>(duration).count();

msg = msg +
"[" +
std::to_string(seconds) +
"] ";

va_list args;
va_start(args, fmt);
size_t size = 1024;
std::vector<char> dynamicbuf(size);
char * buf = &dynamicbuf[0];

va_list argsTmp;

while (1) {
va_copy(argsTmp, args);
int needed = vsnprintf(buf, size, fmt, argsTmp);
va_end(argsTmp);
if (needed < static_cast<int>(size) - 1 && needed >= 0) {
msg.append(buf, static_cast<size_t>(needed));
break;
}
va_end(args);
msg.append(END);
std::cerr << msg.c_str();
size = needed >= 0 ? needed + 2 : size * 2;
dynamicbuf.resize(size);
buf = &dynamicbuf[0];
}
va_end(args);
msg.append(END);
std::cerr << msg.c_str();
}

void set_level(Level level)
Expand Down

0 comments on commit 3bfe3ea

Please sign in to comment.