Skip to content

Commit 8200d9e

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 1a0b019 commit 8200d9e

File tree

7 files changed

+47
-50
lines changed

7 files changed

+47
-50
lines changed

src/Client/Connection.hpp

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

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

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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ EpollNetProvider<BUFFER, Stream>::connect(Conn_t &conn,
142142
opts.address);
143143
return -1;
144144
}
145-
LOG_DEBUG("Connected to ", opts.address, ", socket is ", strm.get_fd());
145+
TNT_LOG_DEBUG("Connected to ", opts.address, ", socket is ", strm.get_fd());
146146

147147
registerEpoll(conn);
148148
return 0;
@@ -168,8 +168,7 @@ EpollNetProvider<BUFFER, Stream>::close(Stream_t& strm)
168168
struct sockaddr_un *sa_un = (struct sockaddr_un *) &sa;
169169
snprintf(addr, 120, "%s", sa_un->sun_path);
170170
}
171-
LOG_DEBUG("Closed connection to socket ", was_fd,
172-
" corresponding to address ", addr);
171+
TNT_LOG_DEBUG("Closed connection to socket ", was_fd, " corresponding to address ", addr);
173172
}
174173
#endif
175174
/*
@@ -209,12 +208,12 @@ EpollNetProvider<BUFFER, Stream>::recv(Conn_t &conn)
209208
if ((size_t) rcvd < Iproto::GREETING_SIZE)
210209
return 0;
211210
/* Receive and decode greetings. */
212-
LOG_DEBUG("Greetings are received, read bytes ", rcvd);
211+
TNT_LOG_DEBUG("Greetings are received, read bytes ", rcvd);
213212
if (decodeGreeting(conn) != 0) {
214213
conn.setError("Failed to decode greetings");
215214
return -1;
216215
}
217-
LOG_DEBUG("Greetings are decoded");
216+
TNT_LOG_DEBUG("Greetings are decoded");
218217
rcvd -= Iproto::GREETING_SIZE;
219218
if (conn.getImpl()->is_auth_required) {
220219
// Finalize auth request in buffer.
@@ -261,7 +260,7 @@ EpollNetProvider<BUFFER, Stream>::wait(int timeout)
261260
assert(timeout >= -1);
262261
if (timeout == -1)
263262
timeout = TIMEOUT_INFINITY;
264-
LOG_DEBUG("Network engine wait for ", timeout, " milliseconds");
263+
TNT_LOG_DEBUG("Network engine wait for ", timeout, " milliseconds");
265264
/* Send pending requests. */
266265
for (auto conn = m_Connector.m_ReadyToSend.begin();
267266
conn != m_Connector.m_ReadyToSend.end();) {
@@ -276,7 +275,7 @@ EpollNetProvider<BUFFER, Stream>::wait(int timeout)
276275
if (event_cnt < 0) {
277276
//Poll error doesn't belong to any connection so just global
278277
//log it.
279-
LOG_ERROR("Poll failed: ", strerror(errno));
278+
TNT_LOG_ERROR("Poll failed: ", strerror(errno));
280279
return -1;
281280
}
282281
for (int i = 0; i < event_cnt; ++i) {

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,
@@ -120,7 +122,9 @@ log(LogLevel level, const char *file, int line, ARGS&& ...args)
120122
gLogger.log(fd, level, file, line, std::forward<ARGS>(args)...);
121123
}
122124

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

0 commit comments

Comments
 (0)