Convert things from AOS logging to glog

This is necessary so ceres (which includes glog) can be included in more
places.

Change-Id: Ie0a54dc6a9e21e0c2f11899d08c87d08c5c0e028
diff --git a/y2019/jevois/BUILD b/y2019/jevois/BUILD
index afca4ef..599bcba 100644
--- a/y2019/jevois/BUILD
+++ b/y2019/jevois/BUILD
@@ -189,7 +189,6 @@
         "cobs.h",
     ],
     deps = [
-        "//aos/logging",
         "//third_party/GSL",
     ],
 )
diff --git a/y2019/jevois/camera/BUILD b/y2019/jevois/camera/BUILD
index e6e33c5..bf716a0 100644
--- a/y2019/jevois/camera/BUILD
+++ b/y2019/jevois/camera/BUILD
@@ -5,19 +5,21 @@
     srcs = ["reader.cc"],
     hdrs = ["reader.h"],
     deps = [
-        "//aos/vision/image:reader",
+        "//aos/time",
         "//aos/vision/image:camera_params",
         "//aos/vision/image:image_types",
-        "//aos/logging",
-        "//aos/time",
+        "//aos/vision/image:reader",
+        "@com_github_google_glog//:glog",
     ],
 )
 
 cc_library(
     name = "image_stream",
+    srcs = ["image_stream.cc"],
     hdrs = ["image_stream.h"],
     deps = [
-        "//aos/vision/events:epoll_events",
         ":reader",
+        "//aos/logging",
+        "//aos/vision/events:epoll_events",
     ],
 )
diff --git a/y2019/jevois/camera/image_stream.cc b/y2019/jevois/camera/image_stream.cc
new file mode 100644
index 0000000..29a1513
--- /dev/null
+++ b/y2019/jevois/camera/image_stream.cc
@@ -0,0 +1,18 @@
+#include "y2019/jevois/camera/image_stream.h"
+
+#include "aos/logging/logging.h"
+
+namespace y2019 {
+namespace camera {
+
+void ImageStreamEvent::ProcessHelper(
+    aos::vision::DataRef data, aos::monotonic_clock::time_point timestamp) {
+  if (data.size() < 300) {
+    LOG(INFO, "got bad img of size(%d)\n", static_cast<int>(data.size()));
+    return;
+  }
+  ProcessImage(data, timestamp);
+}
+
+}  // namespace camera
+}  // namespace y2019
diff --git a/y2019/jevois/camera/image_stream.h b/y2019/jevois/camera/image_stream.h
index 62661c6..578e24f 100644
--- a/y2019/jevois/camera/image_stream.h
+++ b/y2019/jevois/camera/image_stream.h
@@ -1,5 +1,5 @@
-#ifndef _AOS_VISION_IMAGE_IMAGE_STREAM_H_
-#define _AOS_VISION_IMAGE_IMAGE_STREAM_H_
+#ifndef Y2019_JEVOIS_CAMERA_IMAGE_STREAM_H_
+#define Y2019_JEVOIS_CAMERA_IMAGE_STREAM_H_
 
 #include "aos/vision/events/epoll_events.h"
 #include "aos/vision/image/camera_params.pb.h"
@@ -33,13 +33,7 @@
       : ImageStreamEvent(GetCamera(fname, this, params)) {}
 
   void ProcessHelper(aos::vision::DataRef data,
-                     aos::monotonic_clock::time_point timestamp) {
-    if (data.size() < 300) {
-      LOG(INFO, "got bad img of size(%d)\n", static_cast<int>(data.size()));
-      return;
-    }
-    ProcessImage(data, timestamp);
-  }
+                     aos::monotonic_clock::time_point timestamp);
   virtual void ProcessImage(aos::vision::DataRef data,
                             aos::monotonic_clock::time_point timestamp) = 0;
 
@@ -49,7 +43,7 @@
   std::unique_ptr<Reader> reader_;
 };
 
