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 3830f5e4 Prevent SMTP appender removing a blank line in the message 
(#327)
3830f5e4 is described below

commit 3830f5e4e7f34b3c9dc834dfe614049078ffb20c
Author: Stephen Webb <[email protected]>
AuthorDate: Thu Jan 11 09:57:26 2024 +1100

    Prevent SMTP appender removing a blank line in the message (#327)
    
    * Document smtp server-in-the-loop testing
    
    * Make the default error handler usable in unit tests
---
 src/main/cpp/onlyonceerrorhandler.cpp              |  6 ++++
 src/main/cpp/smtpappender.cpp                      | 12 +++----
 .../include/log4cxx/helpers/onlyonceerrorhandler.h |  5 +++
 src/test/cpp/net/smtpappendertestcase.cpp          | 23 ++++++++++---
 src/test/resources/input/xml/smtpAppenderValid.xml | 39 ++++++++++++++++++++++
 5 files changed, 75 insertions(+), 10 deletions(-)

diff --git a/src/main/cpp/onlyonceerrorhandler.cpp 
b/src/main/cpp/onlyonceerrorhandler.cpp
index b9c99be0..a124ede7 100644
--- a/src/main/cpp/onlyonceerrorhandler.cpp
+++ b/src/main/cpp/onlyonceerrorhandler.cpp
@@ -91,3 +91,9 @@ void OnlyOnceErrorHandler::setAppender(const AppenderPtr&)
 void OnlyOnceErrorHandler::setBackupAppender(const AppenderPtr&)
 {
 }
+
+bool OnlyOnceErrorHandler::errorReported() const
+{
+       return !m_priv->firstTime;
+}
+
diff --git a/src/main/cpp/smtpappender.cpp b/src/main/cpp/smtpappender.cpp
index 2b71610f..a09d6250 100644
--- a/src/main/cpp/smtpappender.cpp
+++ b/src/main/cpp/smtpappender.cpp
@@ -260,7 +260,7 @@ class SMTPMessage
                        char* retval = p.pstralloc(str.length() + feedCount + 
1);
                        char* current = retval;
                        char* startOfLine = current;
-                       bool ignoreCRLF = false;
+                       unsigned int ignoreChar = 0;
 
                        //
                        //    iterator through message
@@ -279,15 +279,15 @@ class SMTPMessage
                                        //
                                        //   replace any stray CR or LF with 
CRLF
                                        //      reset start of line
-                                       if (!ignoreCRLF || startOfLine < 
current)
+                                       if (c == ignoreChar && current == 
startOfLine)
+                                               ignoreChar = 0;
+                                       else
                                        {
                                                *current++ = 0x0D;
                                                *current++ = 0x0A;
                                                startOfLine = current;
-                                               ignoreCRLF = true;
+                                               ignoreChar = (c == 0x0A ? 0x0D 
: 0x0A);
                                        }
-                                       else
-                                               ignoreCRLF = false;
                                }
                                else
                                {
@@ -764,7 +764,7 @@ void SMTPAppender::sendBuffer(Pool& p)
        }
        catch (std::exception& e)
        {
-               LogLog::error(LOG4CXX_STR("Error occured while sending e-mail 
notification."), e);
+               _priv->errorHandler->error(LOG4CXX_STR("Error occured while 
sending e-mail to [") + _priv->smtpHost + LOG4CXX_STR("]."), e, 0);
        }
 
 #endif
diff --git a/src/main/include/log4cxx/helpers/onlyonceerrorhandler.h 
b/src/main/include/log4cxx/helpers/onlyonceerrorhandler.h
index 0c7f7508..d2a8cf91 100644
--- a/src/main/include/log4cxx/helpers/onlyonceerrorhandler.h
+++ b/src/main/include/log4cxx/helpers/onlyonceerrorhandler.h
@@ -103,6 +103,11 @@ class LOG4CXX_EXPORT OnlyOnceErrorHandler :
                Does not do anything.
                */
                void setBackupAppender(const AppenderPtr& appender) override;
+
+               /**
+               Has an error been reported?
+               */
+               bool errorReported() const;
 };
 }  // namespace helpers
 } // namespace log4cxx
diff --git a/src/test/cpp/net/smtpappendertestcase.cpp 
b/src/test/cpp/net/smtpappendertestcase.cpp
index c67e631c..213cf4f3 100644
--- a/src/test/cpp/net/smtpappendertestcase.cpp
+++ b/src/test/cpp/net/smtpappendertestcase.cpp
@@ -23,6 +23,7 @@
 #include <log4cxx/xml/domconfigurator.h>
 #include <log4cxx/logmanager.h>
 #include <log4cxx/simplelayout.h>
+#include <log4cxx/helpers/onlyonceerrorhandler.h>
 
 namespace LOG4CXX_NS
 {
@@ -74,7 +75,13 @@ class SMTPAppenderTestCase : public AppenderSkeletonTestCase
                LOGUNIT_TEST(testSetOptionThreshold);
                LOGUNIT_TEST(testTrigger);
                LOGUNIT_TEST(testInvalid);
-               //LOGUNIT_TEST(testValid);
+//#define LOG4CXX_TEST_EMAIL_AND_SMTP_HOST_ARE_IN_ENVIRONMENT_VARIABLES
+#ifdef LOG4CXX_TEST_EMAIL_AND_SMTP_HOST_ARE_IN_ENVIRONMENT_VARIABLES
+               // This test requires the following environment variables:
+               // LOG4CXX_TEST_EMAIL_RECIPIENT - where the email is sent
+               // LOG4CXX_TEST_SMTP_HOST_NAME - the email server
+               LOGUNIT_TEST(testValid);
+#endif
                LOGUNIT_TEST_SUITE_END();
 
 
@@ -119,7 +126,10 @@ class SMTPAppenderTestCase : public 
AppenderSkeletonTestCase
                        auto root = Logger::getRootLogger();
                        root->addAppender(appender);
                        LOG4CXX_INFO(root, "Hello, World.");
-                       LOG4CXX_ERROR(root, "Sending Message");
+                       LOG4CXX_ERROR(root, "Sending Message"); // The 
DefaultEvaluator should trigger e-mail generation
+                       auto eh = 
dynamic_cast<helpers::OnlyOnceErrorHandler*>(appender->getErrorHandler().get());
+                       LOGUNIT_ASSERT(eh);
+                       LOGUNIT_ASSERT(eh->errorReported());
                }
 
 
@@ -127,8 +137,13 @@ class SMTPAppenderTestCase : public 
AppenderSkeletonTestCase
                {
                        
xml::DOMConfigurator::configure("input/xml/smtpAppenderValid.xml");
                        auto root = Logger::getRootLogger();
-                       LOG4CXX_INFO(root, "Hello, World.");
-                       LOG4CXX_ERROR(root, "Sending Message");
+                       LOG4CXX_INFO(root, "Hello, World.\n\nThis paragraph 
should be preceeded by a blank line.");
+
+                       auto appender = 
log4cxx::cast<SMTPAppender>(root->getAppender(LOG4CXX_STR("A1")));
+                       LOGUNIT_ASSERT(appender);
+                       auto eh = 
dynamic_cast<helpers::OnlyOnceErrorHandler*>(appender->getErrorHandler().get());
+                       LOGUNIT_ASSERT(eh);
+                       LOGUNIT_ASSERT(!eh->errorReported());
                }
 };
 
diff --git a/src/test/resources/input/xml/smtpAppenderValid.xml 
b/src/test/resources/input/xml/smtpAppenderValid.xml
new file mode 100644
index 00000000..7df3d148
--- /dev/null
+++ b/src/test/resources/input/xml/smtpAppenderValid.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+<!--
+  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:configuration xmlns:log4j="http://jakarta.apache.org/log4j/";>
+  <appender name="A1" class="org.apache.log4j.net.SMTPAppender">
+    <param name="from" value="[email protected]" />
+    <param name="to" value="${LOG4CXX_TEST_EMAIL_RECIPIENT}" />
+    <param name="subject" value="Test message" />
+    <param name="SMTPHost" value="${LOG4CXX_TEST_SMTP_HOST_NAME}"/>
+    <param name="SMTPPort" value="587"/>
+    <triggeringPolicy 
class="org.apache.log4j.net.SMTPAppenderTest$MockTriggeringEventEvaluator"/>
+
+    <layout class="org.apache.log4j.PatternLayout">
+      <param name="ConversionPattern" value="%-5p %c{2} - %m%n"/>
+    </layout>
+  </appender>
+
+  <root>
+    <priority value ="info" />
+    <appender-ref ref="A1" />
+  </root>
+
+</log4j:configuration>

Reply via email to