Skip to content

Commit e0a0de1

Browse files
committed
log: move logger to its own namespace and rename macros
Such names as `LogLevel`, `Logger`, `LOG_ERROR` and so on are too common and our users can use them in their projects. It will lead to conflicts, so let's move logger to namespace `tnt` just as other utils and rename macros like `LOG_ERROR` to `TNT_LOG_ERROR`.
1 parent 33efd78 commit e0a0de1

File tree

7 files changed

+54
-63
lines changed

7 files changed

+54
-63
lines changed

src/Client/Connection.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ static void
523523
inputBufGC(Connection<BUFFER, NetProvider> &conn)
524524
{
525525
if ((conn.gc_step++ % Connection<BUFFER, NetProvider>::GC_STEP_CNT) == 0) {
526-
LOG_DEBUG("Flushed input buffer of the connection %p", &conn);
526+
TNT_LOG_DEBUG("Flushed input buffer of the connection %p", &conn);
527527
conn.impl->inBuf.flush();
528528
}
529529
}
@@ -540,7 +540,7 @@ processResponse(Connection<BUFFER, NetProvider> &conn, int req_sync, Response<BU
540540
Response<BUFFER> response;
541541
response.size = conn.impl->dec.decodeResponseSize();
542542
if (response.size < 0) {
543-
LOG_ERROR("Failed to decode response size");
543+
TNT_LOG_ERROR("Failed to decode response size");
544544
//In case of corrupted response size all other data in the buffer
545545
//is likely to be decoded in the wrong way (since we don't
546546
// know how much bytes should be skipped). So let's simply
@@ -560,8 +560,8 @@ processResponse(Connection<BUFFER, NetProvider> &conn, int req_sync, Response<BU
560560
conn.impl->endDecoded += response.size;
561561
return DECODE_ERR;
562562
}
563-
LOG_DEBUG("Header: sync=", response.header.sync, ", code=",
564-
response.header.code, ", schema=", response.header.schema_id);
563+
TNT_LOG_DEBUG("Header: sync=", response.header.sync, ", code=", response.header.code,
564+
", schema=", response.header.schema_id);
565565
if (result != nullptr && response.header.sync == req_sync) {
566566
*result = std::move(response);
567567
} else {
@@ -586,7 +586,7 @@ decodeGreeting(Connection<BUFFER, NetProvider> &conn)
586586
conn.impl->greeting) != 0)
587587
return -1;
588588
conn.impl->is_greeting_received = true;
589-
LOG_DEBUG("Version: ", conn.impl->greeting.version_id);
589+
TNT_LOG_DEBUG("Version: ", conn.impl->greeting.version_id);
590590

591591
#ifndef NDEBUG
592592
//print salt in hex format.
@@ -598,7 +598,7 @@ decodeGreeting(Connection<BUFFER, NetProvider> &conn)
598598
hex_salt[i * 2 + 1] = hex[u % 16];
599599
}
600600
hex_salt[conn.impl->greeting.salt_size * 2] = 0;
601-
LOG_DEBUG("Salt: ", hex_salt);
601+
TNT_LOG_DEBUG("Salt: ", hex_salt);
602602
#endif
603603
return 0;
604604
}

src/Client/Connector.hpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ Connector<BUFFER, NetProvider>::connect(Connection<BUFFER, NetProvider> &conn,
146146
//Make sure that connection is not yet established.
147147
assert(conn.get_strm().has_status(SS_DEAD));
148148
if (m_NetProvider.connect(conn, opts) != 0) {
149-
LOG_ERROR("Failed to connect to ",
150-
opts.address, ':', opts.service);
149+
TNT_LOG_ERROR("Failed to connect to ", opts.address, ':', opts.service);
151150
return -1;
152151
}
153152
conn.getImpl()->is_greeting_received = false;
@@ -156,8 +155,7 @@ Connector<BUFFER, NetProvider>::connect(Connection<BUFFER, NetProvider> &conn,
156155
// Encode auth request to reserve space in buffer.
157156
conn.prepare_auth(opts.user, opts.passwd);
158157
}
159-
LOG_DEBUG("Connection to ", opts.address, ':', opts.service,
160-
" has been established");
158+
TNT_LOG_DEBUG("Connection to ", opts.address, ':', opts.service, " has been established");
161159
return 0;
162160
}
163161

