diff --git a/format.cc b/format.cc index 144a8e5c..50b3bf1b 100644 --- a/format.cc +++ b/format.cc @@ -1,7 +1,7 @@ /* Formatting library for C++ - Copyright (c) 2012, Victor Zverovich + Copyright (c) 2012 - 2014, Victor Zverovich All rights reserved. Redistribution and use in source and binary forms, with or without @@ -1122,6 +1122,12 @@ void fmt::print(std::FILE *f, StringRef format, const ArgList &args) { std::fwrite(w.data(), 1, w.size(), f); } +void fmt::print(std::ostream &os, StringRef format, const ArgList &args) { + Writer w; + w.write(format, args); + os.write(w.data(), w.size()); +} + void fmt::print_colored(Color c, StringRef format, const ArgList &args) { char escape[] = "\x1b[30m"; escape[3] = '0' + static_cast(c); diff --git a/format.h b/format.h index e74bb657..ddc9f28d 100644 --- a/format.h +++ b/format.h @@ -1,7 +1,7 @@ /* Formatting library for C++ - Copyright (c) 2012, Victor Zverovich + Copyright (c) 2012 - 2014, Victor Zverovich All rights reserved. Redistribution and use in source and binary forms, with or without @@ -128,8 +128,6 @@ class BasicWriter; typedef BasicWriter Writer; typedef BasicWriter WWriter; -struct FormatSpec; - template class BasicFormatter; @@ -751,6 +749,8 @@ class ArgList { } }; +struct FormatSpec; + // A formatter. template class BasicFormatter { @@ -1691,6 +1691,17 @@ void print(StringRef format, const ArgList &args); */ void print(std::FILE *f, StringRef format, const ArgList &args); +/** + \rst + Prints formatted data to a stream. + + **Example**:: + + print(cerr, "Don't {}!", "panic"); + \endrst + */ +void print(std::ostream &os, StringRef format, const ArgList &args); + template void printf(BasicWriter &w, BasicStringRef format, const ArgList &args) { @@ -1901,6 +1912,7 @@ FMT_VARIADIC(std::string, format, StringRef) FMT_VARIADIC_W(std::wstring, format, WStringRef) FMT_VARIADIC(void, print, StringRef) FMT_VARIADIC(void, print, std::FILE *, StringRef) +FMT_VARIADIC(void, print, std::ostream &, StringRef) FMT_VARIADIC(void, print_colored, Color, StringRef) FMT_VARIADIC(std::string, sprintf, StringRef) FMT_VARIADIC(void, printf, StringRef) diff --git a/test/format-test.cc b/test/format-test.cc index 9992cecd..04eaad14 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1486,19 +1486,22 @@ TEST(FormatIntTest, FormatDec) { EXPECT_EQ("42", FormatDec(42ull)); } -#if FMT_USE_FILE_DESCRIPTORS - TEST(FormatTest, Print) { +#if FMT_USE_FILE_DESCRIPTORS EXPECT_WRITE(stdout, fmt::print("Don't {}!", "panic"), "Don't panic!"); EXPECT_WRITE(stderr, fmt::print(stderr, "Don't {}!", "panic"), "Don't panic!"); +#endif + std::ostringstream os; + fmt::print(os, "Don't {}!", "panic"); + EXPECT_EQ("Don't panic!", os.str()); } +#if FMT_USE_FILE_DESCRIPTORS TEST(FormatTest, PrintColored) { EXPECT_WRITE(stdout, fmt::print_colored(fmt::RED, "Hello, {}!\n", "world"), "\x1b[31mHello, world!\n\x1b[0m"); } - #endif TEST(FormatTest, Variadic) {