removed lots of asserts
Some of them relied on the side effects of evaluating their argument but
for most of them it just makes more sense to use CHECK.
diff --git a/aos/common/logging/logging.h b/aos/common/logging/logging.h
index e6e14f9..c54e434 100644
--- a/aos/common/logging/logging.h
+++ b/aos/common/logging/logging.h
@@ -131,7 +131,6 @@
// don't support streaming in extra text. Some of the implementation is borrowed
// from there too.
// They all LOG(FATAL) with a helpful message when the check fails.
-// TODO(brians): Replace assert with CHECK
// Portions copyright (c) 1999, Google Inc.
// All rights reserved.
//
@@ -221,6 +220,18 @@
// initializer lists.
#define CHECK_NOTNULL(val) ::aos::CheckNotNull(STRINGIFY(val), val)
+inline int CheckSyscall(const char *syscall_string, int value) {
+ if (__builtin_expect(value == -1, false)) {
+ PLOG(FATAL, "%s failed", syscall_string);
+ }
+ return value;
+}
+
+// Check that syscall does not return -1. If it does, PLOG(FATAL)s. This is
+// useful for quickly checking syscalls where it's not very useful to print out
+// the values of any of the arguments.
+#define PCHECK(syscall) ::aos::CheckSyscall(STRINGIFY(syscall), syscall)
+
} // namespace aos
#endif // __cplusplus
diff --git a/aos/common/logging/logging_impl.cc b/aos/common/logging/logging_impl.cc
index 969c0ee..8aa66f5 100644
--- a/aos/common/logging/logging_impl.cc
+++ b/aos/common/logging/logging_impl.cc
@@ -1,6 +1,5 @@
#include "aos/common/logging/logging_impl.h"
-#include <assert.h>
#include <stdarg.h>
#include "aos/common/time.h"
diff --git a/aos/common/logging/logging_impl.h b/aos/common/logging/logging_impl.h
index f24ce6a..35e24a0 100644
--- a/aos/common/logging/logging_impl.h
+++ b/aos/common/logging/logging_impl.h
@@ -351,6 +351,7 @@
// Runs the given function with the current LogImplementation (handles switching
// it out while running function etc).
+// levels is how many LogImplementations to not use off the stack.
void RunWithCurrentImplementation(
int levels, ::std::function<void(LogImplementation *)> function);
diff --git a/aos/common/logging/logging_impl_test.cc b/aos/common/logging/logging_impl_test.cc
index 34bcdd1..8331c37 100644
--- a/aos/common/logging/logging_impl_test.cc
+++ b/aos/common/logging/logging_impl_test.cc
@@ -1,10 +1,11 @@
#include <string>
+#include <inttypes.h>
+
#include "gtest/gtest.h"
#include "aos/common/logging/logging_impl.h"
#include "aos/common/time.h"
-#include <inttypes.h>
using ::testing::AssertionResult;
using ::testing::AssertionSuccess;
@@ -19,6 +20,11 @@
virtual void DoLog(log_level level, const char *format, va_list ap) {
internal::FillInMessage(level, format, ap, &message_);
+ if (level == FATAL) {
+ internal::PrintMessage(stderr, message_);
+ abort();
+ }
+
used_ = true;
}
@@ -82,8 +88,6 @@
first = false;
Init();
- // We'll end up with several of them stacked up, but it really doesn't
- // matter.
AddImplementation(log_implementation = new TestLogImplementation());
}
@@ -133,13 +137,20 @@
EXPECT_TRUE(WasLogged(WARNING, expected.str()));
}
-#ifndef __VXWORKS__
TEST_F(LoggingDeathTest, Fatal) {
ASSERT_EXIT(LOG(FATAL, "this should crash it\n"),
::testing::KilledBySignal(SIGABRT),
"this should crash it");
}
-#endif
+
+TEST_F(LoggingDeathTest, PCHECK) {
+ EXPECT_DEATH(PCHECK(fprintf(stdin, "nope")),
+ ".*fprintf\\(stdin, \"nope\"\\).*failed.*");
+}
+
+TEST_F(LoggingTest, PCHECK) {
+ EXPECT_EQ(7, PCHECK(printf("abc123\n")));
+}
TEST_F(LoggingTest, PrintfDirectives) {
LOG(INFO, "test log %%1 %%d\n");
diff --git a/aos/common/logging/logging_interface.cc b/aos/common/logging/logging_interface.cc
index e4c6fcd..0bec52c 100644
--- a/aos/common/logging/logging_interface.cc
+++ b/aos/common/logging/logging_interface.cc
@@ -1,6 +1,5 @@
#include "aos/common/logging/logging_impl.h"
-#include <assert.h>
#include <stdarg.h>
#include <string.h>
@@ -47,7 +46,6 @@
LogImplementation *const top_implementation = context->implementation;
LogImplementation *new_implementation = top_implementation;
LogImplementation *implementation = NULL;
- assert(levels >= 1);
for (int i = 0; i < levels; ++i) {
implementation = new_implementation;
if (new_implementation == NULL) {