You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by sw...@apache.org on 2023/11/14 03:33:35 UTC

(logging-log4cxx) branch master updated: Use default std::stringstream formatting attributes in each log request (#288)

This is an automated email from the ASF dual-hosted git repository.

swebb2066 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git


The following commit(s) were added to refs/heads/master by this push:
     new 4b20aa6f Use default std::stringstream formatting attributes in each log request (#288)
4b20aa6f is described below

commit 4b20aa6f4d56b8452e741fb6524d84bdb17b9dd0
Author: vopl <vo...@bk.ru>
AuthorDate: Tue Nov 14 06:33:31 2023 +0300

    Use default std::stringstream formatting attributes in each log request (#288)
    
    * add test to verify outputstream resets
    
    ---------
    
    Co-authored-by: Robert Middleton <ro...@rm5248.com>
---
 src/main/cpp/messagebuffer.cpp                     |  5 ++++
 src/test/cpp/helpers/messagebuffertest.cpp         | 28 ++++++++++++++++++++++
 src/test/resources/input/messagebuffer1.properties | 21 ++++++++++++++++
 src/test/resources/witness/messagebuffer.1         |  2 ++
 4 files changed, 56 insertions(+)

diff --git a/src/main/cpp/messagebuffer.cpp b/src/main/cpp/messagebuffer.cpp
index bad3296a..461e9a9c 100644
--- a/src/main/cpp/messagebuffer.cpp
+++ b/src/main/cpp/messagebuffer.cpp
@@ -54,9 +54,14 @@ struct StringOrStream
 		if (!this->stream)
 		{
 #if LOG4CXX_HAS_THREAD_LOCAL
+			const static std::basic_ostringstream<T> initialState;
 			thread_local static std::basic_ostringstream<T> sStream;
 			this->stream = &sStream;
 			this->stream->clear();
+			this->stream->precision(initialState.precision());
+			this->stream->width(initialState.width());
+			this->stream->setf(initialState.flags(), ~initialState.flags());
+			this->stream->fill(initialState.fill());
 #else
 			this->stream = new std::basic_ostringstream<T>();
 #endif
diff --git a/src/test/cpp/helpers/messagebuffertest.cpp b/src/test/cpp/helpers/messagebuffertest.cpp
index deba396b..f953ea30 100644
--- a/src/test/cpp/helpers/messagebuffertest.cpp
+++ b/src/test/cpp/helpers/messagebuffertest.cpp
@@ -21,6 +21,9 @@
 #include "../logunit.h"
 #include <log4cxx/logstring.h>
 #include <log4cxx/helpers/loglog.h>
+#include <log4cxx/logger.h>
+#include <log4cxx/propertyconfigurator.h>
+#include "util/compare.h"
 
 #if LOG4CXX_CFSTRING_API
 	#include <CoreFoundation/CFString.h>
@@ -42,6 +45,7 @@ LOGUNIT_CLASS(MessageBufferTest)
 	LOGUNIT_TEST(testInsertNull);
 	LOGUNIT_TEST(testInsertInt);
 	LOGUNIT_TEST(testInsertManipulator);
+	LOGUNIT_TEST(testBaseChange);
 #if LOG4CXX_WCHAR_T_API
 	LOGUNIT_TEST(testInsertConstWStr);
 	LOGUNIT_TEST(testInsertWString);
@@ -136,6 +140,30 @@ public:
 		LOGUNIT_ASSERT_EQUAL(true, buf.hasStream());
 	}
 
+	void testBaseChange()
+	{
+		LoggerPtr root;
+		LoggerPtr logger;
+
+		root = Logger::getRootLogger();
+		logger = Logger::getLogger(LOG4CXX_STR("java.org.apache.log4j.PatternLayoutTest"));
+
+		PropertyConfigurator::configure(LOG4CXX_FILE("input/messagebuffer1.properties"));
+
+		int num = 220;
+		LOG4CXX_INFO(logger, "number in hex: " << std::hex << num);
+		LOG4CXX_INFO(logger, "number in dec: " << num);
+
+		LOGUNIT_ASSERT(Compare::compare(LOG4CXX_STR("output/messagebuffer"), LOG4CXX_FILE("witness/messagebuffer.1")));
+
+		auto rep = root->getLoggerRepository();
+
+		if (rep)
+		{
+			rep->resetConfiguration();
+		}
+	}
+
 #if LOG4CXX_WCHAR_T_API
 	void testInsertConstWStr()
 	{
diff --git a/src/test/resources/input/messagebuffer1.properties b/src/test/resources/input/messagebuffer1.properties
new file mode 100644
index 00000000..f80075be
--- /dev/null
+++ b/src/test/resources/input/messagebuffer1.properties
@@ -0,0 +1,21 @@
+# 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.
+#
+log4j.rootCategory=DEBUG, testAppender
+log4j.appender.testAppender=org.apache.log4j.FileAppender
+log4j.appender.testAppender.file=output/messagebuffer
+log4j.appender.testAppender.Append=false
+log4j.appender.testAppender.layout=org.apache.log4j.PatternLayout
+log4j.appender.testAppender.layout.ConversionPattern=%-5p - %m%n
diff --git a/src/test/resources/witness/messagebuffer.1 b/src/test/resources/witness/messagebuffer.1
new file mode 100644
index 00000000..71145f64
--- /dev/null
+++ b/src/test/resources/witness/messagebuffer.1
@@ -0,0 +1,2 @@
+INFO  - number in hex: dc
+INFO  - number in dec: 220