Skip to content

Commit

Permalink
migrate to fmt::format
Browse files Browse the repository at this point in the history
Reviewed By: Mizuchi

Differential Revision: D67628085

fbshipit-source-id: f548a65c0ad806364c6441f48226a1696716d68f
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Jan 4, 2025
1 parent 749dfbc commit 47e0d89
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 57 deletions.
74 changes: 35 additions & 39 deletions third-party/folly/src/folly/logging/test/CustomLogFormatterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <cstdlib>

#include <fmt/format.h>

#include <folly/Format.h>
#include <folly/init/Init.h>
#include <folly/logging/LogMessage.h>
Expand All @@ -28,6 +30,7 @@

FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations")

using namespace fmt::literals;
using namespace folly;

namespace {
Expand Down Expand Up @@ -178,21 +181,17 @@ TEST(CustomLogFormatter, fileNoExt) {
}

TEST(CustomLogFormatter, multiline) {
auto tid = getOSThreadID();
std::map<std::string, std::string> formatMap{
{"tid", folly::to<std::string>(tid)}};

// Log a multi-line message
auto expected = folly::svformat(
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] Eeek, a mouse!\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] . .\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] ( ).( )\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] (o o) .-._.'\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] ( - )\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] mm mm\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] \n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] =============\n",
formatMap);
auto expected = fmt::format(
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] Eeek, a mouse!\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] . .\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] ( ).( )\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] (o o) .-._.'\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] ( - )\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] mm mm\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] \n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] =============\n",
"tid"_a = getOSThreadID());
EXPECT_EQ(
expected,
formatMsg(
Expand Down Expand Up @@ -238,15 +237,11 @@ TEST(CustomLogFormatter, multiline) {
}

TEST(CustomLogFormatter, singleNewline) {
auto tid = getOSThreadID();
std::map<std::string, std::string> formatMap{
{"tid", folly::to<std::string>(tid)}};

// Logging a single newline is basically two empty strings.
auto expected = folly::svformat(
"V0417 13:45:56.123456 {tid:>5s} foo.txt:123] \n"
"V0417 13:45:56.123456 {tid:>5s} foo.txt:123] \n",
formatMap);
auto expected = fmt::format(
"V0417 13:45:56.123456 {tid:>5} foo.txt:123] \n"
"V0417 13:45:56.123456 {tid:>5} foo.txt:123] \n",
"tid"_a = getOSThreadID());
EXPECT_EQ(
expected,
formatMsg(
Expand All @@ -265,9 +260,9 @@ TEST(CustomLogFormatter, coloring) {

{ // Logging a DBG9 message should log in grey color (\033[1;30m) and should
// call reset color at the end of the message (\033[0m)
auto expected = folly::svformat(
"\033[1;30mV0417 13:45:56.123456 {tid:>5s} foo.txt:123] DBG9\033[0m\n",
formatMap);
auto expected = fmt::format(
"\033[1;30mV0417 13:45:56.123456 {tid:>5} foo.txt:123] DBG9\033[0m\n",
"tid"_a = getOSThreadID());
EXPECT_EQ(
expected,
formatMsg(
Expand All @@ -281,8 +276,9 @@ TEST(CustomLogFormatter, coloring) {
}
{ // Logging an INFO message when coloring enabled is displayed as regular
// message. i.e. no color or reset sequence
auto expected = folly::svformat(
"I0417 13:45:56.123456 {tid:>5s} foo.txt:123] INFO\n", formatMap);
auto expected = fmt::format(
"I0417 13:45:56.123456 {tid:>5} foo.txt:123] INFO\n",
"tid"_a = getOSThreadID());
EXPECT_EQ(
expected,
formatMsg(
Expand All @@ -296,9 +292,9 @@ TEST(CustomLogFormatter, coloring) {
}
{ // Logging a WARN message should log in yellow color (\033[33m) and should
// call reset color at the end of the message (\033[0m)
auto expected = folly::svformat(
"\033[33mW0417 13:45:56.123456 {tid:>5s} foo.txt:123] WARN\033[0m\n",
formatMap);
auto expected = fmt::format(
"\033[33mW0417 13:45:56.123456 {tid:>5} foo.txt:123] WARN\033[0m\n",
"tid"_a = getOSThreadID());
EXPECT_EQ(
expected,
formatMsg(
Expand All @@ -312,9 +308,9 @@ TEST(CustomLogFormatter, coloring) {
}
{ // Logging a ERR message should log in red color (\033[31m) and should
// call reset color at the end of the message (\033[0m)
auto expected = folly::svformat(
"\033[31mE0417 13:45:56.123456 {tid:>5s} foo.txt:123] ERR\033[0m\n",
formatMap);
auto expected = fmt::format(
"\033[31mE0417 13:45:56.123456 {tid:>5} foo.txt:123] ERR\033[0m\n",
"tid"_a = getOSThreadID());
EXPECT_EQ(
expected,
formatMsg(
Expand All @@ -328,10 +324,10 @@ TEST(CustomLogFormatter, coloring) {
}
{ // Logging a CRITICAL message should log bold in red background (\033[1;41m)
// and should call reset color at the end of the message (\033[0m)
auto expected = folly::svformat(
"\033[1;41mC0417 13:45:56.123456 {tid:>5s} foo.txt:123] "
auto expected = fmt::format(
"\033[1;41mC0417 13:45:56.123456 {tid:>5} foo.txt:123] "
"CRITICAL\033[0m\n",
formatMap);
"tid"_a = getOSThreadID());
EXPECT_EQ(
expected,
formatMsg(
Expand All @@ -345,10 +341,10 @@ TEST(CustomLogFormatter, coloring) {
}
{ // Logging a FATAL message should log bold in red background (\033[1;41m)
// and should call reset color at the end of the message (\033[0m)
auto expected = folly::svformat(
"\033[1;41mF0417 13:45:56.123456 {tid:>5s} foo.txt:123] "
auto expected = fmt::format(
"\033[1;41mF0417 13:45:56.123456 {tid:>5} foo.txt:123] "
"FATAL\033[0m\n",
formatMap);
"tid"_a = getOSThreadID());
EXPECT_EQ(
expected,
formatMsg(
Expand Down
37 changes: 19 additions & 18 deletions third-party/folly/src/folly/logging/test/GlogFormatterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <cstdlib>

#include <fmt/format.h>

#include <folly/Format.h>
#include <folly/init/Init.h>
#include <folly/logging/GlogStyleFormatter.h>
Expand All @@ -28,6 +30,7 @@

FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations")

using namespace fmt::literals;
using namespace folly;

namespace {
Expand Down Expand Up @@ -166,20 +169,19 @@ TEST(GlogFormatter, filename) {

TEST(GlogFormatter, multiline) {
auto tid = getOSThreadID();
std::map<std::string, std::string> formatMap{
{"tid", folly::to<std::string>(tid)}};

// Log a multi-line message
auto expected = folly::svformat(
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] Eeek, a mouse!\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] . .\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] ( ).( )\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] (o o) .-._.'\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] ( - )\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] mm mm\n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] \n"
"V0417 13:45:56.123456 {tid:>5s} rodent.cpp:777] =============\n",
formatMap);
auto expected = fmt::format(
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] Eeek, a mouse!\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] . .\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] ( ).( )\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] (o o) .-._.'\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] ( - )\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] mm mm\n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] \n"
"V0417 13:45:56.123456 {tid:>5} rodent.cpp:777] =============\n",

"tid"_a = tid);
EXPECT_EQ(
expected,
formatMsg(
Expand All @@ -199,14 +201,13 @@ TEST(GlogFormatter, multiline) {

TEST(GlogFormatter, singleNewline) {
auto tid = getOSThreadID();
std::map<std::string, std::string> formatMap{
{"tid", folly::to<std::string>(tid)}};

// Logging a single newline is basically two empty strings.
auto expected = folly::svformat(
"V0417 13:45:56.123456 {tid:>5s} foo.txt:123] \n"
"V0417 13:45:56.123456 {tid:>5s} foo.txt:123] \n",
formatMap);
auto expected = fmt::format(
"V0417 13:45:56.123456 {tid:>5} foo.txt:123] \n"
"V0417 13:45:56.123456 {tid:>5} foo.txt:123] \n",

"tid"_a = tid);
EXPECT_EQ(
expected,
formatMsg(LogLevel::DBG9, "\n", "foo.txt", 123, "testFunction"));
Expand Down

0 comments on commit 47e0d89

Please sign in to comment.