From: hama Date: Wed, 18 Mar 2015 00:23:38 +0000 (+0100) Subject: fix ReSlaveAppender, ReThread, debug log in send/receive X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=64cfef687f48cbe8383437034aabd7d5ff127d00;p=crepublib fix ReSlaveAppender, ReThread, debug log in send/receive --- diff --git a/base/ReAppenders.cpp b/base/ReAppenders.cpp index f483299..3faac42 100644 --- a/base/ReAppenders.cpp +++ b/base/ReAppenders.cpp @@ -81,6 +81,5 @@ ReSlaveAppender::~ReSlaveAppender() { void ReSlaveAppender::say(ReLogger* logger, const char* message) { ReByteBuffer buffer(logger->standardPrefix(m_charPrefix)); buffer.append(message == NULL ? logger->asCString() : message); - m_masterLogger->say(logger->currentMode(), logger->currentLocation(), - buffer.str()); + m_masterLogger->say(logger->currentMode(), 0, buffer.str()); } diff --git a/base/ReByteBuffer.cpp b/base/ReByteBuffer.cpp index 2447077..431e324 100644 --- a/base/ReByteBuffer.cpp +++ b/base/ReByteBuffer.cpp @@ -206,6 +206,38 @@ ReByteBuffer& ReByteBuffer::appendFix(const char* data, size_t length, return *this; } +/** + * Appends data as string or as binary (hexdump). + * + * If data contains non ASCII char the data will be treated as binary. + * + * @param data the data to append + * @param length -1 (strlen()) or the length of data + * @param maxLength the maximal count of appended chars + * @return *this (chaining) + */ +ReByteBuffer& ReByteBuffer::appendDump(const char* data, size_t length, int maxLength){ + if (length == size_t(-1)){ + length = strlen(data); + } + if (length > maxLength) + length = maxLength; + // test if text or binary: + bool isBinary = false; + unsigned char cc; + for (int ii = 0; ii < length; ii++){ + if ( (cc = (unsigned char) data[ii]) > 126 + || (cc < ' ' && cc != '\n' && cc != '\r' && cc != '\t')){ + isBinary = true; + break; + } + } + if (! isBinary) + append(data, length); + else + appendHexDump(data, length / 5); + return *this; +} /** * Appends a hexadecimal dump of a memory array. * diff --git a/base/ReByteBuffer.hpp b/base/ReByteBuffer.hpp index d18ff71..3a4012f 100644 --- a/base/ReByteBuffer.hpp +++ b/base/ReByteBuffer.hpp @@ -70,6 +70,7 @@ public: } ReByteBuffer& appendFix(const char* data, size_t length, int maxLength, int minLength = 0, const char* separator = "*", char padding = ' '); + ReByteBuffer& appendDump(const char* data, size_t length = -1, int maxLength = 80); ReByteBuffer& appendHexDump(const char* data, size_t length = -1, int offset = 0, int bytePerLine = 16, const char* offsetFormat = "%04x: ", bool withAscii = true, int groupWidth = 1, int gapBehind = diff --git a/base/ReLogger.cpp b/base/ReLogger.cpp index 7294b47..d2acda4 100644 --- a/base/ReLogger.cpp +++ b/base/ReLogger.cpp @@ -113,7 +113,8 @@ ReStreamAppender::ReStreamAppender(FILE* stream) : */ ReStreamAppender::~ReStreamAppender() { if (m_stream != NULL) { - fclose(m_stream); + if (m_stream != stdout && m_stream != stderr) + fclose(m_stream); m_stream = NULL; } } @@ -255,6 +256,8 @@ void ReFileAppender::changeFile() { } /** @brief Constructor. + * + * @param isGlobal true: the local will be the global logger */ ReLogger::ReLogger(bool isGlobal) : ReVarArgs(), @@ -293,10 +296,10 @@ ReLogger::~ReLogger() { * * The message will be put to all appenders which accept the log message. * - * @param mode The mode controlling the issuing of the logging. - * This mode will be compared to the settings of the appenders. - * @param position The identification of the call. - * @param message The message to log. + * @param mode the mode controlling the issuing of the logging. + * This mode will be compared to the settings of the appenders + * @param position the identification of the call + * @param message the message to log * * @return true */ @@ -319,10 +322,11 @@ bool ReLogger::say(ReClassCategoryGranularity mode, ReLogLocation location, * * The message will be put to all appenders which accept the log message. * - * @param mode The mode controlling the issuing of the logging. - * This mode will be compared to the settings of the appenders. - * @param position The identification of the call. - * @param message The message to log. + * @param mode the mode controlling the issuing of the logging. + * this mode will be compared to the settings of the appenders + * @param location The identification of the call.
+ * 0: no prefix with date and location will be part of the message + * @param message the message to log * * @return true */ @@ -351,10 +355,11 @@ bool ReLogger::say(char prefix, ReClassCategoryGranularity mode, * Note:
* Don't forget to finish the argument list with ReVarArgs::end()! * - * @param mode The mode controlling the issuing of the logging. - * This mode will be compared to the settings of the appenders. - * @param location The identification of the call. - * @param message The message to log. + * @param mode the mode controlling the issuing of the logging. + * This mode will be compared to the settings of the appenders + * @param location the identification of the call.
+ * 0: no prefix with date and location will be part of the message + * @param message the message to log * * @return true */ @@ -377,7 +382,7 @@ ReVarArgs& ReLogger::sayF(ReClassCategoryGranularity mode, } /** Adds an appender to the appender list. * - * @param appender The appender. + * @param appender the appender. */ void ReLogger::addAppender(ReAppender* appender) { if (m_appenderListLength >= m_appenderListSize) @@ -389,11 +394,11 @@ void ReLogger::addAppender(ReAppender* appender) { /** Adds standard appenders to the appender list. * - * @param console true: A console appender will be added. - * @param file NULL: No file appender. - * Otherwise: A file appender will be appended. - * @param maxFiles The maximal count of log files produced by the file appender. - * @param masSize The maximal size of one log file produced by the file appender. + * @param console true: a console appender will be added + * @param file NULL: no file appender
+ * otherwise: A file appender will be appended + * @param maxFiles the maximal count of log files produced by the file appender + * @param masSize the maximal size of one log file produced by the file appender */ void ReLogger::addStandardAppenders(bool console, const char* file, int maxFiles, int maxSize) { @@ -415,12 +420,13 @@ void ReLogger::addStandardAppenders(bool console, const char* file, * Most of the appenders will use this prefix. * It will be build on demand. * - * @return The standard prefix. + * @return the standard prefix */ const char* ReLogger::standardPrefix(char charPrefix) { - if (m_standardPrefix[0] == 0) { + if (m_standardPrefix[0] == 0 && m_location != 0) { char cc; - switch (m_mode & LOG_CLASS_ALL) { + int level = (m_mode & LOG_CLASS_ALL); + switch (level) { case LOG_ABORT: cc = '#'; break; @@ -430,6 +436,9 @@ const char* ReLogger::standardPrefix(char charPrefix) { case LOG_WARNING: cc = '+'; break; + case LOG_DEBUG: + cc = '_'; + break; default: cc = ' '; break; diff --git a/base/ReLogger.hpp b/base/ReLogger.hpp index 607abee..037dba6 100644 --- a/base/ReLogger.hpp +++ b/base/ReLogger.hpp @@ -21,9 +21,9 @@ typedef unsigned long int ReLogLocation; enum ReLogClass { LOG_ABORT = 1, LOG_ERROR = 2, - LOG_WARNING = 4, - LOG_INFO = 8, - + LOG_WARNING = 3, + LOG_INFO = 4, + LOG_DEBUG = 5, LOG_CLASS_ALL = 0xf }; /* diff --git a/base/ReThread.cpp b/base/ReThread.cpp index 6d6c768..7a06693 100644 --- a/base/ReThread.cpp +++ b/base/ReThread.cpp @@ -79,22 +79,25 @@ void ReThread::kill() { * * @param id the thread id * @param masterLogger the logger for error handling - * @param starter the instance which has started the thread + * @param pool the instance which has started the thread */ bool ReThread::prepareToRun(int id, ReLogger* masterLogger, - ReThreadPool* starter) { + ReThreadPool* pool) { bool rc = false; if (m_pool != NULL) { - globalLogger()->say(LOG_ERROR | CAT_LIB, LC_PREPARE_TO_RUN_1, - i18n("setMasterLogger() is called multiple times")); + ReLogger* current = masterLogger == NULL ? globalLogger() : masterLogger; + current->sayF(LOG_ERROR | CAT_LIB, LC_PREPARE_TO_RUN_1, + i18n("prepareToRun($1) is called multiple times")).arg(id).end(); } else { m_threadId = id; - if (m_appender == NULL) - m_appender = new ReSlaveAppender(masterLogger, - '0' + (id % ('z' - '0' + 1))); - else + if (m_appender != NULL){ m_appender->setMasterLogger(masterLogger); - m_pool = starter; + } else { + m_appender = new ReSlaveAppender(masterLogger, + '@' + (id % ('z' - '@' + 1))); + m_threadLogger.addAppender(m_appender); + } + m_pool = pool; rc = true; } return rc; diff --git a/base/rebase.hpp b/base/rebase.hpp index dd2c61b..965a99e 100644 --- a/base/rebase.hpp +++ b/base/rebase.hpp @@ -96,6 +96,22 @@ public: public: virtual void process() = 0; }; +/** Returns the minimum of 2 values. + * @param a first value + * @param b 2nd value + * @return minimum of a and b + */ +inline int min(int a, int b){ + return a < b ? a : b; +} +/** Returns the maximum of 2 values. + * @param a first value + * @param b 2nd value + * @return maximum of a and b + */ +inline int max(int a, int b){ + return a < b ? a : b; +} #include "base/ReMutex.hpp" #include "base/ReByteBuffer.hpp" #include "base/ReVarArgs.hpp" diff --git a/cunit/cuReTCP.cpp b/cunit/cuReTCP.cpp index 83f46ca..472e7b2 100644 --- a/cunit/cuReTCP.cpp +++ b/cunit/cuReTCP.cpp @@ -19,7 +19,7 @@ public: public: virtual void run() { ReLogger* theLogger = logger(); - int location = 999; + int location = 60999; if (strcmp(m_task, "server") == 0) { theLogger->say(LOG_INFO | CAT_LIB, location, "starting server..."); ReLogger logger(false); @@ -53,7 +53,7 @@ public: } } void speedTest(ReLogger* logger, bool upload) { - int location = 998; + int location = 60998; const char* direction = upload ? "upload" : "download"; logger->sayF(LOG_INFO | CAT_LIB, location, "$1 client started..").arg( direction).end(); @@ -103,13 +103,15 @@ public: private: void run() { ReLogger logger(false); + int location = 60997; logger.addStandardAppenders(true, NULL, 0, 0); - ReThreadPool threadStarter(1, &logger); - threadStarter.startThread(new TCPThread("server")); - threadStarter.startThread(new TCPThread("echo")); - threadStarter.startThread(new TCPThread("upload")); - threadStarter.startThread(new TCPThread("download")); - threadStarter.waitForAlmostAll(1, 20); + logger.say(LOG_INFO | CAT_LIB, location, "TCP test"); + ReThreadPool pool(4, &logger); + pool.startThread(new TCPThread("server")); + pool.startThread(new TCPThread("echo")); + pool.startThread(new TCPThread("upload")); + pool.startThread(new TCPThread("download")); + pool.waitForAlmostAll(1, 20000); ReTCPStopClient stopper(&logger); stopper.stopServer(s_port); } diff --git a/net/ReTCP.cpp b/net/ReTCP.cpp index 1cc70cf..313341d 100644 --- a/net/ReTCP.cpp +++ b/net/ReTCP.cpp @@ -27,6 +27,8 @@ enum LOCATION_DIRTOOL { LC_CONNECT_2, // 50516 LC_CONNECT_3, // 50517 LC_TCP_CONNECTION_1, // 50518 + LC_WRITE_2, // 50519 + LC_RECEIVE_5, // 50520 }; #if defined __WIN32__ @@ -235,6 +237,12 @@ void ReTCPConnection::setConnectedAddress(int family, const char* ip, m_name.appendChar(':').appendInt(port); } +/** + * Receives a message. + * + * @param command OUT: the received command + * @param data OUT: the received data + */ void ReTCPConnection::receive(ReByteBuffer& command, ReByteBuffer& data) { command.setLength(8); int received = recv(m_handleSocket, command.buffer(), 8, 0); @@ -279,6 +287,10 @@ void ReTCPConnection::receive(ReByteBuffer& command, ReByteBuffer& data) { m_loggerOwner->logger()->sayF(LOG_ERROR | CAT_NETWORK, LC_RECEIVE_4, i18n("too few bytes read: $1/$2 [$3]")).arg( readBytes).arg(received).arg(m_peerName).end(); + } else { + m_loggerOwner->logger()->sayF(LOG_DEBUG | CAT_NETWORK, LC_RECEIVE_5, + i18n("received: $1 bytes in $2 round(s) [$3]: $4")).arg(length).arg(rounds).arg( + m_peerName).arg(ReByteBuffer().appendDump(data.str(), data.length(), 80).str()).end(); } command.setLength(0); if (readBytes >= 8) { @@ -311,7 +323,9 @@ void ReTCPConnection::send(const char* command, const char* data, int length) { int sent = 0; int rest = length; const char* buf = m_toSend.str(); + int rounds = 0; while (rest > 0) { + rounds++; if ((sent = ::send(m_handleSocket, buf, rest, 0)) > 0) { buf += sent; rest -= sent; @@ -321,6 +335,10 @@ void ReTCPConnection::send(const char* command, const char* data, int length) { break; } } + if (rest == 0) + m_loggerOwner->logger()->sayF(LOG_DEBUG | CAT_NETWORK, LC_WRITE_2, + i18n("sent: $1 bytes in $2 round(s): $3")).arg(length).arg(rounds) + .arg(ReByteBuffer().appendDump(data, length, 80).str()).end(); } /**