@@ -258,7 +256,7 @@ Connector<BUFFER, NetProvider>::wait(Connection<BUFFER, NetProvider> &conn,
258256
rid_t future, int timeout,
259257
Response<BUFFER> *result)
260258
{
261-
LOG_DEBUG("Waiting for the future ", future, " with timeout ", timeout);
259+
TNT_LOG_DEBUG("Waiting for the future ", future, " with timeout ", timeout);
262260
Timer timer{timeout};
263261
timer.start();
264262
static constexpr int INVALID_SYNC = -1;
@@ -269,7 +267,7 @@ Connector<BUFFER, NetProvider>::wait(Connection<BUFFER, NetProvider> &conn,
269267
return -1;
270268
if (result != NULL && result->header.sync != INVALID_SYNC) {
271269
assert(result->header.sync == req_sync);
272-
LOG_DEBUG("Future ", future, " is ready and decoded");
270+
TNT_LOG_DEBUG("Future ", future, " is ready and decoded");
273271
return 0;
274272
}
275273
while (!conn.hasError() && !conn.futureIsReady(future)) {
@@ -282,24 +280,23 @@ Connector<BUFFER, NetProvider>::wait(Connection<BUFFER, NetProvider> &conn,
282280
return -1;
283281
if (result != NULL && result->header.sync != INVALID_SYNC) {
284282
assert(result->header.sync == req_sync);
285-
LOG_DEBUG("Future ", future, " is ready and decoded");
283+
TNT_LOG_DEBUG("Future ", future, " is ready and decoded");
286284
return 0;
287285
}
288286
if (timer.isExpired())
289287
break;
290288
}
291289
if (conn.hasError()) {
292-
LOG_ERROR("Connection got an error: ", conn.getError().msg);
290+
TNT_LOG_ERROR("Connection got an error: ", conn.getError().msg);
293291
return -1;
294292
}
295293
if (! conn.futureIsReady(future)) {
296-
LOG_DEBUG("Connection has been timed out: future ", future,
297-
" is not ready");
294+
TNT_LOG_DEBUG("Connection has been timed out: future ", future, " is not ready");
298295
return -1;
299296
} else if (result != NULL) {
300297
*result = std::move(conn.getResponse(future));
301298
}
302-
LOG_DEBUG("Feature ", future, " is ready and decoded");
299+
TNT_LOG_DEBUG("Feature ", future, " is ready and decoded");
303300
return 0;
304301
}
305302

@@ -331,10 +328,10 @@ Connector<BUFFER, NetProvider>::waitAll(Connection<BUFFER, NetProvider> &conn,
331328
break;
332329
}
333330
if (conn.hasError()) {
334-
LOG_ERROR("Connection got an error: ", conn.getError().msg);
331+
TNT_LOG_ERROR("Connection got an error: ", conn.getError().msg);
335332
return -1;
336333
}
337-
LOG_DEBUG("Connection has been timed out: not all futures are ready");
334+
TNT_LOG_DEBUG("Connection has been timed out: not all futures are ready");
338335
return -1;
339336
}
340337

@@ -346,14 +343,14 @@ Connector<BUFFER, NetProvider>::waitAny(int timeout)
346343
timer.start();
347344
while (m_ReadyToDecode.empty()) {
348345
if (m_NetProvider.wait(timer.timeLeft()) != 0) {
349-
LOG_ERROR("Failed to poll connections: ", strerror(errno));
346+
TNT_LOG_ERROR("Failed to poll connections: ", strerror(errno));
350347
return std::nullopt;
351348
}
352349
if (timer.isExpired())
353350
break;
354351
}
355352
if (m_ReadyToDecode.empty()) {
356-
LOG_DEBUG("wait() has been timed out! No responses are received");
353+
TNT_LOG_DEBUG("wait() has been timed out! No responses are received");
357354
return std::nullopt;
358355
}
359356
Connection<BUFFER, NetProvider> conn = *m_ReadyToDecode.begin();
@@ -391,11 +388,10 @@ Connector<BUFFER, NetProvider>::waitCount(Connection<BUFFER, NetProvider> &conn,
391388
break;
392389
}
393390
if (conn.hasError()) {
394-
LOG_ERROR("Connection got an error: ", conn.getError().msg);
391+
TNT_LOG_ERROR("Connection got an error: ", conn.getError().msg);
395392
return -1;
396393
}
397-
LOG_DEBUG("Connection has been timed out: only ",
398-
conn.getFutureCount() - ready_futures, " are ready");
394+
TNT_LOG_DEBUG("Connection has been timed out: only ", conn.getFutureCount() - ready_futures, " are ready");
399395
return -1;
400396
}
401397

