Remove usage of CHECK_NOTNULL

We want to switch to absl logging instead of glog.  gtest and ceres are
going there, and we already have absl as a dependency.  ABSL doesn't
have CHECK_NOTNULL, and we can move things over in an easier to review
fashion.

Change-Id: Ifd9a11ec34a2357cec43f88dba015db9c28ed2cf
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/aos/events/logging/buffer_encoder_param_test.cc b/aos/events/logging/buffer_encoder_param_test.cc
index 7c9bb4b..f6bc3b6 100644
--- a/aos/events/logging/buffer_encoder_param_test.cc
+++ b/aos/events/logging/buffer_encoder_param_test.cc
@@ -2,6 +2,8 @@
 
 #include "gmock/gmock.h"
 
+#include "aos/testing/tmpdir.h"
+
 namespace aos::logger::testing {
 
 // Verifies that Clear affects the sizes as expected.
@@ -39,8 +41,8 @@
 // it comes back out the same.
 TEST_P(BufferEncoderTest, RoundTrip) {
   std::uniform_int_distribution<int> quantity_distribution(20, 60);
-  const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
-  const std::string file_path = std::string(test_dir) + "/foo";
+  const std::string test_dir = aos::testing::TestTmpDir();
+  const std::string file_path = test_dir + "/foo";
 
   std::vector<std::vector<uint8_t>> encoded_buffers;
   {
diff --git a/aos/events/logging/buffer_encoder_test.cc b/aos/events/logging/buffer_encoder_test.cc
index 7dda300..127fb4f 100644
--- a/aos/events/logging/buffer_encoder_test.cc
+++ b/aos/events/logging/buffer_encoder_test.cc
@@ -9,6 +9,7 @@
 #include "gtest/gtest.h"
 
 #include "aos/events/logging/buffer_encoder_param_test.h"
+#include "aos/testing/tmpdir.h"
 
 namespace aos::logger::testing {
 
@@ -44,8 +45,7 @@
 TEST(DummyDecoderTest, ReadsIntoExactBuffer) {
   static const std::string kTestString{"Just some random words."};
 
-  const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
-  const std::string file_path = std::string(test_dir) + "/foo";
+  const std::string file_path = aos::testing::TestTmpDir() + "/foo";
   std::ofstream(file_path, std::ios::binary) << kTestString;
 
   // Read the contents of the file into the buffer.
@@ -65,8 +65,7 @@
 TEST(DummyDecoderTest, ReadsIntoLargerBuffer) {
   static const std::string kTestString{"Just some random words."};
 
-  const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
-  const std::string file_path = std::string(test_dir) + "/foo";
+  const std::string file_path = aos::testing::TestTmpDir() + "/foo";
   std::ofstream(file_path, std::ios::binary) << kTestString;
 
   DummyDecoder dummy_decoder(file_path.c_str());
@@ -84,8 +83,7 @@
 TEST(DummyDecoderTest, ReadsRepeatedlyIntoSmallerBuffer) {
   static const std::string kTestString{"Just some random words."};
 
-  const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
-  const std::string file_path = std::string(test_dir) + "/foo";
+  const std::string file_path = aos::testing::TestTmpDir() + "/foo";
   std::ofstream(file_path, std::ios::binary) << kTestString;
 
   DummyDecoder dummy_decoder(file_path.c_str());
diff --git a/aos/events/logging/config_remapper.cc b/aos/events/logging/config_remapper.cc
index 21f45d5..088d393 100644
--- a/aos/events/logging/config_remapper.cc
+++ b/aos/events/logging/config_remapper.cc
@@ -263,7 +263,8 @@
   result.reserve(remapped_channels_.size());
   for (auto &pair : remapped_channels_) {
     const Channel *const original_channel =
-        CHECK_NOTNULL(original_configuration()->channels()->Get(pair.first));
+        original_configuration()->channels()->Get(pair.first);
+    CHECK(original_channel != nullptr);
 
     auto channel_iterator = std::lower_bound(
         remapped_configuration_->channels()->cbegin(),
@@ -472,9 +473,10 @@
 
   // Reconstruct the remapped channels.
   for (auto &pair : remapped_channels_) {
-    const Channel *const c = CHECK_NOTNULL(configuration::GetChannel(
+    const Channel *const c = configuration::GetChannel(
         base_config, original_configuration()->channels()->Get(pair.first), "",
-        nullptr));
+        nullptr);
+    CHECK(c != nullptr);
     channel_offsets.emplace_back(
         CopyChannel(c, pair.second.remapped_name, "", &fbb));
 
diff --git a/aos/events/logging/log_cat.cc b/aos/events/logging/log_cat.cc
index 31d12be..d7347be 100644
--- a/aos/events/logging/log_cat.cc
+++ b/aos/events/logging/log_cat.cc
@@ -245,7 +245,7 @@
         }
         VLOG(1) << "Listening on " << name << " " << type;
 
-        CHECK_NOTNULL(channel->schema());
+        CHECK(channel->schema() != nullptr);
         event_loop_->MakeRawWatcher(channel, [this, channel, start_time,
                                               end_time](
                                                  const aos::Context &context,
diff --git a/aos/events/logging/log_namer.cc b/aos/events/logging/log_namer.cc
index 6c88d89..c065f68 100644
--- a/aos/events/logging/log_namer.cc
+++ b/aos/events/logging/log_namer.cc
@@ -1065,7 +1065,7 @@
 
 void MultiNodeLogNamer::CloseWriter(
     std::unique_ptr<DetachedBufferWriter> *writer_pointer) {
-  CHECK_NOTNULL(writer_pointer);
+  CHECK(writer_pointer != nullptr);
   if (!(*writer_pointer)) {
     return;
   }
diff --git a/aos/events/logging/log_namer.h b/aos/events/logging/log_namer.h
index 08601f2..2c0982a 100644
--- a/aos/events/logging/log_namer.h
+++ b/aos/events/logging/log_namer.h
@@ -422,7 +422,7 @@
   bool ran_out_of_space() const {
     return accumulate_data_writers<bool>(
         ran_out_of_space_, [](bool x, const NewDataWriter &data_writer) {
-          CHECK_NOTNULL(data_writer.writer);
+          CHECK(data_writer.writer != nullptr);
           return x ||
                  (data_writer.writer && data_writer.writer->ran_out_of_space());
         });
@@ -435,7 +435,7 @@
   size_t maximum_total_bytes() const {
     return accumulate_data_writers<size_t>(
         0, [](size_t x, const NewDataWriter &data_writer) {
-          CHECK_NOTNULL(data_writer.writer);
+          CHECK(data_writer.writer != nullptr);
           return std::max(x, data_writer.writer->total_bytes());
         });
   }
@@ -452,7 +452,7 @@
     return accumulate_data_writers(
         max_write_time_,
         [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
-          CHECK_NOTNULL(data_writer.writer);
+          CHECK(data_writer.writer != nullptr);
           return std::max(
               x, data_writer.writer->WriteStatistics()->max_write_time());
         });
@@ -462,7 +462,7 @@
         std::make_tuple(max_write_time_bytes_, max_write_time_),
         [](std::tuple<int, std::chrono::nanoseconds> x,
            const NewDataWriter &data_writer) {
-          CHECK_NOTNULL(data_writer.writer);
+          CHECK(data_writer.writer != nullptr);
           if (data_writer.writer->WriteStatistics()->max_write_time() >
               std::get<1>(x)) {
             return std::make_tuple(
@@ -477,7 +477,7 @@
         std::make_tuple(max_write_time_messages_, max_write_time_),
         [](std::tuple<int, std::chrono::nanoseconds> x,
            const NewDataWriter &data_writer) {
-          CHECK_NOTNULL(data_writer.writer);
+          CHECK(data_writer.writer != nullptr);
           if (data_writer.writer->WriteStatistics()->max_write_time() >
               std::get<1>(x)) {
             return std::make_tuple(
@@ -492,14 +492,14 @@
     return accumulate_data_writers(
         total_write_time_,
         [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
-          CHECK_NOTNULL(data_writer.writer);
+          CHECK(data_writer.writer != nullptr);
           return x + data_writer.writer->WriteStatistics()->total_write_time();
         });
   }
   int total_write_count() const {
     return accumulate_data_writers(
         total_write_count_, [](int x, const NewDataWriter &data_writer) {
-          CHECK_NOTNULL(data_writer.writer);
+          CHECK(data_writer.writer != nullptr);
           return x + data_writer.writer->WriteStatistics()->total_write_count();
         });
   }
@@ -513,7 +513,7 @@
   int total_write_bytes() const {
     return accumulate_data_writers(
         total_write_bytes_, [](int x, const NewDataWriter &data_writer) {
-          CHECK_NOTNULL(data_writer.writer);
+          CHECK(data_writer.writer != nullptr);
           return x + data_writer.writer->WriteStatistics()->total_write_bytes();
         });
   }
@@ -522,7 +522,7 @@
     return accumulate_data_writers(
         total_encode_duration_,
         [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
-          CHECK_NOTNULL(data_writer.writer);
+          CHECK(data_writer.writer != nullptr);
           return x +
                  data_writer.writer->WriteStatistics()->total_encode_duration();
         });
diff --git a/aos/events/logging/log_reader.cc b/aos/events/logging/log_reader.cc
index 192dade..81f8466 100644
--- a/aos/events/logging/log_reader.cc
+++ b/aos/events/logging/log_reader.cc
@@ -196,7 +196,7 @@
   {
     // Log files container validates that log files shared the same config.
     const Configuration *config = log_files_.config().get();
-    CHECK_NOTNULL(config);
+    CHECK(config != nullptr);
   }
 
   if (replay_channels_ != nullptr) {
@@ -386,7 +386,7 @@
 std::vector<
     std::pair<const aos::Channel *, NodeEventLoopFactory::ExclusiveSenders>>
 LogReader::State::NonExclusiveChannels() {
-  CHECK_NOTNULL(node_event_loop_factory_);
+  CHECK(node_event_loop_factory_ != nullptr);
   const aos::Configuration *config = node_event_loop_factory_->configuration();
   std::vector<
       std::pair<const aos::Channel *, NodeEventLoopFactory::ExclusiveSenders>>
@@ -1423,10 +1423,11 @@
 
   if (remote_timestamp_senders_[timestamped_message.channel_index] != nullptr) {
     State *source_state =
-        CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]);
-    std::vector<ContiguousSentTimestamp> *queue_index_map = CHECK_NOTNULL(
-        source_state->queue_index_map_[timestamped_message.channel_index]
-            .get());
+        channel_source_state_[timestamped_message.channel_index];
+    CHECK(source_state != nullptr);
+    std::vector<ContiguousSentTimestamp> *queue_index_map =
+        source_state->queue_index_map_[timestamped_message.channel_index].get();
+    CHECK(queue_index_map != nullptr);
 
     struct SentTimestamp {
       monotonic_clock::time_point monotonic_event_time;
@@ -1495,15 +1496,13 @@
     // Sanity check that we are using consistent boot uuids.
     State *source_state =
         channel_source_state_[timestamped_message.channel_index];
+    CHECK(source_state != nullptr);
+    CHECK(source_state->event_loop_ != nullptr);
     CHECK_EQ(multinode_filters_->boot_uuid(
                  configuration::GetNodeIndex(event_loop_->configuration(),
                                              source_state->node()),
                  timestamped_message.monotonic_remote_time.boot),
-             CHECK_NOTNULL(
-                 CHECK_NOTNULL(
-                     channel_source_state_[timestamped_message.channel_index])
-                     ->event_loop_)
-                 ->boot_uuid());
+             source_state->event_loop_->boot_uuid());
   }
 
   SharedSpan to_send;
@@ -1529,20 +1528,23 @@
 
   // Send!  Use the replayed queue index here instead of the logged queue index
   // for the remote queue index.  This makes re-logging work.
+  const UUID boot_uuid = [&]() -> UUID {
+    if (channel_source_state_[timestamped_message.channel_index] != nullptr) {
+      CHECK(multinode_filters_ != nullptr);
+      return multinode_filters_->boot_uuid(
+          configuration::GetNodeIndex(
+              event_loop_->configuration(),
+              channel_source_state_[timestamped_message.channel_index]->node()),
+          timestamped_message.monotonic_remote_time.boot);
+    } else {
+      return event_loop_->boot_uuid();
+    }
+  }();
   const RawSender::Error err = sender->Send(
       std::move(to_send), timestamped_message.monotonic_remote_time.time,
       timestamped_message.realtime_remote_time,
       timestamped_message.monotonic_remote_transmit_time.time,
-      remote_queue_index,
-      (channel_source_state_[timestamped_message.channel_index] != nullptr
-           ? CHECK_NOTNULL(multinode_filters_)
-                 ->boot_uuid(configuration::GetNodeIndex(
-                                 event_loop_->configuration(),
-                                 channel_source_state_[timestamped_message
-                                                           .channel_index]
-                                     ->node()),
-                             timestamped_message.monotonic_remote_time.boot)
-           : event_loop_->boot_uuid()));
+      remote_queue_index, boot_uuid);
   if (err != RawSender::Error::kOk) return false;
   if (monotonic_start_time(timestamped_message.monotonic_event_time.boot) <=
       timestamped_message.monotonic_event_time.time) {
@@ -1601,7 +1603,8 @@
     // that the timestamps correspond to. This code, as written, also doesn't
     // correctly handle a non-zero clock_offset for the *_remote_time fields.
     State *source_state =
-        CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]);
+        channel_source_state_[timestamped_message.channel_index];
+    CHECK(source_state != nullptr);
 
     flatbuffers::FlatBufferBuilder fbb;
     fbb.ForceDefaults(true);
@@ -1655,14 +1658,14 @@
 
 void LogReader::RemoteMessageSender::ScheduleTimestamp() {
   if (remote_timestamps_.empty()) {
-    CHECK_NOTNULL(timer_);
+    CHECK(timer_ != nullptr);
     timer_->Disable();
     scheduled_time_ = monotonic_clock::min_time;
     return;
   }
 
   if (scheduled_time_ != remote_timestamps_.front().monotonic_timestamp_time) {
-    CHECK_NOTNULL(timer_);
+    CHECK(timer_ != nullptr);
     timer_->Schedule(remote_timestamps_.front().monotonic_timestamp_time);
     scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time;
     CHECK_GE(scheduled_time_, event_loop_->monotonic_now())
diff --git a/aos/events/logging/log_reader.h b/aos/events/logging/log_reader.h
index f25f1f6..85d4d2c 100644
--- a/aos/events/logging/log_reader.h
+++ b/aos/events/logging/log_reader.h
@@ -655,7 +655,7 @@
     }
 
     monotonic_clock::time_point monotonic_now() const {
-      CHECK_NOTNULL(event_loop_);
+      CHECK(event_loop_ != nullptr);
       return event_loop_->monotonic_now();
     }
 
diff --git a/aos/events/logging/log_stats.cc b/aos/events/logging/log_stats.cc
index c0540bf..a45a167 100644
--- a/aos/events/logging/log_stats.cc
+++ b/aos/events/logging/log_stats.cc
@@ -128,7 +128,10 @@
       : channel_(channel),
         config_(factory->configuration()),
         factory_(factory),
-        schema_(CHECK_NOTNULL(schema)),
+        schema_([&]() {
+          CHECK(schema != nullptr);
+          return schema;
+        }()),
         destination_node_(destination_node),
         flatbuffer_type_(schema) {
     // Multi-node channel
diff --git a/aos/events/logging/log_writer.cc b/aos/events/logging/log_writer.cc
index c00d216..42dbbb5 100644
--- a/aos/events/logging/log_writer.cc
+++ b/aos/events/logging/log_writer.cc
@@ -297,8 +297,9 @@
           log_namer_->MakeTimestampWriter(f.event_loop_channel);
     }
     if (f.wants_contents_writer) {
+      CHECK(f.timestamp_node != nullptr);
       f.contents_writer = log_namer_->MakeForwardedTimestampWriter(
-          f.event_loop_channel, CHECK_NOTNULL(f.timestamp_node));
+          f.event_loop_channel, f.timestamp_node);
     }
   }
 
@@ -426,8 +427,9 @@
           log_namer_->MakeTimestampWriter(f.event_loop_channel);
     }
     if (f.wants_contents_writer) {
+      CHECK(f.timestamp_node != nullptr);
       f.contents_writer = log_namer_->MakeForwardedTimestampWriter(
-          f.event_loop_channel, CHECK_NOTNULL(f.timestamp_node));
+          f.event_loop_channel, f.timestamp_node);
     }
 
     // Mark each channel with data as not written.  That triggers each channel
diff --git a/aos/events/logging/logfile_utils.cc b/aos/events/logging/logfile_utils.cc
index 30ed3d7..0d10a99 100644
--- a/aos/events/logging/logfile_utils.cc
+++ b/aos/events/logging/logfile_utils.cc
@@ -2442,7 +2442,7 @@
 
 Message TimestampMapper::MatchingMessageFor(const Message &message) {
   // Figure out what queue index we are looking for.
-  CHECK_NOTNULL(message.header);
+  CHECK(message.header != nullptr);
   CHECK(message.header->remote_queue_index.has_value());
   const BootQueueIndex remote_queue_index =
       BootQueueIndex{.boot = message.monotonic_remote_boot,
diff --git a/aos/events/logging/lzma_encoder_test.cc b/aos/events/logging/lzma_encoder_test.cc
index 60444d3..92b4f54 100644
--- a/aos/events/logging/lzma_encoder_test.cc
+++ b/aos/events/logging/lzma_encoder_test.cc
@@ -4,6 +4,7 @@
 #include "gtest/gtest.h"
 
 #include "aos/events/logging/buffer_encoder_param_test.h"
+#include "aos/testing/tmpdir.h"
 #include "aos/util/file.h"
 
 DECLARE_int32(lzma_threads);
@@ -62,8 +63,7 @@
 // corrupted.
 TEST_F(BufferEncoderBaseTest, CorruptedBuffer) {
   std::uniform_int_distribution<int> quantity_distribution(20, 60);
-  const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
-  const std::string file_path = std::string(test_dir) + "/foo";
+  const std::string file_path = aos::testing::TestTmpDir() + "/foo";
 
   std::vector<std::vector<uint8_t>> encoded_buffers;
   {
diff --git a/aos/events/logging/snappy_encoder.cc b/aos/events/logging/snappy_encoder.cc
index 0160177..b1cd80e 100644
--- a/aos/events/logging/snappy_encoder.cc
+++ b/aos/events/logging/snappy_encoder.cc
@@ -127,7 +127,8 @@
 }
 
 const char *SnappyEncoder::DetachedBufferSource::Peek(size_t *length) {
-  *CHECK_NOTNULL(length) = data_.size() - index_into_first_buffer_;
+  CHECK(length != nullptr);
+  *length = data_.size() - index_into_first_buffer_;
   return reinterpret_cast<char *>(data_.data()) + index_into_first_buffer_;
 }