nn: refactor use of chrome logging library

Addressing comments from crrev.com/c/2725099

BUG=b:178691093
TEST=FEATURES=test emerge-volteer aosp-frameworks-ml-nn

Change-Id: I82f0c3e8200c287c5b45590e06d44cf3152b1196
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/frameworks/ml/+/2727702
Reviewed-by: Michael Pishchagin <mblsha@google.com>
Reviewed-by: Stuart Langley <slangley@chromium.org>
Tested-by: Jim Pollock <jmpollock@chromium.org>
Auto-Submit: Jim Pollock <jmpollock@chromium.org>
Commit-Queue: Stuart Langley <slangley@chromium.org>
diff --git a/nn/BUILD.gn b/nn/BUILD.gn
index 52f787e..c1d5e97 100644
--- a/nn/BUILD.gn
+++ b/nn/BUILD.gn
@@ -24,6 +24,10 @@
   configs += [
     ":target_defaults",
   ]
+  cflags_cc = [
+    # libchrome/base/stl_util.h:95:33: error: unused parameter 'array'
+    "-Wno-unused-parameter",
+  ]
   deps = [
     ":nn-common",
     ":runtime",
@@ -415,6 +419,7 @@
     ]
     sources = [
       "chromeos/tests/common/includes_test.cc",
+      "chromeos/tests/common/logger_enum_test.cc",
       "chromeos/tests/common/random_test.cc",
     ]
   }
diff --git a/nn/chromeos/logger.cpp b/nn/chromeos/logger.cpp
index 3cf8545..84e1647 100644
--- a/nn/chromeos/logger.cpp
+++ b/nn/chromeos/logger.cpp
@@ -3,6 +3,8 @@
 // found in the LICENSE file.
 
 #include <base/logging.h>
+#include <base/strings/strcat.h>
+#include <base/strings/string_number_conversions.h>
 
 // Copied from aosp/system/core/base/include/android-base/logging.h
 enum AndroidLogSeverity {
@@ -15,17 +17,11 @@
   FATAL,
 };
 
-/**
-Maps the android log levels from android-base/logging.h into the equivalent from
-the Chrome logging library. This is useful, since when running from within a
-Chrome process, unless we use base/logging.h from libchrome then all our logs
-will not be visible anywhere.
-*/
 void ChromeLogger(int /*log_buffer_id*/, int severity, const char* tag,
                   const char* file, unsigned int line, const char* message) {
-  std::string logline = std::string(tag) + ":" + std::string(file) + ":" +
-                        std::to_string(line) + ": " + std::string(message);
-  switch (severity) {
+  std::string logline = base::StrCat(
+      {tag, ":", file, ":", base::NumberToString(line), ": ", message});
+  switch (static_cast<AndroidLogSeverity>(severity)) {
     case (AndroidLogSeverity::VERBOSE):
     case (AndroidLogSeverity::DEBUG):
     case (AndroidLogSeverity::INFO):
@@ -41,8 +37,5 @@
     case (AndroidLogSeverity::FATAL):
       LOG(FATAL) << logline;
       break;
-    default:
-      LOG(INFO) << logline;
-      break;
   }
 }
diff --git a/nn/chromeos/logger.h b/nn/chromeos/logger.h
new file mode 100644
index 0000000..794eff7
--- /dev/null
+++ b/nn/chromeos/logger.h
@@ -0,0 +1,16 @@
+// Copyright 2021 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef ML_NN_CHROMEOS_LOGGER_H_
+#define ML_NN_CHROMEOS_LOGGER_H_
+
+// Maps the android log levels from android-base/logging.h into the equivalent from
+// the Chrome logging library. This is useful, since when running from within a
+// Chrome process, unless we use base/logging.h from libchrome then all our logs
+// will not be visible anywhere.
+void ChromeLogger(int log_buffer_id, int severity, const char* tag,
+                  const char* file, unsigned int line, const char* message);
+
+
+#endif  // ML_NN_CHROMEOS_LOGGER_H_
diff --git a/nn/chromeos/startup.cpp b/nn/chromeos/startup.cpp
index 9f01c2a..4b477c9 100644
--- a/nn/chromeos/startup.cpp
+++ b/nn/chromeos/startup.cpp
@@ -3,9 +3,7 @@
 // found in the LICENSE file.
 
 #include <android-base/logging.h>
-
-void ChromeLogger(int log_buffer_id, int severity, const char* tag,
-                  const char* file, unsigned int line, const char* message);
+#include "logger.h"
 
 // Called when the shared library is first loaded.
 __attribute__((constructor))
diff --git a/nn/chromeos/tests/common/logger_enum_test.cc b/nn/chromeos/tests/common/logger_enum_test.cc
new file mode 100644
index 0000000..8f7bbcf
--- /dev/null
+++ b/nn/chromeos/tests/common/logger_enum_test.cc
@@ -0,0 +1,24 @@
+// Copyright 2021 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <android-base/logging.h>
+#include <gtest/gtest.h>
+
+TEST(LoggerEnum, AllEnumsCovered) {
+  android::base::LogSeverity severity = android::base::VERBOSE;
+
+  // **NOTE** **IMPORTANT**: If you get a compile error here you REALLY
+  // REALLY need to go and update logger.cpp and add the new case to
+  // AndroidLogSeverity, otherwise we're going to potentially miss logs.
+  switch (severity) {
+    case (android::base::LogSeverity::VERBOSE):
+    case (android::base::LogSeverity::DEBUG):
+    case (android::base::LogSeverity::INFO):
+    case (android::base::LogSeverity::WARNING):
+    case (android::base::LogSeverity::ERROR):
+    case (android::base::LogSeverity::FATAL_WITHOUT_ABORT):
+    case (android::base::LogSeverity::FATAL):
+      break;
+  }
+}