Use the event loop name instead of thread name for AOS_LOG

This kills 2 birds with 1 stone.
  1) A simulated event loop should print out the name of each event
     loop, not the program name.
  2) prctl(PR_GET_NAME, thread_name_array) can require higher privileges
     sometimes, and is un-necesary for simulations.  See 1)

Change-Id: I48731b1cabe34ec66a97f27ee720ba3081da4e94
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/aos/events/BUILD b/aos/events/BUILD
index 0f19beb..f36da1e 100644
--- a/aos/events/BUILD
+++ b/aos/events/BUILD
@@ -312,6 +312,7 @@
         ":test_message_schema",
         ":timing_report_schema",
         "//aos:realtime",
+        "//aos/logging:log_message_fbs",
         "//aos/logging:log_message_schema",
         "//aos/network:message_bridge_client_schema",
         "//aos/network:message_bridge_server_schema",
diff --git a/aos/events/aos_logging.cc b/aos/events/aos_logging.cc
index 5b6f72c..bc4513b 100644
--- a/aos/events/aos_logging.cc
+++ b/aos/events/aos_logging.cc
@@ -2,7 +2,8 @@
 
 namespace aos {
 
-void AosLogToFbs::Initialize(Sender<logging::LogMessageFbs> log_sender) {
+void AosLogToFbs::Initialize(const std::string *name,
+                             Sender<logging::LogMessageFbs> log_sender) {
   log_sender_ = std::move(log_sender);
   if (log_sender_) {
     implementation_ = std::make_shared<logging::CallbackLogImplementation>(
@@ -23,7 +24,8 @@
           builder.add_name(name_str);
 
           message.Send(builder.Finish());
-        });
+        },
+        name);
   }
 }
 
diff --git a/aos/events/aos_logging.h b/aos/events/aos_logging.h
index e99edb7..efb2b25 100644
--- a/aos/events/aos_logging.h
+++ b/aos/events/aos_logging.h
@@ -11,7 +11,10 @@
  public:
   AosLogToFbs() {}
 
-  void Initialize(Sender<logging::LogMessageFbs> log_sender);
+  // Initializes a sender with the provided name and sender.  Note: the name
+  // needs to be valid until this object is destroyed.
+  void Initialize(const std::string *name,
+                  Sender<logging::LogMessageFbs> log_sender);
   std::shared_ptr<logging::LogImplementation> implementation() const {
     return implementation_;
   }
diff --git a/aos/events/event_loop_param_test.cc b/aos/events/event_loop_param_test.cc
index d3d21ac..2749f60 100644
--- a/aos/events/event_loop_param_test.cc
+++ b/aos/events/event_loop_param_test.cc
@@ -5,6 +5,8 @@
 #include <unordered_set>
 
 #include "aos/flatbuffer_merge.h"
+#include "aos/logging/log_message_generated.h"
+#include "aos/logging/logging.h"
 #include "aos/realtime.h"
 #include "glog/logging.h"
 #include "gmock/gmock.h"
@@ -947,6 +949,74 @@
   Run();
 }
 
