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 "CertificateUtils.h"
#include "Log.h"
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#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;
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();

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 "SonarWebSocketImpl.h"
#include "ConnectionContextStore.h"
#include "Log.h"
#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
namespace facebook {
@@ -45,12 +38,12 @@ SonarClient* SonarClient::instance() {
void SonarClient::setStateListener(
std::shared_ptr<SonarStateUpdateListener> stateListener) {
SONAR_LOG("Setting state listener");
log("Setting state listener");
sonarState_->setUpdateListener(stateListener);
}
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());
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) {
SONAR_LOG(("SonarClient::removePlugin " + plugin->identifier()).c_str());
log("SonarClient::removePlugin " + plugin->identifier());
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex_);
connected_ = true;
}
void SonarClient::onDisconnected() {
SONAR_LOG("SonarClient::onDisconnected");
log("SonarClient::onDisconnected");
auto step = sonarState_->start("Trigger onDisconnected callbacks");
std::lock_guard<std::mutex> lock(mutex_);
connected_ = false;

View File

@@ -9,6 +9,7 @@
#include "SonarWebSocketImpl.h"
#include "SonarStep.h"
#include "ConnectionContextStore.h"
#include "Log.h"
#include <folly/String.h>
#include <folly/futures/Future.h>
#include <folly/io/async/SSLContext.h>
@@ -20,14 +21,6 @@
#include <folly/io/async/AsyncSocketException.h>
#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 \
"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());
}
);
});