diff --git a/include/fmt/os.h b/include/fmt/os.h index 96a9b93ca403..3a10c9303f11 100644 --- a/include/fmt/os.h +++ b/include/fmt/os.h @@ -227,13 +227,17 @@ class buffered_file { FMT_API auto descriptor() const -> int; - void vprint(string_view format_str, format_args args) { - fmt::vprint(file_, format_str, args); + void vprint(string_view fmt, format_args args) { + fmt::vprint(file_, fmt, args); + } + void vprint_locked(string_view fmt, format_args args) { + fmt::vprint_locked(file_, fmt, args); } - template - inline void print(string_view format_str, const Args&... args) { - vprint(format_str, fmt::make_format_args(args...)); + template + inline void print(string_view fmt, const T&... args) { + const auto& vargs = fmt::make_format_args(args...); + detail::is_locking() ? vprint(fmt, vargs) : vprint_locked(fmt, vargs); } }; diff --git a/test/format-test.cc b/test/format-test.cc index c8293258cbef..207fef164aa2 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -242,7 +242,8 @@ TEST(util_test, format_system_error) { throws_on_alloc = true; } if (!throws_on_alloc) { - fmt::print(stderr, "warning: std::allocator allocates {} chars\n", max_size); + fmt::print(stderr, "warning: std::allocator allocates {} chars\n", + max_size); return; } } @@ -1763,24 +1764,24 @@ TEST(format_test, big_print) { TEST(format_test, line_buffering) { auto pipe = fmt::pipe(); + int write_fd = pipe.write_end.descriptor(); auto write_end = pipe.write_end.fdopen("w"); setvbuf(write_end.get(), nullptr, _IOLBF, 4096); write_end.print("42\n"); + close(write_fd); + try { + write_end.close(); + } catch (const std::system_error&) { + } - std::mutex mutex; - std::condition_variable cv; auto read_end = pipe.read_end.fdopen("r"); std::thread reader([&]() { int n = 0; int result = fscanf(read_end.get(), "%d", &n); (void)result; EXPECT_EQ(n, 42); - cv.notify_one(); }); - std::unique_lock lock(mutex); - ASSERT_EQ(cv.wait_for(lock, std::chrono::minutes(1)), - std::cv_status::no_timeout); reader.join(); } #endif