Skip to content

Commit

Permalink
add condition log macro
Browse files Browse the repository at this point in the history
  • Loading branch information
MOON-CLJ committed Apr 19, 2018
1 parent 60e0c88 commit 605456c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
82 changes: 43 additions & 39 deletions include/Common.h
Expand Up @@ -9,6 +9,7 @@
#include <cstdlib>
#include <cerrno>


#define PROJECT_NAME "libmc"
#define MC_DEFAULT_PORT 11211
#define MC_DEFAULT_POLL_TIMEOUT 300
Expand Down Expand Up @@ -54,18 +55,17 @@
# define __VOID_CAST (void)
#endif

void printBacktrace();

#define _mc_clean_errno() (errno == 0 ? "None" : strerror(errno))
#define _mc_output_stderr(LEVEL, FORMAT, ...) ( \

#define _mc_output_stderr(LEVEL, FORMAT, ...) \
fprintf( \
stderr, \
"[" PROJECT_NAME "] [" #LEVEL "] [%s:%d] " FORMAT "\n", \
__FILE__, __LINE__, ##__VA_ARGS__ \
), \
__VOID_CAST(0) \
)
)

void printBacktrace();

#define MC_LOG_LEVEL_ERROR 1
#define MC_LOG_LEVEL_WARNING 2
Expand All @@ -74,67 +74,71 @@ void printBacktrace();

#define MC_LOG_LEVEL MC_LOG_LEVEL_ERROR


#ifdef NDEBUG
#define debug(M, ...) __VOID_CAST(0)

#define _ASSERTION_FAILED(cond) ( \
_mc_output_stderr(PANIC, "failed assertion `%s'" , #cond), \
printBacktrace(), \
__VOID_CAST(0) \
)
#define log_debug_if(cond, M, ...) __VOID_CAST(0)
#define log_debug(M, ...) __VOID_CAST(0)

#define _ASSERTION_FAILED(cond) do { \
_mc_output_stderr(FATAL, "failed assertion `%s'" , #cond); \
printBacktrace(); \
} while (0)

#else

#if MC_LOG_LEVEL >= MC_LOG_LEVEL_DEBUG
#define debug(M, ...) ( \
_mc_output_stderr(DEBUG, "[E: %s] " M, _mc_clean_errno(), ##__VA_ARGS__), \
__VOID_CAST(0) \
)
#define log_debug(M, ...) _mc_output_stderr(DEBUG, "[E: %s] " M, _mc_clean_errno(), ##__VA_ARGS__)
#else
#define debug(M, ...) __VOID_CAST(0)
#define log_debug(M, ...) __VOID_CAST(0)
#endif
#define _ASSERTION_FAILED(cond) do { \
_mc_output_stderr(FATAL, "failed assertion `%s'" , #cond); \
printBacktrace(); \
abort(); \
} while (0)

#define _ASSERTION_FAILED(cond) ( \
_mc_output_stderr(PANIC, "failed assertion `%s'" , #cond), \
printBacktrace(), \
abort() \
)
#endif
#define log_debug_if(cond, M, ...) do { \
if (cond) log_debug(M, ##__VA_ARGS__); \
} while (0)

#endif // NDEBUG

#define ASSERT(cond) ( \
(cond) ? \
__VOID_CAST(0) : \
_ASSERTION_FAILED(cond) \
)
#define ASSERT(cond) if (!(cond)) _ASSERTION_FAILED(cond)

#define NOT_REACHED() ASSERT(0)

#if MC_LOG_LEVEL >= MC_LOG_LEVEL_INFO
#define log_info(M, ...) ( \
_mc_output_stderr(INFO, M, ##__VA_ARGS__), \
__VOID_CAST(0) \
)
#define log_info(M, ...) _mc_output_stderr(INFO, M, ##__VA_ARGS__)
#else
#define log_info(M, ...) __VOID_CAST(0)
#endif

#define log_info_if(cond, M, ...) do { \
if (cond) log_info(M, ##__VA_ARGS__); \
} while (0)

#if MC_LOG_LEVEL >= MC_LOG_LEVEL_WARNING
#define log_warn(M, ...) ( \
_mc_output_stderr(WARN, "[E: %s] " M, _mc_clean_errno(), ##__VA_ARGS__), \
__VOID_CAST(0) \
)
#define log_warn(M, ...) _mc_output_stderr(WARN, "[E: %s] " M, _mc_clean_errno(), ##__VA_ARGS__)
#else
#define log_warn(M, ...) __VOID_CAST(0)
#endif

#define log_warn_if(cond, M, ...) do { \
if (cond) log_warn(M, ##__VA_ARGS__); \
} while (0)

#if MC_LOG_LEVEL >= MC_LOG_LEVEL_ERROR
#define log_err(M, ...) ( \
_mc_output_stderr(ERROR, "[E: %s] " M, _mc_clean_errno(), ##__VA_ARGS__), \
__VOID_CAST(0) \
)
#define log_err(M, ...) _mc_output_stderr(ERROR, "[E: %s] " M, _mc_clean_errno(), ##__VA_ARGS__)
#else
#define log_err(M, ...) __VOID_CAST(0)
#endif

#define log_err_if(cond, M, ...) do { \
if (cond) log_err(M, ##__VA_ARGS__); \
} while (0)


namespace douban {
namespace mc {

Expand Down
4 changes: 2 additions & 2 deletions src/ConnectionPool.cpp
Expand Up @@ -248,7 +248,7 @@ void ConnectionPool::dispatchRetrieval(op_code_t op, const char* const* keys,
conn->getRetrievalResults()->reserve(conn->m_counter);
}
}
// debug("after dispatchRetrieval: m_nActiveConn: %d", this->m_nActiveConn);
// log_debug("after dispatchRetrieval: m_nActiveConn: %d", this->m_nActiveConn);
}


Expand Down Expand Up @@ -465,7 +465,7 @@ err_code_t ConnectionPool::waitPoll() {
pollfd_ptr->events |= POLLIN;

if (nToSend == 0) {
// debug("[%d] all sent", pollfd_ptr->fd);
// log_debug("[%d] all sent", pollfd_ptr->fd);
pollfd_ptr->events &= ~POLLOUT;
if (conn->m_counter == 0) {
// just send, no recv for noreply
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.cpp
Expand Up @@ -187,7 +187,7 @@ void PacketParser::process_packets(err_code_t& err) {
SKIP_BYTES(2); // "\r\n"
#ifndef NDEBUG
char* _k = parseTokenData(mt_kvPtr->key, mt_kvPtr->key_len);
// debug("got %.*s", static_cast<int>(mt_kvPtr->key_len), _k);
// log_debug("got %.*s", static_cast<int>(mt_kvPtr->key_len), _k);
if (mt_kvPtr->key.size() > 1) {
delete[] _k;
}
Expand Down

0 comments on commit 605456c

Please sign in to comment.