Skip to content

Commit 3269bde

Browse files
authored
Merge pull request #166 from mutouyun/feature/imp-log
refactor(log): Replace utility/log with imp/log interface
2 parents 78bbb05 + ab8e6c7 commit 3269bde

File tree

21 files changed

+390
-334
lines changed

21 files changed

+390
-334
lines changed

src/libipc/ipc.cpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "libipc/rw_lock.h"
2020
#include "libipc/waiter.h"
2121

22-
#include "libipc/utility/log.h"
22+
#include "libipc/imp/log.h"
2323
#include "libipc/utility/id_pool.h"
2424
#include "libipc/utility/scope_guard.h"
2525
#include "libipc/utility/utility.h"
@@ -76,6 +76,7 @@ ipc::buff_t make_cache(T &data, std::size_t size) {
7676
}
7777

7878
acc_t *cc_acc(std::string const &pref) {
79+
LIBIPC_LOG();
7980
static auto *phs = new ipc::unordered_map<std::string, ipc::shm::handle>; // no delete
8081
static std::mutex lock;
8182
std::lock_guard<std::mutex> guard {lock};
@@ -84,7 +85,7 @@ acc_t *cc_acc(std::string const &pref) {
8485
std::string shm_name {ipc::make_prefix(pref, "CA_CONN__")};
8586
ipc::shm::handle h;
8687
if (!h.acquire(shm_name.c_str(), sizeof(acc_t))) {
87-
ipc::error("[cc_acc] acquire failed: %s\n", shm_name.c_str());
88+
log.error("[cc_acc] acquire failed: ", shm_name);
8889
return nullptr;
8990
}
9091
it = phs->emplace(pref, std::move(h)).first;
@@ -217,17 +218,19 @@ auto& chunk_storages() {
217218
std::mutex lock_;
218219

219220
static bool make_handle(ipc::shm::handle &h, std::string const &shm_name, std::size_t chunk_size) {
221+
LIBIPC_LOG();
220222
if (!h.valid() &&
221223
!h.acquire( shm_name.c_str(),
222224
sizeof(chunk_info_t) + chunk_info_t::chunks_mem_size(chunk_size) )) {
223-
ipc::error("[chunk_storages] chunk_shm.id_info_.acquire failed: chunk_size = %zd\n", chunk_size);
225+
log.error("[chunk_storages] chunk_shm.id_info_.acquire failed: chunk_size = ", chunk_size);
224226
return false;
225227
}
226228
return true;
227229
}
228230

229231
public:
230232
chunk_info_t *get_info(conn_info_head *inf, std::size_t chunk_size) {
233+
LIBIPC_LOG();
231234
std::string pref {(inf == nullptr) ? std::string{} : inf->prefix_};
232235
std::string shm_name {ipc::make_prefix(pref, "CHUNK_INFO__", chunk_size)};
233236
ipc::shm::handle *h;
@@ -240,7 +243,7 @@ auto& chunk_storages() {
240243
}
241244
auto *info = static_cast<chunk_info_t*>(h->get());
242245
if (info == nullptr) {
243-
ipc::error("[chunk_storages] chunk_shm.id_info_.get failed: chunk_size = %zd\n", chunk_size);
246+
log.error("[chunk_storages] chunk_shm.id_info_.get failed: chunk_size = ", chunk_size);
244247
return nullptr;
245248
}
246249
return info;
@@ -290,8 +293,9 @@ std::pair<ipc::storage_id_t, void*> acquire_storage(conn_info_head *inf, std::si
290293
}
291294

292295
void *find_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size) {
296+
LIBIPC_LOG();
293297
if (id < 0) {
294-
ipc::error("[find_storage] id is invalid: id = %ld, size = %zd\n", (long)id, size);
298+
log.error("[find_storage] id is invalid: id = ", (long)id, ", size = ", size);
295299
return nullptr;
296300
}
297301
std::size_t chunk_size = calc_chunk_size(size);
@@ -301,8 +305,9 @@ void *find_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size)
301305
}
302306

303307
void release_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size) {
308+
LIBIPC_LOG();
304309
if (id < 0) {
305-
ipc::error("[release_storage] id is invalid: id = %ld, size = %zd\n", (long)id, size);
310+
log.error("[release_storage] id is invalid: id = ", (long)id, ", size = ", size);
306311
return;
307312
}
308313
std::size_t chunk_size = calc_chunk_size(size);
@@ -334,8 +339,9 @@ bool sub_rc(ipc::wr<Rp, Rc, ipc::trans::broadcast>,
334339

335340
template <typename Flag>
336341
void recycle_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size, ipc::circ::cc_t curr_conns, ipc::circ::cc_t conn_id) {
342+
LIBIPC_LOG();
337343
if (id < 0) {
338-
ipc::error("[recycle_storage] id is invalid: id = %ld, size = %zd\n", (long)id, size);
344+
log.error("[recycle_storage] id is invalid: id = ", (long)id, ", size = ", size);
339345
return;
340346
}
341347
std::size_t chunk_size = calc_chunk_size(size);
@@ -355,11 +361,12 @@ void recycle_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size
355361

356362
template <typename MsgT>
357363
bool clear_message(conn_info_head *inf, void* p) {
364+
LIBIPC_LOG();
358365
auto msg = static_cast<MsgT*>(p);
359366
if (msg->storage_) {
360367
std::int32_t r_size = static_cast<std::int32_t>(ipc::data_length) + msg->remain_;
361368
if (r_size <= 0) {
362-
ipc::error("[clear_message] invalid msg size: %d\n", (int)r_size);
369+
log.error("[clear_message] invalid msg size: ", (int)r_size);
363370
return true;
364371
}
365372
release_storage(*reinterpret_cast<ipc::storage_id_t*>(&msg->data_),
@@ -518,33 +525,34 @@ static bool wait_for_recv(ipc::handle_t h, std::size_t r_count, std::uint64_t tm
518525

519526
template <typename F>
520527
static bool send(F&& gen_push, ipc::handle_t h, void const * data, std::size_t size) {
528+
LIBIPC_LOG();
521529
if (data == nullptr || size == 0) {
522-
ipc::error("fail: send(%p, %zd)\n", data, size);
530+
log.error("fail: send(", data, ", ", size, ")");
523531
return false;
524532
}
525533
auto que = queue_of(h);
526534
if (que == nullptr) {
527-
ipc::error("fail: send, queue_of(h) == nullptr\n");
535+
log.error("fail: send, queue_of(h) == nullptr");
528536
return false;
529537
}
530538
if (que->elems() == nullptr) {
531-
ipc::error("fail: send, queue_of(h)->elems() == nullptr\n");
539+
log.error("fail: send, queue_of(h)->elems() == nullptr");
532540
return false;
533541
}
534542
if (!que->ready_sending()) {
535-
ipc::error("fail: send, que->ready_sending() == false\n");
543+
log.error("fail: send, que->ready_sending() == false");
536544
return false;
537545
}
538546
ipc::circ::cc_t conns = que->elems()->connections(std::memory_order_relaxed);
539547
if (conns == 0) {
540-
ipc::error("fail: send, there is no receiver on this connection.\n");
548+
log.error("fail: send, there is no receiver on this connection.");
541549
return false;
542550
}
543551
// calc a new message id
544552
conn_info_t *inf = info_of(h);
545553
auto acc = inf->acc();
546554
if (acc == nullptr) {
547-
ipc::error("fail: send, info_of(h)->acc() == nullptr\n");
555+
log.error("fail: send, info_of(h)->acc() == nullptr");
548556
return false;
549557
}
550558
auto msg_id = acc->fetch_add(1, std::memory_order_relaxed);
@@ -558,7 +566,7 @@ static bool send(F&& gen_push, ipc::handle_t h, void const * data, std::size_t s
558566
static_cast<std::int32_t>(ipc::data_length), &(dat.first), 0);
559567
}
560568
// try using message fragment
561-
//ipc::log("fail: shm::handle for big message. msg_id: %zd, size: %zd\n", msg_id, size);
569+
//log.debug("fail: shm::handle for big message. msg_id: ", msg_id, ", size: ", size);
562570
}
563571
// push message fragment
564572
std::int32_t offset = 0;
@@ -581,14 +589,15 @@ static bool send(F&& gen_push, ipc::handle_t h, void const * data, std::size_t s
581589
}
582590

583591
static bool send(ipc::handle_t h, void const * data, std::size_t size, std::uint64_t tm) {
584-
return send([tm](auto *info, auto *que, auto msg_id) {
585-
return [tm, info, que, msg_id](std::int32_t remain, void const * data, std::size_t size) {
592+
LIBIPC_LOG();
593+
return send([tm, &log](auto *info, auto *que, auto msg_id) {
594+
return [tm, &log, info, que, msg_id](std::int32_t remain, void const * data, std::size_t size) {
586595
if (!wait_for(info->wt_waiter_, [&] {
587596
return !que->push(
588597
[](void*) { return true; },
589598
info->cc_id_, msg_id, remain, data, size);
590599
}, tm)) {
591-
ipc::log("force_push: msg_id = %zd, remain = %d, size = %zd\n", msg_id, remain, size);
600+
log.debug("force_push: msg_id = ", msg_id, ", remain = ", remain, ", size = ", size);
592601
if (!que->force_push(
593602
[info](void* p) { return clear_message<typename queue_t::value_t>(info, p); },
594603
info->cc_id_, msg_id, remain, data, size)) {
@@ -618,9 +627,10 @@ static bool try_send(ipc::handle_t h, void const * data, std::size_t size, std::
618627
}
619628

620629
static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
630+
LIBIPC_LOG();
621631
auto que = queue_of(h);
622632
if (que == nullptr) {
623-
ipc::error("fail: recv, queue_of(h) == nullptr\n");
633+
log.error("fail: recv, queue_of(h) == nullptr");
624634
return {};
625635
}
626636
if (!que->connected()) {
@@ -648,7 +658,7 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
648658
// msg.remain_ may minus & abs(msg.remain_) < data_length
649659
std::int32_t r_size = static_cast<std::int32_t>(ipc::data_length) + msg.remain_;
650660
if (r_size <= 0) {
651-
ipc::error("fail: recv, r_size = %d\n", (int)r_size);
661+
log.error("fail: recv, r_size = ", (int)r_size);
652662
return {};
653663
}
654664
std::size_t msg_size = static_cast<std::size_t>(r_size);
@@ -669,7 +679,7 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
669679
que->connected_id()
670680
});
671681
if (r_info == nullptr) {
672-
ipc::log("fail: ipc::mem::$new<recycle_t>.\n");
682+
log.error("fail: ipc::mem::$new<recycle_t>.");
673683
return ipc::buff_t{buf, msg_size}; // no recycle
674684
} else {
675685
return ipc::buff_t{buf, msg_size, [](void* p_info, std::size_t size) {
@@ -685,7 +695,7 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
685695
}, r_info};
686696
}
687697
} else {
688-
ipc::log("fail: shm::handle for large message. msg_id: %zd, buf_id: %zd, size: %zd\n", msg.id_, buf_id, msg_size);
698+
log.error("fail: shm::handle for large message. msg_id: ", msg.id_, ", buf_id: ", buf_id, ", size: ", msg_size);
689699
continue;
690700
}
691701
}

src/libipc/platform/linux/condition.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "libipc/utility/log.h"
3+
#include "libipc/imp/log.h"
44
#include "libipc/mutex.h"
55

66
#include "get_wait_time.h"
@@ -19,20 +19,20 @@ class condition : public sync::obj_impl<a0_cnd_t> {
1919
~condition() = default;
2020

2121
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
22+
LIBIPC_LOG();
2223
if (!valid()) return false;
2324
if (tm == invalid_value) {
2425
int eno = A0_SYSERR(a0_cnd_wait(native(), static_cast<a0_mtx_t *>(mtx.native())));
2526
if (eno != 0) {
26-
ipc::error("fail condition wait[%d]\n", eno);
27+
log.error("fail condition wait[", eno, "]");
2728
return false;
2829
}
2930
} else {
3031
auto ts = linux_::detail::make_timespec(tm);
3132
int eno = A0_SYSERR(a0_cnd_timedwait(native(), static_cast<a0_mtx_t *>(mtx.native()), {ts}));
3233
if (eno != 0) {
3334
if (eno != ETIMEDOUT) {
34-
ipc::error("fail condition timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
35-
eno, tm, ts.tv_sec, ts.tv_nsec);
35+
log.error("fail condition timedwait[", eno, "]: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec);
3636
}
3737
return false;
3838
}
@@ -41,20 +41,22 @@ class condition : public sync::obj_impl<a0_cnd_t> {
4141
}
4242

4343
bool notify(ipc::sync::mutex &mtx) noexcept {
44+
LIBIPC_LOG();
4445
if (!valid()) return false;
4546
int eno = A0_SYSERR(a0_cnd_signal(native(), static_cast<a0_mtx_t *>(mtx.native())));
4647
if (eno != 0) {
47-
ipc::error("fail condition notify[%d]\n", eno);
48+
log.error("fail condition notify[", eno, "]");
4849
return false;
4950
}
5051
return true;
5152
}
5253

5354
bool broadcast(ipc::sync::mutex &mtx) noexcept {
55+
LIBIPC_LOG();
5456
if (!valid()) return false;
5557
int eno = A0_SYSERR(a0_cnd_broadcast(native(), static_cast<a0_mtx_t *>(mtx.native())));
5658
if (eno != 0) {
57-
ipc::error("fail condition broadcast[%d]\n", eno);
59+
log.error("fail condition broadcast[", eno, "]");
5860
return false;
5961
}
6062
return true;

src/libipc/platform/linux/get_wait_time.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <cinttypes>
55
#include <system_error>
66

7-
#include "libipc/utility/log.h"
7+
#include "libipc/imp/log.h"
88

99
#include "a0/time.h"
1010
#include "a0/err_macro.h"
@@ -14,30 +14,31 @@ namespace linux_ {
1414
namespace detail {
1515

1616
inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept {
17+
LIBIPC_LOG();
1718
std::int64_t add_ns = static_cast<std::int64_t>(tm * 1000000ull);
1819
if (add_ns < 0) {
19-
ipc::error("invalid time = " PRIu64 "\n", tm);
20+
log.error("invalid time = ", tm);
2021
return false;
2122
}
2223
a0_time_mono_t now;
2324
int eno = A0_SYSERR(a0_time_mono_now(&now));
2425
if (eno != 0) {
25-
ipc::error("fail get time[%d]\n", eno);
26+
log.error("fail get time[", eno, "]");
2627
return false;
2728
}
2829
a0_time_mono_t *target = reinterpret_cast<a0_time_mono_t *>(&ts);
2930
if ((eno = A0_SYSERR(a0_time_mono_add(now, add_ns, target))) != 0) {
30-
ipc::error("fail get time[%d]\n", eno);
31+
log.error("fail get time[", eno, "]");
3132
return false;
3233
}
3334
return true;
3435
}
3536

3637
inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) {
38+
LIBIPC_LOG();
3739
timespec ts {};
3840
if (!calc_wait_time(ts, tm)) {
39-
ipc::error("fail calc_wait_time: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
40-
tm, ts.tv_sec, ts.tv_nsec);
41+
log.error("fail calc_wait_time: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec);
4142
throw std::system_error{static_cast<int>(errno), std::system_category()};
4243
}
4344
return ts;

src/libipc/platform/linux/mutex.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <atomic>
77

88
#include "libipc/platform/detail.h"
9-
#include "libipc/utility/log.h"
9+
#include "libipc/imp/log.h"
1010
#include "libipc/mem/resource.h"
1111
#include "libipc/shm.h"
1212

@@ -23,6 +23,7 @@ namespace sync {
2323
class robust_mutex : public sync::obj_impl<a0_mtx_t> {
2424
public:
2525
bool lock(std::uint64_t tm) noexcept {
26+
LIBIPC_LOG();
2627
if (!valid()) return false;
2728
for (;;) {
2829
auto ts = linux_::detail::make_timespec(tm);
@@ -37,24 +38,25 @@ class robust_mutex : public sync::obj_impl<a0_mtx_t> {
3738
case EOWNERDEAD: {
3839
int eno2 = A0_SYSERR(a0_mtx_consistent(native()));
3940
if (eno2 != 0) {
40-
ipc::error("fail mutex lock[%d] -> consistent[%d]\n", eno, eno2);
41+
log.error("fail mutex lock[", eno, "] -> consistent[", eno2, "]");
4142
return false;
4243
}
4344
int eno3 = A0_SYSERR(a0_mtx_unlock(native()));
4445
if (eno3 != 0) {
45-
ipc::error("fail mutex lock[%d] -> unlock[%d]\n", eno, eno3);
46+
log.error("fail mutex lock[", eno, "] -> unlock[", eno3, "]");
4647
return false;
4748
}
4849
}
4950
break; // loop again
5051
default:
51-
ipc::error("fail mutex lock[%d]\n", eno);
52+
log.error("fail mutex lock[", eno, "]");
5253
return false;
5354
}
5455
}
5556
}
5657

5758
bool try_lock() noexcept(false) {
59+
LIBIPC_LOG();
5860
if (!valid()) return false;
5961
int eno = A0_SYSERR(a0_mtx_timedlock(native(), {linux_::detail::make_timespec(0)}));
6062
switch (eno) {
@@ -65,28 +67,29 @@ class robust_mutex : public sync::obj_impl<a0_mtx_t> {
6567
case EOWNERDEAD: {
6668
int eno2 = A0_SYSERR(a0_mtx_consistent(native()));
6769
if (eno2 != 0) {
68-
ipc::error("fail mutex try_lock[%d] -> consistent[%d]\n", eno, eno2);
70+
log.error("fail mutex try_lock[", eno, "] -> consistent[", eno2, "]");
6971
break;
7072
}
7173
int eno3 = A0_SYSERR(a0_mtx_unlock(native()));
7274
if (eno3 != 0) {
73-
ipc::error("fail mutex try_lock[%d] -> unlock[%d]\n", eno, eno3);
75+
log.error("fail mutex try_lock[", eno, "] -> unlock[", eno3, "]");
7476
break;
7577
}
7678
}
7779
break;
7880
default:
79-
ipc::error("fail mutex try_lock[%d]\n", eno);
81+
log.error("fail mutex try_lock[", eno, "]");
8082
break;
8183
}
8284
throw std::system_error{eno, std::system_category()};
8385
}
8486

8587
bool unlock() noexcept {
88+
LIBIPC_LOG();
8689
if (!valid()) return false;
8790
int eno = A0_SYSERR(a0_mtx_unlock(native()));
8891
if (eno != 0) {
89-
ipc::error("fail mutex unlock[%d]\n", eno);
92+
log.error("fail mutex unlock[", eno, "]");
9093
return false;
9194
}
9295
return true;

0 commit comments

Comments
 (0)