-}  // namespace vision
-}  // namespace aos
+}  // namespace camera
+}  // namespace y2019
 
-#endif  // _AOS_VISION_DEBUG_IMAGE_STREAM_H_
+#endif  // Y2019_JEVOIS_CAMERA_IMAGE_STREAM_H_
diff --git a/y2019/jevois/camera/reader.cc b/y2019/jevois/camera/reader.cc
index 25b5a3f..14faa24 100644
--- a/y2019/jevois/camera/reader.cc
+++ b/y2019/jevois/camera/reader.cc
@@ -12,8 +12,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "aos/logging/logging.h"
 #include "aos/time/time.h"
+#include "glog/logging.h"
 
 #define CLEAR(x) memset(&(x), 0, sizeof(x))
 
@@ -45,29 +45,27 @@
     : dev_name_(dev_name), process_(std::move(process)), params_(params) {
   struct stat st;
   if (stat(dev_name.c_str(), &st) == -1) {
-    PLOG(FATAL, "Cannot identify '%s'", dev_name.c_str());
+    PLOG(FATAL) << "Cannot identify '" << dev_name << "'";
   }
   if (!S_ISCHR(st.st_mode)) {
-    PLOG(FATAL, "%s is no device\n", dev_name.c_str());
+    PLOG(FATAL) << dev_name << " is no device";
   }
 
   fd_ = open(dev_name.c_str(), O_RDWR /* required */ | O_NONBLOCK, 0);
   if (fd_ == -1) {
-    PLOG(FATAL, "Cannot open '%s'", dev_name.c_str());
+    PLOG(FATAL) << "Cannot open '" << dev_name << "'";
   }
 
   Init();
 
   InitMMap();
-  LOG(INFO, "Bat Vision Successfully Initialized.\n");
+  LOG(INFO) << "Bat Vision Successfully Initialized.";
 }
 
 void Reader::QueueBuffer(v4l2_buffer *buf) {
   if (xioctl(fd_, VIDIOC_QBUF, buf) == -1) {
-    PLOG(WARNING,
-         "ioctl VIDIOC_QBUF(%d, %p)."
-         " losing buf #%" PRIu32 "\n",
-         fd_, &buf, buf->index);
+    PLOG(WARNING) << "ioctl VIDIOC_QBUF(" << fd_ << ", " << &buf
+                  << "). losing buf #" << buf->index;
   } else {
     ++queued_;
   }
@@ -81,7 +79,7 @@
 
   if (xioctl(fd_, VIDIOC_DQBUF, &buf) == -1) {
     if (errno != EAGAIN) {
-      PLOG(ERROR, "ioctl VIDIOC_DQBUF(%d, %p)", fd_, &buf);
+      PLOG(ERROR) << "ioctl VIDIOC_DQBUF(" << fd_ << ", " << &buf << ")";
     }
     return;
   }
@@ -110,15 +108,15 @@
     buf.memory = V4L2_MEMORY_MMAP;
     buf.index = n;
     if (xioctl(fd_, VIDIOC_QUERYBUF, &buf) == -1) {
-      PLOG(FATAL, "ioctl VIDIOC_QUERYBUF(%d, %p)", fd_, &buf);
+      PLOG(FATAL) << "ioctl VIDIOC_QUERYBUF(" << fd_ << ", " << &buf << ")";
     }
     buffers_[n].length = buf.length;
     buffers_[n].start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE,
                              MAP_SHARED, fd_, buf.m.offset);
     if (buffers_[n].start == MAP_FAILED) {
-      PLOG(FATAL,
-           "mmap(NULL, %zd, PROT_READ | PROT_WRITE, MAP_SHARED, %d, %jd)",
-           (size_t)buf.length, fd_, static_cast<intmax_t>(buf.m.offset));
+      PLOG(FATAL) << "mmap(NULL, " << buf.length
+                  << ", PROT_READ | PROT_WRITE, MAP_SHARED, " << fd_ << ", "
+                  << buf.m.offset << ")";
     }
   }
 }
