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/04/12 08:08:07 UTC

[logging-log4cxx] branch smtp_appender created (now 2bebb265)

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

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


      at 2bebb265 Fix SMTP appender

This branch includes the following new commits:

     new 2bebb265 Fix SMTP appender

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[logging-log4cxx] 01/01: Fix SMTP appender

Posted by sw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 2bebb26593a610afae5dc63b3518cf1001b45994
Author: Stephen Webb <sw...@gmail.com>
AuthorDate: Wed Apr 12 18:07:39 2023 +1000

    Fix SMTP appender
---
 src/CMakeLists.txt                            |  4 ++
 src/cmake/FindESMTP.cmake                     | 76 +++++++++++++++++++++++++++
 src/main/cpp/smtpappender.cpp                 | 33 ++++++------
 src/main/include/CMakeLists.txt               | 12 +++--
 src/main/include/log4cxx/private/string_c11.h |  2 +-
 src/test/cpp/CMakeLists.txt                   |  3 ++
 6 files changed, 109 insertions(+), 21 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 788320de..456ea0ba 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -22,6 +22,10 @@ add_subdirectory(main)
 target_compile_definitions(log4cxx PRIVATE ${LOG4CXX_COMPILE_DEFINITIONS} ${APR_COMPILE_DEFINITIONS} ${APR_UTIL_COMPILE_DEFINITIONS} )
 target_include_directories(log4cxx INTERFACE $<INSTALL_INTERFACE:include> PRIVATE ${APR_INCLUDE_DIR} ${APR_UTIL_INCLUDE_DIR})
 target_link_libraries(log4cxx PRIVATE ${APR_UTIL_LIBRARIES} ${EXPAT_LIBRARIES} ${APR_LIBRARIES} ${APR_SYSTEM_LIBS})
+if(HAS_LIBESMTP)
+  target_include_directories(log4cxx PRIVATE ${ESMTP_INCLUDE_DIRS})
+  target_link_libraries(log4cxx PRIVATE ${ESMTP_LIBRARIES})
+endif()
 if(WIN32)
 	# The ODBC appender is always enabled in the Windows configuration
 	target_link_libraries(log4cxx PRIVATE odbc32.lib)
