Skip to content

Commit e9a7dba

Browse files
committed
refactor(log): replace utility/log with imp/log in all platform and sync files
- Replace include "libipc/utility/log.h" with "libipc/imp/log.h" in all files - Add LIBIPC_LOG() to functions that use logging - Replace ipc::error() and ipc::log() calls with log.error() and log.debug() - Use type-safe streaming interface instead of printf-style formatting - Remove manual newline characters from log messages Modified files: - Linux platform: condition.h, get_wait_time.h, mutex.h, sync_obj_impl.h - POSIX platform: condition.h, get_wait_time.h, mutex.h, semaphore_impl.h, shm_posix.cpp - Windows platform: condition.h, get_sa.h, mutex.h, semaphore.h, shm_win.cpp - Sync layer: condition.cpp, mutex.cpp, semaphore.cpp Total: 17 files updated with comprehensive log interface migration
1 parent 6143c23 commit e9a7dba

17 files changed

Lines changed: 282 additions & 272 deletions

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;

src/libipc/platform/linux/sync_obj_impl.h

Lines changed: 3 additions & 2 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/shm.h"
55

66
#include "a0/empty.h"
@@ -19,8 +19,9 @@ class obj_impl {
1919
sync_t *h_ = nullptr;
2020

2121
sync_t *acquire_handle(char const *name) {
22+
LIBIPC_LOG();
2223
if (!shm_.acquire(name, sizeof(sync_t))) {
23-
ipc::error("[acquire_handle] fail shm.acquire: %s\n", name);
24+
log.error("[acquire_handle] fail shm.acquire: ", name);
2425
return nullptr;
2526
}
2627
return static_cast<sync_t *>(shm_.get());

src/libipc/platform/posix/condition.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <pthread.h>
77

8-
#include "libipc/utility/log.h"
8+
#include "libipc/imp/log.h"
99
#include "libipc/utility/scope_guard.h"
1010
#include "libipc/mutex.h"
1111
#include "libipc/shm.h"
@@ -21,8 +21,9 @@ class condition {
2121
pthread_cond_t *cond_ = nullptr;
2222

2323
pthread_cond_t *acquire_cond(char const *name) {
24+
LIBIPC_LOG();
2425
if (!shm_.acquire(name, sizeof(pthread_cond_t))) {
25-
ipc::error("[acquire_cond] fail shm.acquire: %s\n", name);
26+
log.error("[acquire_cond] fail shm.acquire: %s", name);
2627
return nullptr;
2728
}
2829
return static_cast<pthread_cond_t *>(shm_.get());
@@ -47,6 +48,7 @@ class condition {
4748
}
4849

4950
bool open(char const *name) noexcept {
51+
LIBIPC_LOG();
5052
close();
5153
if ((cond_ = acquire_cond(name)) == nullptr) {
5254
return false;
@@ -60,28 +62,29 @@ class condition {
6062
int eno;
6163
pthread_condattr_t cond_attr;
6264
if ((eno = ::pthread_condattr_init(&cond_attr)) != 0) {
63-
ipc::error("fail pthread_condattr_init[%d]\n", eno);
65+
log.error("fail pthread_condattr_init[%d]", eno);
6466
return false;
6567
}
6668
LIBIPC_UNUSED auto guard_cond_attr = guard([&cond_attr] { ::pthread_condattr_destroy(&cond_attr); });
6769
if ((eno = ::pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED)) != 0) {
68-
ipc::error("fail pthread_condattr_setpshared[%d]\n", eno);
70+
log.error("fail pthread_condattr_setpshared[%d]", eno);
6971
return false;
7072
}
7173
*cond_ = PTHREAD_COND_INITIALIZER;
7274
if ((eno = ::pthread_cond_init(cond_, &cond_attr)) != 0) {
73-
ipc::error("fail pthread_cond_init[%d]\n", eno);
75+
log.error("fail pthread_cond_init[%d]", eno);
7476
return false;
7577
}
7678
finally.dismiss();
7779
return valid();
7880
}
7981

8082
void close() noexcept {
83+
LIBIPC_LOG();
8184
if ((shm_.ref() <= 1) && cond_ != nullptr) {
8285
int eno;
8386
if ((eno = ::pthread_cond_destroy(cond_)) != 0) {
84-
ipc::error("fail pthread_cond_destroy[%d]\n", eno);
87+
log.error("fail pthread_cond_destroy[%d]", eno);
8588
}
8689
}
8790
shm_.release();
@@ -92,7 +95,7 @@ class condition {
9295
if ((shm_.ref() <= 1) && cond_ != nullptr) {
9396
int eno;
9497
if ((eno = ::pthread_cond_destroy(cond_)) != 0) {
95-
ipc::error("fail pthread_cond_destroy[%d]\n", eno);
98+
log.error("fail pthread_cond_destroy[%d]", eno);
9699
}
97100
}
98101
shm_.clear(); // Make sure the storage is cleaned up.
@@ -104,12 +107,13 @@ class condition {
104107
}
105108

106109
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
110+
LIBIPC_LOG();
107111
if (!valid()) return false;
108112
switch (tm) {
109113
case invalid_value: {
110114
int eno;
111115
if ((eno = ::pthread_cond_wait(cond_, static_cast<pthread_mutex_t *>(mtx.native()))) != 0) {
112-
ipc::error("fail pthread_cond_wait[%d]\n", eno);
116+
log.error("fail pthread_cond_wait[%d]", eno);
113117
return false;
114118
}
115119
}
@@ -134,7 +138,7 @@ class condition {
134138
if (!valid()) return false;
135139
int eno;
136140
if ((eno = ::pthread_cond_signal(cond_)) != 0) {
137-
ipc::error("fail pthread_cond_signal[%d]\n", eno);
141+
log.error("fail pthread_cond_signal[%d]", eno);
138142
return false;
139143
}
140144
return true;
@@ -144,7 +148,7 @@ class condition {
144148
if (!valid()) return false;
145149
int eno;
146150
if ((eno = ::pthread_cond_broadcast(cond_)) != 0) {
147-
ipc::error("fail pthread_cond_broadcast[%d]\n", eno);
151+
log.error("fail pthread_cond_broadcast[%d]", eno);
148152
return false;
149153
}
150154
return true;

src/libipc/platform/posix/get_wait_time.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <time.h>
88
#include <errno.h>
99

10-
#include "libipc/utility/log.h"
10+
#include "libipc/imp/log.h"
1111

1212
namespace ipc {
1313
namespace posix_ {
@@ -17,7 +17,7 @@ inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept {
1717
timeval now;
1818
int eno = ::gettimeofday(&now, NULL);
1919
if (eno != 0) {
20-
ipc::error("fail gettimeofday [%d]\n", eno);
20+
log.error("fail gettimeofday [", eno, "]");
2121
return false;
2222
}
2323
ts.tv_nsec = (now.tv_usec + (tm % 1000) * 1000) * 1000;

0 commit comments

Comments
 (0)