net: NetworkMonitor: gracefully handle backend initialization failure

Change-Id: I1a22fd53e9905b6e5892573d1473d1f4e8969522
Refs: #4668
diff --git a/src/net/detail/network-monitor-impl-noop.hpp b/src/net/detail/network-monitor-impl-noop.hpp
index 919d231..5b1a00d 100644
--- a/src/net/detail/network-monitor-impl-noop.hpp
+++ b/src/net/detail/network-monitor-impl-noop.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2017 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2018 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -33,7 +33,9 @@
 {
 public:
   explicit
-  NetworkMonitorImplNoop(boost::asio::io_service& io) = default;
+  NetworkMonitorImplNoop(boost::asio::io_service&)
+  {
+  }
 
   uint32_t
   getCapabilities() const final
diff --git a/src/net/detail/network-monitor-impl-osx.cpp b/src/net/detail/network-monitor-impl-osx.cpp
index 839faaa..c4e45e6 100644
--- a/src/net/detail/network-monitor-impl-osx.cpp
+++ b/src/net/detail/network-monitor-impl-osx.cpp
@@ -66,13 +66,13 @@
 #include <boost/asio/ip/address.hpp>
 #include <boost/asio/ip/udp.hpp>
 
+NDN_LOG_INIT(ndn.NetworkMonitor);
+
 namespace ndn {
 namespace net {
 
 using util::CFReleaser;
 
-NDN_LOG_INIT(ndn.NetworkMonitor);
-
 class IfAddrs : noncopyable
 {
 public:
diff --git a/src/net/network-monitor.cpp b/src/net/network-monitor.cpp
index 4874dd4..89af14a 100644
--- a/src/net/network-monitor.cpp
+++ b/src/net/network-monitor.cpp
@@ -24,6 +24,9 @@
 
 #include "network-monitor.hpp"
 #include "ndn-cxx-config.hpp"
+#include "../util/logger.hpp"
+
+#include "detail/network-monitor-impl-noop.hpp"
 
 #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS)
 #include "detail/network-monitor-impl-osx.hpp"
@@ -32,15 +35,29 @@
 #include "detail/network-monitor-impl-netlink.hpp"
 #define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplNetlink
 #else
-#include "detail/network-monitor-impl-noop.hpp"
 #define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplNoop
 #endif
 
+NDN_LOG_INIT(ndn.NetworkMonitor);
+
 namespace ndn {
 namespace net {
 
+static unique_ptr<NetworkMonitorImpl>
+makeNetworkMonitorImpl(boost::asio::io_service& io)
+{
+  try {
+    return make_unique<NETWORK_MONITOR_IMPL_TYPE>(io);
+  }
+  catch (const std::runtime_error& e) {
+    NDN_LOG_WARN("failed to initialize " BOOST_STRINGIZE(NETWORK_MONITOR_IMPL_TYPE) ": " << e.what());
+    // fallback to dummy implementation
+    return make_unique<NetworkMonitorImplNoop>(io);
+  }
+}
+
 NetworkMonitor::NetworkMonitor(boost::asio::io_service& io)
-  : NetworkMonitor(make_unique<NETWORK_MONITOR_IMPL_TYPE>(io))
+  : NetworkMonitor(makeNetworkMonitorImpl(io))
 {
 }