Skip to content

Commit 664e7a1

Browse files
Switching from usage of libprotobuf internals to loguru, which has a similar interface.
1 parent 8c80034 commit 664e7a1

File tree

16 files changed

+95
-154
lines changed

16 files changed

+95
-154
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ project(ipfs_client
1313
)
1414

1515
set(TESTING ON CACHE BOOL "Do we want to run tests?")
16+
set(TEST_BY_DEFAULT OFF CACHE BOOL "If TESTING, do we want to run tests as a part of the default 'all' target.")
1617
set(CXX_VERSION 20 CACHE STRING "Should be no less than your conan profile.")
1718

1819
file(READ bld/protobuf_version.txt pb_ver)
@@ -24,7 +25,7 @@ dependency(protobuf VERSION ${pb_ver})
2425
dependency(absl)
2526
dependency(Boost)
2627
dependency(c-ares)
27-
dependency(glog)
28+
dependency(loguru)
2829
dependency(nlohmann_json)
2930
dependency(OpenSSL)
3031
if(TESTING)
@@ -138,7 +139,7 @@ foreach(libname ipfs_client ipfs_client_covered)
138139
absl::strings
139140
Boost::headers
140141
c-ares::cares
141-
glog::glog
142+
loguru::loguru
142143
nlohmann_json::nlohmann_json
143144
)
144145
endforeach()

conanfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class IpfsChromium(ConanFile):
4040
'boost/1.81.0',
4141
'bzip2/1.0.8',
4242
'c-ares/1.22.1',
43-
'glog/0.7.1',
4443
'nlohmann_json/3.11.2',
4544
'openssl/1.1.1w',
4645
f'protobuf/{protobuf_version()}',
@@ -51,7 +50,7 @@ class IpfsChromium(ConanFile):
5150
default_options = {
5251
"testing": False,
5352
"boost/*:bzip2": True,
54-
"boost/*:with_stacktrace_backtrace": True
53+
"boost/*:with_stacktrace_backtrace": True,
5554
}
5655
tool_requires = [
5756
'cmake/3.22.6',
@@ -99,6 +98,7 @@ def requirements(self):
9998
self.requires(lib, transitive_headers=True)
10099
if self.options.testing:
101100
self.requires('gtest/1.15.0')
101+
self.requires('loguru/cci.20230406', options={'enable_streams': 'True'})
102102

103103
@property
104104
def _min_cppstd(self):

include/ipfs_client/logger.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef IPFS_LOGGER_H_
22
#define IPFS_LOGGER_H_
33

4+
#include <functional>
45
#include <string>
56

67
namespace ipfs::log {
@@ -15,19 +16,14 @@ enum class Level {
1516
Off
1617
};
1718

19+
Level GetLevel();
1820
void SetLevel(Level);
1921

20-
using Handler = void (*)(std::string const&, char const*, int, Level);
21-
void SetHandler(Handler) noexcept;
22-
23-
void DefaultHandler(std::string const& message,
24-
char const* source_file,
25-
int source_line,
26-
Level for_prefix);
27-
2822
std::string_view LevelDescriptor(Level);
2923

30-
bool IsInitialized() noexcept;
24+
using Hook = std::function<void(std::string_view, std::string_view, int, Level)>;
25+
void AddHook(std::string id, Hook);
26+
void Unhook(std::string id);
3127

3228
} // namespace ipfs::log
3329

include/ipfs_client/opinionated_context.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,14 @@
1515

1616
#define HAS_OPINIONATED_CONTEXT 1
1717

18-
#include <google/protobuf/stubs/logging.h>
19-
2018
namespace boost::asio {
2119
class io_context;
2220
}
2321

24-
#pragma GCC diagnostic push
25-
#pragma GCC diagnostic ignored "-Wunused-variable"
26-
namespace google::protobuf {
27-
namespace {
28-
LogLevel LOGLEVEL_DEBUG = static_cast<LogLevel>(-1);
29-
LogLevel LOGLEVEL_TRACE = static_cast<LogLevel>(-2);
30-
}
31-
} // namespace google::protobuf
32-
#pragma GCC diagnostic pop
33-
3422
class HttpSession;
3523

3624
namespace ipfs {
3725

38-
// std::pair<std::shared_ptr<ContextApi>, std::shared_ptr<Partition>>
3926
std::shared_ptr<Client> start_default(boost::asio::io_context& io);
4027

4128
} // namespace ipfs

src/ipfs_client/client.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ auto Self::json() -> ctx::JsonParser& {
130130
LOG(FATAL) << "A JSON parser must be provided.";
131131
#endif
132132
}
133-
DCHECK(json_parser_);
133+
DCHECK(!!json_parser_);
134134
return *json_parser_;
135135
}
136136
auto Self::cbor() -> ctx::CborParser& {
@@ -142,7 +142,7 @@ auto Self::cbor() -> ctx::CborParser& {
142142
cbor_parser_ = std::make_unique<ctx::NullCborParser>();
143143
#endif
144144
}
145-
DCHECK(cbor_parser_);
145+
DCHECK(!!cbor_parser_);
146146
return *cbor_parser_;
147147
}
148148
auto Self::requestor() -> std::shared_ptr<gw::Requestor> {

src/ipfs_client/ctx/null_dns_txt_lookup.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ using Self = ipfs::ctx::NullDnsTxtLookup;
88
void Self::SendDnsTextRequest(std::string /*hostname*/,
99
DnsTextResultsCallback /*unused*/,
1010
DnsTextCompleteCallback callback) {
11-
LOG(ERROR)
12-
<< "DNS TXT lookup provider not instantiated. DNSLink will not work.";
11+
LOG(ERROR) << "DNS TXT lookup provider not instantiated. DNSLink will not work.";
1312
callback();
1413
}

src/ipfs_client/gw/gateway_request.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ auto Self::RespondSuccessfully(std::string_view bytes,
278278
}
279279
} break;
280280
case GatewayRequestType::Car: {
281-
DCHECK(api);
281+
DCHECK(!!api);
282282
Car car(as_bytes(bytes), api->cbor());
283283
AddBlocks(car, api, success, valid, src);
284284
break;
@@ -369,7 +369,7 @@ void Self::AddBlock(std::string_view bytes,
369369
success = false;
370370
return;
371371
}
372-
DCHECK(api);
372+
DCHECK(!!api);
373373
auto node = ipld::DagNode::fromBytes(api, cid().value(), bytes);
374374
if (!node) {
375375
success = false;
@@ -405,7 +405,7 @@ void Self::AddBlocks(Car& car, const std::shared_ptr<Client>& api, bool& success
405405
}
406406
auto Self::IpnsResponse(ByteView bytes, std::shared_ptr<Client> const& api, bool& success, bool* valid, ipld::BlockSource src) -> bool {
407407
if (cid().has_value()) {
408-
DCHECK(api);
408+
DCHECK(!!api);
409409
auto rec = ipfs::ValidateIpnsRecord(bytes, cid().value(), *api);
410410
if (rec.has_value()) {
411411
ValidatedIpns const validated{rec.value()};

src/ipfs_client/gw/gateway_state.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ void Self::timed_out() {
106106
}
107107
// NOLINTEND(readability-magic-numbers)
108108
auto Self::cfg() -> ctx::GatewayConfig& {
109-
DCHECK(api_);
109+
DCHECK(!!api_);
110110
return api_->gw_cfg();
111111
}
112112
auto Self::cfg() const -> ctx::GatewayConfig const& {
113-
DCHECK(api_);
113+
DCHECK(!!api_);
114114
return api_->gw_cfg();
115115
}

src/ipfs_client/ipns_names_unittest.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#include <ipfs_client/ipns_names.h>
22

3-
#include <google/protobuf/stubs/logging.h>
43
#include <gtest/gtest.h>
54

65
namespace {
76
struct IpnsNamesTest : public ::testing::Test {
87
IpnsNamesTest();
98
ipfs::IpnsNames tested;
10-
google::protobuf::LogSilencer no_log;
9+
// google::protobuf::LogSilencer no_log;
1110
void fake(std::string n, std::string t);
1211
};
1312
} // namespace

src/ipfs_client/ipns_record_unittest.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ struct Api final : public i::Client {
7474
void AddGateway(std::string_view) {}
7575
virtual ~Api() noexcept {}
7676
};
77-
std::vector<std::string> log_lines;
78-
std::string all_log;
79-
void save_log(std::string const& msg, char const*, int, il::Level) {
80-
log_lines.push_back(msg);
81-
all_log.append(msg).push_back(';');
82-
}
8377
} // namespace
8478

8579
TEST(IpnsRecordTest, AKnownKuboRecord) {
@@ -432,13 +426,17 @@ TEST(IpnsRecordTest, ValueMismatch) {
432426
auto real_validates = ipfs::ValidateIpnsRecord(i::as_bytes(xxd), cid, api);
433427
EXPECT_TRUE(real_validates);
434428
xxd[12]++;//cause a mismatch
435-
il::SetLevel(il::Level::Error);
436-
il::SetHandler(save_log);
429+
//il::SetLevel(il::Level::Error);
430+
//il::SetHandler(save_log);
431+
LogRecorder rec;
437432
auto modified_validates = ipfs::ValidateIpnsRecord(i::as_bytes(xxd), cid, api);
438-
EXPECT_FALSE(modified_validates) << all_log;
439-
il::SetHandler(il::DefaultHandler);
440-
EXPECT_EQ(log_lines.size(),1UL) << all_log;
441-
auto& log_line = log_lines.at(0);
442-
EXPECT_LT(log_line.find("Mismatch on Value"),log_line.size()) << all_log;
433+
EXPECT_FALSE(modified_validates);
434+
//il::SetHandler(il::DefaultHandler);
435+
//EXPECT_EQ(log_lines.size(),1UL);
436+
//auto& log_line = log_lines.at(0);
437+
//EXPECT_LT(log_line.find("Mismatch on Value"),log_line.size()) << all_log;
438+
auto is_expected = [](auto&m){return m.message.find("Mismatch on Value") < m.message.size();};
439+
auto expected_log = std::count_if(rec.messages.begin(), rec.messages.end(), is_expected);
440+
EXPECT_EQ(expected_log, 1);
443441
}
444442
#endif

0 commit comments

Comments
 (0)