Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
logchecker.cc
Go to the documentation of this file.
1 
18 #include "ion/base/logchecker.h"
19 
20 #include "ion/base/stringutils.h"
21 
22 namespace ion {
23 namespace base {
24 
26  : previous_writer_(base::GetLogEntryWriter()) {
27  SetLogEntryWriter(this);
28 }
29 
32  SetLogEntryWriter(previous_writer_);
33 
35  if (HasAnyMessages()) {
36  LOG(ERROR) << "LogChecker destroyed with messages: " << GetLogString();
37  }
38 }
39 
40 bool LogChecker::HasMessage(const std::string& severity_string,
41  const std::string& substring) {
42 #if ION_PRODUCTION
43  return true;
45 #else
46  const std::string log = GetLogString();
48  const std::vector<std::string> messages = SplitString(log, "\n");
49  const size_t count = messages.size();
50  for (size_t i = 0; i < count; ++i) {
53  const bool check_dcheck = severity_string == "DFATAL" &&
54  messages[i].find("DCHECK failed") != std::string::npos &&
55  i < count - 1U;
56  if ((messages[i].find(severity_string) == 0 &&
57  messages[i].find(substring) != std::string::npos) ||
58  (check_dcheck &&
59  messages[i + 1U].find(substring) != std::string::npos)) {
61  ClearLog();
62  return true;
63  }
64  }
65  return false;
66 #endif
67 }
68 
69 bool LogChecker::HasNoMessage(const std::string& severity_string,
70  const std::string& substring) {
71 #if ION_PRODUCTION
72  return true;
74 #else
75  const std::string log = GetLogString();
78  const std::vector<std::string> messages = SplitString(log, "\n");
79  const size_t count = messages.size();
80  for (size_t i = 0; i < count; ++i) {
81  if (messages[i].find(severity_string) == 0 &&
82  messages[i].find(substring) != std::string::npos)
83  return false;
84  }
85  return true;
86 #endif
87 }
88 
89 const std::vector<std::string> LogChecker::GetAllMessages() {
90  const std::string log = GetLogString();
91  ClearLog();
92  return SplitString(log, "\n");
93 }
94 
95 void LogChecker::Write(port::LogSeverity severity, const std::string& message) {
96  stream_ << GetSeverityName(severity) << " " << message << std::endl;
97 }
98 
99 } // namespace base
100 } // namespace ion
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...
Definition: logchecker.cc:40
bool HasAnyMessages() const
Returns true if any messages were logged since the instance was constructed or since the last call to...
Definition: logchecker.h:84
const std::vector< std::string > GetAllMessages()
This function is useful for testing code that produces more than one log message at a time...
Definition: logchecker.cc:89
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
void Write(port::LogSeverity severity, const std::string &message) override
LogEntryWriter impl.
Definition: logchecker.cc:95
void ClearLog()
Clears any messages that may be in the log.
Definition: logchecker.h:95
LogSeverity
Definition: logging.h:26
std::vector< std::string > ION_API SplitString(const std::string &str, const std::string &delimiters)
Splits a string into a vector of substrings, given a set of delimiter characters (expressed as a stri...
Definition: stringutils.cc:187
const std::string GetLogString() const
Returns a string containing all current logged messages.
Definition: logchecker.h:100
port::LogEntryWriter * GetLogEntryWriter()
Returns the log-writer that messages are currently logged to.
Definition: logging.cc:192
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 ...
Definition: logchecker.cc:69
~LogChecker() override
The destructor logs an error if unexpected messages were logged, and unconditionally restores the pre...
Definition: logchecker.cc:30
const char * GetSeverityName(LogSeverity severity) const
Convenient way to map a severity-code to a printable represenation.
Definition: logging.cc:24
void SetLogEntryWriter(port::LogEntryWriter *w)
Public functions.
Definition: logging.cc:188
LogChecker()
The constructor sets up to trap all log output.
Definition: logchecker.cc:25