@@ -131,14 +129,14 @@
   req.memory = V4L2_MEMORY_MMAP;
   if (xioctl(fd_, VIDIOC_REQBUFS, &req) == -1) {
     if (EINVAL == errno) {
-      LOG(FATAL, "%s does not support memory mapping\n", dev_name_.c_str());
+      LOG(FATAL) << dev_name_ << " does not support memory mapping\n";
     } else {
-      PLOG(FATAL, "ioctl VIDIOC_REQBUFS(%d, %p)\n", fd_, &req);
+      LOG(FATAL) << "ioctl VIDIOC_REQBUFS(" << fd_ << ", " << &req << ")";
     }
   }
   queued_ = kNumBuffers;
   if (req.count != kNumBuffers) {
-    LOG(FATAL, "Insufficient buffer memory on %s\n", dev_name_.c_str());
+    LOG(FATAL) << "Insufficient buffer memory on " << dev_name_;
   }
 }
 
@@ -153,11 +151,11 @@
   r = xioctl(fd_, 0xc00c561b, &getArg);
   if (r == 0) {
     if (getArg.value == value) {
-      LOG(DEBUG, "Camera control %s was already %d\n", name, getArg.value);
+      VLOG(1) << "Camera control " << name << " was already " << getArg.value;
       return true;
     }
   } else if (errno == EINVAL) {
-    LOG(DEBUG, "Camera control %s is invalid\n", name);
+    VLOG(1) << "Camera control " << name << " is invalid";
     errno = 0;
     return false;
   }
@@ -167,12 +165,12 @@
   // Jevois wants this incorrect number below:.
   r = xioctl(fd_, 0xc00c561c, &setArg);
   if (r == 0) {
-    LOG(DEBUG, "Set camera control %s from %d to %d\n", name, getArg.value,
-        value);
+    VLOG(1) << "Set camera control " << name << " from " << getArg.value
+            << " to " << value;
     return true;
   }
 
-  LOG(DEBUG, "Couldn't set camera control %s to %d", name, value);
+  VLOG(1) << "Couldn't set camera control " << name << " to " << value;
   errno = 0;
   return false;
 }
@@ -181,16 +179,16 @@
   v4l2_capability cap;
   if (xioctl(fd_, VIDIOC_QUERYCAP, &cap) == -1) {
     if (EINVAL == errno) {
-      LOG(FATAL, "%s is no V4L2 device\n", dev_name_.c_str());
+      LOG(FATAL) << dev_name_ << " is no V4L2 device";
     } else {
-      PLOG(FATAL, "ioctl VIDIOC_QUERYCAP(%d, %p)", fd_, &cap);
+      PLOG(FATAL) << "ioctl VIDIOC_QUERYCAP(" << fd_ << ", " << &cap << ")";
     }
   }
   if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
-    LOG(FATAL, "%s is no video capture device\n", dev_name_.c_str());
+    LOG(FATAL) << dev_name_ << " is no video capture device";
   }
   if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
-    LOG(FATAL, "%s does not support streaming i/o\n", dev_name_.c_str());
+    LOG(FATAL) << dev_name_ << " does not support streaming i/o";
   }
 
   int camidx = -1;
@@ -208,8 +206,7 @@
   }
 
   if (xioctl(fd_, VIDIOC_S_INPUT, &camidx) == -1) {
-    LOG(FATAL, "ioctl VIDIOC_S_INPUT(%d) failed with %d: %s\n", fd_, errno,
-        strerror(errno));
+    PLOG(FATAL) << "ioctl VIDIOC_S_INPUT(" << fd_ << ") failed";
   }
   printf("camera idx: %d\n", camidx);
 
