Skip to content

Commit

Permalink
finally use a proper logging library instead of hand-rolled sh*t: spdlog
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-pandabear committed Oct 6, 2023
1 parent a866cd1 commit b1c53dd
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 255 deletions.
1 change: 1 addition & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[requires]
spdlog/1.12.0
zlib/1.2.13
openssl/3.0.0
uwebsockets/19.3.0
Expand Down
1 change: 0 additions & 1 deletion src/core/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ std::optional<json> verifyTransaction(string host_url, Transaction &t) {

std::string responseStr =
std::string{response.body.begin(), response.body.end()};
cout << "|" << responseStr << "|" << endl;
return into_json(response.body);
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/crypto.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

#include "crypto.hpp"
#include "helpers.hpp"
#include "logger.hpp"
#include "constants.hpp"
#include <iostream>
#include <atomic>
#include <thread>
#include <random>
#include "spdlog/spdlog.h"
#include "../external/ed25519/ed25519.h" //https://github.com/orlp/ed25519
#include "../external/pufferfish/pufferfish.h" //https://github.com/epixoip/pufferfish
#include "../external/sha256/sha2.hpp"
Expand Down Expand Up @@ -36,7 +36,7 @@ SHA256Hash PUFFERFISH(const char* buffer, size_t len, bool useCache) {
memset(hash, 0, PF_HASHSPACE);
int ret = 0;
if ((ret = pf_newhash((const void*) buffer, len, 0, 8, hash)) != 0) {
Logger::logStatus("PUFFERFISH failed to compute hash");
spdlog::error("PUFFERFISH failed to compute hash");
}
size_t sz = PF_HASHSPACE;

Expand Down Expand Up @@ -278,7 +278,7 @@ SHA256Hash mineHash(SHA256Hash target, unsigned char challengeSize, bool usePuff

if (hashes++ > 1024) {
double hps = (double)hashes / ((double)(getTimeMilliseconds() - st) / 1000);
Logger::logStatus("[thread" + std::to_string(threadId) + "] mining at " + std::to_string(hps) + "h/sec");
spdlog::info("[thread {}] mining at {} h/sec", std::to_string(threadId),std::to_string(hps));
hashes = 0;
st = getTimeMilliseconds();
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/header_chain.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "header_chain.hpp"
#include "api.hpp"
#include "block.hpp"
#include "logger.hpp"
#include "spdlog/spdlog.h"
#include <iostream>
#include <thread>
using namespace std;
Expand Down Expand Up @@ -103,15 +103,15 @@ void HeaderChain::load() {
if (this->bannedHashes.find(curr) != this->bannedHashes.end()) {
// check if the current hash corresponds to a banned hash
if (block.getHash() == this->bannedHashes[curr]) {
Logger::logStatus("Banned hash found for block: " + to_string(curr));
spdlog::info("Banned hash found for block: {}", to_string(curr));
failure = true;
break;
}
}
if (this->checkPoints.find(curr) != this->checkPoints.end()) {
// check the checkpoint hash:
if (block.getHash() != this->checkPoints[curr]) {
// Logger::logStatus("Checkpoint hash failed for block: " + to_string(curr));
// spdlog::info("Checkpoint hash failed for block: {}", to_string(curr));
failure = true;
break;
}
Expand All @@ -132,7 +132,7 @@ void HeaderChain::load() {
this->totalWork = totalWork;
}
if (failure) {
Logger::logStatus("header chain sync failed host=" + this->host);
spdlog::warn("header chain sync failed host={}", this->host);
this->failed = true;
this->reset();
return;
Expand All @@ -149,7 +149,7 @@ void HeaderChain::load() {
}
this->failed = false;
if (numBlocks != startBlocks) {
// Logger::logStatus("Chain for " + this->host + " updated to length=" + to_string(this->chainLength) + " total_work=" + to_string(this->totalWork));
spdlog::info("Chain for {} updated to length= {} total_work= {}" , this->host, to_string(this->chainLength), to_string(this->totalWork));
}
}

Expand Down
34 changes: 17 additions & 17 deletions src/core/host_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#include "helpers.hpp"
#include "api.hpp"
#include "constants.hpp"
#include "logger.hpp"
#include "header_chain.hpp"
#include "../external/http.hpp"
#include "spdlog/spdlog.h"
#include <iostream>
#include <sstream>
#include <thread>
#include <mutex>
#include <fstream>
#include <future>
#include <cstdio>
using namespace std;
Expand Down Expand Up @@ -62,7 +63,7 @@ string HostManager::computeAddress() {
}

if (!found) {
Logger::logError("IP discovery", "Could not determine current IP address");
spdlog::error("IP discovery {} Could not determine current IP address");
}
} else {
this->address = this->ip + ":" + to_string(this->port);
Expand Down Expand Up @@ -92,14 +93,13 @@ void peer_sync(HostManager& hm) {
void header_stats(HostManager& hm) {
std::this_thread::sleep_for(std::chrono::seconds(30));
while(true) {
Logger::logStatus("================ Header Sync Status ===============");
spdlog::info("================ Header Sync Status ===============");
map<string, pair<uint64_t, std::string>> stats = hm.getHeaderChainStats();
for(auto item : stats) {
stringstream ss;
ss << "Host: " << item.first << ", blocks: " << item.second.first << ", node_ver: " << item.second.second;
Logger::logStatus(ss.str());
spdlog::info("Host: {} blocks: {}, node_ver: {}", item.first, item.second.first, item.second.second);
}
Logger::logStatus("===================================================");
spdlog::info("===================================================");
std::this_thread::sleep_for(std::chrono::seconds(30));
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ HostManager::HostManager(json config) {
blocked = blocked.substr(0, blocked.size() - 1);
}
this->blacklist.insert(blocked);
Logger::logStatus("Ignoring host " + blocked);
spdlog::info("Ignoring host {}", blocked);
}
}
}
Expand All @@ -153,7 +153,7 @@ HostManager::HostManager(json config) {
enabled = enabled.substr(0, enabled.size() - 1);
}
this->whitelist.insert(enabled);
Logger::logStatus("Enabling host " + enabled);
spdlog::info("Enabling host {}", enabled);
}
}
}
Expand Down Expand Up @@ -327,7 +327,7 @@ set<string> HostManager::sampleFreshHosts(int count) const {
}

if (freshHostsWithHeight.empty()) {
//Logger::logStatus("HostManager::sampleFreshHosts No fresh hosts found. Falling back to fixed hosts.");
spdlog::debug("HostManager::sampleFreshHosts No fresh hosts found. Falling back to fixed hosts.");
return set<string>(fixedHosts.begin(), fixedHosts.end());
}

Expand All @@ -337,7 +337,7 @@ set<string> HostManager::sampleFreshHosts(int count) const {
return a.second > b.second;
});
if (!freshHostsWithHeight.empty()) {
Logger::logStatus("HostManager::sampleFreshHosts Top-synced host: " + freshHostsWithHeight[0].first + " with block height: " + std::to_string(freshHostsWithHeight[0].second));
spdlog::info("HostManager::sampleFreshHosts Top-synced host: {} with block height: {}", freshHostsWithHeight[0].first,std::to_string(freshHostsWithHeight[0].second));
}

int numToPick = min(count, (int)freshHostsWithHeight.size());
Expand Down Expand Up @@ -376,7 +376,7 @@ void HostManager::addPeer(string addr, uint64_t time, string version, string net

// add to our host list
if (this->whitelist.size() == 0 || this->whitelist.find(addr) != this->whitelist.end()){
Logger::logStatus("Added new peer: " + addr);
spdlog::info("Added new peer: {}", addr);
hosts.push_back(addr);
} else {
return;
Expand All @@ -399,7 +399,7 @@ void HostManager::addPeer(string addr, uint64_t time, string version, string net
try {
pingPeer(neighbor, addr, std::time(0), _version, networkName);
} catch(...) {
Logger::logStatus("Could not add peer " + addr + " to " + neighbor);
spdlog::info("Could not add peer " + addr + " to " + neighbor);
}
}));
}
Expand All @@ -425,7 +425,7 @@ bool HostManager::isDisabled() {
void HostManager::refreshHostList() {
if (this->hostSources.size() == 0) return;

Logger::logStatus("Finding peers...");
spdlog::info("Finding peers...");

set<string> fullHostList;

Expand Down Expand Up @@ -465,22 +465,22 @@ void HostManager::refreshHostList() {
if (auto hostInfo{getName(hostUrl)}; hostInfo.has_value()) {
auto v{extractHostVersion(*hostInfo)};
if (!v.has_value()){
Logger::logStatus(RED + "[ BAD_JSON ] " + RESET + hostUrl);
spdlog::error("[ BAD_JSON ] {}", hostUrl);
return;
}
std::string& version{*v};
if ((*hostInfo)["version"] < hm.minHostVersion) {
Logger::logStatus(RED + "[ DEPRECATED ] " + RESET + hostUrl);
spdlog::warn("[ DEPRECATED ] {}", hostUrl);
return;
}
std::unique_lock<std::mutex> ul(lock);
if (hm.whitelist.size() == 0 || hm.whitelist.find(hostUrl) != hm.whitelist.end()){
hm.hosts.push_back(hostUrl);
Logger::logStatus(GREEN + "[ CONNECTED ] " + RESET + hostUrl);
spdlog::info("[ CONNECTED ] {}", hostUrl);
hm.hostPingTimes[hostUrl] = std::time(0);
}
}else{
Logger::logStatus(RED + "[ UNREACHABLE ] " + RESET + hostUrl);
spdlog::warn("[ UNREACHABLE ] {}", hostUrl);
}
})
);
Expand Down
7 changes: 0 additions & 7 deletions src/core/logger.cpp

This file was deleted.

73 changes: 0 additions & 73 deletions src/core/logger.hpp

This file was deleted.

1 change: 0 additions & 1 deletion src/server/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <thread>
#include "../core/crypto.hpp"
#include "../core/transaction.hpp"
#include "../core/logger.hpp"
#include "block_store.hpp"
using namespace std;

Expand Down
Loading

0 comments on commit b1c53dd

Please sign in to comment.