+// Verify that AOS_LOG has the right name.
+TEST_P(AbstractEventLoopTest, AOSLog) {
+  auto loop2 = MakePrimary("loop1");
+  auto loop1 = Make("loop0");
+
+  auto fetcher = loop1->MakeFetcher<aos::logging::LogMessageFbs>("/aos");
+
+  EXPECT_FALSE(fetcher.Fetch());
+
+  loop2->OnRun([&]() {
+    AOS_LOG(INFO, "Testing123");
+    this->Exit();
+  });
+
+  Run();
+  EXPECT_TRUE(fetcher.Fetch());
+  EXPECT_EQ(fetcher->name()->string_view(), "loop1");
+}
+
+// Verify that AOS_LOG has the right name in a watcher.
+TEST_P(AbstractEventLoopTest, AOSLogWatcher) {
+  auto loop2 = MakePrimary("loop1");
+  auto loop1 = Make("loop0");
+
+  auto fetcher = loop1->MakeFetcher<aos::logging::LogMessageFbs>("/aos");
+
+  EXPECT_FALSE(fetcher.Fetch());
+
+  auto sender = loop1->MakeSender<TestMessage>("/test2");
+
+  loop2->MakeWatcher("/test2", [&](const TestMessage & /*message*/) {
+    AOS_LOG(INFO, "Testing123");
+    this->Exit();
+  });
+
+  loop2->OnRun([&]() {
+    aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
+    TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
+    builder.add_value(200);
+    ASSERT_TRUE(msg.Send(builder.Finish()));
+  });
+
+  Run();
+  EXPECT_TRUE(fetcher.Fetch());
+  EXPECT_EQ(fetcher->name()->string_view(), "loop1");
+}
+
+// Verify that AOS_LOG has the right name in a timer.
+TEST_P(AbstractEventLoopTest, AOSLogTimer) {
+  auto loop2 = MakePrimary("loop1");
+  auto loop1 = Make("loop0");
+
+  auto fetcher = loop1->MakeFetcher<aos::logging::LogMessageFbs>("/aos");
+
+  EXPECT_FALSE(fetcher.Fetch());
+
+  auto test_timer = loop2->AddTimer([&]() {
+    AOS_LOG(INFO, "Testing123");
+    this->Exit();
+  });
+
+  loop2->OnRun([&]() { test_timer->Setup(loop2->monotonic_now()); });
+
+  Run();
+  EXPECT_TRUE(fetcher.Fetch());
+  EXPECT_EQ(fetcher->name()->string_view(), "loop1");
+}
+
 // Verify that timer intervals and duration function properly.
 TEST_P(AbstractEventLoopTest, TimerIntervalAndDuration) {
   // Force a slower rate so we are guaranteed to have reports for our timer.
diff --git a/aos/events/shm_event_loop.cc b/aos/events/shm_event_loop.cc
index c70fef6..a215040 100644
--- a/aos/events/shm_event_loop.cc
+++ b/aos/events/shm_event_loop.cc
@@ -1092,7 +1092,7 @@
     logging::ScopedLogRestorer prev_logger;
     AosLogToFbs aos_logger;
     if (!skip_logger_) {
-      aos_logger.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
+      aos_logger.Initialize(&name_, MakeSender<logging::LogMessageFbs>("/aos"));
       prev_logger.Swap(aos_logger.implementation());
     }
 
diff --git a/aos/events/shm_event_loop.h b/aos/events/shm_event_loop.h
index 3245f11..71dff4e 100644
--- a/aos/events/shm_event_loop.h
+++ b/aos/events/shm_event_loop.h
@@ -38,8 +38,11 @@
   ShmEventLoop(const Flatbuffer<Configuration> &configuration)
       : ShmEventLoop(&configuration.message()) {}
   ShmEventLoop(const Configuration *configuration);
+  ShmEventLoop(const ShmEventLoop &) = delete;
   ~ShmEventLoop() override;
 
+  void operator=(ShmEventLoop const &) = delete;
+
   // Runs the event loop until Exit is called, or ^C is caught.
   void Run();
   // Exits the event loop.  Async safe.
diff --git a/aos/events/simulated_event_loop.cc b/aos/events/simulated_event_loop.cc
index 121e164..9e12481 100644
--- a/aos/events/simulated_event_loop.cc
+++ b/aos/events/simulated_event_loop.cc
@@ -609,6 +609,10 @@
   void OnRun(::std::function<void()> on_run) override {
     CHECK(!is_running()) << ": Cannot register OnRun callback while running.";
     scheduler_->ScheduleOnRun([this, on_run = std::move(on_run)]() {
+      logging::ScopedLogRestorer prev_logger;
+      if (log_impl_) {
+        prev_logger.Swap(log_impl_);
+      }
       ScopedMarkRealtimeRestorer rt(priority() > 0);
       SetTimerContext(monotonic_now());
       on_run();
@@ -638,7 +642,8 @@
   void Setup() {
     MaybeScheduleTimingReports();
     if (!skip_logger_) {
-      log_sender_.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
+      log_sender_.Initialize(&name_,
+                             MakeSender<logging::LogMessageFbs>("/aos"));
       log_impl_ = log_sender_.implementation();
     }
   }