Skip to content
Snippets Groups Projects
Commit 7ee62814 authored by Simon Désaulniers's avatar Simon Désaulniers Committed by Adrien Béraud
Browse files

log: display time before each line in log

parent 28a5740c
Branches log-with-time
Tags
No related merge requests found
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <chrono>
namespace dht { namespace dht {
namespace log { namespace log {
...@@ -63,13 +64,23 @@ constexpr const Color::Modifier yellow(Color::FG_YELLOW); ...@@ -63,13 +64,23 @@ constexpr const Color::Modifier yellow(Color::FG_YELLOW);
*/ */
void void
printLog(std::ostream& s, char const* m, va_list args) { printLog(std::ostream& s, char const* m, va_list args) {
static constexpr int BUF_SZ = 8192; // print log to buffer
char buffer[BUF_SZ]; std::array<char, 8192> buffer;
int ret = vsnprintf(buffer, sizeof(buffer), m, args); int ret = vsnprintf(buffer.data(), buffer.size(), m, args);
if (ret < 0) if (ret < 0)
return; return;
s.write(buffer, std::min(ret, BUF_SZ));
if (ret >= BUF_SZ) // write timestamp
using namespace std::chrono;
using log_precision = microseconds;
constexpr auto den = log_precision::period::den;
auto micro = duration_cast<log_precision>(steady_clock::now().time_since_epoch()).count();
s << "[" << std::setfill('0') << std::setw(6) << micro / den << "."
<< std::setfill('0') << std::setw(6) << micro % den << "]" << " ";
// write log
s.write(buffer.data(), std::min((size_t)ret, buffer.size()));
if ((size_t)ret >= buffer.size())
s << "[[TRUNCATED]]"; s << "[[TRUNCATED]]";
s.put('\n'); s.put('\n');
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment