Add option to compile with glog

Adding option to choose between glog and protobuf logger

Change-Id: I596fdde57b82f63f055dc3010a085a321808ea29
diff --git a/BUILD b/BUILD
index 1d3c2e1..2f7de98 100644
--- a/BUILD
+++ b/BUILD
@@ -4,12 +4,18 @@
 )
 
 cc_library(
+    name = "roughtime_logging",
+    hdrs = ["logging.h"],
+    deps = ["@protobuf//:protobuf"],
+)
+
+cc_library(
     name = "protocol",
     srcs = ["protocol.cc"],
     hdrs = ["protocol.h"],
     deps = [
+        ":roughtime_logging",
         "@boringssl//:crypto",
-        "@protobuf//:protobuf",
     ],
 )
 
@@ -27,7 +33,10 @@
     name = "client",
     srcs = ["client.cc"],
     hdrs = ["client.h"],
-    deps = [":protocol"],
+    deps = [
+        ":protocol",
+        ":roughtime_logging",
+    ],
 )
 
 cc_test(
@@ -54,6 +63,7 @@
     deps = [
         ":protocol",
         ":time_source",
+        ":roughtime_logging",
         "@boringssl//:crypto",
     ],
 )
@@ -100,6 +110,7 @@
 cc_library(
     name = "open_source_fillins",
     hdrs = ["open_source_fillins.h"],
+    deps = [":roughtime_logging"],
     defines = ["ROUGHTIME_OPEN_SOURCE"],
 )
 
@@ -124,6 +135,7 @@
         ":protocol",
         ":server",
         ":time_source",
+        ":roughtime_logging",
     ],
 )
 
@@ -133,6 +145,6 @@
     hdrs = ["sys_time.h"],
     deps = [
         ":time_source",
-        "@protobuf//:protobuf",
+        ":roughtime_logging",
     ],
 )
diff --git a/client.cc b/client.cc
index 8543e0d..db47b9c 100644
--- a/client.cc
+++ b/client.cc
@@ -16,10 +16,10 @@
 
 #include <stdint.h>
 
-#include <google/protobuf/stubs/logging.h>
 #include <openssl/curve25519.h>
 
 #include "client.h"
+#include "logging.h"
 
 namespace roughtime {
 
@@ -32,10 +32,10 @@
   uint8_t* padding;
 
   static_assert(kTagNONC < kTagPAD, "Tags must be written in order");
-  GOOGLE_CHECK(query.AddTagData(kTagNONC, nonce, kNonceLength) &&
+  ROUGHTIME_CHECK(query.AddTagData(kTagNONC, nonce, kNonceLength) &&
                query.AddTag(&padding, kTagPAD, kPaddingLen) &&
                query.Finish(&query_len));
-  GOOGLE_CHECK_EQ(query_len, sizeof(query_bytes));
+  ROUGHTIME_CHECK_EQ(query_len, sizeof(query_bytes));
 
   memset(padding, 0, kPaddingLen);
 
diff --git a/clock_linux.cc b/clock_linux.cc
index cdd17f8..cb86e8f 100644
--- a/clock_linux.cc
+++ b/clock_linux.cc
@@ -13,7 +13,7 @@
  * limitations under the License. */
 
 
-#if defined(__linux)
+#if defined(__linux) || defined (__fuchsia__)
 
 #include <stdint.h>
 #include <stdlib.h>
diff --git a/logging.h b/logging.h
new file mode 100644
index 0000000..1928ce5
--- /dev/null
+++ b/logging.h
@@ -0,0 +1,75 @@
+/* Copyright 2017 The Roughtime Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License. */
+
+
+#ifndef ROUGHTIME_LOGGER_H_
+#define ROUGHTIME_LOGGER_H_
+
+#if defined(USE_GLOG)
+#include <glog/logging.h>
+
+#define GLOG_logtostderr 1
+#define ROUGHTIME_LOG LOG
+#define ROUGHTIME_LOG_IF LOG_IF
+
+#define ROUGHTIME_CHECK CHECK
+
+#define ROUGHTIME_CHECK_EQ CHECK_EQ
+#define ROUGHTIME_CHECK_NE CHECK_NE
+#define ROUGHTIME_CHECK_LT CHECK_LT
+#define ROUGHTIME_CHECK_LE CHECK_LE
+#define ROUGHTIME_CHECK_GT CHECK_GT
+#define ROUGHTIME_CHECK_GE CHECK_GE
+
+#define ROUGHTIME_DLOG DLOG
+#define ROUGHTIME_DCHECK    DCHECK
+#define ROUGHTIME_DCHECK_OK DCHECK_OK
+#define ROUGHTIME_DCHECK_EQ DCHECK_EQ
+#define ROUGHTIME_DCHECK_NE DCHECK_NE
+#define ROUGHTIME_DCHECK_LT DCHECK_LT
+#define ROUGHTIME_DCHECK_LE DCHECK_LE
+#define ROUGHTIME_DCHECK_GT DCHECK_GT
+#define ROUGHTIME_DCHECK_GE DCHECK_GE
+
+#define ROUGHTIME_INIT_LOGGER  google::InitGoogleLogging
+
+#else
+#include <google/protobuf/stubs/logging.h>
+#include <google/protobuf/stubs/macros.h>
+
+#define ROUGHTIME_LOG GOOGLE_LOG
+#define ROUGHTIME_LOG_IF GOOGLE_LOG_IF
+
+#define ROUGHTIME_CHECK GOOGLE_CHECK
+
+#define ROUGHTIME_CHECK_EQ GOOGLE_CHECK_EQ
+#define ROUGHTIME_CHECK_NE GOOGLE_CHECK_NE
+#define ROUGHTIME_CHECK_LT GOOGLE_CHECK_LT
+#define ROUGHTIME_CHECK_LE GOOGLE_CHECK_LE
+#define ROUGHTIME_CHECK_GT GOOGLE_CHECK_GT
+#define ROUGHTIME_CHECK_GE GOOGLE_CHECK_GE
+
+#define ROUGHTIME_DLOG GOOGLE_DLOG
+#define ROUGHTIME_DCHECK    GOOGLE_DCHECK
+#define ROUGHTIME_DCHECK_OK GOOGLE_DCHECK_OK
+#define ROUGHTIME_DCHECK_EQ GOOGLE_DCHECK_EQ
+#define ROUGHTIME_DCHECK_NE GOOGLE_DCHECK_NE
+#define ROUGHTIME_DCHECK_LT GOOGLE_DCHECK_LT
+#define ROUGHTIME_DCHECK_LE GOOGLE_DCHECK_LE
+#define ROUGHTIME_DCHECK_GT GOOGLE_DCHECK_GT
+#define ROUGHTIME_DCHECK_GE GOOGLE_DCHECK_GE
+#define ROUGHTIME_INIT_LOGGER(a)
+#endif
+
+#endif
diff --git a/open_source_fillins.h b/open_source_fillins.h
index 5ca49c3..280fa26 100644
--- a/open_source_fillins.h
+++ b/open_source_fillins.h
@@ -20,12 +20,11 @@
 #include <errno.h>
 #include <memory>
 
-#include <google/protobuf/stubs/logging.h>
-#include <google/protobuf/stubs/macros.h>
+#include "logging.h"
 
-#define GOOGLE_PLOG(level) GOOGLE_LOG(level) << strerror(errno) << ": "
-#define GOOGLE_LOG_IF_EVERY_N_SEC(level, condition, time) \
-  GOOGLE_LOG_IF(level, condition)
+#define ROUGHTIME_PLOG(level) ROUGHTIME_LOG(level) << strerror(errno) << ": "
+#define ROUGHTIME_LOG_IF_EVERY_N_SEC(level, condition, time) \
+  ROUGHTIME_LOG_IF(level, condition)
 
 #if !defined(arraysize)
 template <typename T, size_t N>
diff --git a/protocol.cc b/protocol.cc
index 358755e..751f1cc 100644
--- a/protocol.cc
+++ b/protocol.cc
@@ -23,9 +23,10 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include <google/protobuf/stubs/logging.h>
 #include <openssl/sha.h>
 
+#include "logging.h"
+
 namespace roughtime {
 
 static_assert(BYTE_ORDER == LITTLE_ENDIAN,
diff --git a/protocol.h b/protocol.h
index 1ae044d..a64932f 100644
--- a/protocol.h
+++ b/protocol.h
@@ -18,7 +18,6 @@
 #include <stdint.h>
 #include <string.h>
 
-#include <google/protobuf/stubs/macros.h>
 #include <openssl/curve25519.h>
 
 namespace roughtime {
diff --git a/server.cc b/server.cc
index e10a956..bbbf01a 100644
--- a/server.cc
+++ b/server.cc
@@ -18,9 +18,9 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <google/protobuf/stubs/logging.h>
 #include <openssl/curve25519.h>
 
+#include "logging.h"
 #include "protocol.h"
 #include "server.h"
 
@@ -52,7 +52,7 @@
 }
 
 bool Server::AddRequest(const uint8_t *packet, size_t len) {
-  GOOGLE_DCHECK_LE(num_leaves_, kBatchSize);
+  ROUGHTIME_DCHECK_LE(num_leaves_, kBatchSize);
 
   if (len < kMinRequestSize) {
     return false;
@@ -76,7 +76,7 @@
 }
 
 bool Server::Sign() {
-  GOOGLE_DCHECK_GT(num_leaves_, 0);
+  ROUGHTIME_DCHECK_GT(num_leaves_, 0ul);
 
   // The signature is over the root hash and the timestamp---that's it!
   tree_.Build(num_leaves_);
@@ -96,15 +96,15 @@
           kTagMIDP, reinterpret_cast<const uint8_t *>(&now), sizeof(now)) ||
       !to_be_signed.AddTagData(kTagROOT, tree_.GetRoot(), kNonceLength) ||
       !to_be_signed.Finish(&to_be_signed_len)) {
-    GOOGLE_LOG(ERROR) << "failed to construct to_be_signed";
+    ROUGHTIME_LOG(ERROR) << "failed to construct to_be_signed";
     return false;
   }
-  GOOGLE_CHECK_EQ(to_be_signed_len, kToBeSignedSize);
+  ROUGHTIME_CHECK_EQ(to_be_signed_len, kToBeSignedSize);
 
   if (!ED25519_sign(signature_, to_be_signed_with_context_,
                     sizeof(to_be_signed_with_context_),
                     identity_->private_key)) {
-    GOOGLE_LOG(ERROR) << "signature failure";
+    ROUGHTIME_LOG(ERROR) << "signature failure";
     return false;
   }
   return true;
@@ -112,7 +112,7 @@
 
 bool Server::MakeResponse(uint8_t *out_response, size_t *out_len,
                           uint32_t index) {
-  GOOGLE_DCHECK_LT(index, num_leaves_);
+  ROUGHTIME_DCHECK_LT(index, num_leaves_);
   static_assert(kMaxResponseSize <= kMaxRecvPacketSize,
                 "Receive buffers are too small to use as send buffers");
   Builder response(out_response, kMaxResponseSize, 5);
@@ -129,7 +129,7 @@
       !response.AddTagData(kTagCERT, identity_->certificate, kCertSize) ||
       !response.AddTagData(kTagINDX, pindex, sizeof(index)) ||
       !response.Finish(out_len)) {
-    GOOGLE_LOG(ERROR) << "failed to construct response";
+    ROUGHTIME_LOG(ERROR) << "failed to construct response";
     return false;
   }
 
@@ -142,7 +142,7 @@
                        const uint8_t root_private_key[ED25519_PRIVATE_KEY_LEN],
                        rough_time_t start_time, rough_time_t end_time,
                        const uint8_t public_key[ED25519_PUBLIC_KEY_LEN]) {
-  GOOGLE_CHECK_LT(start_time, end_time);
+  ROUGHTIME_CHECK_LT(start_time, end_time);
   uint8_t to_be_signed_bytes[sizeof(kCertContextString) + kToBeSignedCertSize];
   size_t to_be_signed_len;
   memcpy(to_be_signed_bytes, kCertContextString, sizeof(kCertContextString));
@@ -158,15 +158,15 @@
       !to_be_signed.AddTagData(kTagMAXT, reinterpret_cast<uint8_t *>(&end_time),
                                sizeof(end_time)) ||
       !to_be_signed.Finish(&to_be_signed_len)) {
-    GOOGLE_LOG(ERROR) << "failed to construct signed portion of certificate";
+    ROUGHTIME_LOG(ERROR) << "failed to construct signed portion of certificate";
     return false;
   }
-  GOOGLE_CHECK_EQ(to_be_signed_len, kToBeSignedCertSize);
+  ROUGHTIME_CHECK_EQ(to_be_signed_len, kToBeSignedCertSize);
 
   uint8_t signature[ED25519_SIGNATURE_LEN];
   if (!ED25519_sign(signature, to_be_signed_bytes, sizeof(to_be_signed_bytes),
                     root_private_key)) {
-    GOOGLE_LOG(ERROR) << "failed to sign certificate";
+    ROUGHTIME_LOG(ERROR) << "failed to sign certificate";
     return false;
   }
 
@@ -179,15 +179,15 @@
                        to_be_signed_bytes + sizeof(kCertContextString),
                        to_be_signed_len) ||
       !cert.Finish(&cert_len)) {
-    GOOGLE_LOG(ERROR) << "failed to construct certificate";
+    ROUGHTIME_LOG(ERROR) << "failed to construct certificate";
     return false;
   }
-  GOOGLE_CHECK_EQ(cert_len, kCertSize);
+  ROUGHTIME_CHECK_EQ(cert_len, kCertSize);
   return true;
 }
 
 void Tree::Build(size_t num_nodes) {
-  GOOGLE_DCHECK_GT(num_nodes, 0);
+  ROUGHTIME_DCHECK_GT(num_nodes, 0ul);
   size_t level;
   for (level = 0; num_nodes > 1; level++, num_nodes /= 2) {
     // Even out the level with a dummy node, if need be.
@@ -199,7 +199,7 @@
       HashNode(tree_[level + 1][i / 2], tree_[level][i], tree_[level][i + 1]);
     }
   }
