forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.hh
141 lines (126 loc) · 3.49 KB
/
print.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/
#ifndef PRINT_HH_
#define PRINT_HH_
#include <fmt/ostream.h>
#include <fmt/printf.h>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <sstream>
#include "core/sstring.hh"
#if 0
inline
std::ostream&
operator<<(std::ostream& os, const void* ptr) {
auto flags = os.flags();
os << "0x" << std::hex << reinterpret_cast<uintptr_t>(ptr);
os.flags(flags);
return os;
}
#endif
inline
std::ostream&
operator<<(std::ostream&& os, const void* ptr) {
return os << ptr; // selects non-rvalue version
}
namespace seastar {
template <typename... A>
std::ostream&
fprint(std::ostream& os, const char* fmt, A&&... a) {
::fmt::fprintf(os, fmt, std::forward<A>(a)...);
return os;
}
template <typename... A>
void
print(const char* fmt, A&&... a) {
::fmt::printf(fmt, std::forward<A>(a)...);
}
template <typename... A>
std::string
sprint(const char* fmt, A&&... a) {
std::ostringstream os;
::fmt::fprintf(os, fmt, std::forward<A>(a)...);
return os.str();
}
template <typename... A>
std::string
sprint(const sstring& fmt, A&&... a) {
std::ostringstream os;
::fmt::fprintf(os, fmt.c_str(), std::forward<A>(a)...);
return os.str();
}
template <typename Iterator>
std::string
format_separated(Iterator b, Iterator e, const char* sep = ", ") {
std::string ret;
if (b == e) {
return ret;
}
ret += *b++;
while (b != e) {
ret += sep;
ret += *b++;
}
return ret;
}
template <typename TimePoint>
struct usecfmt_wrapper {
TimePoint val;
};
template <typename TimePoint>
inline
usecfmt_wrapper<TimePoint>
usecfmt(TimePoint tp) {
return { tp };
};
template <typename Clock, typename Rep, typename Period>
std::ostream&
operator<<(std::ostream& os, usecfmt_wrapper<std::chrono::time_point<Clock, std::chrono::duration<Rep, Period>>> tp) {
auto usec = std::chrono::duration_cast<std::chrono::microseconds>(tp.val.time_since_epoch()).count();
std::ostream tmp(os.rdbuf());
tmp << std::setw(12) << (usec / 1000000) << "." << std::setw(6) << std::setfill('0') << (usec % 1000000);
return os;
}
template <typename... A>
void
log(A&&... a) {
std::cout << usecfmt(std::chrono::high_resolution_clock::now()) << " ";
print(std::forward<A>(a)...);
}
/**
* Evaluate the formatted string in a native fmt library format
*
* @param fmt format string with the native fmt library syntax
* @param a positional parameters
*
* @return sstring object with the result of applying the given positional
* parameters on a given format string.
*/
template <typename... A>
sstring
format(const char* fmt, A&&... a) {
fmt::MemoryWriter out;
out.write(fmt, std::forward<A>(a)...);
return sstring{out.data(), out.size()};
}
}
#endif /* PRINT_HH_ */