diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-03-15 09:48:58 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-03-20 23:45:01 +0800 |
| commit | 28ad3d86ecd2ed6212ec4febd0bb90bd55c152c3 (patch) | |
| tree | 3bd8cf8509ef9742b5ce3938fea702de5365ee7d /src | |
| parent | 649965ec50eb2028c08a8cb6bfa77a62891f1797 (diff) | |
debug: only run hooks that is for debug purposes in debug builds
log: only log TRACE in debug builds
Diffstat (limited to 'src')
| -rw-r--r-- | src/format.hpp | 2 | ||||
| -rw-r--r-- | src/log.hpp | 8 | ||||
| -rw-r--r-- | src/session.cpp | 26 | ||||
| -rw-r--r-- | src/session.hpp | 6 |
4 files changed, 35 insertions, 7 deletions
diff --git a/src/format.hpp b/src/format.hpp index 019f3d6..40e85f3 100644 --- a/src/format.hpp +++ b/src/format.hpp @@ -28,9 +28,11 @@ inline std::string fmt(eFormatType type, std::string str) { case SYS: prefix = "[SYSTEM] "; break; +#ifdef DEBUG case TRACE: prefix = "[DEBUG] "; break; +#endif default: prefix = ""; break; diff --git a/src/log.hpp b/src/log.hpp index 8e7f0c1..996b6bc 100644 --- a/src/log.hpp +++ b/src/log.hpp @@ -6,11 +6,19 @@ #include <unistd.h> inline void log(FILE *_stream, eFormatType type, std::string str) { +#ifndef DEBUG + if (type == TRACE) + return; +#endif std::println(_stream, "{}", NFormatter::fmt(type, str)); } template <typename... Args> void log(FILE *_stream, eFormatType type, std::format_string<Args...> fmt, Args &&...args) { +#ifndef DEBUG + if (type == TRACE) + return; +#endif std::println(_stream, "{}", NFormatter::fmt(type, fmt, std::forward<Args>(args)...)); } diff --git a/src/session.cpp b/src/session.cpp index df4e506..d561a51 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -18,19 +18,24 @@ CSession::CSession() inet_ntop(AF_INET, &m_addr.sin_addr, m_ip, INET_ADDRSTRLEN); m_port = ntohs(m_addr.sin_port); - log(TRACE, "session({}): connected on port {}", m_ip, m_port); + log(LOG, "Client {} connected on port {}", m_ip, m_port); log(TRACE, "session: initialized"); } CSession::~CSession() { +#ifdef DEBUG onDisconnect(); +#endif m_sockfd.reset(); self->second.reset(); - log(TRACE, "session({}): bye", m_name); + log(LOG, "Client {}@{} disconnected", m_name, m_ip); + + log(TRACE, "session({}@{}): bye", m_name, m_ip); } +#ifdef DEBUG void CSession::onConnect() { log(LOG, "Client connected"); } @@ -38,17 +43,19 @@ void CSession::onConnect() { void CSession::onDisconnect() { log(LOG, "Client disconnected"); } +#endif void CSession::onErrno(eEventType eventType) { const auto err = strerror(errno); switch (eventType) { case READ: - log(LOG, "Error while reading: {}", err); + log(WARN, "Error while reading: {}", err); case WRITE: - log(LOG, "Error while writing: {}", err); + log(WARN, "Error while writing: {}", err); } } +#ifdef DEBUG void CSession::onRecv(const SRecvData &data) { log(LOG, "Received: {}", data.data); } @@ -56,6 +63,7 @@ void CSession::onRecv(const SRecvData &data) { void CSession::onSend(const std::string &msg) { log(LOG, "Sent: {}", msg); } +#endif void CSession::recvLoop() { while (true) { @@ -93,11 +101,12 @@ std::unique_ptr<CSession::SRecvData> CSession::read() { onErrno(READ); } else if (size == 0) recvData->good = false; - // onDisconnect(); else recvData->data.resize(size); +#ifdef DEBUG onRecv(*recvData); +#endif m_isReading = false; return recvData; } @@ -109,9 +118,10 @@ std::unique_ptr<CSession::SRecvData> CSession::read(const std::string &msg) { } void CSession::run() { +#ifdef DEBUG onConnect(); +#endif if (registerSession()) { - // g_pSessionManager->broadcast(g_pChatManager->getChat()); for (const auto &chat : g_pChatManager->getChat()) write(g_pChatManager->fmtBroadcastMessage(chat)); recvLoop(); @@ -164,7 +174,9 @@ bool CSession::write(std::format_string<Args...> fmt, Args &&...args) { onErrno(WRITE); return false; } +#ifdef DEBUG onSend(msg); +#endif return true; } @@ -178,7 +190,9 @@ bool CSession::write(eFormatType type, std::format_string<Args...> fmt, Args &&. onErrno(WRITE); return false; } +#ifdef DEBUG onSend(msg); +#endif return true; } diff --git a/src/session.hpp b/src/session.hpp index ddbfff9..a00168f 100644 --- a/src/session.hpp +++ b/src/session.hpp @@ -22,7 +22,7 @@ class CSession { }; const std::string &getName() const; - void setSelf(std::pair<std::jthread, std::shared_ptr<CSession>> *self); + void setSelf(std::pair<std::jthread, std::shared_ptr<CSession>> *self); std::unique_ptr<SRecvData> read(); std::unique_ptr<SRecvData> read(const std::string &msg); @@ -55,11 +55,15 @@ class CSession { void recvLoop(); bool registerSession(); +#ifdef DEBUG void onConnect(); void onDisconnect(); +#endif void onErrno(eEventType); +#ifdef DEBUG void onRecv(const SRecvData &data); void onSend(const std::string &msg); +#endif void onShutdown(); friend class CSessionManager; |
