Seastar
High performance C++ framework for concurrent servers
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 <seastar/core/sstring.hh>
25#include <seastar/util/modules.hh>
26#ifndef SEASTAR_MODULE
27#include <fmt/ostream.h>
28#include <fmt/printf.h>
29#include <iostream>
30#include <iomanip>
31#include <chrono>
32#include <sstream>
33#endif
34
35SEASTAR_MODULE_EXPORT
36inline
37std::ostream&
38operator<<(std::ostream&& os, const void* ptr) {
39 return os << ptr; // selects non-rvalue version
40}
41
42namespace seastar {
43
44template <typename... A>
45[[deprecated("use std::format_to() or fmt::print()")]]
46std::ostream&
47fprint(std::ostream& os, const char* fmt, A&&... a) {
48 ::fmt::fprintf(os, fmt, std::forward<A>(a)...);
49 return os;
50}
51
52template <typename... A>
53[[deprecated("use std::format_to() or fmt::print()")]]
54void
55print(const char* fmt, A&&... a) {
56 ::fmt::printf(fmt, std::forward<A>(a)...);
57}
58
59template <typename... A>
60[[deprecated("use std::format() or fmt::format()")]]
61std::string
62sprint(const char* fmt, A&&... a) {
63 std::ostringstream os;
64 ::fmt::fprintf(os, fmt, std::forward<A>(a)...);
65 return os.str();
66}
67
68template <typename... A>
69[[deprecated("use std::format() or fmt::format()")]]
70std::string
71sprint(const sstring& fmt, A&&... a) {
72 std::ostringstream os;
73 ::fmt::fprintf(os, fmt.c_str(), std::forward<A>(a)...);
74 return os.str();
75}
76
77template <typename Iterator>
78[[deprecated("use fmt::join()")]]
79std::string
80format_separated(Iterator b, Iterator e, const char* sep = ", ") {
81 std::string ret;
82 if (b == e) {
83 return ret;
84 }
85 ret += *b++;
86 while (b != e) {
87 ret += sep;
88 ret += *b++;
89 }
90 return ret;
91}
92
93template <typename TimePoint>
95 TimePoint val;
96};
97
98template <typename TimePoint>
99inline
101usecfmt(TimePoint tp) {
102 return { tp };
103};
104
105template <typename Clock, typename Rep, typename Period>
106std::ostream&
107operator<<(std::ostream& os, usecfmt_wrapper<std::chrono::time_point<Clock, std::chrono::duration<Rep, Period>>> tp) {
108 auto usec = std::chrono::duration_cast<std::chrono::microseconds>(tp.val.time_since_epoch()).count();
109 std::ostream tmp(os.rdbuf());
110 tmp << std::setw(12) << (usec / 1000000) << "." << std::setw(6) << std::setfill('0') << (usec % 1000000);
111 return os;
112}
113
114template <typename... A>
115void
116log(A&&... a) {
117 std::cout << usecfmt(std::chrono::high_resolution_clock::now()) << " ";
118 print(std::forward<A>(a)...);
119}
120
130template <typename... A>
131sstring
132format(fmt::format_string<A...> fmt, A&&... a) {
133 fmt::memory_buffer out;
134 fmt::format_to(fmt::appender(out), fmt, std::forward<A>(a)...);
135 return sstring{out.data(), out.size()};
136}
137
138// temporary, use fmt::print() instead
139template <typename... A>
140[[deprecated("use std::format() or fmt::print()")]]
141std::ostream&
142fmt_print(std::ostream& os, const char* format, A&&... a) {
143 fmt::print(os, format, std::forward<A>(a)...);
144 return os;
145}
146
147}
future now()
Returns a ready future.
Definition: later.hh:35
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
sstring format(fmt::format_string< A... > fmt, A &&... a)
Definition: print.hh:132
Definition: print.hh:94