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
7878acc_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
292295void *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
303307void 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
335340template <typename Flag>
336341void 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
356362template <typename MsgT>
357363bool 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
519526template <typename F>
520527static 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
583591static 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
620629static 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 }
0 commit comments