This is an automated email from the ASF dual-hosted git repository. rmiddleton pushed a commit to branch remove-object-outputstream in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
commit 580cc50f59e924fdbcb1b9161df8350c379a1454 Author: Robert Middleton <[email protected]> AuthorDate: Fri Dec 16 21:24:46 2022 -0500 LOGCXX-570 Remove the final bits of java serialization --- src/main/cpp/CMakeLists.txt | 2 - src/main/cpp/class.cpp | 2 - src/main/cpp/locationinfo.cpp | 68 ----- src/main/cpp/loggingevent.cpp | 1 - src/main/cpp/objectoutputstream.cpp | 249 ----------------- src/main/cpp/sockethubappender.cpp | 293 --------------------- .../include/log4cxx/helpers/objectoutputstream.h | 96 ------- .../include/log4cxx/net/socketappenderskeleton.h | 1 - src/main/include/log4cxx/net/sockethubappender.h | 183 ------------- .../include/log4cxx/spi/location/locationinfo.h | 3 - src/test/CMakeLists.txt | 4 - src/test/cpp/net/CMakeLists.txt | 1 - src/test/cpp/net/sockethubappendertestcase.cpp | 87 ------ src/test/java/CMakeLists.txt | 61 ----- .../org/apache/log4j/net/ShortSocketServer.java | 89 ------- 15 files changed, 1140 deletions(-) diff --git a/src/main/cpp/CMakeLists.txt b/src/main/cpp/CMakeLists.txt index acd4f46a..cac1f167 100644 --- a/src/main/cpp/CMakeLists.txt +++ b/src/main/cpp/CMakeLists.txt @@ -40,7 +40,6 @@ if(LOG4CXX_NETWORKING_SUPPORT) aprsocket.cpp telnetappender.cpp socketappenderskeleton.cpp - sockethubappender.cpp socketoutputstream.cpp xmlsocketappender.cpp syslogwriter.cpp @@ -145,7 +144,6 @@ target_sources(log4cxx ndc.cpp ndcpatternconverter.cpp nteventlogappender.cpp - objectoutputstream.cpp odbcappender.cpp onlyonceerrorhandler.cpp optionconverter.cpp diff --git a/src/main/cpp/class.cpp b/src/main/cpp/class.cpp index c16f2cf1..f5b90ee4 100644 --- a/src/main/cpp/class.cpp +++ b/src/main/cpp/class.cpp @@ -39,7 +39,6 @@ #include <log4cxx/nt/outputdebugstringappender.h> #endif #include <log4cxx/net/smtpappender.h> -#include <log4cxx/net/sockethubappender.h> #include <log4cxx/helpers/datagramsocket.h> #include <log4cxx/net/syslogappender.h> #include <log4cxx/net/telnetappender.h> @@ -191,7 +190,6 @@ void Class::registerClasses() #if LOG4CXX_HAS_NETWORKING TelnetAppender::registerClass(); XMLSocketAppender::registerClass(); - SocketHubAppender::registerClass(); SyslogAppender::registerClass(); #endif } diff --git a/src/main/cpp/locationinfo.cpp b/src/main/cpp/locationinfo.cpp index a6a74cb2..713408a8 100644 --- a/src/main/cpp/locationinfo.cpp +++ b/src/main/cpp/locationinfo.cpp @@ -16,7 +16,6 @@ */ #include <log4cxx/spi/location/locationinfo.h> -#include <log4cxx/helpers/objectoutputstream.h> #include <log4cxx/helpers/pool.h> using namespace ::log4cxx::spi; @@ -179,71 +178,4 @@ const std::string LocationInfo::getClassName() const return tmp; } -void LocationInfo::write(ObjectOutputStream& os, Pool& p) const -{ - if (lineNumber == -1 && fileName == NA && methodName == NA_METHOD) - { - os.writeNull(p); - } - else - { - unsigned char prolog[] = - { - 0x72, - 0x00, - 0x21, 0x6F, 0x72, 0x67, 0x2E, 0x61, 0x70, 0x61, 0x63, 0x68, 0x65, 0x2E, - 0x6C, 0x6F, 0x67, 0x34, 0x6A, 0x2E, 0x73, 0x70, 0x69, 0x2E, 0x4C, 0x6F, - 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x49, 0x6E, 0x66, 0x6F, 0xED, 0x99, - 0xBB, 0xE1, 0x4A, 0x91, 0xA5, 0x7C, 0x02, - 0x00, - 0x01, 0x4C, - 0x00, - 0x08, 0x66, 0x75, 0x6C, 0x6C, 0x49, 0x6E, 0x66, 0x6F, 0x74, - 0x00, - 0x12, 0x4C, 0x6A, 0x61, 0x76, 0x61, 0x2F, 0x6C, 0x61, 0x6E, 0x67, 0x2F, - 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x3B, 0x78, 0x70 - }; - os.writeProlog("org.apache.log4j.spi.LocationInfo", 2, (char*) prolog, sizeof(prolog), p); - char* line = p.itoa(lineNumber); - // - // construct Java-like fullInfo (replace "::" with ".") - // - std::string fullInfo(methodName); - size_t openParen = fullInfo.find('('); - - if (openParen != std::string::npos) - { - size_t space = fullInfo.find(' '); - - if (space != std::string::npos && space < openParen) - { - fullInfo.erase(0, space + 1); - } - } - - openParen = fullInfo.find('('); - - if (openParen != std::string::npos) - { - size_t classSep = fullInfo.rfind("::", openParen); - - if (classSep != std::string::npos) - { - fullInfo.replace(classSep, 2, "."); - } - else - { - fullInfo.insert(0, "."); - } - } - - fullInfo.append(1, '('); - fullInfo.append(fileName); - fullInfo.append(1, ':'); - fullInfo.append(line); - fullInfo.append(1, ')'); - os.writeUTFString(fullInfo, p); - } -} - diff --git a/src/main/cpp/loggingevent.cpp b/src/main/cpp/loggingevent.cpp index adba3c43..daf89500 100644 --- a/src/main/cpp/loggingevent.cpp +++ b/src/main/cpp/loggingevent.cpp @@ -33,7 +33,6 @@ #include <apr_portable.h> #include <apr_strings.h> #include <log4cxx/helpers/stringhelper.h> -#include <log4cxx/helpers/objectoutputstream.h> #include <log4cxx/helpers/bytebuffer.h> #include <log4cxx/logger.h> #include <log4cxx/private/log4cxx_private.h> diff --git a/src/main/cpp/objectoutputstream.cpp b/src/main/cpp/objectoutputstream.cpp deleted file mode 100644 index 0fee6e02..00000000 --- a/src/main/cpp/objectoutputstream.cpp +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <log4cxx/logstring.h> -#include <log4cxx/helpers/objectoutputstream.h> -#include <log4cxx/helpers/bytebuffer.h> -#include <log4cxx/helpers/outputstream.h> -#include <log4cxx/helpers/charsetencoder.h> - -using namespace log4cxx; -using namespace log4cxx::helpers; - -IMPLEMENT_LOG4CXX_OBJECT(ObjectOutputStream) - -typedef std::map<std::string, unsigned int> ClassDescriptionMap; - -struct ObjectOutputStream::ObjectOutputStreamPriv -{ - - OutputStreamPtr os; - log4cxx::helpers::CharsetEncoderPtr utf8Encoder; - const unsigned int objectHandleDefault; - unsigned int objectHandle; - ClassDescriptionMap classDescriptions; - - ObjectOutputStreamPriv(OutputStreamPtr outputStream, Pool& p) - : os(outputStream) - , utf8Encoder(CharsetEncoder::getUTF8Encoder()) - , objectHandleDefault(0x7E0000) - , objectHandle(objectHandleDefault) - {} -}; - -ObjectOutputStream::ObjectOutputStream(OutputStreamPtr outputStream, Pool& p) - : m_priv(std::make_unique<ObjectOutputStreamPriv>(outputStream, p)) -{ - unsigned char start[] = { 0xAC, 0xED, 0x00, 0x05 }; - ByteBuffer buf((char*) start, sizeof(start)); - m_priv->os->write(buf, p); -} - -ObjectOutputStream::~ObjectOutputStream() -{ -} - -void ObjectOutputStream::close(Pool& p) -{ - m_priv->os->close(p); -} - -void ObjectOutputStream::flush(Pool& p) -{ - m_priv->os->flush(p); -} - -void ObjectOutputStream::reset(Pool& p) -{ - m_priv->os->flush(p); - writeByte(TC_RESET, p); - m_priv->os->flush(p); - - m_priv->objectHandle = m_priv->objectHandleDefault; - m_priv->classDescriptions.clear(); -} - -void ObjectOutputStream::writeObject(const LogString& val, Pool& p) -{ - m_priv->objectHandle++; - writeByte(TC_STRING, p); - char bytes[2]; -#if LOG4CXX_LOGCHAR_IS_UTF8 - size_t len = val.size(); - ByteBuffer dataBuf(const_cast<char*>(val.data()), val.size()); -#else - size_t maxSize = 6 * val.size(); - char* data = p.pstralloc(maxSize); - ByteBuffer dataBuf(data, maxSize); - LogString::const_iterator iter(val.begin()); - m_priv->utf8Encoder->encode(val, iter, dataBuf); - dataBuf.flip(); - size_t len = dataBuf.limit(); -#endif - bytes[1] = (char) (len & 0xFF); - bytes[0] = (char) ((len >> 8) & 0xFF); - ByteBuffer lenBuf(bytes, sizeof(bytes)); - - m_priv->os->write(lenBuf, p); - m_priv->os->write(dataBuf, p); -} - -void ObjectOutputStream::writeObject(const MDC::Map& val, Pool& p) -{ - // - // TC_OBJECT and the classDesc for java.util.Hashtable - // - unsigned char prolog[] = - { - 0x72, 0x00, 0x13, 0x6A, 0x61, 0x76, 0x61, - 0x2E, 0x75, 0x74, 0x69, 0x6C, 0x2E, 0x48, 0x61, - 0x73, 0x68, 0x74, 0x61, 0x62, 0x6C, 0x65, 0x13, - 0xBB, 0x0F, 0x25, 0x21, 0x4A, 0xE4, 0xB8, 0x03, - 0x00, 0x02, 0x46, 0x00, 0x0A, 0x6C, 0x6F, 0x61, - 0x64, 0x46, 0x61, 0x63, 0x74, 0x6F, 0x72, 0x49, - 0x00, 0x09, 0x74, 0x68, 0x72, 0x65, 0x73, 0x68, - 0x6F, 0x6C, 0x64, 0x78, 0x70 - }; - writeProlog("java.util.Hashtable", 1, (char*) prolog, sizeof(prolog), p); - - // loadFactor = 0.75, threshold = 5, blockdata start, buckets.size = 7 - char data[] = { 0x3F, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, - TC_BLOCKDATA, 0x08, 0x00, 0x00, 0x00, 0x07 - }; - ByteBuffer dataBuf(data, sizeof(data)); - m_priv->os->write(dataBuf, p); - - char size[4]; - size_t sz = val.size(); - - size[3] = (char) (sz & 0xFF); - size[2] = (char) ((sz >> 8) & 0xFF); - size[1] = (char) ((sz >> 16) & 0xFF); - size[0] = (char) ((sz >> 24) & 0xFF); - - ByteBuffer sizeBuf(size, sizeof(size)); - m_priv->os->write(sizeBuf, p); - - for (MDC::Map::const_iterator iter = val.begin(); - iter != val.end(); - iter++) - { - writeObject(iter->first, p); - writeObject(iter->second, p); - } - - writeByte(TC_ENDBLOCKDATA, p); -} - -void ObjectOutputStream::writeUTFString(const std::string& val, Pool& p) -{ - char bytes[3]; - size_t len = val.size(); - ByteBuffer dataBuf(const_cast<char*>(val.data()), val.size()); - m_priv->objectHandle++; - - bytes[0] = 0x74; - bytes[1] = (char) ((len >> 8) & 0xFF); - bytes[2] = (char) (len & 0xFF); - - ByteBuffer lenBuf(bytes, sizeof(bytes)); - m_priv->os->write(lenBuf, p); - m_priv->os->write(dataBuf, p); -} - -void ObjectOutputStream::writeByte(char val, Pool& p) -{ - ByteBuffer buf(&val, 1); - m_priv->os->write(buf, p); -} - -void ObjectOutputStream::writeInt(int val, Pool& p) -{ - char bytes[4]; - - bytes[3] = (char) (val & 0xFF); - bytes[2] = (char) ((val >> 8) & 0xFF); - bytes[1] = (char) ((val >> 16) & 0xFF); - bytes[0] = (char) ((val >> 24) & 0xFF); - - ByteBuffer buf(bytes, sizeof(bytes)); - m_priv->os->write(buf, p); -} - -void ObjectOutputStream::writeLong(log4cxx_time_t val, Pool& p) -{ - char bytes[8]; - - bytes[7] = (char) (val & 0xFF); - bytes[6] = (char) ((val >> 8) & 0xFF); - bytes[5] = (char) ((val >> 16) & 0xFF); - bytes[4] = (char) ((val >> 24) & 0xFF); - bytes[3] = (char) ((val >> 32) & 0xFF); - bytes[2] = (char) ((val >> 40) & 0xFF); - bytes[1] = (char) ((val >> 48) & 0xFF); - bytes[0] = (char) ((val >> 56) & 0xFF); - - ByteBuffer buf(bytes, sizeof(bytes)); - m_priv->os->write(buf, p); -} - -void ObjectOutputStream::writeBytes(const char* bytes, size_t len, Pool& p) -{ - ByteBuffer buf(const_cast<char*>(bytes), len); - m_priv->os->write(buf, p); -} - -void ObjectOutputStream::writeNull(Pool& p) -{ - writeByte(TC_NULL, p); -} - -void ObjectOutputStream::writeProlog(const char* className, - int classDescIncrement, - char* classDesc, - size_t len, - Pool& p) -{ - ClassDescriptionMap::const_iterator match = m_priv->classDescriptions.find(className); - - if (match != m_priv->classDescriptions.end()) - { - char bytes[6]; - - bytes[0] = TC_OBJECT; - bytes[1] = TC_REFERENCE; - bytes[2] = (char) ((match->second >> 24) & 0xFF); - bytes[3] = (char) ((match->second >> 16) & 0xFF); - bytes[4] = (char) ((match->second >> 8) & 0xFF); - bytes[5] = (char) (match->second & 0xFF); - - ByteBuffer buf(bytes, sizeof(bytes)); - m_priv->os->write(buf, p); - - m_priv->objectHandle++; - } - else - { - m_priv->classDescriptions.insert(ClassDescriptionMap::value_type(className, m_priv->objectHandle)); - writeByte(TC_OBJECT, p); - - ByteBuffer buf(classDesc, len); - m_priv->os->write(buf, p); - - m_priv->objectHandle += (classDescIncrement + 1); - } -} diff --git a/src/main/cpp/sockethubappender.cpp b/src/main/cpp/sockethubappender.cpp deleted file mode 100644 index 676c1cb4..00000000 --- a/src/main/cpp/sockethubappender.cpp +++ /dev/null @@ -1,293 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <log4cxx/net/sockethubappender.h> - -#include <log4cxx/helpers/loglog.h> -#include <log4cxx/helpers/optionconverter.h> -#include <log4cxx/helpers/stringhelper.h> -#include <log4cxx/helpers/serversocket.h> -#include <log4cxx/spi/loggingevent.h> -#include <apr_atomic.h> -#include <apr_thread_proc.h> -#include <log4cxx/helpers/objectoutputstream.h> -#include <log4cxx/helpers/socketoutputstream.h> -#include <log4cxx/helpers/exception.h> -#include <log4cxx/helpers/threadutility.h> -#include <log4cxx/private/appenderskeleton_priv.h> -#include <mutex> - -namespace log4cxx -{ -using namespace helpers; -using namespace spi; - -namespace net -{ - -IMPLEMENT_LOG4CXX_OBJECT(SocketHubAppender) - -int SocketHubAppender::DEFAULT_PORT = 4560; - -struct SocketHubAppender::SocketHubAppenderPriv : public AppenderSkeleton::AppenderSkeletonPrivate -{ - SocketHubAppenderPriv(int port) : - AppenderSkeletonPrivate(), - port(port), - streams(), - locationInfo(false), - thread() {} - - int port; - ObjectOutputStreamList streams; - bool locationInfo; - std::thread thread; -}; - -#define _priv static_cast<SocketHubAppenderPriv*>(m_priv.get()) - -SocketHubAppender::~SocketHubAppender() -{ - finalize(); -} - -SocketHubAppender::SocketHubAppender() - : AppenderSkeleton (std::make_unique<SocketHubAppenderPriv>(SocketHubAppender::DEFAULT_PORT)) -{ -} - -SocketHubAppender::SocketHubAppender(int port1) - : AppenderSkeleton (std::make_unique<SocketHubAppenderPriv>(port1)) -{ - startServer(); -} - -void SocketHubAppender::activateOptions(Pool& /* p */ ) -{ - startServer(); -} - -void SocketHubAppender::setOption(const LogString& option, - const LogString& value) -{ - if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("PORT"), LOG4CXX_STR("port"))) - { - setPort(OptionConverter::toInt(value, DEFAULT_PORT)); - } - else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("LOCATIONINFO"), LOG4CXX_STR("locationinfo"))) - { - setLocationInfo(OptionConverter::toBoolean(value, true)); - } - else - { - AppenderSkeleton::setOption(option, value); - } -} - - -void SocketHubAppender::close() -{ - { - std::lock_guard<std::recursive_mutex> lock(_priv->mutex); - - if (_priv->closed) - { - return; - } - - _priv->closed = true; - } - - LogLog::debug(LOG4CXX_STR("closing SocketHubAppender ") + getName()); - - // - // wait until the server thread completes - // - if ( _priv->thread.joinable() ) - { - _priv->thread.join(); - } - - std::lock_guard<std::recursive_mutex> lock(_priv->mutex); - // close all of the connections - LogLog::debug(LOG4CXX_STR("closing client connections")); - - for (std::vector<helpers::ObjectOutputStreamPtr>::iterator iter = _priv->streams.begin(); - iter != _priv->streams.end(); - iter++) - { - if ( (*iter) != NULL) - { - try - { - (*iter)->close(_priv->pool); - } - catch (SocketException& e) - { - LogLog::error(LOG4CXX_STR("could not close socket: "), e); - } - } - } - - _priv->streams.erase(_priv->streams.begin(), _priv->streams.end()); - - - LogLog::debug(LOG4CXX_STR("SocketHubAppender ") - + getName() + LOG4CXX_STR(" closed")); -} - -void SocketHubAppender::append(const spi::LoggingEventPtr& event, Pool& p) -{ - - // if no open connections, exit now - if (_priv->streams.empty()) - { - return; - } - - LogString ndcVal; - event->getNDC(ndcVal); - event->getThreadName(); - // Get a copy of this thread's MDC. - event->getMDCCopy(); - - - // loop through the current set of open connections, appending the event to each - std::vector<ObjectOutputStreamPtr>::iterator it = _priv->streams.begin(); - std::vector<ObjectOutputStreamPtr>::iterator itEnd = _priv->streams.end(); - - while (it != itEnd) - { - // list size changed unexpectedly? Just exit the append. - if (*it == 0) - { - break; - } - - try - { - // event->write(**it, p); - (*it)->flush(p); - it++; - } - catch (std::exception& e) - { - // there was an io exception so just drop the connection - it = _priv->streams.erase(it); - itEnd = _priv->streams.end(); - LogLog::debug(LOG4CXX_STR("dropped connection"), e); - } - } -} - -void SocketHubAppender::startServer() -{ - _priv->thread = ThreadUtility::instance()->createThread( LOG4CXX_STR("SocketHub"), &SocketHubAppender::monitor, this ); -} - -void SocketHubAppender::monitor() -{ - ServerSocketUniquePtr serverSocket = 0; - - try - { - serverSocket = ServerSocket::create(_priv->port); - serverSocket->setSoTimeout(1000); - } - catch (SocketException& e) - { - LogLog::error(LOG4CXX_STR("exception setting timeout, shutting down server socket."), e); - return; - } - - bool stopRunning = _priv->closed; - - while (!stopRunning) - { - SocketPtr socket; - - try - { - socket = serverSocket->accept(); - } - catch (InterruptedIOException&) - { - // timeout occurred, so just loop - } - catch (SocketException& e) - { - LogLog::error(LOG4CXX_STR("exception accepting socket, shutting down server socket."), e); - stopRunning = true; - } - catch (IOException& e) - { - LogLog::error(LOG4CXX_STR("exception accepting socket."), e); - } - - // if there was a socket accepted - if (socket != 0) - { - try - { - InetAddressPtr remoteAddress = socket->getInetAddress(); - LogLog::debug(LOG4CXX_STR("accepting connection from ") - + remoteAddress->getHostName() - + LOG4CXX_STR(" (") - + remoteAddress->getHostAddress() - + LOG4CXX_STR(")")); - - // add it to the oosList. - std::lock_guard<std::recursive_mutex> lock(_priv->mutex); - OutputStreamPtr os(new SocketOutputStream(socket)); - Pool p; - ObjectOutputStreamPtr oos(new ObjectOutputStream(os, p)); - _priv->streams.push_back(oos); - } - catch (IOException& e) - { - LogLog::error(LOG4CXX_STR("exception creating output stream on socket."), e); - } - } - - stopRunning = (stopRunning || _priv->closed); - } -} - -void SocketHubAppender::setPort(int port1) -{ - _priv->port = port1; -} - -int SocketHubAppender::getPort() const -{ - return _priv->port; -} - -void SocketHubAppender::setLocationInfo(bool locationInfo1) -{ - _priv->locationInfo = locationInfo1; -} - -bool SocketHubAppender::getLocationInfo() const -{ - return _priv->locationInfo; -} - - -} // namespace net - -} //namespace log4cxx diff --git a/src/main/include/log4cxx/helpers/objectoutputstream.h b/src/main/include/log4cxx/helpers/objectoutputstream.h deleted file mode 100644 index 3d09bf96..00000000 --- a/src/main/include/log4cxx/helpers/objectoutputstream.h +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _LOG4CXX_HELPERS_OBJECTOUTPUTSTREAM_H -#define _LOG4CXX_HELPERS_OBJECTOUTPUTSTREAM_H - -#include <log4cxx/helpers/object.h> -#include <log4cxx/mdc.h> -#include <log4cxx/helpers/outputstream.h> -#include <log4cxx/helpers/charsetencoder.h> - -namespace log4cxx -{ -namespace helpers -{ -/** - * Emulates java serialization. - */ -class LOG4CXX_EXPORT ObjectOutputStream : public Object -{ - public: - DECLARE_ABSTRACT_LOG4CXX_OBJECT(ObjectOutputStream) - BEGIN_LOG4CXX_CAST_MAP() - LOG4CXX_CAST_ENTRY(ObjectOutputStream) - END_LOG4CXX_CAST_MAP() - - ObjectOutputStream(OutputStreamPtr os, Pool& p); - virtual ~ObjectOutputStream(); - - void close(Pool& p); - void flush(Pool& p); - void reset(Pool& p); - - void writeObject(const LogString&, Pool& p); - void writeUTFString(const std::string&, Pool& p); - void writeObject(const MDC::Map& mdc, Pool& p); - void writeInt(int val, Pool& p); - void writeLong(log4cxx_time_t val, Pool& p); - void writeProlog(const char* className, - int classDescIncrement, - char* bytes, - size_t len, - Pool& p); - void writeNull(Pool& p); - - enum { STREAM_MAGIC = 0xACED }; - enum { STREAM_VERSION = 5 }; - enum - { - TC_NULL = 0x70, - TC_REFERENCE = 0x71, - TC_CLASSDESC = 0x72, - TC_OBJECT = 0x73, - TC_STRING = 0x74, - TC_ARRAY = 0x75, - TC_CLASS = 0x76, - TC_BLOCKDATA = 0x77, - TC_ENDBLOCKDATA = 0x78, - TC_RESET = 0x79 - }; - enum - { - SC_WRITE_METHOD = 0x01, - SC_SERIALIZABLE = 0x02 - }; - - void writeByte(char val, Pool& p); - void writeBytes(const char* bytes, size_t len, Pool& p); - - private: - ObjectOutputStream(const ObjectOutputStream&); - ObjectOutputStream& operator=(const ObjectOutputStream&); - LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(ObjectOutputStreamPriv, m_priv) -}; - -LOG4CXX_PTR_DEF(ObjectOutputStream); -} // namespace helpers - -} //namespace log4cxx - -#endif //_LOG4CXX_HELPERS_OUTPUTSTREAM_H - diff --git a/src/main/include/log4cxx/net/socketappenderskeleton.h b/src/main/include/log4cxx/net/socketappenderskeleton.h index 9d511bc5..a5a01313 100644 --- a/src/main/include/log4cxx/net/socketappenderskeleton.h +++ b/src/main/include/log4cxx/net/socketappenderskeleton.h @@ -20,7 +20,6 @@ #include <log4cxx/appenderskeleton.h> #include <log4cxx/helpers/socket.h> -#include <log4cxx/helpers/objectoutputstream.h> #include <thread> #include <condition_variable> diff --git a/src/main/include/log4cxx/net/sockethubappender.h b/src/main/include/log4cxx/net/sockethubappender.h deleted file mode 100644 index 38c95a5d..00000000 --- a/src/main/include/log4cxx/net/sockethubappender.h +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _LOG4CXX_NET_SOCKET_HUB_APPENDER_H -#define _LOG4CXX_NET_SOCKET_HUB_APPENDER_H - -#include <log4cxx/appenderskeleton.h> -#include <vector> -#include <thread> -#include <log4cxx/helpers/objectoutputstream.h> - - -namespace log4cxx -{ -namespace helpers -{ -class ObjectOutputStream; -typedef std::shared_ptr<ObjectOutputStream> ObjectOutputStreamPtr; -} -namespace net -{ -LOG4CXX_LIST_DEF(ObjectOutputStreamList, log4cxx::helpers::ObjectOutputStreamPtr); - -/** -Sends {@link log4cxx::spi::LoggingEvent LoggingEvent} objects to a set of remote log -servers, usually a SocketNode. - -<p>Acts just like SocketAppender except that instead of -connecting to a given remote log server, -<code>SocketHubAppender</code> accepts connections from the remote -log servers as clients. It can accept more than one connection. -When a log event is received, the event is sent to the set of -currently connected remote log servers. Implemented this way it does -not require any update to the configuration file to send data to -another remote log server. The remote log server simply connects to -the host and port the <code>SocketHubAppender</code> is running on. - -<p>The <code>SocketHubAppender</code> does not store events such -that the remote side will events that arrived after the -establishment of its connection. Once connected, events arrive in -order as guaranteed by the TCP protocol. - -<p>This implementation borrows heavily from the SocketAppender. - -<p>The SocketHubAppender has the following characteristics: - -- If sent to a SocketNode, logging is non-intrusive as -far as the log event is concerned. In other words, the event will be -logged with the same time stamp, NDC, -location info as if it were logged locally. - -- <code>SocketHubAppender</code> does not use a layout. It -ships a serialized spi::LoggingEvent object to the remote side. - -- <code>SocketHubAppender</code> relies on the TCP -protocol. Consequently, if the remote side is reachable, then log -events will eventually arrive at remote client. - -- If no remote clients are attached, the logging requests are -simply dropped. - -- Logging events are automatically <em>buffered</em> by the -native TCP implementation. This means that if the link to remote -client is slow but still faster than the rate of (log) event -production, the application will not be affected by the slow network -connection. However, if the network connection is slower then the -rate of event production, then the local application can only -progress at the network rate. In particular, if the network link to -the the remote client is down, the application will be blocked. -@n @n On the other hand, if the network link is up, but the remote -client is down, the client will not be blocked when making log -requests but the log events will be lost due to client -unavailability. -@n @n The single remote client case extends to multiple clients -connections. The rate of logging will be determined by the slowest -link. - -- If the application hosting the <code>SocketHubAppender</code> -exits before the <code>SocketHubAppender</code> is closed either -explicitly or subsequent to garbage collection, then there might -be untransmitted data in the pipe which might be lost. This is a -common problem on Windows based systems. -@n @n To avoid lost data, it is usually sufficient to #close -the <code>SocketHubAppender</code> either explicitly or by calling -the LogManager#shutdown method before -exiting the application. -*/ - -class LOG4CXX_EXPORT SocketHubAppender : public AppenderSkeleton -{ - private: - /** - The default port number of the ServerSocket will be created on. - */ - static int DEFAULT_PORT; - - public: - DECLARE_LOG4CXX_OBJECT(SocketHubAppender) - BEGIN_LOG4CXX_CAST_MAP() - LOG4CXX_CAST_ENTRY(SocketHubAppender) - LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton) - END_LOG4CXX_CAST_MAP() - - SocketHubAppender(); - ~SocketHubAppender(); - - /** - Connects to remote server at <code>address</code> and <code>port</code>. - */ - SocketHubAppender(int port) ; - - /** - Set up the socket server on the specified port. - */ - void activateOptions(helpers::Pool& p) override; - - /** - Set options - */ - void setOption(const LogString& option, const LogString& value) override; - - void close() override; - - /** - Append an event to all of current connections. */ - void append(const spi::LoggingEventPtr& event, helpers::Pool& p) override; - - /** - The SocketHubAppender does not use a layout. Hence, this method returns - <code>false</code>. */ - bool requiresLayout() const override - { - return false; - } - - /** - The <b>Port</b> option takes a positive integer representing - the port where the server is waiting for connections. */ - void setPort(int port1); - - /** - Returns value of the <b>Port</b> option. */ - int getPort() const; - - /** - The <b>LocationInfo</b> option takes a boolean value. If true, - the information sent to the remote host will include location - information. By default no location information is sent to the server. */ - void setLocationInfo(bool locationInfo1); - - /** - Returns value of the <b>LocationInfo</b> option. */ - bool getLocationInfo() const; - - /** - Start the ServerMonitor thread. */ - private: - void startServer(); - - void monitor(); - - struct SocketHubAppenderPriv; - -}; // class SocketHubAppender -LOG4CXX_PTR_DEF(SocketHubAppender); -} // namespace net -} // namespace log4cxx - -#endif // _LOG4CXX_NET_SOCKET_HUB_APPENDER_H diff --git a/src/main/include/log4cxx/spi/location/locationinfo.h b/src/main/include/log4cxx/spi/location/locationinfo.h index 51b1ce4c..e473ff23 100644 --- a/src/main/include/log4cxx/spi/location/locationinfo.h +++ b/src/main/include/log4cxx/spi/location/locationinfo.h @@ -20,7 +20,6 @@ #include <log4cxx/log4cxx.h> #include <string> -#include <log4cxx/helpers/objectoutputstream.h> #if __cpp_lib_string_view || (_MSVC_LANG >= 201703L) #include <string_view> @@ -130,8 +129,6 @@ class LOG4CXX_EXPORT LocationInfo /** Returns the method name of the caller. */ const std::string getMethodName() const; - void write(log4cxx::helpers::ObjectOutputStream& os, log4cxx::helpers::Pool& p) const; - private: /** Caller's line number. */ diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 89fba0a8..a315cf5c 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -15,8 +15,4 @@ # limitations under the License. # -find_package(Java COMPONENTS Development) -if(Java_Development_FOUND) - add_subdirectory(java) -endif(Java_Development_FOUND) add_subdirectory(cpp) diff --git a/src/test/cpp/net/CMakeLists.txt b/src/test/cpp/net/CMakeLists.txt index d692f5ef..32e8a17a 100644 --- a/src/test/cpp/net/CMakeLists.txt +++ b/src/test/cpp/net/CMakeLists.txt @@ -18,7 +18,6 @@ # Tests defined in this directory if(LOG4CXX_NETWORKING_SUPPORT) set(NET_TESTS - sockethubappendertestcase syslogappendertestcase telnetappendertestcase xmlsocketappendertestcase diff --git a/src/test/cpp/net/sockethubappendertestcase.cpp b/src/test/cpp/net/sockethubappendertestcase.cpp deleted file mode 100644 index 3082c5ac..00000000 --- a/src/test/cpp/net/sockethubappendertestcase.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <log4cxx/net/sockethubappender.h> -#include "../appenderskeletontestcase.h" -#include <apr.h> - -using namespace log4cxx; -using namespace log4cxx::net; -using namespace log4cxx::helpers; - -#if APR_HAS_THREADS -/** - Unit tests of log4cxx::SocketHubAppender - */ -class SocketHubAppenderTestCase : public AppenderSkeletonTestCase -{ - LOGUNIT_TEST_SUITE(SocketHubAppenderTestCase); - // - // tests inherited from AppenderSkeletonTestCase - // - LOGUNIT_TEST(testDefaultThreshold); - LOGUNIT_TEST(testSetOptionThreshold); - LOGUNIT_TEST(testActivateClose); - LOGUNIT_TEST(testActivateSleepClose); - LOGUNIT_TEST(testActivateWriteClose); - LOGUNIT_TEST_SUITE_END(); - - - public: - - AppenderSkeleton* createAppenderSkeleton() const - { - return new log4cxx::net::SocketHubAppender(); - } - - void testActivateClose() - { - SocketHubAppenderPtr hubAppender(new SocketHubAppender()); - Pool p; - hubAppender->activateOptions(p); - hubAppender->close(); - } - - void testActivateSleepClose() - { - SocketHubAppenderPtr hubAppender(new SocketHubAppender()); - Pool p; - hubAppender->activateOptions(p); - std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) ); - hubAppender->close(); - } - - - void testActivateWriteClose() - { - SocketHubAppenderPtr hubAppender(new SocketHubAppender()); - Pool p; - hubAppender->activateOptions(p); - LoggerPtr root(Logger::getRootLogger()); - root->addAppender(hubAppender); - - for (int i = 0; i < 50; i++) - { - LOG4CXX_INFO(root, "Hello, World " << i); - } - - hubAppender->close(); - } -}; - -LOGUNIT_TEST_SUITE_REGISTRATION(SocketHubAppenderTestCase); -#endif diff --git a/src/test/java/CMakeLists.txt b/src/test/java/CMakeLists.txt deleted file mode 100644 index a5968efe..00000000 --- a/src/test/java/CMakeLists.txt +++ /dev/null @@ -1,61 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -find_package(Java) -include(UseJava) - -include(FetchContent) -FetchContent_Declare(log4j - URL https://downloads.apache.org/logging/log4j/1.2.17/log4j-1.2.17.tar.gz - URL_HASH SHA256=a528e3771ee6234661b5eb0fa02e14faee89dfec38de94088c163ddf2a0a8663 -) - -find_jar(LOG4J NAMES log4j-1.2 DOC "System log4j location") - -if(${LOG4J} STREQUAL "LOG4J-NOTFOUND") - FetchContent_GetProperties(log4j) - if(NOT log4j_POPULATED) - FetchContent_Populate(log4j) - endif() - set(log4j_CLASSPATH "${log4j_SOURCE_DIR}/log4j-1.2.17.jar" ) -else() - set(log4j_CLASSPATH "${LOG4J}" ) -endif() - -set(SOCKET_SERVER_SOURCES org/apache/log4j/net/ShortSocketServer.java) -add_custom_target(test-classes - COMMAND ${Java_JAVAC_EXECUTABLE} -d ${CMAKE_CURRENT_BINARY_DIR} - -classpath "${log4j_CLASSPATH}" ${SOCKET_SERVER_SOURCES} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} -) -if(WIN32) - set(SOCKET_SERVER_CLASSPATH "${CMAKE_CURRENT_BINARY_DIR};${log4j_CLASSPATH}") -else() - set(SOCKET_SERVER_CLASSPATH "${CMAKE_CURRENT_BINARY_DIR}:${log4j_CLASSPATH}") -endif() - -# Use a parameter file to avoid issues with processing ';' -file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/StartSocketServerParams.txt -"${Java_JAVA_EXECUTABLE} --classpath -${SOCKET_SERVER_CLASSPATH} -org.apache.log4j.net.ShortSocketServer -8 -input/socketServer -" -) -set(START_SOCKET_SERVER_PARAMETER_FILE ${CMAKE_CURRENT_BINARY_DIR}/StartSocketServerParams.txt PARENT_SCOPE) diff --git a/src/test/java/org/apache/log4j/net/ShortSocketServer.java b/src/test/java/org/apache/log4j/net/ShortSocketServer.java deleted file mode 100644 index 76a46d18..00000000 --- a/src/test/java/org/apache/log4j/net/ShortSocketServer.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -package org.apache.log4j.net; - -import java.net.Socket; -import java.net.ServerSocket; -import java.io.IOException; - -import org.apache.log4j.Logger; -import org.apache.log4j.LogManager; -import org.apache.log4j.PropertyConfigurator; -import org.apache.log4j.MDC; -import org.apache.log4j.helpers.LogLog; -import org.apache.log4j.net.SocketNode; -import org.apache.log4j.net.SocketServer; - -/** - * This SocketServer exits after certain number of connections from a - * client. This number is determined the totalsTest parameter, that is - * the first argument on the commmand line. The second argument, - * prefix, determines the prefix of the configuration file to - * use. Each run of the server will use a different properties - * file. For the i-th run, the path to the file is - * (prefix+i+".properties"). - * - * @author Ceki Gulcu */ - -public class ShortSocketServer { - - static Logger cat = Logger.getLogger(ShortSocketServer.class); - - public - static - void main(String args[]) throws Exception { - int totalTests = 0; - String prefix = null; - - if(args.length == 2) { - totalTests = Integer.parseInt(args[0]); - prefix = args[1]; - } else { - usage("Wrong number of arguments."); - } - - - int port = Integer.valueOf(System.getProperty("port", "12345")); - LogLog.debug("Listening on port " + port); - ServerSocket serverSocket = new ServerSocket(port); - - MDC.put("hostID", "shortSocketServer"); - - for(int i = 1; i <= totalTests; i++) { - PropertyConfigurator.configure(prefix+i+".properties"); - LogLog.debug("Waiting to accept a new client."); - Socket socket = serverSocket.accept(); - LogLog.debug("Connected to client at " + socket.getInetAddress()); - LogLog.debug("Starting new socket node."); - SocketNode sn = new SocketNode(socket, LogManager.getLoggerRepository()); - Thread t = new Thread(sn); - t.start(); - t.join(); - } - } - - - static - void usage(String msg) { - System.err.println(msg); - System.err.println( - "Usage: java " +ShortSocketServer.class.getName() + " totalTests configFilePrefix"); - System.exit(1); - } -}