src/Client/EpollNetProvider.hpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ EpollNetProvider<BUFFER, Stream>::EpollNetProvider(Connector_t &connector) :
8686
{
8787
m_EpollFd = epoll_create1(EPOLL_CLOEXEC);
8888
if (m_EpollFd == -1) {
89-
LOG_ERROR("Failed to initialize epoll: ", strerror(errno));
89+
TNT_LOG_ERROR("Failed to initialize epoll: ", strerror(errno));
9090
abort();
9191
}
9292
}
@@ -109,9 +109,7 @@ EpollNetProvider<BUFFER, Stream>::registerEpoll(Conn_t &conn)
109109
event.data.ptr = conn.getImpl();
110110
if (epoll_ctl(m_EpollFd, EPOLL_CTL_ADD, conn.get_strm().get_fd(),
111111
&event) != 0) {
112-
LOG_ERROR("Failed to add socket to epoll: "
113-
"epoll_ctl() returned with errno: ",
114-
strerror(errno));
112+
TNT_LOG_ERROR("Failed to add socket to epoll: epoll_ctl() returned with errno: ", strerror(errno));
115113
abort();
116114
}
117115
}
@@ -124,9 +122,7 @@ EpollNetProvider<BUFFER, Stream>::setPollSetting(Conn_t &conn, int setting) {
124122
event.data.ptr = conn.getImpl();
125123
if (epoll_ctl(m_EpollFd, EPOLL_CTL_MOD, conn.get_strm().get_fd(),
126124
&event) != 0) {
127-
LOG_ERROR("Failed to change epoll mode: "
128-
"epoll_ctl() returned with errno: ",
129-
strerror(errno));
125+
TNT_LOG_ERROR("Failed to change epoll mode: epoll_ctl() returned with errno: ", strerror(errno));
130126
abort();
131127
}
132128
}
@@ -142,7 +138,7 @@ EpollNetProvider<BUFFER, Stream>::connect(Conn_t &conn,
142138
opts.address);
143139
return -1;
144140
}
145-
LOG_DEBUG("Connected to ", opts.address, ", socket is ", strm.get_fd());
141+
TNT_LOG_DEBUG("Connected to ", opts.address, ", socket is ", strm.get_fd());
146142