@@ -220,8 +217,7 @@
 
   fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
   if (xioctl(fd_, VIDIOC_G_FMT, &fmt) == -1) {
-    LOG(FATAL, "ioctl VIDIC_G_FMT(%d, %p) failed with %d: %s\n", fd_, &fmt,
-        errno, strerror(errno));
+    PLOG(FATAL) << "ioctl VIDIC_G_FMT(" << fd_ << ", " << &fmt << ") failed";
   }
 
   fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
@@ -231,8 +227,7 @@
   fmt.fmt.pix.field = V4L2_FIELD_NONE;
   fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
   if (xioctl(fd_, VIDIOC_S_FMT, &fmt) == -1) {
-    LOG(FATAL, "ioctl VIDIC_S_FMT(%d, %p) failed with %d: %s\n", fd_, &fmt,
-        errno, strerror(errno));
+    PLOG(FATAL) << "ioctl VIDIC_S_FMT(" << fd_ << ", " << &fmt << ") failed";
   }
   /* Note VIDIOC_S_FMT may change width and height. */
 
@@ -250,21 +245,21 @@
   setfps->parm.capture.timeperframe.numerator = 1;
   setfps->parm.capture.timeperframe.denominator = params_.fps();
   if (xioctl(fd_, VIDIOC_S_PARM, setfps) == -1) {
-    PLOG(FATAL, "ioctl VIDIOC_S_PARM(%d, %p)\n", fd_, setfps);
+    PLOG(FATAL) << "ioctl VIDIOC_S_PARM(" << fd_ << ", " << setfps << ")";
   }
-  LOG(INFO, "framerate ended up at %d/%d\n",
-      setfps->parm.capture.timeperframe.numerator,
-      setfps->parm.capture.timeperframe.denominator);
+  LOG(INFO) << "framerate ended up at "
+            << setfps->parm.capture.timeperframe.numerator << "/"
+            << setfps->parm.capture.timeperframe.denominator;
 
   for (int j = 0; j < 2; ++j) {
     if (!SetCameraControl(V4L2_CID_EXPOSURE_AUTO, "V4L2_CID_EXPOSURE_AUTO",
                           V4L2_EXPOSURE_MANUAL)) {
-      LOG(FATAL, "Failed to set exposure\n");
+      LOG(FATAL) << "Failed to set exposure";
     }
 
     if (!SetCameraControl(V4L2_CID_EXPOSURE_ABSOLUTE,
                           "V4L2_CID_EXPOSURE_ABSOLUTE", params_.exposure())) {
-      LOG(FATAL, "Failed to set exposure\n");
+      LOG(FATAL) << "Failed to set exposure";
     }
     sleep(1);
   }
@@ -274,7 +269,7 @@
   struct v4l2_format fmt;
   fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
   if (xioctl(fd_, VIDIOC_G_FMT, &fmt) == -1) {
-    PLOG(FATAL, "ioctl VIDIC_G_FMT(%d, %p)\n", fd_, &fmt);
+    PLOG(FATAL) << "ioctl VIDIC_G_FMT(" << fd_ << ", " << &fmt << ")";
   }
 
   return aos::vision::ImageFormat{(int)fmt.fmt.pix.width,
@@ -282,7 +277,7 @@
 }
 
 void Reader::Start() {
-  LOG(DEBUG, "queueing buffers for the first time\n");
+  VLOG(1) << "queueing buffers for the first time";
   v4l2_buffer buf;
   for (unsigned int i = 0; i < kNumBuffers; ++i) {
     CLEAR(buf);
@@ -291,11 +286,11 @@
     buf.index = i;
     QueueBuffer(&buf);
   }
-  LOG(DEBUG, "done with first queue\n");
+  VLOG(1) << "done with first queue";
 
   v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
   if (xioctl(fd_, VIDIOC_STREAMON, &type) == -1) {
-    PLOG(FATAL, "ioctl VIDIOC_STREAMON(%d, %p)\n", fd_, &type);
+    PLOG(FATAL) << "ioctl VIDIOC_STREAMON(" << fd_ << ", " << &type << ")";
   }
 }
 