diff --git a/src/cmake/FindESMTP.cmake b/src/cmake/FindESMTP.cmake
new file mode 100644
index 00000000..2e6a6533
--- /dev/null
+++ b/src/cmake/FindESMTP.cmake
@@ -0,0 +1,76 @@
+# Locate libesmtp include paths and libraries
+include(FindPackageHandleStandardArgs)
+
+# This module defines
+# ESMTP_INCLUDE_DIR, where to find libesmtp.h, etc.
+# ESMTP_LIBRARIES, the libraries to link against to use libesmtp.
+# ESMTP_FOUND, set to 'yes' if found
+macro(_esmtp_invoke _varname)
+    execute_process(
+        COMMAND ${_esmtp_CONFIG_EXECUTABLE} ${ARGN}
+        OUTPUT_VARIABLE _esmtp_output
+        RESULT_VARIABLE _esmtp_failed
+    )
+
+    if(_esmtp_failed)
+        message(FATAL_ERROR "${_esmtp_CONFIG_EXECUTABLE} ${ARGN} failed with result ${_esmtp_failed}")
+    else()
+        string(REGEX REPLACE "[\r\n]"  "" _esmtp_output "${_esmtp_output}")
+        string(REGEX REPLACE " +$"     "" _esmtp_output "${_esmtp_output}")
+        string(REGEX REPLACE "^ +"     "" _esmtp_output "${_esmtp_output}")
+
+        separate_arguments(_esmtp_output)
+        set(${_varname} "${_esmtp_output}")
+    endif(_esmtp_failed)
+endmacro(_esmtp_invoke)
+
+if(NOT LIBESMTP_STATIC) # libesmtp-config does not support --static used in FindPkgConfig.cmake
+find_package(PkgConfig)
+pkg_check_modules(esmtp esmtp)
+message("esmtp_FOUND=${esmtp_FOUND}")
+endif()
+
+if(esmtp_FOUND)
+  find_path(ESMTP_INCLUDE_DIR
+            NAMES libesmtp.h
+            HINTS ${ESMTP_INCLUDE_DIRS}
+            PATH_SUFFIXES esmtp)
+  if (LIBESMTP_STATIC OR NOT BUILD_SHARED_LIBS)
+    set(ESMTP_LIBRARIES ${LIBESMTP_STATIC_LINK_LIBRARIES})
+  else()
+    set(ESMTP_LIBRARIES ${ESMTP_LINK_LIBRARIES})
+  endif()
+else()
+  find_program(_esmtp_CONFIG_EXECUTABLE
+      libesmtp-config
+      PATHS /usr/local/opt/libesmtp/bin    $ENV{ProgramFiles}/esmtp/bin
+      )
+  mark_as_advanced(_esmtp_CONFIG_EXECUTABLE)
+  if(EXISTS ${_esmtp_CONFIG_EXECUTABLE})
+      _esmtp_invoke(_esmtp_cflags_args  --cflags)
+      message("_esmtp_cflags_args=${_esmtp_cflags_args}")
+      string(REGEX MATCH "-I([^ ;]+)" _esmtp_include_flag "${_esmtp_cflags_args}")
+      set(ESMTP_INCLUDE_DIR  "${CMAKE_MATCH_1}")
+      if (LIBESMTP_STATIC OR NOT BUILD_SHARED_LIBS)
+        find_library(ESMTP_LIBRARIES NAMES libesmtp.a)
+      else()
+        _esmtp_invoke(ESMTP_LIBRARIES  --libs)
+      endif()
+  else()
+      find_path(ESMTP_INCLUDE_DIR libesmtp.h PATH_SUFFIXES esmtp)
+      if (LIBESMTP_STATIC OR NOT BUILD_SHARED_LIBS)
+        find_library(ESMTP_LIBRARIES NAMES libesmtp.a)
+      else()
+        find_library(ESMTP_LIBRARIES NAMES esmtp)
+      endif()
+  endif()
+endif()
+if(ESMTP_LIBRARIES)
+  set(ESMTP_FOUND TRUE)
+endif()
+
+message("ESMTP_FOUND=${ESMTP_FOUND}")
+message("ESMTP_INCLUDE_DIR=${ESMTP_INCLUDE_DIR}")
+message("ESMTP_LIBRARIES=${ESMTP_LIBRARIES}")
+find_package_handle_standard_args(ESMTP
+    ESMTP_INCLUDE_DIR ESMTP_LIBRARIES)
diff --git a/src/main/cpp/smtpappender.cpp b/src/main/cpp/smtpappender.cpp
index b295c1cf..47f4bfe8 100644
--- a/src/main/cpp/smtpappender.cpp
+++ b/src/main/cpp/smtpappender.cpp
@@ -19,6 +19,7 @@
 #include <log4cxx/helpers/loglog.h>
 #include <log4cxx/helpers/optionconverter.h>
 #include <log4cxx/spi/loggingevent.h>
+#include <log4cxx/private/string_c11.h>
 #include <log4cxx/helpers/stringhelper.h>
 #include <log4cxx/helpers/stringtokenizer.h>
 #include <log4cxx/helpers/transcoder.h>
