diff --git a/.pubnub.yml b/.pubnub.yml index f78cf876..10d368be 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -1,8 +1,13 @@ name: c-core schema: 1 -version: "4.16.1" +version: "4.17.0" scm: github.com/pubnub/c-core changelog: + - date: 2025-01-16 + version: v4.17.0 + changes: + - type: feature + text: "Add possibility to replace default log function with a callback provided by the user." - date: 2025-01-09 version: v4.16.1 changes: @@ -902,7 +907,7 @@ sdks: distribution-type: source code distribution-repository: GitHub release package-name: C-Core - location: https://github.com/pubnub/c-core/releases/tag/v4.16.1 + location: https://github.com/pubnub/c-core/releases/tag/v4.17.0 requires: - name: "miniz" @@ -968,7 +973,7 @@ sdks: distribution-type: source code distribution-repository: GitHub release package-name: C-Core - location: https://github.com/pubnub/c-core/releases/tag/v4.16.1 + location: https://github.com/pubnub/c-core/releases/tag/v4.17.0 requires: - name: "miniz" @@ -1034,7 +1039,7 @@ sdks: distribution-type: source code distribution-repository: GitHub release package-name: C-Core - location: https://github.com/pubnub/c-core/releases/tag/v4.16.1 + location: https://github.com/pubnub/c-core/releases/tag/v4.17.0 requires: - name: "miniz" @@ -1096,7 +1101,7 @@ sdks: distribution-type: source code distribution-repository: GitHub release package-name: C-Core - location: https://github.com/pubnub/c-core/releases/tag/v4.16.1 + location: https://github.com/pubnub/c-core/releases/tag/v4.17.0 requires: - name: "miniz" @@ -1157,7 +1162,7 @@ sdks: distribution-type: source code distribution-repository: GitHub release package-name: C-Core - location: https://github.com/pubnub/c-core/releases/tag/v4.16.1 + location: https://github.com/pubnub/c-core/releases/tag/v4.17.0 requires: - name: "miniz" @@ -1213,7 +1218,7 @@ sdks: distribution-type: source code distribution-repository: GitHub release package-name: C-Core - location: https://github.com/pubnub/c-core/releases/tag/v4.16.1 + location: https://github.com/pubnub/c-core/releases/tag/v4.17.0 requires: - name: "miniz" @@ -1266,7 +1271,7 @@ sdks: distribution-type: source code distribution-repository: GitHub release package-name: C-Core - location: https://github.com/pubnub/c-core/releases/tag/v4.16.1 + location: https://github.com/pubnub/c-core/releases/tag/v4.17.0 requires: - name: "miniz" diff --git a/CHANGELOG.md b/CHANGELOG.md index 763d8966..d162d54b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v4.17.0 +January 16 2025 + +#### Added +- Add possibility to replace default log function with a callback provided by the user. + ## v4.16.1 January 09 2025 diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b815e02..7ef301ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,7 @@ num_option(USE_IPV6 "Use IPv6" ON) num_option(USE_SET_DNS_SERVERS "Use set DNS servers [CALLBACK=ON]" ${DEFAULT_USE_CALLBACK_API}) num_option(USE_EXTERN_API "Use extern C API [WITH_CPP=ON]" ON) num_option(USE_LEGACY_CRYPTO_RANDOM_IV "Use random IV for legacy crypto module [OpenSSL only]" ON) +num_option(USE_LOG_CALLBACK "Use possibility to replace default logging function with user provided callback" OFF) log_set(OPENSSL_ROOT_DIR "" "OpenSSL root directory (leave empty for find_package() defaults)[OPENSSL=ON needed]") log_set(CUSTOM_OPENSSL_LIB_DIR "lib" "OpenSSL lib directory relative to OPENSSL_ROOT_DIR [used only if find_package() failed]") log_set(CUSTOM_OPENSSL_INCLUDE_DIR "include" "OpenSSL include directory relative to OPENSSL_ROOT_DIR [used only if find_package() failed]") @@ -118,6 +119,7 @@ set(FLAGS "\ -D PUBNUB_USE_FETCH_HISTORY=${USE_FETCH_HISTORY} \ -D PUBNUB_CRYPTO_API=${USE_CRYPTO_API} \ -D PUBNUB_RAND_INIT_VECTOR=${USE_LEGACY_CRYPTO_RANDOM_IV} \ + -D PUBNUB_USE_LOG_CALLBACK=${USE_LOG_CALLBACK} \ -D PUBNUB_MBEDTLS=${MBEDTLS}") #Required on windows with Event Engine, so only winsock2 is used (otherwise it tries to use winsock and winsock2 and there are redefinition errors) @@ -182,6 +184,7 @@ set(CORE_SOURCEFILES ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_netcore.c ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_blocking_io.c ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_timers.c + ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_log.c ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_json_parse.c ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_helper.c ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_generate_uuid_v3_md5.c diff --git a/core/Makefile b/core/Makefile index 08f32a6c..7f240798 100644 --- a/core/Makefile +++ b/core/Makefile @@ -1,4 +1,4 @@ -PROJECT_SOURCEFILES = pbcc_set_state.c pubnub_pubsubapi.c pubnub_coreapi.c pubnub_ccore_pubsub.c pubnub_ccore.c pubnub_netcore.c pubnub_alloc_static.c pubnub_assert_std.c pubnub_json_parse.c pubnub_keep_alive.c pubnub_helper.c pubnub_url_encode.c ../lib/pb_strnlen_s.c ../lib/pb_strncasecmp.c ../lib/base64/pbbase64.c pubnub_coreapi_ex.c +PROJECT_SOURCEFILES = pbcc_set_state.c pubnub_pubsubapi.c pubnub_coreapi.c pubnub_ccore_pubsub.c pubnub_ccore.c pubnub_netcore.c pubnub_alloc_static.c pubnub_assert_std.c pubnub_json_parse.c pubnub_keep_alive.c pubnub_helper.c pubnub_url_encode.c ../lib/pb_strnlen_s.c ../lib/pb_strncasecmp.c ../lib/base64/pbbase64.c pubnub_coreapi_ex.c pubnub_log.c # TODO: move coreapi_ex to new module all: pubnub_crypto_unittest pubnub_subscribe_v2_unittest pbcc_crypto_unittest pubnub_grant_token_api_unittest pubnub_proxy_unittest pubnub_timer_list_unittest unittest @@ -57,7 +57,7 @@ unittest: $(PROJECT_SOURCEFILES) pubnub_core_unit_test.c #$(GCOVR) -r . --html --html-details -o coverage.html -TIMER_LIST_SOURCEFILES = pubnub_alloc_static.c pubnub_assert_std.c pubnub_timers.c +TIMER_LIST_SOURCEFILES = pubnub_alloc_static.c pubnub_assert_std.c pubnub_timers.c pubnub_log.c pubnub_timer_list_unittest: pubnub_timer_list.c pubnub_timer_list_unit_test.c gcc -o pubnub_timer_list_unit_test.so -shared $(CFLAGS) $(LDFLAGS) -D PUBNUB_CALLBACK_API -D PUBNUB_ASSERT_LEVEL_NONE -Wall $(COVERAGE_FLAGS) -fPIC $(TIMER_LIST_SOURCEFILES) pubnub_timer_list.c pubnub_timer_list_unit_test.c -lcgreen -lm diff --git a/core/pubnub_internal_common.h b/core/pubnub_internal_common.h index f30cdc82..6d5ae3b5 100644 --- a/core/pubnub_internal_common.h +++ b/core/pubnub_internal_common.h @@ -17,6 +17,10 @@ #define PUBNUB_USE_IPV6 0 #endif +#if !defined(PUBNUB_USE_LOG_CALLBACK) +#define PUBNUB_USE_LOG_CALLBACK 0 +#endif + #if !defined(PUBNUB_SET_DNS_SERVERS) #define PUBNUB_SET_DNS_SERVERS 0 #endif diff --git a/core/pubnub_log.c b/core/pubnub_log.c new file mode 100644 index 00000000..fbd0d257 --- /dev/null +++ b/core/pubnub_log.c @@ -0,0 +1,39 @@ +/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ +#include "pubnub_log.h" + +#if PUBNUB_USE_LOG_CALLBACK + +#include +#include +#include + +void (*pubnub_log_callback)(enum pubnub_log_level log_level, const char* message) = NULL; + +void pubnub_set_log_callback(void (*callback)(enum pubnub_log_level log_level, const char* message)) { + pubnub_log_callback = callback; +} + +void log_with_callback(enum pubnub_log_level log_level, const char* format, ...) { + //Check needed buffer size for the message + va_list args; + va_start(args, format); + int needed_size = vsnprintf(NULL, 0, format, args) + 1; + va_end(args); + + if(needed_size <= 0) {return;} + + //Allocate required size to the message + char* logMessage = (char*)malloc(needed_size); + if (!logMessage) {return;} + + //Format string with message and all parameters + va_start(args, format); + vsnprintf(logMessage, needed_size, format, args); + va_end(args); + + //Execute the callback + pubnub_log_callback(log_level, logMessage); + free(logMessage); +} + +#endif /* PUBNUB_USE_LOG_CALLBACK */ \ No newline at end of file diff --git a/core/pubnub_log.h b/core/pubnub_log.h index 1bb725ad..b8ee6f2a 100644 --- a/core/pubnub_log.h +++ b/core/pubnub_log.h @@ -43,10 +43,45 @@ enum pubnub_log_level { #define PUBNUB_LOG_PRINTF(...) printf(__VA_ARGS__) #endif +#if PUBNUB_USE_LOG_CALLBACK + +#include "../lib/pb_extern.h" + +/** A global variable to store log function provided by the user. + This shouldn't be accessed or set directly. Use pubnub_set_log_callback instead. +*/ +extern void (*pubnub_log_callback)(enum pubnub_log_level log_level, const char* message); + +/** Replaces default logging function "printf" with provided callback. + + @param callback The callback that will be executed instead of default log + */ +PUBNUB_EXTERN void pubnub_set_log_callback(void (*callback)(enum pubnub_log_level log_level, const char* message)); + +void log_with_callback(enum pubnub_log_level log_level, const char* format, ...); + +//Redefine this macro to include callback functionality /** Generic logging macro, logs to given @a LVL using a printf-like - interface + interface. Uses callback instead of printf if pubnub_log_callback + is set. +*/ +#define PUBNUB_LOG(LVL, ...) \ +do { \ + if (LVL <= PUBNUB_LOG_LEVEL) { \ + if (pubnub_log_callback) { \ + log_with_callback(LVL, __VA_ARGS__); \ + } else { \ + PUBNUB_LOG_PRINTF(__VA_ARGS__); \ + } \ + } \ +} while(0) +#else +/** Generic logging macro, logs to given @a LVL using a printf-like + interface. */ #define PUBNUB_LOG(LVL, ...) do { if (LVL <= PUBNUB_LOG_LEVEL) PUBNUB_LOG_PRINTF(__VA_ARGS__); } while(0) +#endif /* PUBNUB_USE_LOG_CALLBACK */ + /** Helper macro to log an error message */ #define PUBNUB_LOG_ERROR(...) PUBNUB_LOG(PUBNUB_LOG_LEVEL_ERROR, __VA_ARGS__) @@ -108,4 +143,4 @@ enum pubnub_log_level { -#endif /*!defined INC_PUBNUB_LOG*/ +#endif /* !defined INC_PUBNUB_LOG */ diff --git a/core/pubnub_version_internal.h b/core/pubnub_version_internal.h index 1ec076ed..e6cbe484 100644 --- a/core/pubnub_version_internal.h +++ b/core/pubnub_version_internal.h @@ -3,7 +3,7 @@ #define INC_PUBNUB_VERSION_INTERNAL -#define PUBNUB_SDK_VERSION "4.16.1" +#define PUBNUB_SDK_VERSION "4.17.0" #endif /* !defined INC_PUBNUB_VERSION_INTERNAL */ diff --git a/core/test/pubnub_config.h b/core/test/pubnub_config.h index b800a3e9..421adf92 100644 --- a/core/test/pubnub_config.h +++ b/core/test/pubnub_config.h @@ -118,6 +118,10 @@ #define PUBNUB_RECEIVE_GZIP_RESPONSE 1 #endif +#if !defined(PUBNUB_USE_LOG_CALLBACK) +#define PUBNUB_USE_LOG_CALLBACK 0 +#endif + #define PUBNUB_DEFAULT_TRANSACTION_TIMER 310000 #define PUBNUB_MIN_TRANSACTION_TIMER 200 diff --git a/make/common/preprocessing.mk b/make/common/preprocessing.mk index af130e32..954d6182 100644 --- a/make/common/preprocessing.mk +++ b/make/common/preprocessing.mk @@ -31,7 +31,8 @@ DEFINES_COMMON = \ $(OPTION_PREFIX)D PUBNUB_USE_REVOKE_TOKEN_API=$(USE_REVOKE_TOKEN) \ $(OPTION_PREFIX)D PUBNUB_USE_SSL=$(USE_SSL) \ $(OPTION_PREFIX)D PUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=$(USE_SUBSCRIBE_EVENT_ENGINE) \ - $(OPTION_PREFIX)D PUBNUB_USE_SUBSCRIBE_V2=$(USE_SUBSCRIBE_V2) + $(OPTION_PREFIX)D PUBNUB_USE_SUBSCRIBE_V2=$(USE_SUBSCRIBE_V2) \ + $(OPTION_PREFIX)D PUBNUB_USE_LOG_CALLBACK=$(USE_LOG_CALLBACK) # Preprocessing flags for synchronous PubNub library version. CPPFLAGS_ = $(INCLUDES) $(DEFINES_PLATFORM) $(DEFINES_COMMON) $(DEFINES_EXTERN_C) $(DEFINES_RANDOM_IV) diff --git a/make/common/preprocessing_defaults.mk b/make/common/preprocessing_defaults.mk index 7077c40c..ffdfd5ad 100644 --- a/make/common/preprocessing_defaults.mk +++ b/make/common/preprocessing_defaults.mk @@ -81,4 +81,7 @@ DEFAULT_USE_REVOKE_TOKEN = 0 DEFAULT_USE_SUBSCRIBE_EVENT_ENGINE = 0 # Whether subscribe v2 feature should be enabled or not. -DEFAULT_USE_SUBSCRIBE_V2 = 1 \ No newline at end of file +DEFAULT_USE_SUBSCRIBE_V2 = 1 + +# Whether user defined callback for logging should be enabled or not. +DEFAULT_USE_LOG_CALLBACK = 0 \ No newline at end of file diff --git a/make/common/source_files.mk b/make/common/source_files.mk index 9a4ba59b..70054a50 100644 --- a/make/common/source_files.mk +++ b/make/common/source_files.mk @@ -22,6 +22,7 @@ CORE_SOURCE_FILES = \ ../core/pubnub_generate_uuid_v3_md5.c \ ../core/pubnub_helper.c \ ../core/pubnub_json_parse.c \ + ../core/pubnub_log.c \ ../core/pubnub_memory_block.c \ ../core/pubnub_netcore.c \ ../core/pubnub_pubsubapi.c \ diff --git a/make/posix_preprocessing.mk b/make/posix_preprocessing.mk index 8a902274..090473d7 100644 --- a/make/posix_preprocessing.mk +++ b/make/posix_preprocessing.mk @@ -143,6 +143,9 @@ USE_SUBSCRIBE_EVENT_ENGINE ?= $(DEFAULT_USE_SUBSCRIBE_EVENT_ENGINE) # Whether subscribe v2 feature should be enabled or not. USE_SUBSCRIBE_V2 ?= $(DEFAULT_USE_SUBSCRIBE_V2) +# Whether user defined callback for logging should be enabled or not. +USE_LOG_CALLBACK ?= $(DEFAULT_USE_LOG_CALLBACK) + # Additional user-provided compiler flags (C/C++). USER_C_FLAGS ?= USER_CXX_FLAGS ?= diff --git a/make/windows_preprocessing.mk b/make/windows_preprocessing.mk index 6c6b9d91..fd7b6678 100644 --- a/make/windows_preprocessing.mk +++ b/make/windows_preprocessing.mk @@ -218,6 +218,11 @@ USE_SUBSCRIBE_EVENT_ENGINE = $(DEFAULT_USE_SUBSCRIBE_EVENT_ENGINE) USE_SUBSCRIBE_V2 = $(DEFAULT_USE_SUBSCRIBE_V2) !endif +# Whether user defined callback for logging should be enabled or not. +!ifndef USE_LOG_CALLBACK +USE_LOG_CALLBACK = $(DEFAULT_USE_LOG_CALLBACK) +!endif + # Additional user-provided compiler flags (C/C++). !ifndef USER_C_FLAGS USER_C_FLAGS = diff --git a/openssl/pubnub_config.h b/openssl/pubnub_config.h index 48bd5080..5f975e3a 100644 --- a/openssl/pubnub_config.h +++ b/openssl/pubnub_config.h @@ -159,6 +159,10 @@ #define PUBNUB_COMPRESSED_MAXLEN 32000 #endif +#if !defined(PUBNUB_USE_LOG_CALLBACK) +#define PUBNUB_USE_LOG_CALLBACK 1 +#endif + /** The maximum length (in characters) of the host name of the proxy that will be saved in the Pubnub context. */ diff --git a/posix/pubnub_config.h b/posix/pubnub_config.h index 0d8ec46d..1d3f2c27 100644 --- a/posix/pubnub_config.h +++ b/posix/pubnub_config.h @@ -161,6 +161,9 @@ #define PUBNUB_CRYPTO_API 0 #endif +#if !defined(PUBNUB_USE_LOG_CALLBACK) +#define PUBNUB_USE_LOG_CALLBACK 0 +#endif #if !defined(PUBNUB_ONLY_PUBSUB_API) /** If true (!=0), will enable only publish and subscribe. All diff --git a/windows/pubnub_config.h b/windows/pubnub_config.h index 5ee13596..5029e56d 100644 --- a/windows/pubnub_config.h +++ b/windows/pubnub_config.h @@ -162,6 +162,9 @@ #define PUBNUB_CRYPTO_API 0 #endif +#if !defined(PUBNUB_USE_LOG_CALLBACK) +#define PUBNUB_USE_LOG_CALLBACK 0 +#endif #if !defined(PUBNUB_ONLY_PUBSUB_API) /** If true (!=0), will enable only publish and subscribe. All