diff --git a/y2019/jevois/cobs.h b/y2019/jevois/cobs.h
index 112873d..0e92ed6 100644
--- a/y2019/jevois/cobs.h
+++ b/y2019/jevois/cobs.h
@@ -7,12 +7,6 @@
 #include <array>
 
 #include "third_party/GSL/include/gsl/gsl"
-#ifdef __linux__
-#include "aos/logging/logging.h"
-#else
-#define CHECK(...)
-#define CHECK_LE(...)
-#endif
 
 // This file contains code for encoding and decoding Consistent Overhead Byte
 // Stuffing data. <http://www.stuartcheshire.org/papers/cobsforton.pdf> has
@@ -101,14 +95,18 @@
     gsl::span<const char> input,
     std::array<char, CobsMaxEncodedSize(max_decoded_size)> *output_buffer) {
   static_assert(max_decoded_size > 0, "Empty buffers not supported");
-  CHECK_LE(static_cast<size_t>(input.size()), max_decoded_size);
+  if (static_cast<size_t>(input.size()) > max_decoded_size) {
+    __builtin_trap();
+  }
   auto input_pointer = input.begin();
   auto output_pointer = output_buffer->begin();
   auto code_pointer = output_pointer;
   ++output_pointer;
   uint8_t code = 1;
   while (input_pointer < input.end()) {
-    CHECK(output_pointer < output_buffer->end());
+    if (output_pointer >= output_buffer->end()) {
+      __builtin_trap();
+    }
     if (*input_pointer == 0u) {
       *code_pointer = code;
       code_pointer = output_pointer;
@@ -128,7 +126,9 @@
     ++input_pointer;
   }
   *code_pointer = code;
-  CHECK(output_pointer <= output_buffer->end());
+  if (output_pointer > output_buffer->end()) {
+    __builtin_trap();
+  }
   return gsl::span<char>(*output_buffer)
       .subspan(0, output_pointer - output_buffer->begin());
 }
@@ -137,8 +137,10 @@
 gsl::span<char> CobsDecode(gsl::span<const char> input,
                            std::array<char, max_decoded_size> *output_buffer) {
   static_assert(max_decoded_size > 0, "Empty buffers not supported");
-  CHECK_LE(static_cast<size_t>(input.size()),
-           CobsMaxEncodedSize(max_decoded_size));
+  if (static_cast<size_t>(input.size()) >
+      CobsMaxEncodedSize(max_decoded_size)) {
+    __builtin_trap();
+  }
   auto input_pointer = input.begin();
   auto output_pointer = output_buffer->begin();
   while (input_pointer < input.end()) {
diff --git a/y2019/vision/BUILD b/y2019/vision/BUILD
index 344e4da..bf71116 100644
--- a/y2019/vision/BUILD
+++ b/y2019/vision/BUILD
@@ -93,8 +93,6 @@
     restricted_to = VISION_TARGETS,
     deps = [
         ":target_finder",
-        "//aos/logging",
-        "//aos/logging:implementations",
         "//aos/vision/blob:codec",
         "//aos/vision/blob:find_blob",
         "//aos/vision/events:epoll_events",
@@ -105,6 +103,7 @@
         "//y2019/jevois:uart",
         "//y2019/jevois/camera:image_stream",
         "//y2019/jevois/camera:reader",
+        "@com_github_google_glog//:glog",
         "@com_google_ceres_solver//:ceres",
     ],
 )
diff --git a/y2019/vision/target_sender.cc b/y2019/vision/target_sender.cc
index 52b81ad..03c236e 100644
--- a/y2019/vision/target_sender.cc
+++ b/y2019/vision/target_sender.cc
@@ -1,7 +1,7 @@
+#include "y2019/vision/target_finder.h"
+
 #include <fstream>
 
-#include "aos/logging/implementations.h"
-#include "aos/logging/logging.h"
 #include "aos/vision/blob/codec.h"
 #include "aos/vision/blob/find_blob.h"
 #include "aos/vision/events/socket_types.h"
@@ -12,7 +12,10 @@
 #include "y2019/jevois/serial.h"
 #include "y2019/jevois/structures.h"
 #include "y2019/jevois/uart.h"
-#include "y2019/vision/target_finder.h"
+
+// This has to be last to preserve compatibility with other headers using AOS
+// logging.
+#include "glog/logging.h"
 
 using ::aos::events::DataSocket;
 using ::aos::events::RXUdpSocket;
@@ -29,7 +32,7 @@
       : ImageStreamEvent(fname, params) {}
 
   void ProcessImage(DataRef data, monotonic_clock::time_point monotonic_now) {
-    LOG(INFO, "got frame: %d\n", (int)data.size());
+    LOG(INFO) << "got frame: " << data.size();
 
     if (on_frame_) on_frame_(data, monotonic_now);
   }
@@ -70,9 +73,8 @@
   using namespace y2019::vision;
   using frc971::jevois::CameraCommand;
   // gflags::ParseCommandLineFlags(&argc, &argv, false);
-  ::aos::logging::Init();
-  ::aos::logging::AddImplementation(
-      new ::aos::logging::StreamLogImplementation(stderr));
+  FLAGS_logtostderr = true;
+  google::InitGoogleLogging(argv[0]);
 
   int itsDev = open_terminos("/dev/ttyS0");
   frc971::jevois::CobsPacketizer<frc971::jevois::uart_to_camera_size()> cobs;
@@ -97,7 +99,7 @@
     aos::vision::BlobList imgs = aos::vision::FindBlobs(
         aos::vision::FastYuyvYThreshold(fmt, data.data(), 120));
     finder_.PreFilter(&imgs);
-    LOG(INFO, "Blobs: (%zu).\n", imgs.size());
+    LOG(INFO) << "Blobs: " << imgs.size();
 
     constexpr bool verbose = false;
     ::std::vector<Polygon> raw_polys;
@@ -112,27 +114,27 @@
         raw_polys.push_back(polygon);
       }
     }
