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