From 7f7077f5566e11ee5ac98818d190c4a119ea9a17 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Tue, 23 Jul 2019 13:40:44 -0300 Subject: [PATCH] Adding option to use syslog (when available). --- configure.ac | 1 + src/include/config.h.in | 1 + src/include/lib3270/log.h | 10 ++++++++++ src/lib3270/init.c | 11 +++++++++++ src/lib3270/linux/log.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib3270/log.c | 2 ++ src/lib3270/private.h | 6 ++++++ src/lib3270/windows/log.c | 5 +++++ 8 files changed, 82 insertions(+), 0 deletions(-) diff --git a/configure.ac b/configure.ac index 799e2fc..3f61c8f 100644 --- a/configure.ac +++ b/configure.ac @@ -463,6 +463,7 @@ dnl Check for headers dnl --------------------------------------------------------------------------- AC_CHECK_HEADER(malloc.h, AC_DEFINE(HAVE_MALLOC_H,,[do we have malloc.h?])) +AC_CHECK_HEADER(syslog.h, AC_DEFINE(HAVE_SYSLOG,,[do we have malloc.h?])) AC_CHECK_FUNCS(getaddrinfo, AC_DEFINE(HAVE_GETADDRINFO) ) AC_CHECK_FUNC(vasprintf, AC_DEFINE(HAVE_VASPRINTF) ) diff --git a/src/include/config.h.in b/src/include/config.h.in index 3bf894e..77c1e8c 100644 --- a/src/include/config.h.in +++ b/src/include/config.h.in @@ -55,6 +55,7 @@ #undef HAVE_INET_NTOP #undef HAVE_DBUS #undef HAVE_LIBCURL + #undef HAVE_SYSLOG #undef HAVE_ICONV #undef ICONV_CONST diff --git a/src/include/lib3270/log.h b/src/include/lib3270/log.h index 7a69ea2..a838c72 100644 --- a/src/include/lib3270/log.h +++ b/src/include/lib3270/log.h @@ -57,6 +57,16 @@ LIB3270_EXPORT int lib3270_write_rc(H3270 *session, const char *module, int rc, const char *fmt, ...) LIB3270_GNUC_FORMAT(4,5); LIB3270_EXPORT void lib3270_write_va_log(H3270 *session, const char *module, const char *fmt, va_list arg); + /** + * @brief Send logs to system log (if available) + * + * @param flag Non-zero to use syslog. + * + * @return 0 if ok, non zero if not. + * + */ + LIB3270_EXPORT int lib3270_set_syslog(int flag); + #ifdef DEBUG #include #undef trace diff --git a/src/lib3270/init.c b/src/lib3270/init.c index b7856f9..d4a3116 100644 --- a/src/lib3270/init.c +++ b/src/lib3270/init.c @@ -54,6 +54,10 @@ #include #include "private.h" +#ifdef HAVE_SYSLOG + #include +#endif // HAVE_SYSLOG + #if defined WIN32 BOOL WINAPI DllMain(HANDLE hinst, DWORD dwcallpurpose, LPVOID lpvResvd); #else @@ -118,6 +122,13 @@ int lib3270_unloaded(void) curl_global_cleanup(); #endif // HAVE_LIBCURL +#ifdef HAVE_SYSLOG + if(use_syslog) + { + closelog(); + } +#endif // HAVE_SYSLOG + return 0; } diff --git a/src/lib3270/linux/log.c b/src/lib3270/linux/log.c index 32a944d..9f73a99 100644 --- a/src/lib3270/linux/log.c +++ b/src/lib3270/linux/log.c @@ -34,13 +34,59 @@ #include #include +#ifdef HAVE_SYSLOG + #include +#endif // HAVE_SYSLOG + /*---[ Implementacao ]--------------------------------------------------------------------------------------*/ + int use_syslog = 0; + void default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int GNUC_UNUSED(rc), const char *fmt, va_list arg_ptr) { +#ifdef HAVE_SYSLOG + if(use_syslog) + { + vsyslog(LOG_USER, fmt, arg_ptr); + } + else + { + printf("%s:\t",module); + vprintf(fmt,arg_ptr); + printf("\n"); + fflush(stdout); + } +#else printf("%s:\t",module); vprintf(fmt,arg_ptr); printf("\n"); fflush(stdout); +#endif } + LIB3270_EXPORT int lib3270_set_syslog(int flag) + { +#ifdef HAVE_SYSLOG + if(flag) + { + if(!use_syslog) + { + openlog(LIB3270_STRINGIZE_VALUE_OF(LIB3270_NAME), LOG_NDELAY, LOG_USER); + use_syslog = 1; + } + } + else + { + if(use_syslog) + { + closelog(); + use_syslog = 0; + } + } + + return 0; + +#else + return errno = ENOENT; +#endif // HAVE_SYSLOG + } diff --git a/src/lib3270/log.c b/src/lib3270/log.c index a3215fd..e03ccc3 100644 --- a/src/lib3270/log.c +++ b/src/lib3270/log.c @@ -41,6 +41,7 @@ #include #include #include +#include /*---[ Constants ]------------------------------------------------------------------------------------------*/ @@ -75,3 +76,4 @@ { loghandler(session,module,0,fmt,arg); } + diff --git a/src/lib3270/private.h b/src/lib3270/private.h index 929fcf7..eeaefda 100644 --- a/src/lib3270/private.h +++ b/src/lib3270/private.h @@ -691,6 +691,12 @@ struct _h3270 LIB3270_INTERNAL HANDLE hEventLog; #endif // _WIN32 +#ifdef HAVE_SYSLOG +/// @brief Windows Event Log Handler. +LIB3270_INTERNAL int use_syslog; +#endif // HAVE_SYSLOG + + /* Library internal calls */ LIB3270_INTERNAL int key_ACharacter(H3270 *hSession, unsigned char c, enum keytype keytype, enum iaction cause,Boolean *skipped); LIB3270_INTERNAL int cursor_move(H3270 *session, int baddr); diff --git a/src/lib3270/windows/log.c b/src/lib3270/windows/log.c index d5d94cd..dedc075 100644 --- a/src/lib3270/windows/log.c +++ b/src/lib3270/windows/log.c @@ -74,3 +74,8 @@ } + LIB3270_EXPORT int lib3270_set_syslog(int flag) + { + return errno = ENOENT; + } + -- libgit2 0.21.2