Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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/format.hh>
25#include <seastar/core/sstring.hh>
26#include <seastar/util/modules.hh>
27#ifndef SEASTAR_MODULE
28#include <fmt/ostream.h>
29#include <iostream>
30#include <iomanip>
31#include <chrono>
32#endif
33
34SEASTAR_MODULE_EXPORT
35inline
36std::ostream&
37operator<<(std::ostream&& os, const void* ptr) {
38 return os << ptr; // selects non-rvalue version
39}
40
41namespace seastar {
42template <typename Iterator>
43[[deprecated("use fmt::join()")]]
44std::string
45format_separated(Iterator b, Iterator e, const char* sep = ", ") {
46 std::string ret;
47 if (b == e) {
48 return ret;
49 }
50 ret += *b++;
51 while (b != e) {
52 ret += sep;
53 ret += *b++;
54 }
55 return ret;
56}
57
58template <typename TimePoint>
60 TimePoint val;
61};
62
63template <typename TimePoint>
64inline
66usecfmt(TimePoint tp) {
67 return { tp };
68};
69
70template <typename Clock, typename Rep, typename Period>
71std::ostream&
72operator<<(std::ostream& os, usecfmt_wrapper<std::chrono::time_point<Clock, std::chrono::duration<Rep, Period>>> tp) {
73 auto usec = std::chrono::duration_cast<std::chrono::microseconds>(tp.val.time_since_epoch()).count();
74 std::ostream tmp(os.rdbuf());
75 tmp << std::setw(12) << (usec / 1000000) << "." << std::setw(6) << std::setfill('0') << (usec % 1000000);
76 return os;
77}
78
79template <typename... A>
80void
81log(A&&... a) {
82 std::cout << usecfmt(std::chrono::high_resolution_clock::now()) << " ";
83 print(std::forward<A>(a)...);
84}
85
86
87// temporary, use fmt::print() instead
88template <typename... A>
89[[deprecated("use std::format() or fmt::print()")]]
90std::ostream&
91fmt_print(std::ostream& os, const char* format, A&&... a) {
92 fmt::print(os, format, std::forward<A>(a)...);
93 return os;
94}
95
96}
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: format.hh:42
Definition: print.hh:59