-    LOG(INFO, "Polygons: (%zu).\n", raw_polys.size());
+    LOG(INFO) << "Polygons: " << raw_polys.size();
 
     // Calculate each component side of a possible target.
     ::std::vector<TargetComponent> target_component_list =
         finder_.FillTargetComponentList(raw_polys, verbose);
-    LOG(INFO, "Components: (%zu).\n", target_component_list.size());
+    LOG(INFO) << "Components: " << target_component_list.size();
 
     // Put the compenents together into targets.
     ::std::vector<Target> target_list =
         finder_.FindTargetsFromComponents(target_component_list, verbose);
-    LOG(INFO, "Potential Target: (%zu).\n", target_list.size());
+    LOG(INFO) << "Potential Target: " << target_list.size();
 
     // Use the solver to generate an intermediate version of our results.
     ::std::vector<IntermediateResult> results;
     for (const Target &target : target_list) {
       results.emplace_back(finder_.ProcessTargetToResult(target, verbose));
     }
-    LOG(INFO, "Raw Results: (%zu).\n", results.size());
+    LOG(INFO) << "Raw Results: " << results.size();
 
     results = finder_.FilterResults(results, 30, verbose);
-    LOG(INFO, "Results: (%zu).\n", results.size());
+    LOG(INFO) << "Results: " << results.size();
 
     // TODO: Select top 3 (randomly?)
 
@@ -160,7 +162,7 @@
           write(itsDev, serialized_frame.data(), serialized_frame.size());
 
       if (n != (ssize_t)serialized_frame.size()) {
-        LOG(INFO, "Some problem happened");
+        LOG(INFO) << "Some problem happened";
       }
     }
   });
@@ -198,7 +200,7 @@
                 return system("touch /tmp/do_not_export_sd_card");
             }
           } else {
-            printf("bad frame\n");
+            fprintf(stderr, "bad frame\n");
           }
           cobs.clear_received_packet();
         }