29#include <gmock/gmock.h>
30#include <gtest/gtest.h>
68 ASSERT_EQ(
gtestLogOutput.str(),
"File:10: test: sample message\n\n");
74 "File:10: test: sample message\nwith \n3 lines\n");
85 ASSERT_EQ(
gtestLogOutput.str(),
"File:10: test: sample message\n");
89 (
const char *)
"sample %s with %d arguments\n",
"message", 2);
91 "File:10: test: sample message with 2 arguments\n");
101 ASSERT_EQ(
gtestLogOutput.str(),
"File:10: test: sample message\n");
105 std::string(
"sample %s with %d arguments\n"),
"message", 2);
107 "File:10: test: sample message with 2 arguments\n");
117 ASSERT_EQ(
gtestLogOutput.str(),
"File:10: test: <extra arg>% message\n");
123 "File:10: test: sample messa<extra arg>%ge\n");
133 ASSERT_EQ(
gtestLogOutput.str(),
"File:10: test: <extra arg>% message\n");
139 "File:10: test: sample messa<extra arg>%ge\n");
145 class DisabledLogger :
public Logger
148 DisabledLogger(
const char *prefix)
224 testing::HasSubstr(
"File:10: fatal: message\nMemory Usage:"));
244 ::testing::HasSubstr(
"File:10: panic: message\nMemory Usage:"));
258 ASSERT_THAT(
gtestLogOutput.str(), ::testing::HasSubstr(
"test: message\n"));
262 ASSERT_THAT(
gtestLogOutput.str(), ::testing::HasSubstr(
"test: message\n"));
267 ::testing::HasSubstr(
"test: sample message\n\n"));
272 ::testing::HasSubstr(
"test: sample message\nwith \n3 lines\n"));
275 base_message(logger,
"sample %s with %d arguments\n",
"message", 2);
277 ::testing::HasSubstr(
"test: sample message with 2 arguments\n"));
285 for (
int i = 0;
i < 10;
i++) {
290 ::testing::HasSubstr(
"test: message\n"));
305 ::testing::HasSubstr(
"warn: message\n"));
310 ::testing::HasSubstr(
"warn: message\n"));
313 warn(
"sample message\n\n");
315 ::testing::HasSubstr(
"warn: sample message\n\n"));
318 warn(
"sample message\nwith \n3 lines");
320 ::testing::HasSubstr(
"warn: sample message\nwith \n3 lines\n"));
323 warn(
"sample %s with %d arguments\n",
"message", 2);
325 ::testing::HasSubstr(
"warn: sample message with 2 arguments\n"));
336 ::testing::HasSubstr(
"info: message\n"));
341 ::testing::HasSubstr(
"info: message\n"));
344 inform(
"sample message\n\n");
346 ::testing::HasSubstr(
"info: sample message\n\n"));
349 inform(
"sample message\nwith \n3 lines");
351 ::testing::HasSubstr(
"info: sample message\nwith \n3 lines\n"));
354 inform(
"sample %s with %d arguments\n",
"message", 2);
356 ::testing::HasSubstr(
"info: sample message with 2 arguments\n"));
367 ::testing::HasSubstr(
"hack: message\n"));
372 ::testing::HasSubstr(
"hack: message\n"));
375 hack(
"sample message\n\n");
377 ::testing::HasSubstr(
"hack: sample message\n\n"));
380 hack(
"sample message\nwith \n3 lines");
382 ::testing::HasSubstr(
"hack: sample message\nwith \n3 lines\n"));
385 hack(
"sample %s with %d arguments\n",
"message", 2);
387 ::testing::HasSubstr(
"hack: sample message with 2 arguments\n"));
393 for (
int i = 0;
i < 10;
i++) {
398 ::testing::HasSubstr(
"warn: message\n"));
408 for (
int i = 0;
i < 10;
i++) {
413 ::testing::HasSubstr(
"info: message\n"));
423 for (
int i = 0;
i < 10;
i++) {
428 ::testing::HasSubstr(
"hack: message\n"));
441 ::testing::HasSubstr(
"warn: message\n"));
451 for (
int i = 0;
i < 10;
i++) {
456 ::testing::HasSubstr(
"warn: message\n"));
464TEST(LoggingDeathTest, EmptyPrefix)
467 GTEST_SKIP() <<
"Skipping as assertions are "
468 "stripped out of fast builds";
470 ASSERT_DEATH(
Logger(
nullptr),
"");
474TEST(LoggingDeathTest, ExitHelper)
476 ASSERT_DEATH(
Logger(
"test: ").exit_helper(),
"");
480TEST(LoggingDeathTest, WarnLoggerExitHelper)
486TEST(LoggingDeathTest, InfoLoggerExitHelper)
492TEST(LoggingDeathTest, HackLoggerExitHelper)
498TEST(LoggingDeathTest, FatalLoggerExitHelper)
504TEST(LoggingDeathTest, PanicLoggerExitHelper)
510TEST(LoggingDeathTest, ExitMessage)
513 ASSERT_DEATH(
exit_message(logger,
"message\n"),
"test: message\n");
519 ASSERT_DEATH(
panic(
"message\n"),
520 ::testing::HasSubstr(
"panic: message\nMemory Usage:"));
526 ASSERT_DEATH(
fatal(
"message\n"),
527 ::testing::HasSubstr(
"fatal: message\nMemory Usage:"));
534 ASSERT_DEATH(
panic_if(
true,
"message\n"), ::testing::HasSubstr(
535 "panic: panic condition true occurred: message\nMemory Usage:"));
542 ASSERT_DEATH(
fatal_if(
true,
"message\n"), ::testing::HasSubstr(
543 "fatal: fatal condition true occurred: message\nMemory Usage:"));
547TEST(LoggingDeathTest, gem5Assert)
550 GTEST_SKIP() <<
"Skipping as assertions are "
551 "stripped out of fast builds";
554 ASSERT_DEATH(
gem5_assert(
false,
"message\n"), ::testing::HasSubstr(
555 "panic: assert(false) failed: message\nMemory Usage:"));
556 ASSERT_DEATH(
gem5_assert(
false,
"%s, %s!\n",
"Hello",
"World"),
557 ::testing::HasSubstr(
558 "panic: assert(false) failed: Hello, World!\nMemory Usage:"));
560 ASSERT_DEATH(
gem5_assert(
false), ::testing::HasSubstr(
561 "panic: assert(false) failed\nMemory Usage:"));
Temporarily redirects cerr to gtestLogOutput.
std::streambuf * old
Used to restore cerr's streambuf.
static Logger & getWarn()
Logger(const char *prefix)
static Logger & getInfo()
static Logger & getHack()
void print(const Loc &loc, const char *format, const Args &...args)
static Logger & getPanic()
Get a Logger for the specified type of message.
static Logger & getFatal()
static void setLevel(LogLevel ll)
#define panic(...)
This implements a cprintf based panic() function.
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
#define gem5_assert(cond,...)
The assert macro will function like a normal assert, but will use panic instead of straight abort().
#define warn_if_once(cond,...)
#define fatal(...)
This implements a cprintf based fatal() function.
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
#define base_message_once(logger,...)
#define exit_message(logger,...)
#define base_message(logger,...)
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
TEST_F(LoggingFixture, BasicPrint)
Test the most basic print.
TEST(LoggingDeathTest, EmptyPrefix)
Test that a logger cannot be created with an empty prefix.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
thread_local GTestLogOutput gtestLogOutput