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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright (C) 2014 Cloudius Systems, Ltd.
24 #include <fmt/ostream.h>
25 #include <fmt/printf.h>
30 #include <seastar/core/sstring.hh>
35 operator<<(std::ostream& os, const void* ptr) {
36 auto flags = os.flags();
37 os << "0x" << std::hex << reinterpret_cast<uintptr_t>(ptr);
45 operator<<(std::ostream&& os, const void* ptr) {
46 return os << ptr; // selects non-rvalue version
51 template <typename... A>
52 [[deprecated("use std::format_to() or fmt::print()")]]
54 fprint(std::ostream& os, const char* fmt, A&&... a) {
55 ::fmt::fprintf(os, fmt, std::forward<A>(a)...);
59 template <typename... A>
60 [[deprecated("use std::format_to() or fmt::print()")]]
62 print(const char* fmt, A&&... a) {
63 ::fmt::printf(fmt, std::forward<A>(a)...);
66 template <typename... A>
67 [[deprecated("use std::format() or fmt::format()")]]
69 sprint(const char* fmt, A&&... a) {
70 std::ostringstream os;
71 ::fmt::fprintf(os, fmt, std::forward<A>(a)...);
75 template <typename... A>
76 [[deprecated("use std::format() or fmt::format()")]]
78 sprint(const sstring& fmt, A&&... a) {
79 std::ostringstream os;
80 ::fmt::fprintf(os, fmt.c_str(), std::forward<A>(a)...);
84 template <typename Iterator>
86 format_separated(Iterator b, Iterator e, const char* sep = ", ") {
99 template <typename TimePoint>
100 struct usecfmt_wrapper {
104 template <typename TimePoint>
106 usecfmt_wrapper<TimePoint>
107 usecfmt(TimePoint tp) {
111 template <typename Clock, typename Rep, typename Period>
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);
120 template <typename... A>
123 std::cout << usecfmt(std::chrono::high_resolution_clock::now()) << " ";
124 print(std::forward<A>(a)...);
128 * Evaluate the formatted string in a native fmt library format
130 * @param fmt format string with the native fmt library syntax
131 * @param a positional parameters
133 * @return sstring object with the result of applying the given positional
134 * parameters on a given format string.
136 template <typename... A>
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)...);
143 fmt::format_to(out, fmt, std::forward<A>(a)...);
145 return sstring{out.data(), out.size()};
148 // temporary, use fmt::print() instead
149 template <typename... A>
150 [[deprecated("use std::format() or fmt::print()")]]
152 fmt_print(std::ostream& os, const char* format, A&&... a) {
153 fmt::print(os, format, std::forward<A>(a)...);