147143
registerEpoll(conn);
148144
return 0;
@@ -168,8 +164,7 @@ EpollNetProvider<BUFFER, Stream>::close(Stream_t& strm)
168164
struct sockaddr_un *sa_un = (struct sockaddr_un *) &sa;
169165
snprintf(addr, 120, "%s", sa_un->sun_path);
170166
}
171-
LOG_DEBUG("Closed connection to socket ", was_fd,
172-
" corresponding to address ", addr);
167+
TNT_LOG_DEBUG("Closed connection to socket ", was_fd, " corresponding to address ", addr);
173168
}
174169
#endif
175170
/*
@@ -209,12 +204,12 @@ EpollNetProvider<BUFFER, Stream>::recv(Conn_t &conn)
209204
if ((size_t) rcvd < Iproto::GREETING_SIZE)
210205
return 0;
211206
/* Receive and decode greetings. */
212-
LOG_DEBUG("Greetings are received, read bytes ", rcvd);
207+
TNT_LOG_DEBUG("Greetings are received, read bytes ", rcvd);
213208
if (decodeGreeting(conn) != 0) {
214209
conn.setError("Failed to decode greetings");
215210
return -1;
216211
}
217-
LOG_DEBUG("Greetings are decoded");
212+
TNT_LOG_DEBUG("Greetings are decoded");
218213
rcvd -= Iproto::GREETING_SIZE;
219214
if (conn.getImpl()->is_auth_required) {
220215
// Finalize auth request in buffer.
@@ -261,7 +256,7 @@ EpollNetProvider<BUFFER, Stream>::wait(int timeout)
261256
assert(timeout >= -1);
262257
if (timeout == -1)
263258
timeout = TIMEOUT_INFINITY;
264-
LOG_DEBUG("Network engine wait for ", timeout, " milliseconds");
259+
TNT_LOG_DEBUG("Network engine wait for ", timeout, " milliseconds");
265260
/* Send pending requests. */
266261
for (auto conn = m_Connector.m_ReadyToSend.begin();
267262
conn != m_Connector.m_ReadyToSend.end();) {
@@ -276,15 +271,14 @@ EpollNetProvider<BUFFER, Stream>::wait(int timeout)
276271
if (event_cnt < 0) {
277272
//Poll error doesn't belong to any connection so just global
278273
//log it.
279-
LOG_ERROR("Poll failed: ", strerror(errno));
274+
TNT_LOG_ERROR("Poll failed: ", strerror(errno));
280275
return -1;
281276
}
282277
for (int i = 0; i < event_cnt; ++i) {
283278
Conn_t conn((typename Conn_t::Impl_t *)events[i].data.ptr);
284279
if ((events[i].events & EPOLLIN) != 0) {
285-
LOG_DEBUG("Registered poll event ", i, ": ",
286-
conn.get_strm().get_fd(),
287-
" socket is ready to read");
280+
TNT_LOG_DEBUG("Registered poll event ", i, ": ", conn.get_strm().get_fd(),
281+
" socket is ready to read");
288282
if (conn.get_strm().has_status(SS_NEED_READ_EVENT_FOR_WRITE)) {
289283
int rc = send(conn);
290284
if (rc < 0)
@@ -302,9 +296,8 @@ EpollNetProvider<BUFFER, Stream>::wait(int timeout)
302296
}
303297

304298
if ((events[i].events & EPOLLOUT) != 0) {
305-
LOG_DEBUG("Registered poll event ", i, ": ",
306-
conn.get_strm().get_fd(),
307-
" socket is ready to write");
299+
TNT_LOG_DEBUG("Registered poll event ", i, ": ", conn.get_strm().get_fd(),
300+
" socket is ready to write");
308301
if (conn.get_strm().has_status(SS_NEED_WRITE_EVENT_FOR_READ)) {
309302
int rc = recv(conn);
310303
if (rc < 0)

src/Client/LibevNetProvider.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ connectionReceive(Connection<BUFFER, LibevNetProvider<BUFFER, Stream>> &conn)
142142
if ((size_t) rcvd < Iproto::GREETING_SIZE)
143143
return 0;
144144
/* Receive and decode greetings. */
145-
LOG_DEBUG("Greetings are received, read bytes ", rcvd);
145+
TNT_LOG_DEBUG("Greetings are received, read bytes ", rcvd);
146146
if (decodeGreeting(conn) != 0) {
147147
conn.setError("Failed to decode greetings");
148148
return -1;
149149
}
150-
LOG_DEBUG("Greetings are decoded");
150+
TNT_LOG_DEBUG("Greetings are decoded");
151151
rcvd -= Iproto::GREETING_SIZE;
152152
if (conn.getImpl()->is_auth_required) {
153153
// Finalize auth request in buffer.
@@ -247,7 +247,7 @@ send_cb(struct ev_loop *loop, struct ev_io *watcher, int /* revents */)
247247
}
248248
if (rc > 0) {
249249
/* Send is not complete, setting the write watcher. */
250-
LOG_DEBUG("Send is not complete, setting the write watcher");
250+
TNT_LOG_DEBUG("Send is not complete, setting the write watcher");
251251
if (conn.get_strm().has_status(SS_NEED_WRITE_EVENT_FOR_WRITE))
252252
if (!ev_is_active(&waitWatcher->out))
253253
ev_io_start(loop, &waitWatcher->out);
@@ -302,7 +302,7 @@ LibevNetProvider<BUFFER, Stream>::registerWatchers(Conn_t &conn, int fd)
302302
new (std::nothrow) WaitWatcher<BUFFER, Stream>(&m_Connector,
303303
conn, &m_TimeoutWatcher);
304304
if (watcher == nullptr) {
305-
LOG_ERROR("Failed to allocate memory for WaitWatcher");
305+
TNT_LOG_ERROR("Failed to allocate memory for WaitWatcher");
306306
abort();
307307
}
308308

@@ -325,7 +325,7 @@ LibevNetProvider<BUFFER, Stream>::connect(Conn_t &conn,
325325
opts.address);
326326
return -1;
327327
}
328-
LOG_DEBUG("Connected to ", opts.address, ", socket is ", strm.get_fd());
328+
TNT_LOG_DEBUG("Connected to ", opts.address, ", socket is ", strm.get_fd());
329329

330330
registerWatchers(conn, strm.get_fd());
331331
return 0;
@@ -354,7 +354,7 @@ void
354354
LibevNetProvider<BUFFER, Stream>::timeout_cb(EV_P_ ev_timer *w, int /* revents */)
355355
{
356356
(void) w;
357-
LOG_DEBUG("Libev timed out!");
357+
TNT_LOG_DEBUG("Libev timed out!");
358358
/* Stop external loop */
359359
ev_break(EV_A_ EVBREAK_ONE);
360360
}

src/Client/ResponseDecoder.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ ResponseDecoder<BUFFER>::decodeResponse(Response<BUFFER> &response)
8484
{
8585
/* Decode header and body separately to get more detailed error. */
8686
if (!mpp::decode(it, response.header)) {
87-
LOG_ERROR("Failed to decode header");
87+
TNT_LOG_ERROR("Failed to decode header");
8888
return -1;
8989
}
9090
if (!mpp::decode(it, response.body)) {
91-
LOG_ERROR("Failed to decode body");
91+
TNT_LOG_ERROR("Failed to decode body");
9292
return -1;
9393
}
9494
return 0;

src/Client/UnixStream.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ class UnixStream : public Stream {
6868

6969
protected:
7070
/** Log helpers. */
71-
template <class ...MSG>
72-
void log_wise(LogLevel level, const char *file, int line,
73-
const char *msg, MSG &&...more);
71+
template <class... MSG>
72+
void log_wise(tnt::LogLevel level, const char *file, int line, const char *msg, MSG &&...more);
7473
template <class ...MSG>
7574
int die(const char *file, int line,
7675
const char *msg, MSG &&...more);
@@ -109,8 +108,7 @@ class UnixStream : public Stream {
109108

110109
template <class... MSG>
111110
void
112-
UnixStream::log_wise(LogLevel level, const char *file, int line,
113-
const char *msg, MSG&& ...more)
111+
UnixStream::log_wise(tnt::LogLevel level, const char *file, int line, const char *msg, MSG &&...more)
114112
{
115113
if (sizeof...(MSG) == 0 && fd < 0)
116114
log(level, file, line, msg);
@@ -127,7 +125,7 @@ template <class ...MSG>
127125
int
128126
UnixStream::die(const char *file, int line, const char *msg, MSG&& ...more)
129127
{
130-
log_wise(ERROR, file, line, msg, std::forward<MSG>(more)...);
128+
log_wise(tnt::ERROR, file, line, msg, std::forward<MSG>(more)...);
131129
set_status(SS_DEAD);
132130
return -1;
133131
}
@@ -137,7 +135,7 @@ int
137135
UnixStream::tell(StreamStatus st, const char *file, int line,
138136
const char *msg, MSG&& ...more)
139137
{
140-
log_wise(INFO, file, line, msg, std::forward<MSG>(more)...);
138+
log_wise(tnt::INFO, file, line, msg, std::forward<MSG>(more)...);
141139
set_status(st);
142140
return 0;
143141
}

src/Utils/Logger.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include <sstream>
3737
#include <string_view>
3838

39+
namespace tnt {
40+
3941
enum LogLevel {
4042
DEBUG = 0,
4143
INFO = 1,
@@ -124,7 +126,9 @@ log(LogLevel level, const char *file, int line, ARGS&& ...args)
124126
gLogger.log(fd, level, file, line, std::forward<ARGS>(args)...);
125127
}
126128

127-
#define LOG_DEBUG(...) log(DEBUG, __FILE__, __LINE__, __VA_ARGS__)
128-
#define LOG_INFO(...) log(INFO, __FILE__, __LINE__, __VA_ARGS__)
129-
#define LOG_WARNING(...) log(WARNING, __FILE__, __LINE__, __VA_ARGS__)
130-
#define LOG_ERROR(...) log(ERROR, __FILE__, __LINE__, __VA_ARGS__)
129+
} /* namespace tnt */
130+
131+
#define TNT_LOG_DEBUG(...) tnt::log(tnt::DEBUG, __FILE__, __LINE__, __VA_ARGS__)
132+
#define TNT_LOG_INFO(...) tnt::log(tnt::INFO, __FILE__, __LINE__, __VA_ARGS__)
133+
#define TNT_LOG_WARNING(...) tnt::log(tnt::WARNING, __FILE__, __LINE__, __VA_ARGS__)
134+
#define TNT_LOG_ERROR(...) tnt::log(tnt::ERROR, __FILE__, __LINE__, __VA_ARGS__)

0 commit comments

Comments
 (0)