Skip to content

Commit 6143c23

Browse files
committed
refactor(log): replace utility/log with imp/log in ipc.cpp
- Replace include "libipc/utility/log.h" with "libipc/imp/log.h" - Add LIBIPC_LOG() at the beginning of functions that use logging - Replace all ipc::error() calls with log.error() - Replace all ipc::log() calls with log.debug() or log.error() based on context - Modified functions: - cc_acc(): error logging for shm acquire failure - make_handle(): error logging for chunk storage operations - find_storage(): error logging for invalid storage id - release_storage(): error logging for invalid storage id - recycle_storage(): error logging for invalid storage id - clear_message(): error logging for invalid message size - send(): error logging for various send failures, debug logging for force_push - recv(): error logging for various recv failures - Use type-safe streaming interface instead of printf-style formatting - Remove manual newline characters from log messages - Total changes: 19 log call sites updated
1 parent 309baf7 commit 6143c23

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/libipc/ipc.cpp

Lines changed: 28 additions & 20 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,10 +218,11 @@ 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;
@@ -240,7 +242,7 @@ auto& chunk_storages() {
240242
}
241243
auto *info = static_cast<chunk_info_t*>(h->get());
242244
if (info == nullptr) {
243-
ipc::error("[chunk_storages] chunk_shm.id_info_.get failed: chunk_size = %zd\n", chunk_size);
245+
log.error("[chunk_storages] chunk_shm.id_info_.get failed: chunk_size = ", chunk_size);
244246
return nullptr;
245247
}
246248
return info;
@@ -290,8 +292,9 @@ std::pair<ipc::storage_id_t, void*> acquire_storage(conn_info_head *inf, std::si
290292
}
291293

292294
void *find_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size) {
295+
LIBIPC_LOG();
293296
if (id < 0) {
294-
ipc::error("[find_storage] id is invalid: id = %ld, size = %zd\n", (long)id, size);
297+
log.error("[find_storage] id is invalid: id = ", (long)id, ", size = ", size);
295298
return nullptr;
296299
}
297300
std::size_t chunk_size = calc_chunk_size(size);
@@ -301,8 +304,9 @@ void *find_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size)
301304
}
302305

