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>
53 fprint(std::ostream& os, const char* fmt, A&&... a) {
54 ::fmt::fprintf(os, fmt, std::forward<A>(a)...);
58 template <typename... A>
60 print(const char* fmt, A&&... a) {
61 ::fmt::printf(fmt, std::forward<A>(a)...);
64 template <typename... A>
66 sprint(const char* fmt, A&&... a) {
67 std::ostringstream os;
68 ::fmt::fprintf(os, fmt, std::forward<A>(a)...);
72 template <typename... A>
74 sprint(const sstring& fmt, A&&... a) {
75 std::ostringstream os;
76 ::fmt::fprintf(os, fmt.c_str(), std::forward<A>(a)...);
80 template <typename Iterator>
82 format_separated(Iterator b, Iterator e, const char* sep = ", ") {
95 template <typename TimePoint>
96 struct usecfmt_wrapper {
100 template <typename TimePoint>
102 usecfmt_wrapper<TimePoint>
103 usecfmt(TimePoint tp) {
107 template <typename Clock, typename Rep, typename Period>
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);
116 template <typename... A>
119 std::cout << usecfmt(std::chrono::high_resolution_clock::now()) << " ";
120 print(std::forward<A>(a)...);
124 * Evaluate the formatted string in a native fmt library format
126 * @param fmt format string with the native fmt library syntax
127 * @param a positional parameters
129 * @return sstring object with the result of applying the given positional
130 * parameters on a given format string.
132 template <typename... A>
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()};
140 // temporary, use fmt::print() instead
141 template <typename... A>
143 fmt_print(std::ostream& os, const char* format, A&&... a) {
144 fmt::print(os, format, std::forward<A>(a)...);