Skip to content

Commit

Permalink
Add logging callback (#201)
Browse files Browse the repository at this point in the history
* Add possibility to replace default logging function with provided callback.
  • Loading branch information
KGronek-Pubnub authored Jan 16, 2025
1 parent 11fc03a commit 595409b
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 15 deletions.
21 changes: 13 additions & 8 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions core/pubnub_internal_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions core/pubnub_log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
#include "pubnub_log.h"

#if PUBNUB_USE_LOG_CALLBACK

#include <stddef.h>
#include <stdarg.h>
#include <stdlib.h>

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 */
39 changes: 37 additions & 2 deletions core/pubnub_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -108,4 +143,4 @@ enum pubnub_log_level {



#endif /*!defined INC_PUBNUB_LOG*/
#endif /* !defined INC_PUBNUB_LOG */
2 changes: 1 addition & 1 deletion core/pubnub_version_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
4 changes: 4 additions & 0 deletions core/test/pubnub_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion make/common/preprocessing.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion make/common/preprocessing_defaults.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
DEFAULT_USE_SUBSCRIBE_V2 = 1

# Whether user defined callback for logging should be enabled or not.
DEFAULT_USE_LOG_CALLBACK = 0
1 change: 1 addition & 0 deletions make/common/source_files.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
3 changes: 3 additions & 0 deletions make/posix_preprocessing.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?=
Expand Down
5 changes: 5 additions & 0 deletions make/windows_preprocessing.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
4 changes: 4 additions & 0 deletions openssl/pubnub_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
3 changes: 3 additions & 0 deletions posix/pubnub_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions windows/pubnub_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 595409b

Please sign in to comment.