Factor out SONAR_LOG

Reviewed By: passy

Differential Revision: D9555739

fbshipit-source-id: 022763ea03047e1c4cd88d5f389b66be4cbe416b
This commit is contained in:
John Knox
2018-09-03 11:10:59 -07:00
committed by Facebook Github Bot
parent 243b4207e7
commit 9939a9e3bb
5 changed files with 48 additions and 41 deletions

View File

@@ -1,18 +1,11 @@
#include "ConnectionContextStore.h" #include "ConnectionContextStore.h"
#include "CertificateUtils.h" #include "CertificateUtils.h"
#include "Log.h"
#include <sys/stat.h> #include <sys/stat.h>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <folly/json.h> #include <folly/json.h>
#ifdef __ANDROID__
#include <android/log.h>
#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; using namespace facebook::sonar;
static constexpr auto CSR_FILE_NAME = "app.csr"; static constexpr auto CSR_FILE_NAME = "app.csr";
@@ -98,9 +91,7 @@ bool ConnectionContextStore::ensureSonarDirExists() {
} else if (info.st_mode & S_IFDIR) { } else if (info.st_mode & S_IFDIR) {
return true; return true;
} else { } else {
SONAR_LOG(std::string( log("ERROR: Sonar path exists but is not a directory: " + dirPath);
"ERROR: Sonar path exists but is not a directory: " + dirPath)
.c_str());
return false; return false;
} }
} }
@@ -114,8 +105,7 @@ std::string loadStringFromFile(std::string fileName) {
std::string line; std::string line;
stream.open(fileName.c_str()); stream.open(fileName.c_str());
if (!stream) { if (!stream) {
SONAR_LOG( log("ERROR: Unable to open ifstream: " + fileName);
std::string("ERROR: Unable to open ifstream: " + fileName).c_str());
return ""; return "";
} }
buffer << stream.rdbuf(); buffer << stream.rdbuf();

20
xplat/Sonar/Log.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "Log.h"
#ifdef __ANDROID__
#include <android/log.h>
#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

11
xplat/Sonar/Log.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <string>
namespace facebook {
namespace sonar {
void log(const std::string& message);
} // namespace sonar
} // namespace facebook

View File

@@ -13,16 +13,9 @@
#include "SonarStep.h" #include "SonarStep.h"
#include "SonarWebSocketImpl.h" #include "SonarWebSocketImpl.h"
#include "ConnectionContextStore.h" #include "ConnectionContextStore.h"
#include "Log.h"
#include <vector> #include <vector>
#ifdef __ANDROID__
#include <android/log.h>
#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 #if FB_SONARKIT_ENABLED
namespace facebook { namespace facebook {
@@ -45,12 +38,12 @@ SonarClient* SonarClient::instance() {
void SonarClient::setStateListener( void SonarClient::setStateListener(
std::shared_ptr<SonarStateUpdateListener> stateListener) { std::shared_ptr<SonarStateUpdateListener> stateListener) {
SONAR_LOG("Setting state listener"); log("Setting state listener");
sonarState_->setUpdateListener(stateListener); sonarState_->setUpdateListener(stateListener);
} }
void SonarClient::addPlugin(std::shared_ptr<SonarPlugin> plugin) { void SonarClient::addPlugin(std::shared_ptr<SonarPlugin> plugin) {
SONAR_LOG(("SonarClient::addPlugin " + plugin->identifier()).c_str()); log("SonarClient::addPlugin " + plugin->identifier());
auto step = sonarState_->start("Add plugin " + plugin->identifier()); auto step = sonarState_->start("Add plugin " + plugin->identifier());
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
@@ -68,7 +61,7 @@ void SonarClient::addPlugin(std::shared_ptr<SonarPlugin> plugin) {
} }
void SonarClient::removePlugin(std::shared_ptr<SonarPlugin> plugin) { void SonarClient::removePlugin(std::shared_ptr<SonarPlugin> plugin) {
SONAR_LOG(("SonarClient::removePlugin " + plugin->identifier()).c_str()); log("SonarClient::removePlugin " + plugin->identifier());
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
performAndReportError([this, plugin]() { performAndReportError([this, plugin]() {
@@ -111,14 +104,14 @@ void SonarClient::refreshPlugins() {
} }
void SonarClient::onConnected() { void SonarClient::onConnected() {
SONAR_LOG("SonarClient::onConnected"); log("SonarClient::onConnected");
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
connected_ = true; connected_ = true;
} }
void SonarClient::onDisconnected() { void SonarClient::onDisconnected() {
SONAR_LOG("SonarClient::onDisconnected"); log("SonarClient::onDisconnected");
auto step = sonarState_->start("Trigger onDisconnected callbacks"); auto step = sonarState_->start("Trigger onDisconnected callbacks");
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
connected_ = false; connected_ = false;

View File

@@ -9,6 +9,7 @@
#include "SonarWebSocketImpl.h" #include "SonarWebSocketImpl.h"
#include "SonarStep.h" #include "SonarStep.h"
#include "ConnectionContextStore.h" #include "ConnectionContextStore.h"
#include "Log.h"
#include <folly/String.h> #include <folly/String.h>
#include <folly/futures/Future.h> #include <folly/futures/Future.h>
#include <folly/io/async/SSLContext.h> #include <folly/io/async/SSLContext.h>
@@ -20,14 +21,6 @@
#include <folly/io/async/AsyncSocketException.h> #include <folly/io/async/AsyncSocketException.h>
#include <stdexcept> #include <stdexcept>
#ifdef __ANDROID__
#include <android/log.h>
#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 \ #define WRONG_THREAD_EXIT_MSG \
"ERROR: Aborting sonar initialization because it's not running in the sonar thread." "ERROR: Aborting sonar initialization because it's not running in the sonar thread."
@@ -104,11 +97,11 @@ void SonarWebSocketImpl::start() {
void SonarWebSocketImpl::startSync() { void SonarWebSocketImpl::startSync() {
if (!isRunningInOwnThread()) { if (!isRunningInOwnThread()) {
SONAR_LOG(WRONG_THREAD_EXIT_MSG); log(WRONG_THREAD_EXIT_MSG);
return; return;
} }
if (isOpen()) { if (isOpen()) {
SONAR_LOG("Already connected"); log("Already connected");
return; return;
} }
auto connect = sonarState_->start("Connect to desktop"); auto connect = sonarState_->start("Connect to desktop");
@@ -126,13 +119,13 @@ void SonarWebSocketImpl::startSync() {
// Don't count as a failed attempt. // Don't count as a failed attempt.
connect->fail("Port not open"); connect->fail("Port not open");
} else { } else {
SONAR_LOG(e.what()); log(e.what());
failedConnectionAttempts_++; failedConnectionAttempts_++;
connect->fail(e.what()); connect->fail(e.what());
} }
reconnect(); reconnect();
} catch (const std::exception& e) { } catch (const std::exception& e) {
SONAR_LOG(e.what()); log(e.what());
connect->fail(e.what()); connect->fail(e.what());
failedConnectionAttempts_++; failedConnectionAttempts_++;
reconnect(); reconnect();
@@ -264,7 +257,7 @@ void SonarWebSocketImpl::requestSignedCertFromSonar() {
contextStore_->storeConnectionConfig(config); contextStore_->storeConnectionConfig(config);
} }
gettingCert->complete(); gettingCert->complete();
SONAR_LOG("Certificate exchange complete."); log("Certificate exchange complete.");
// Disconnect after message sending is complete. // Disconnect after message sending is complete.
// This will trigger a reconnect which should use the secure channel. // This will trigger a reconnect which should use the secure channel.
// TODO: Connect immediately, without waiting for reconnect // TODO: Connect immediately, without waiting for reconnect
@@ -276,13 +269,13 @@ void SonarWebSocketImpl::requestSignedCertFromSonar() {
std::string errorMessage = errorWithPayload.payload.moveDataToString(); std::string errorMessage = errorWithPayload.payload.moveDataToString();
if (errorMessage.compare("not implemented")) { 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 { } else {
sendLegacyCertificateRequest(message); sendLegacyCertificateRequest(message);
} }
}, },
[e](...) { [e](...) {
SONAR_LOG(("Error during certificate exchange:" + e.what()).c_str()); log(("Error during certificate exchange:" + e.what()).c_str());
} }
); );
}); });