diff options
| author | Odd Stranne <odd@mullvad.net> | 2019-09-10 09:58:25 +0200 |
|---|---|---|
| committer | Odd Stranne <odd@mullvad.net> | 2019-09-10 11:02:40 +0200 |
| commit | b38ee8eb5d0938f1e5d2ad9521fb786748b589fc (patch) | |
| tree | 74d11b440c3f7ca5d568603fb53101d680dceec7 | |
| parent | 3fedc167f03c55732f1c6c4f30d7be2402627e57 (diff) | |
| download | mullvadvpn-b38ee8eb5d0938f1e5d2ad9521fb786748b589fc.tar.xz mullvadvpn-b38ee8eb5d0938f1e5d2ad9521fb786748b589fc.zip | |
Use formalized logging interfaces in 'winnet'
| -rw-r--r-- | windows/winnet/src/winnet/winnet.cpp | 101 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.h | 49 |
2 files changed, 73 insertions, 77 deletions
diff --git a/windows/winnet/src/winnet/winnet.cpp b/windows/winnet/src/winnet/winnet.cpp index 2465ec667b..a3e47c7b7c 100644 --- a/windows/winnet/src/winnet/winnet.cpp +++ b/windows/winnet/src/winnet/winnet.cpp @@ -2,16 +2,30 @@ #include "winnet.h"
#include "NetworkInterfaces.h"
#include "interfaceutils.h"
-#include "libcommon/error.h"
#include "netmonitor.h"
+#include "../../shared/logsinkadapter.h"
+#include <libcommon/error.h>
#include <cstdint>
#include <stdexcept>
+#include <memory>
namespace
{
NetMonitor *g_NetMonitor = nullptr;
+void UnwindAndLog(MullvadLogSink logSink, void *logSinkContext, const std::exception &err)
+{
+ if (nullptr == logSink)
+ {
+ return;
+ }
+
+ auto logger = std::make_shared<shared::LogSinkAdapter>(logSink, logSinkContext);
+
+ common::error::UnwindException(err, logger);
+}
+
} //anonymous namespace
extern "C"
@@ -20,28 +34,24 @@ WINNET_ETM_STATUS WINNET_API
WinNet_EnsureTopMetric(
const wchar_t *deviceAlias,
- WinNetErrorSink errorSink,
- void *errorSinkContext
+ MullvadLogSink logSink,
+ void *logSinkContext
)
{
try
{
NetworkInterfaces interfaces;
bool metrics_set = interfaces.SetTopMetricForInterfacesByAlias(deviceAlias);
- return metrics_set ? WINNET_ETM_STATUS::METRIC_SET : WINNET_ETM_STATUS::METRIC_NO_CHANGE;
+ return metrics_set ? WINNET_ETM_STATUS_METRIC_SET : WINNET_ETM_STATUS_METRIC_NO_CHANGE;
}
- catch (std::exception &err)
+ catch (const std::exception &err)
{
- if (nullptr != errorSink)
- {
- errorSink(err.what(), errorSinkContext);
- }
-
- return WINNET_ETM_STATUS::FAILURE;
+ UnwindAndLog(logSink, logSinkContext, err);
+ return WINNET_ETM_STATUS_FAILURE;
}
catch (...)
{
- return WINNET_ETM_STATUS::FAILURE;
+ return WINNET_ETM_STATUS_FAILURE;
}
};
@@ -50,8 +60,8 @@ WINNET_LINKAGE WINNET_GTII_STATUS
WINNET_API
WinNet_GetTapInterfaceIpv6Status(
- WinNetErrorSink errorSink,
- void *errorSinkContext
+ MullvadLogSink logSink,
+ void *logSinkContext
)
{
try
@@ -65,28 +75,24 @@ WinNet_GetTapInterfaceIpv6Status( if (NO_ERROR == status)
{
- return WINNET_GTII_STATUS::ENABLED;
+ return WINNET_GTII_STATUS_ENABLED;
}
if (ERROR_NOT_FOUND == status)
{
- return WINNET_GTII_STATUS::DISABLED;
+ return WINNET_GTII_STATUS_DISABLED;
}
common::error::Throw("Resolve TAP IPv6 interface", status);
}
- catch (std::exception &err)
+ catch (const std::exception &err)
{
- if (nullptr != errorSink)
- {
- errorSink(err.what(), errorSinkContext);
- }
-
- return WINNET_GTII_STATUS::FAILURE;
+ UnwindAndLog(logSink, logSinkContext, err);
+ return WINNET_GTII_STATUS_FAILURE;
}
catch (...)
{
- return WINNET_GTII_STATUS::FAILURE;
+ return WINNET_GTII_STATUS_FAILURE;
}
}
@@ -96,8 +102,8 @@ bool WINNET_API
WinNet_GetTapInterfaceAlias(
wchar_t **alias,
- WinNetErrorSink errorSink,
- void *errorSinkContext
+ MullvadLogSink logSink,
+ void *logSinkContext
)
{
try
@@ -111,13 +117,9 @@ WinNet_GetTapInterfaceAlias( return true;
}
- catch (std::exception &err)
+ catch (const std::exception &err)
{
- if (nullptr != errorSink)
- {
- errorSink(err.what(), errorSinkContext);
- }
-
+ UnwindAndLog(logSink, logSinkContext, err);
return false;
}
catch (...)
@@ -151,8 +153,8 @@ WinNet_ActivateConnectivityMonitor( WinNetConnectivityMonitorCallback callback,
void *callbackContext,
bool *currentConnectivity,
- WinNetErrorSink errorSink,
- void *errorSinkContext
+ MullvadLogSink logSink,
+ void *logSinkContext
)
{
try
@@ -169,7 +171,9 @@ WinNet_ActivateConnectivityMonitor( bool connected = false;
- g_NetMonitor = new NetMonitor(forwarder, connected);
+ auto logger = std::make_shared<shared::LogSinkAdapter>(logSink, logSinkContext);
+
+ g_NetMonitor = new NetMonitor(logger, forwarder, connected);
if (nullptr != currentConnectivity)
{
@@ -178,13 +182,9 @@ WinNet_ActivateConnectivityMonitor( return true;
}
- catch (std::exception &err)
+ catch (const std::exception &err)
{
- if (nullptr != errorSink)
- {
- errorSink(err.what(), errorSinkContext);
- }
-
+ UnwindAndLog(logSink, logSinkContext, err);
return false;
}
catch (...)
@@ -215,25 +215,22 @@ WINNET_LINKAGE WINNET_CC_STATUS
WINNET_API
WinNet_CheckConnectivity(
- WinNetErrorSink errorSink,
- void *errorSinkContext
+ MullvadLogSink logSink,
+ void *logSinkContext
)
{
try
{
- return (NetMonitor::CheckConnectivity() ? WINNET_CC_STATUS::CONNECTED : WINNET_CC_STATUS::NOT_CONNECTED);
+ return (NetMonitor::CheckConnectivity(std::make_shared<shared::LogSinkAdapter>(logSink, logSinkContext))
+ ? WINNET_CC_STATUS_CONNECTED : WINNET_CC_STATUS_NOT_CONNECTED);
}
- catch (std::exception &err)
+ catch (const std::exception &err)
{
- if (nullptr != errorSink)
- {
- errorSink(err.what(), errorSinkContext);
- }
-
- return WINNET_CC_STATUS::CONNECTIVITY_UNKNOWN;
+ UnwindAndLog(logSink, logSinkContext, err);
+ return WINNET_CC_STATUS_CONNECTIVITY_UNKNOWN;
}
catch (...)
{
- return WINNET_CC_STATUS::CONNECTIVITY_UNKNOWN;
+ return WINNET_CC_STATUS_CONNECTIVITY_UNKNOWN;
}
}
diff --git a/windows/winnet/src/winnet/winnet.h b/windows/winnet/src/winnet/winnet.h index 40ed25f567..210c2ff349 100644 --- a/windows/winnet/src/winnet/winnet.h +++ b/windows/winnet/src/winnet/winnet.h @@ -1,5 +1,6 @@ #pragma once -#include <stdint.h> + +#include "../../shared/logsink.h" #include <stdbool.h> #ifdef WINNET_EXPORTS @@ -10,13 +11,11 @@ #define WINNET_API __stdcall -typedef void (WINNET_API *WinNetErrorSink)(const char *errorMessage, void *context); - -enum class WINNET_ETM_STATUS : uint32_t +enum WINNET_ETM_STATUS { - METRIC_NO_CHANGE = 0, - METRIC_SET = 1, - FAILURE = 2, + WINNET_ETM_STATUS_METRIC_NO_CHANGE = 0, + WINNET_ETM_STATUS_METRIC_SET = 1, + WINNET_ETM_STATUS_FAILURE = 2, }; extern "C" @@ -25,15 +24,15 @@ WINNET_ETM_STATUS WINNET_API WinNet_EnsureTopMetric( const wchar_t *deviceAlias, - WinNetErrorSink errorSink, - void *errorSinkContext + MullvadLogSink logSink, + void *logSinkContext ); -enum class WINNET_GTII_STATUS : uint32_t +enum WINNET_GTII_STATUS { - ENABLED = 0, - DISABLED = 1, - FAILURE = 2, + WINNET_GTII_STATUS_ENABLED = 0, + WINNET_GTII_STATUS_DISABLED = 1, + WINNET_GTII_STATUS_FAILURE = 2, }; extern "C" @@ -41,8 +40,8 @@ WINNET_LINKAGE WINNET_GTII_STATUS WINNET_API WinNet_GetTapInterfaceIpv6Status( - WinNetErrorSink errorSink, - void *errorSinkContext + MullvadLogSink logSink, + void *logSinkContext ); extern "C" @@ -51,8 +50,8 @@ bool WINNET_API WinNet_GetTapInterfaceAlias( wchar_t **alias, - WinNetErrorSink errorSink, - void *errorSinkContext + MullvadLogSink logSink, + void *logSinkContext ); // @@ -77,8 +76,8 @@ WinNet_ActivateConnectivityMonitor( WinNetConnectivityMonitorCallback callback, void *callbackContext, bool *currentConnectivity, - WinNetErrorSink errorSink, - void *errorSinkContext + MullvadLogSink logSink, + void *logSinkContext ); extern "C" @@ -88,11 +87,11 @@ WINNET_API WinNet_DeactivateConnectivityMonitor( ); -enum class WINNET_CC_STATUS : uint32_t +enum WINNET_CC_STATUS { - NOT_CONNECTED = 0, - CONNECTED = 1, - CONNECTIVITY_UNKNOWN = 2, + WINNET_CC_STATUS_NOT_CONNECTED = 0, + WINNET_CC_STATUS_CONNECTED = 1, + WINNET_CC_STATUS_CONNECTIVITY_UNKNOWN = 2, }; extern "C" @@ -100,6 +99,6 @@ WINNET_LINKAGE WINNET_CC_STATUS WINNET_API WinNet_CheckConnectivity( - WinNetErrorSink errorSink, - void *errorSinkContext + MullvadLogSink logSink, + void *logSinkContext ); |