-  GOOGLE_DCHECK_EQ(1, num_nodes);  // Root node.
+  ROUGHTIME_DCHECK_EQ(1ul, num_nodes);  // Root node.
   levels_ = level + 1;
 }
 
@@ -215,7 +215,7 @@
     out_path += kNonceLength;
     index /= 2;
   }
-  GOOGLE_DCHECK_EQ(0, index);
+  ROUGHTIME_DCHECK_EQ(0ul, index);
 }
 
 BrokenReplyGenerator::~BrokenReplyGenerator() {}
diff --git a/server.h b/server.h
index d10a2e3..6a119b5 100644
--- a/server.h
+++ b/server.h
@@ -18,7 +18,6 @@
 #include <memory>
 #include <utility>
 
-#include <google/protobuf/stubs/macros.h>
 
 #include "protocol.h"
 #include "time_source.h"
diff --git a/simple_server.cc b/simple_server.cc
index 8b3a764..4388999 100644
--- a/simple_server.cc
+++ b/simple_server.cc
@@ -14,9 +14,9 @@
 
 #include "simple_server.h"
 
-#include <google/protobuf/stubs/logging.h>
 #include <openssl/curve25519.h>
 
+#include "logging.h"
 #include "server.h"
 
 namespace roughtime {
@@ -40,7 +40,7 @@
 std::unique_ptr<Identity> SimpleServer::MakeIdentity(
     const uint8_t root_private_key[ED25519_PRIVATE_KEY_LEN], rough_time_t mint,
     rough_time_t maxt) {
-  GOOGLE_CHECK(mint <= maxt);
+  ROUGHTIME_CHECK(mint <= maxt);
   uint8_t delegated_private_key[ED25519_PRIVATE_KEY_LEN];
   uint8_t delegated_public_key[ED25519_PUBLIC_KEY_LEN];
   ED25519_keypair(delegated_public_key, delegated_private_key);
diff --git a/simple_server_main.cc b/simple_server_main.cc
index 99048ef..c008513 100644
--- a/simple_server_main.cc
+++ b/simple_server_main.cc
@@ -18,7 +18,9 @@
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <stdio.h>
 
+#include "logging.h"
 #include "simple_server.h"
 #include "sys_time.h"
 
@@ -35,7 +37,7 @@
 
 int main(int argc, char **argv) {
   int requested_port = -1;
-
+  ROUGHTIME_INIT_LOGGER(argv[0]);
   if (argc == 2) {
     char *endptr;
     requested_port = strtoul(argv[1], &endptr, 10);
diff --git a/sys_time.cc b/sys_time.cc
index c0b86df..68d2b4b 100644
--- a/sys_time.cc
+++ b/sys_time.cc
@@ -20,7 +20,7 @@
 #include <time.h>
 #endif
 
-#include <google/protobuf/stubs/logging.h>
+#include "logging.h"
 
 namespace roughtime {
 
@@ -33,7 +33,7 @@
 #if defined(__MACH__)
 std::pair<rough_time_t, uint32_t> SystemTimeSource::Now() {
   struct timeval tv;
-  GOOGLE_CHECK_EQ(0, gettimeofday(&tv, nullptr));
+  ROUGHTIME_CHECK_EQ(0, gettimeofday(&tv, nullptr));
   uint64_t now = tv.tv_sec;
   now *= 1000000;
   now += tv.tv_usec;
@@ -43,7 +43,7 @@
 #else
 std::pair<rough_time_t, uint32_t> SystemTimeSource::Now() {
   struct timespec ts;
-  GOOGLE_CHECK_EQ(0, clock_gettime(CLOCK_REALTIME_COARSE, &ts));
+  ROUGHTIME_CHECK_EQ(0, clock_gettime(CLOCK_REALTIME_COARSE, &ts));
   uint64_t now = ts.tv_sec;
   now *= 1000000;
   now += ts.tv_nsec / 1000;
diff --git a/udp_processor.cc b/udp_processor.cc
index 9266193..d7a098a 100644
--- a/udp_processor.cc
+++ b/udp_processor.cc
@@ -17,8 +17,8 @@
 #include <fcntl.h>
 #include <unistd.h>
 
-#include <google/protobuf/stubs/logging.h>
 
+#include "logging.h"
 #include "open_source_fillins.h"
 
 namespace roughtime {
@@ -67,7 +67,7 @@
 
   const int fd = socket(AF_INET6, SOCK_DGRAM, 0);
   if (fd == -1) {
-    GOOGLE_PLOG(ERROR) << "socket";
+    ROUGHTIME_PLOG(ERROR) << "socket";
     return false;
   }
 
@@ -77,14 +77,14 @@
   sin6.sin6_addr = in6addr_any;
   sin6.sin6_port = htons(port);
   if (bind(fd, reinterpret_cast<sockaddr *>(&sin6), sizeof(sin6))) {
-    GOOGLE_PLOG(ERROR) << "bind";
+    ROUGHTIME_PLOG(ERROR) << "bind";
     close(fd);
     return false;
   }
 
   socklen_t sin6_len = sizeof(sin6);
   if (getsockname(fd, reinterpret_cast<sockaddr *>(&sin6), &sin6_len)) {
-    GOOGLE_PLOG(ERROR) << "getsockname";
+    ////ROUGHTIME_PLOG(ERROR) << "getsockname";
     close(fd);
     return false;
   }
@@ -125,7 +125,7 @@
 }
 
 int sendmmsg(int fd, struct mmsghdr *msgvec, unsigned vlen, unsigned flags) {
-  GOOGLE_CHECK_EQ(1, vlen);
+  ROUGHTIME_CHECK_EQ(1, vlen);
   ssize_t r = sendmsg(fd, &msgvec->msg_hdr, 0);
   if (r < 0) {
     return r;
@@ -148,7 +148,7 @@
   } while (r == -1 && errno == EINTR);
 
   if (r < 0) {
-    GOOGLE_PLOG(ERROR) << "recvmmsg";
+    ROUGHTIME_PLOG(ERROR) << "recvmmsg";
     return false;
   } else if (r == 0) {
     return true;
@@ -186,7 +186,7 @@
   for (size_t i = 0; i < index; i++) {
     size_t reply_len;
     if (!server->MakeResponse(send_buf_[i], &reply_len, i)) {
-      GOOGLE_LOG(ERROR) << "failed to assemble responses";
+      ROUGHTIME_LOG(ERROR) << "failed to assemble responses";
       return false;
     }
     requests_processed_++;
@@ -195,7 +195,7 @@
     if (MaybeBreakResponse(broken_output_buf, &reply_len,
                            sizeof(broken_output_buf), send_buf_[i], reply_len,
                            recv_buf_[i], recv_mmsghdrs_[i].msg_len)) {
-      GOOGLE_DCHECK_LE(reply_len, sizeof(broken_output_buf));
+      ROUGHTIME_DCHECK_LE(reply_len, sizeof(broken_output_buf));
       memcpy(send_buf_[i], broken_output_buf, reply_len);
     }
 
@@ -209,10 +209,10 @@
   } while (messages_sent == -1 && errno == EINTR);
 
   if (messages_sent == -1) {
-    GOOGLE_PLOG(ERROR) << "sendmmsg";
+    ROUGHTIME_PLOG(ERROR) << "sendmmsg";
     return false;
   }
-  GOOGLE_LOG_IF_EVERY_N_SEC(ERROR, (static_cast<size_t>(messages_sent) < index),
+  ROUGHTIME_LOG_IF_EVERY_N_SEC(ERROR, (static_cast<size_t>(messages_sent) < index),
                             30)
       << "only " << messages_sent << " of " << index << " messages were sent";