@@ -192,7 +193,7 @@ class SMTPMessage
 			const LogString msg, Pool& p)
 		{
 			message = smtp_add_message(session);
-			current_len = str.length();
+			current_len = msg.length();
 			body = current = toMessage(msg, p);
 			messagecbState = 0;
 			smtp_set_reverse_path(message, toAscii(from, p));
@@ -676,23 +677,23 @@ bool SMTPAppender::checkEntryConditions()
 {
 #if LOG4CXX_HAVE_LIBESMTP
 
-	if ((to.empty() && cc.empty() && bcc.empty()) || from.empty() || smtpHost.empty())
+	if ((_priv->to.empty() && _priv->cc.empty() && _priv->bcc.empty()) || _priv->from.empty() || _priv->smtpHost.empty())
 	{
-		errorHandler->error(LOG4CXX_STR("Message not configured."));
+		_priv->errorHandler->error(LOG4CXX_STR("Message not configured."));
 		return false;
 	}
 
-	if (evaluator == 0)
+	if (_priv->evaluator == 0)
 	{
-		errorHandler->error(LOG4CXX_STR("No TriggeringEventEvaluator is set for appender [") +
-			name + LOG4CXX_STR("]."));
+		_priv->errorHandler->error(LOG4CXX_STR("No TriggeringEventEvaluator is set for appender [") +
+			_priv->name + LOG4CXX_STR("]."));
 		return false;
 	}
 
 
-	if (layout == 0)
+	if (_priv->layout == 0)
 	{
-		errorHandler->error(LOG4CXX_STR("No layout set for appender named [") + name + LOG4CXX_STR("]."));
+		_priv->errorHandler->error(LOG4CXX_STR("No layout set for appender named [") + _priv->name + LOG4CXX_STR("]."));
 		return false;
 	}
 
@@ -751,22 +752,22 @@ void SMTPAppender::sendBuffer(Pool& p)
 	try
 	{
 		LogString sbuf;
-		layout->appendHeader(sbuf, p);
+		_priv->layout->appendHeader(sbuf, p);
 
-		int len = cb.length();
+		int len = _priv->cb.length();
 
 		for (int i = 0; i < len; i++)
 		{
-			LoggingEventPtr event = cb.get();
-			layout->format(sbuf, event, p);
+			LoggingEventPtr event = _priv->cb.get();
+			_priv->layout->format(sbuf, event, p);
 		}
 
-		layout->appendFooter(sbuf, p);
+		_priv->layout->appendFooter(sbuf, p);
 
-		SMTPSession session(smtpHost, smtpPort, smtpUsername, smtpPassword, p);
+		SMTPSession session(_priv->smtpHost, _priv->smtpPort, _priv->smtpUsername, _priv->smtpPassword, p);
 
-		SMTPMessage message(session, from, to, cc,
-			bcc, subject, sbuf, p);
+		SMTPMessage message(session, _priv->from, _priv->to, _priv->cc,
+			_priv->bcc, _priv->subject, sbuf, p);
 
 		session.send(p);
 
diff --git a/src/main/include/CMakeLists.txt b/src/main/include/CMakeLists.txt
index 4b142fdc..49dceb6d 100644
--- a/src/main/include/CMakeLists.txt
+++ b/src/main/include/CMakeLists.txt
@@ -129,11 +129,15 @@ else()
 endif(LOG4CXX_ENABLE_ODBC)
 
 option(LOG4CXX_ENABLE_ESMTP "Support logging via libesmtp" OFF)
+option(LIBESMTP_STATIC "Link to the libesmtp static library" OFF)
 if(LOG4CXX_ENABLE_ESMTP)
-    CHECK_LIBRARY_EXISTS(esmtp smtp_create_session "" HAS_LIBESMTP)
-    if(NOT HAS_LIBESMTP)
-        message(SEND_ERROR "SMTP support with libesmtp not found but requested")
-    endif()
+    find_package(ESMTP)
+    if(ESMTP_FOUND)
+       set(HAS_LIBESMTP 1)
+    else()
+       set(HAS_LIBESMTP 0)
+       message(SEND_ERROR "libesmtp not found but SMTP support requested")
+    endif(ESMTP_FOUND)
 else()
     set(HAS_LIBESMTP 0)
 endif(LOG4CXX_ENABLE_ESMTP)
diff --git a/src/main/include/log4cxx/private/string_c11.h b/src/main/include/log4cxx/private/string_c11.h
index f7182454..4c5a1b44 100644
--- a/src/main/include/log4cxx/private/string_c11.h
+++ b/src/main/include/log4cxx/private/string_c11.h
@@ -24,7 +24,7 @@
 #include <limits.h>
 #if !defined(RSIZE_MAX) && defined(SSIZE_MAX)
 #define RSIZE_MAX (SSIZE_MAX >> 4)
-#else !defined(RSIZE_MAX)
+#else // !defined(RSIZE_MAX)
 #define RSIZE_MAX (2 << 20)
 #endif
 
diff --git a/src/test/cpp/CMakeLists.txt b/src/test/cpp/CMakeLists.txt
index 751abf8c..35f1cf2d 100644
--- a/src/test/cpp/CMakeLists.txt
+++ b/src/test/cpp/CMakeLists.txt
@@ -107,6 +107,9 @@ foreach(testName IN LISTS ALL_LOG4CXX_TESTS)
     target_compile_definitions(${testName} PRIVATE ${TEST_COMPILE_DEFINITIONS} ${LOG4CXX_COMPILE_DEFINITIONS} ${APR_COMPILE_DEFINITIONS} ${APR_UTIL_COMPILE_DEFINITIONS} )
     target_include_directories(${testName} PRIVATE ${CMAKE_CURRENT_LIST_DIR} $<TARGET_PROPERTY:log4cxx,INCLUDE_DIRECTORIES>)
     target_link_libraries(${testName} PRIVATE testingFramework testingUtilities log4cxx ${APR_LIBRARIES} ${APR_SYSTEM_LIBS} Threads::Threads)
+    if(HAS_LIBESMTP)
+      target_link_libraries(${testName} PRIVATE ${ESMTP_LIBRARIES})
+    endif()
     add_test(NAME ${testName}
         COMMAND ${testName} -v
         WORKING_DIRECTORY ${UNIT_TEST_WORKING_DIR}