Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ion::base::LogChecker Class Reference

The LogChecker class can be used inside unit tests to trap all log output and verify that it matches what is expected. More...

#include "logchecker.h"

Inheritance diagram for ion::base::LogChecker:
Collaboration diagram for ion::base::LogChecker:

Public Member Functions

 LogChecker ()
 The constructor sets up to trap all log output. More...
 
 ~LogChecker () override
 The destructor logs an error if unexpected messages were logged, and unconditionally restores the previous log writer. More...
 
void Write (port::LogSeverity severity, const std::string &message) override
 LogEntryWriter impl. More...
 
bool HasMessage (const std::string &severity, const std::string &substring)
 Returns true if exactly one message was logged since the LogChecker was constructed or since the last call to HasMessage(). More...
 
bool HasNoMessage (const std::string &severity, const std::string &substring)
 Returns true if no message of the given severity and containing the given substring was logged since the LogChecker was constructed or since the last call to HasMessage(). More...
 
bool HasAnyMessages () const
 Returns true if any messages were logged since the instance was constructed or since the last call to CheckLog(). More...
 
const std::vector< std::string > GetAllMessages ()
 This function is useful for testing code that produces more than one log message at a time. More...
 
void ClearLog ()
 Clears any messages that may be in the log. More...
 
const std::string GetLogString () const
 Returns a string containing all current logged messages. More...
 

Protected Member Functions

const char * GetSeverityName (LogSeverity severity) const
 Convenient way to map a severity-code to a printable represenation. More...
 

Detailed Description

The LogChecker class can be used inside unit tests to trap all log output and verify that it matches what is expected.

The destructor will log an error (to the regular stream) that no unexpected messages were logged, but it is up to the client to test for this.

For example, if you have a test that is known to generate two log messages, you can set up the test like this:

TEST(MyTest, Something) { LogChecker log_checker;

SomeFunction("hello");
EXPECT_TRUE(log_checker.HasMessage("ERROR", "invalid type"));
SomeOtherFunction(0);
EXPECT_TRUE(log_checker.HasMessage("WARNING", "zero size"));
EXPECT_FALSE(log_checker.HasAnyMessages());

}

Definition at line 55 of file logchecker.h.

Constructor & Destructor Documentation

ion::base::LogChecker::LogChecker ( )

The constructor sets up to trap all log output.

Definition at line 25 of file logchecker.cc.

References ion::base::SetLogEntryWriter().

ion::base::LogChecker::~LogChecker ( )
override

The destructor logs an error if unexpected messages were logged, and unconditionally restores the previous log writer.

Definition at line 30 of file logchecker.cc.

References ion::port::ERROR, GetLogString(), HasAnyMessages(), LOG, and ion::base::SetLogEntryWriter().

Member Function Documentation

void ion::base::LogChecker::ClearLog ( )
inline

Clears any messages that may be in the log.

Definition at line 95 of file logchecker.h.

Referenced by GetAllMessages(), and HasMessage().

const std::vector< std::string > ion::base::LogChecker::GetAllMessages ( )

This function is useful for testing code that produces more than one log message at a time.

It returns a vector containing all message strings (split at newlines) logged since the LogChecker was constructed or the log was last cleared. This clears the log afterwards.

Definition at line 89 of file logchecker.cc.

References ClearLog(), GetLogString(), and ion::base::SplitString().

const std::string ion::base::LogChecker::GetLogString ( ) const
inline

Returns a string containing all current logged messages.

Definition at line 100 of file logchecker.h.

Referenced by GetAllMessages(), HasMessage(), HasNoMessage(), and ~LogChecker().

const char * ion::port::LogEntryWriter::GetSeverityName ( LogSeverity  severity) const
protectedinherited

Convenient way to map a severity-code to a printable represenation.

Maps a Severity code to a name for printing.

Definition at line 24 of file logging.cc.

References ion::port::DFATAL, ion::port::ERROR, ion::port::FATAL, ion::port::INFO, and ion::port::WARNING.

Referenced by Write().

bool ion::base::LogChecker::HasAnyMessages ( ) const
inline

Returns true if any messages were logged since the instance was constructed or since the last call to CheckLog().

Definition at line 84 of file logchecker.h.

Referenced by ~LogChecker().

bool ion::base::LogChecker::HasMessage ( const std::string &  severity,
const std::string &  substring 
)

Returns true if exactly one message was logged since the LogChecker was constructed or since the last call to HasMessage().

The message must be of the given severity and must contain the given substring. If this returns true, it clears the stream for the next call. If it returns false, it leaves the stream alone so the caller can use GetLogString() to examine the log.

Definition at line 40 of file logchecker.cc.

References ClearLog(), GetLogString(), and ion::base::SplitString().

bool ion::base::LogChecker::HasNoMessage ( const std::string &  severity,
const std::string &  substring 
)

Returns true if no message of the given severity and containing the given substring was logged since the LogChecker was constructed or since the last call to HasMessage().

This leaves the stream alone so the caller can use GetLogString() to examine the log.

Definition at line 69 of file logchecker.cc.

References GetLogString(), and ion::base::SplitString().

void ion::base::LogChecker::Write ( port::LogSeverity  severity,
const std::string &  message 
)
overridevirtual

LogEntryWriter impl.

Implements ion::port::LogEntryWriter.

Definition at line 95 of file logchecker.cc.

References ion::port::LogEntryWriter::GetSeverityName().


The documentation for this class was generated from the following files: