]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/core/print.hh
import quincy beta 17.1.0
[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 [[deprecated("use std::format_to() or fmt::print()")]]
53 std::ostream&
54 fprint(std::ostream& os, const char* fmt, A&&... a) {
55 ::fmt::fprintf(os, fmt, std::forward<A>(a)...);
56 return os;
57 }
58
59 template <typename... A>
60 [[deprecated("use std::format_to() or fmt::print()")]]
61 void
62 print(const char* fmt, A&&... a) {
63 ::fmt::printf(fmt, std::forward<A>(a)...);
64 }
65
66 template <typename... A>
67 [[deprecated("use std::format() or fmt::format()")]]
68 std::string
69 sprint(const char* fmt, A&&... a) {
70 std::ostringstream os;
71 ::fmt::fprintf(os, fmt, std::forward<A>(a)...);
72 return os.str();
73 }
74
75 template <typename... A>
76 [[deprecated("use std::format() or fmt::format()")]]
77 std::string
78 sprint(const sstring& fmt, A&&... a) {
79 std::ostringstream os;
80 ::fmt::fprintf(os, fmt.c_str(), std::forward<A>(a)...);
81 return os.str();
82 }
83
84 template <typename Iterator>
85 std::string
86 format_separated(Iterator b, Iterator e, const char* sep = ", ") {
87 std::string ret;
88 if (b == e) {
89 return ret;
90 }
91 ret += *b++;
92 while (b != e) {
93 ret += sep;
94 ret += *b++;
95 }
96 return ret;
97 }
98
99 template <typename TimePoint>
100 struct usecfmt_wrapper {
101 TimePoint val;
102 };
103
104 template <typename TimePoint>
105 inline
106 usecfmt_wrapper<TimePoint>
107 usecfmt(TimePoint tp) {
108 return { tp };
109 };
110
111 template <typename Clock, typename Rep, typename Period>
112 std::ostream&
113 operator<<(std::ostream& os, usecfmt_wrapper<std::chrono::time_point<Clock, std::chrono::duration<Rep, Period>>> tp) {
114 auto usec = std::chrono::duration_cast<std::chrono::microseconds>(tp.val.time_since_epoch()).count();
115 std::ostream tmp(os.rdbuf());
116 tmp << std::setw(12) << (usec / 1000000) << "." << std::setw(6) << std::setfill('0') << (usec % 1000000);
117 return os;
118 }
119
120 template <typename... A>
121 void
122 log(A&&... a) {
123 std::cout << usecfmt(std::chrono::high_resolution_clock::now()) << " ";
124 print(std::forward<A>(a)...);
125 }
126
127 /**
128 * Evaluate the formatted string in a native fmt library format
129 *
130 * @param fmt format string with the native fmt library syntax
131 * @param a positional parameters
132 *
133 * @return sstring object with the result of applying the given positional
134 * parameters on a given format string.
135 */
136 template <typename... A>
137 sstring
138 format(const char* fmt, A&&... a) {
139 fmt::memory_buffer out;
140 #if FMT_VERSION >= 80000
141 fmt::format_to(fmt::appender(out), fmt::runtime(fmt), std::forward<A>(a)...);
142 #else
143 fmt::format_to(out, fmt, std::forward<A>(a)...);
144 #endif
145 return sstring{out.data(), out.size()};
146 }
147
148 // temporary, use fmt::print() instead
149 template <typename... A>
150 [[deprecated("use std::format() or fmt::print()")]]
151 std::ostream&
152 fmt_print(std::ostream& os, const char* format, A&&... a) {
153 fmt::print(os, format, std::forward<A>(a)...);
154 return os;
155 }
156
157 }