18 inline std::string format(std::thread::id
id)
26 inline void formatBuf(
char(&buf)[N],
const char* format, ...)
29 va_start(args, format);
30 vsnprintf(buf, N, format, args);
35 #pragma warning(disable : 4996)
38 inline void unsafe(
char* buf,
const char* format, ...)
41 va_start(args, format);
42 vsprintf(buf, format, args);
46 inline void unsafe(
char* buf,
const char* format, va_list args)
48 vsprintf(buf, format, args);
53 inline std::string format(
const char* format, ...)
57 va_start(args, format);
58 vsnprintf(buf,
sizeof(buf), format, args);
60 return std::string(buf);
63 inline void timespan(
char* buf, std::chrono::milliseconds ms)
65 if (ms < std::chrono::seconds(1))
67 unsafe(buf,
"%lldms", ms.count());
70 if (ms < std::chrono::minutes(1))
72 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(ms);
73 unsafe(buf,
"%llds %lldms", seconds.count(), (ms - seconds).count());
76 if (ms < std::chrono::hours(1)) {
77 auto minutes = std::chrono::duration_cast<std::chrono::minutes>(ms);
78 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(ms - minutes);
79 unsafe(buf,
"%lldm %llds", minutes.count(), seconds.count());
82 if (ms < std::chrono::hours(24))
84 auto hours = std::chrono::duration_cast<std::chrono::hours>(ms);
85 auto minutes = std::chrono::duration_cast<std::chrono::minutes>(ms - hours);
86 unsafe(buf,
"%lldh %lldm", hours.count(), minutes.count());
89 auto days = std::chrono::duration_cast<std::chrono::hours>(ms) / 24;
90 auto hours = std::chrono::duration_cast<std::chrono::hours>(ms) % 24;
91 unsafe(buf,
"%lldd %lldh", days.count(), hours.count());
104 inline char* timer(
char* buf,
int s,
int ms)
106 div_t d = div(s, 60);
113 buf[0] =
'0' + d.quot;
114 buf[1] =
'0' + d.rem;
117 buf[3] =
'0' + d.quot;
118 buf[4] =
'0' + d.rem;
121 buf[6] =
'0' + d.quot;
122 buf[7] =
'0' + d.rem;
123 if (ms < 0)
return &buf[8];
126 buf[11] =
'0' + d.rem;
128 buf[10] =
'0' + d.rem;
129 buf[9] =
'0' + d.quot;
141 inline char* unsignedIntegerReverse(
unsigned long long d,
char* end)
145 lldiv_t result = lldiv(d, 10);
146 *(--end) =
static_cast<char>(
'0' + result.rem);
153 inline char* integerReverse(
long long d,
char* end)
155 bool negative = d < 0;
156 d = negative ? -d : d;
157 end = unsignedIntegerReverse(d, end);
159 return end - negative;
165 inline char* integer(
char* p, int64_t d)
168 char* end = buf +
sizeof(buf);
169 char* start = integerReverse(d, end);
170 size_t len = end - start;
171 memcpy(p, start, len);
177 inline char* fractionalReverse(
unsigned long long d,
char** pEnd,
int precision,
bool zeroFill);
179 inline char* doubleReverse(
char** pEnd,
double d,
int precision=15,
bool zeroFill=
false);
181 inline char* integerNice(
char* p, int64_t d);
186 static const char* HEX_DIGITS_LOWER =
"0123456789abcdef";
187 static const char* HEX_DIGITS_UPPER =
"0123456789ABCDEF";
189 inline char* hex(
char* buf, uint64_t v,
int nDigits,
const char* digitChars)
191 assert(nDigits > 0 && nDigits <= 16);
192 for (
int i=nDigits-1; i >= 0; i--)
194 buf[i] = digitChars[v & 0xf];
201 inline char* hex(
char* buf, uint64_t v,
int nDigits)
203 return hex(buf, v, nDigits, HEX_DIGITS_LOWER);
206 inline char* hexUpper(
char* buf, uint64_t v,
int nDigits)
208 return hex(buf, v, nDigits, HEX_DIGITS_UPPER);
211 char* timeAgo(
char* buf, int64_t secs);