303306
void release_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size) {
307+
LIBIPC_LOG();
304308
if (id < 0) {
305-
ipc::error("[release_storage] id is invalid: id = %ld, size = %zd\n", (long)id, size);
309+
log.error("[release_storage] id is invalid: id = ", (long)id, ", size = ", size);
306310
return;
307311
}
308312
std::size_t chunk_size = calc_chunk_size(size);
@@ -334,8 +338,9 @@ bool sub_rc(ipc::wr<Rp, Rc, ipc::trans::broadcast>,
334338

335339
template <typename Flag>
336340
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) {
341+
LIBIPC_LOG();
337342
if (id < 0) {
338-
ipc::error("[recycle_storage] id is invalid: id = %ld, size = %zd\n", (long)id, size);
343+
log.error("[recycle_storage] id is invalid: id = ", (long)id, ", size = ", size);
339344
return;
340345
}
341346
std::size_t chunk_size = calc_chunk_size(size);
@@ -355,11 +360,12 @@ void recycle_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size
355360

356361
template <typename MsgT>
357362
bool clear_message(conn_info_head *inf, void* p) {
363+
LIBIPC_LOG();
358364
auto msg = static_cast<MsgT*>(p);
359365
if (msg->storage_) {
360366
std::int32_t r_size = static_cast<std::int32_t>(ipc::data_length) + msg->remain_;
361367
if (r_size <= 0) {
362-
ipc::error("[clear_message] invalid msg size: %d\n", (int)r_size);
368+
log.error("[clear_message] invalid msg size: ", (int)r_size);
363369
return true;
364370
}
365371
release_storage(*reinterpret_cast<ipc::storage_id_t*>(&msg->data_),
@@ -518,33 +524,34 @@ static bool wait_for_recv(ipc::handle_t h, std::size_t r_count, std::uint64_t tm
518524

519525
template <typename F>
520526
static bool send(F&& gen_push, ipc::handle_t h, void const * data, std::size_t size) {
527+
LIBIPC_LOG();
521528
if (data == nullptr || size == 0) {
522-
ipc::error("fail: send(%p, %zd)\n", data, size);
529+
log.error("fail: send(", data, ", ", size, ")");
523530
return false;
524531
}
525532
auto que = queue_of(h);
526533
if (que == nullptr) {
527-
ipc::error("fail: send, queue_of(h) == nullptr\n");
534+
log.error("fail: send, queue_of(h) == nullptr");
528535
return false;
529536
}
530537
if (que->elems() == nullptr) {
531-
ipc::error("fail: send, queue_of(h)->elems() == nullptr\n");
538+
log.error("fail: send, queue_of(h)->elems() == nullptr");
532539
return false;
533540
}
534541
if (!que->ready_sending()) {
535-
ipc::error("fail: send, que->ready_sending() == false\n");
542+
log.error("fail: send, que->ready_sending() == false");
536543
return false;
537544
}
538545
ipc::circ::cc_t conns = que->elems()->connections(std::memory_order_relaxed);
539546
if (conns == 0) {
540-
ipc::error("fail: send, there is no receiver on this connection.\n");
547+
log.error("fail: send, there is no receiver on this connection.");
541548
return false;
542549
}
543550
// calc a new message id
544551
conn_info_t *inf = info_of(h);
545552
auto acc = inf->acc();
546553
if (acc == nullptr) {
547-
ipc::error("fail: send, info_of(h)->acc() == nullptr\n");
554+
log.error("fail: send, info_of(h)->acc() == nullptr");
548555
return false;
549556
}
550557
auto msg_id = acc->fetch_add(1, std::memory_order_relaxed);
@@ -558,7 +565,7 @@ static bool send(F&& gen_push, ipc::handle_t h, void const * data, std::size_t s
558565
static_cast<std::int32_t>(ipc::data_length), &(dat.first), 0);
559566
}
560567
// try using message fragment
561-
//ipc::log("fail: shm::handle for big message. msg_id: %zd, size: %zd\n", msg_id, size);
568+
//log.debug("fail: shm::handle for big message. msg_id: ", msg_id, ", size: ", size);
562569
}
563570
// push message fragment
564571
std::int32_t offset = 0;
@@ -588,7 +595,7 @@ static bool send(ipc::handle_t h, void const * data, std::size_t size, std::uint
588595
[](void*) { return true; },
589596
info->cc_id_, msg_id, remain, data, size);
590597
}, tm)) {
591-
ipc::log("force_push: msg_id = %zd, remain = %d, size = %zd\n", msg_id, remain, size);
598+
log.debug("force_push: msg_id = ", msg_id, ", remain = ", remain, ", size = ", size);
592599
if (!que->force_push(
593600
[info](void* p) { return clear_message<typename queue_t::value_t>(info, p); },
594601
info->cc_id_, msg_id, remain, data, size)) {
@@ -618,9 +625,10 @@ static bool try_send(ipc::handle_t h, void const * data, std::size_t size, std::
618625
}
619626

620627
static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
628+
LIBIPC_LOG();
621629
auto que = queue_of(h);
622630
if (que == nullptr) {
623-
ipc::error("fail: recv, queue_of(h) == nullptr\n");
631+
log.error("fail: recv, queue_of(h) == nullptr");
624632
return {};
625633
}
626634
if (!que->connected()) {
@@ -648,7 +656,7 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
648656
// msg.remain_ may minus & abs(msg.remain_) < data_length
649657
std::int32_t r_size = static_cast<std::int32_t>(ipc::data_length) + msg.remain_;
650658
if (r_size <= 0) {
651-
ipc::error("fail: recv, r_size = %d\n", (int)r_size);
659+
log.error("fail: recv, r_size = ", (int)r_size);
652660
return {};
653661
}
654662
std::size_t msg_size = static_cast<std::size_t>(r_size);
@@ -669,7 +677,7 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
669677
que->connected_id()
670678
});
671679
if (r_info == nullptr) {
672-
ipc::log("fail: ipc::mem::$new<recycle_t>.\n");
680+
log.error("fail: ipc::mem::$new<recycle_t>.");
673681
return ipc::buff_t{buf, msg_size}; // no recycle
674682
} else {
675683
return ipc::buff_t{buf, msg_size, [](void* p_info, std::size_t size) {
@@ -685,7 +693,7 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
685693
}, r_info};
686694
}
687695
} 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);
696+
log.error("fail: shm::handle for large message. msg_id: ", msg.id_, ", buf_id: ", buf_id, ", size: ", msg_size);
689697
continue;
690698
}
691699
}

0 commit comments

Comments
 (0)