diff --git a/xplat/Sonar/ConnectionContextStore.cpp b/xplat/Sonar/ConnectionContextStore.cpp index 056977eb0..8dd9a2634 100644 --- a/xplat/Sonar/ConnectionContextStore.cpp +++ b/xplat/Sonar/ConnectionContextStore.cpp @@ -1,18 +1,11 @@ #include "ConnectionContextStore.h" #include "CertificateUtils.h" +#include "Log.h" #include #include #include #include -#ifdef __ANDROID__ -#include -#define SONAR_LOG(message) \ - __android_log_print(ANDROID_LOG_INFO, "sonar", "sonar: %s", message) -#else -#define SONAR_LOG(message) printf("sonar: %s\n", message) -#endif - using namespace facebook::sonar; static constexpr auto CSR_FILE_NAME = "app.csr"; @@ -98,9 +91,7 @@ bool ConnectionContextStore::ensureSonarDirExists() { } else if (info.st_mode & S_IFDIR) { return true; } else { - SONAR_LOG(std::string( - "ERROR: Sonar path exists but is not a directory: " + dirPath) - .c_str()); + log("ERROR: Sonar path exists but is not a directory: " + dirPath); return false; } } @@ -114,8 +105,7 @@ std::string loadStringFromFile(std::string fileName) { std::string line; stream.open(fileName.c_str()); if (!stream) { - SONAR_LOG( - std::string("ERROR: Unable to open ifstream: " + fileName).c_str()); + log("ERROR: Unable to open ifstream: " + fileName); return ""; } buffer << stream.rdbuf(); diff --git a/xplat/Sonar/Log.cpp b/xplat/Sonar/Log.cpp new file mode 100644 index 000000000..44b0f7513 --- /dev/null +++ b/xplat/Sonar/Log.cpp @@ -0,0 +1,20 @@ +#include "Log.h" + +#ifdef __ANDROID__ +#include +#endif + +namespace facebook { +namespace sonar { + + void log(const std::string& message) { + #ifdef __ANDROID__ + __android_log_print(ANDROID_LOG_INFO, "sonar", "sonar: %s", message.c_str()); + #else + printf("sonar: %s\n", message.c_str()); + #endif + } + + +} // namespace sonar +} // namespace facebook diff --git a/xplat/Sonar/Log.h b/xplat/Sonar/Log.h new file mode 100644 index 000000000..9a20b54bc --- /dev/null +++ b/xplat/Sonar/Log.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace facebook { +namespace sonar { + + void log(const std::string& message); + +} // namespace sonar +} // namespace facebook diff --git a/xplat/Sonar/SonarClient.cpp b/xplat/Sonar/SonarClient.cpp index 544e97604..16f5393b4 100644 --- a/xplat/Sonar/SonarClient.cpp +++ b/xplat/Sonar/SonarClient.cpp @@ -13,16 +13,9 @@ #include "SonarStep.h" #include "SonarWebSocketImpl.h" #include "ConnectionContextStore.h" +#include "Log.h" #include -#ifdef __ANDROID__ -#include -#define SONAR_LOG(message) \ - __android_log_print(ANDROID_LOG_INFO, "sonar", "sonar: %s", message) -#else -#define SONAR_LOG(message) printf("sonar: %s\n", message) -#endif - #if FB_SONARKIT_ENABLED namespace facebook { @@ -45,12 +38,12 @@ SonarClient* SonarClient::instance() { void SonarClient::setStateListener( std::shared_ptr stateListener) { - SONAR_LOG("Setting state listener"); + log("Setting state listener"); sonarState_->setUpdateListener(stateListener); } void SonarClient::addPlugin(std::shared_ptr plugin) { - SONAR_LOG(("SonarClient::addPlugin " + plugin->identifier()).c_str()); + log("SonarClient::addPlugin " + plugin->identifier()); auto step = sonarState_->start("Add plugin " + plugin->identifier()); std::lock_guard lock(mutex_); @@ -68,7 +61,7 @@ void SonarClient::addPlugin(std::shared_ptr plugin) { } void SonarClient::removePlugin(std::shared_ptr plugin) { - SONAR_LOG(("SonarClient::removePlugin " + plugin->identifier()).c_str()); + log("SonarClient::removePlugin " + plugin->identifier()); std::lock_guard lock(mutex_); performAndReportError([this, plugin]() { @@ -111,14 +104,14 @@ void SonarClient::refreshPlugins() { } void SonarClient::onConnected() { - SONAR_LOG("SonarClient::onConnected"); + log("SonarClient::onConnected"); std::lock_guard lock(mutex_); connected_ = true; } void SonarClient::onDisconnected() { - SONAR_LOG("SonarClient::onDisconnected"); + log("SonarClient::onDisconnected"); auto step = sonarState_->start("Trigger onDisconnected callbacks"); std::lock_guard lock(mutex_); connected_ = false; diff --git a/xplat/Sonar/SonarWebSocketImpl.cpp b/xplat/Sonar/SonarWebSocketImpl.cpp index 199512789..aad82a819 100644 --- a/xplat/Sonar/SonarWebSocketImpl.cpp +++ b/xplat/Sonar/SonarWebSocketImpl.cpp @@ -9,6 +9,7 @@ #include "SonarWebSocketImpl.h" #include "SonarStep.h" #include "ConnectionContextStore.h" +#include "Log.h" #include #include #include @@ -20,14 +21,6 @@ #include #include -#ifdef __ANDROID__ -#include -#define SONAR_LOG(message) \ - __android_log_print(ANDROID_LOG_INFO, "sonar", "sonar: %s", message) -#else -#define SONAR_LOG(message) printf("sonar: %s\n", message) -#endif - #define WRONG_THREAD_EXIT_MSG \ "ERROR: Aborting sonar initialization because it's not running in the sonar thread." @@ -104,11 +97,11 @@ void SonarWebSocketImpl::start() { void SonarWebSocketImpl::startSync() { if (!isRunningInOwnThread()) { - SONAR_LOG(WRONG_THREAD_EXIT_MSG); + log(WRONG_THREAD_EXIT_MSG); return; } if (isOpen()) { - SONAR_LOG("Already connected"); + log("Already connected"); return; } auto connect = sonarState_->start("Connect to desktop"); @@ -126,13 +119,13 @@ void SonarWebSocketImpl::startSync() { // Don't count as a failed attempt. connect->fail("Port not open"); } else { - SONAR_LOG(e.what()); + log(e.what()); failedConnectionAttempts_++; connect->fail(e.what()); } reconnect(); } catch (const std::exception& e) { - SONAR_LOG(e.what()); + log(e.what()); connect->fail(e.what()); failedConnectionAttempts_++; reconnect(); @@ -264,7 +257,7 @@ void SonarWebSocketImpl::requestSignedCertFromSonar() { contextStore_->storeConnectionConfig(config); } gettingCert->complete(); - SONAR_LOG("Certificate exchange complete."); + log("Certificate exchange complete."); // Disconnect after message sending is complete. // This will trigger a reconnect which should use the secure channel. // TODO: Connect immediately, without waiting for reconnect @@ -276,13 +269,13 @@ void SonarWebSocketImpl::requestSignedCertFromSonar() { std::string errorMessage = errorWithPayload.payload.moveDataToString(); if (errorMessage.compare("not implemented")) { - SONAR_LOG(("Desktop failed to provide certificates. Error from sonar desktop:\n" + errorMessage).c_str()); + log("Desktop failed to provide certificates. Error from sonar desktop:\n" + errorMessage); } else { sendLegacyCertificateRequest(message); } }, [e](...) { - SONAR_LOG(("Error during certificate exchange:" + e.what()).c_str()); + log(("Error during certificate exchange:" + e.what()).c_str()); } ); });