Skip to content

Commit 595409b

Browse files
Add logging callback (#201)
* Add possibility to replace default logging function with provided callback.
1 parent 11fc03a commit 595409b

17 files changed

+134
-15
lines changed

.pubnub.yml

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
name: c-core
22
schema: 1
3-
version: "4.16.1"
3+
version: "4.17.0"
44
scm: github.com/pubnub/c-core
55
changelog:
6+
- date: 2025-01-16
7+
version: v4.17.0
8+
changes:
9+
- type: feature
10+
text: "Add possibility to replace default log function with a callback provided by the user."
611
- date: 2025-01-09
712
version: v4.16.1
813
changes:
@@ -902,7 +907,7 @@ sdks:
902907
distribution-type: source code
903908
distribution-repository: GitHub release
904909
package-name: C-Core
905-
location: https://github.com/pubnub/c-core/releases/tag/v4.16.1
910+
location: https://github.com/pubnub/c-core/releases/tag/v4.17.0
906911
requires:
907912
-
908913
name: "miniz"
@@ -968,7 +973,7 @@ sdks:
968973
distribution-type: source code
969974
distribution-repository: GitHub release
970975
package-name: C-Core
971-
location: https://github.com/pubnub/c-core/releases/tag/v4.16.1
976+
location: https://github.com/pubnub/c-core/releases/tag/v4.17.0
972977
requires:
973978
-
974979
name: "miniz"
@@ -1034,7 +1039,7 @@ sdks:
10341039
distribution-type: source code
10351040
distribution-repository: GitHub release
10361041
package-name: C-Core
1037-
location: https://github.com/pubnub/c-core/releases/tag/v4.16.1
1042+
location: https://github.com/pubnub/c-core/releases/tag/v4.17.0
10381043
requires:
10391044
-
10401045
name: "miniz"
@@ -1096,7 +1101,7 @@ sdks:
10961101
distribution-type: source code
10971102
distribution-repository: GitHub release
10981103
package-name: C-Core
1099-
location: https://github.com/pubnub/c-core/releases/tag/v4.16.1
1104+
location: https://github.com/pubnub/c-core/releases/tag/v4.17.0
11001105
requires:
11011106
-
11021107
name: "miniz"
@@ -1157,7 +1162,7 @@ sdks:
11571162
distribution-type: source code
11581163
distribution-repository: GitHub release
11591164
package-name: C-Core
1160-
location: https://github.com/pubnub/c-core/releases/tag/v4.16.1
1165+
location: https://github.com/pubnub/c-core/releases/tag/v4.17.0
11611166
requires:
11621167
-
11631168
name: "miniz"
@@ -1213,7 +1218,7 @@ sdks:
12131218
distribution-type: source code
12141219
distribution-repository: GitHub release
12151220
package-name: C-Core
1216-
location: https://github.com/pubnub/c-core/releases/tag/v4.16.1
1221+
location: https://github.com/pubnub/c-core/releases/tag/v4.17.0
12171222
requires:
12181223
-
12191224
name: "miniz"
@@ -1266,7 +1271,7 @@ sdks:
12661271
distribution-type: source code
12671272
distribution-repository: GitHub release
12681273
package-name: C-Core
1269-
location: https://github.com/pubnub/c-core/releases/tag/v4.16.1
1274+
location: https://github.com/pubnub/c-core/releases/tag/v4.17.0
12701275
requires:
12711276
-
12721277
name: "miniz"

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v4.17.0
2+
January 16 2025
3+
4+
#### Added
5+
- Add possibility to replace default log function with a callback provided by the user.
6+
17
## v4.16.1
28
January 09 2025
39

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ num_option(USE_IPV6 "Use IPv6" ON)
8484
num_option(USE_SET_DNS_SERVERS "Use set DNS servers [CALLBACK=ON]" ${DEFAULT_USE_CALLBACK_API})
8585
num_option(USE_EXTERN_API "Use extern C API [WITH_CPP=ON]" ON)
8686
num_option(USE_LEGACY_CRYPTO_RANDOM_IV "Use random IV for legacy crypto module [OpenSSL only]" ON)
87+
num_option(USE_LOG_CALLBACK "Use possibility to replace default logging function with user provided callback" OFF)
8788
log_set(OPENSSL_ROOT_DIR "" "OpenSSL root directory (leave empty for find_package() defaults)[OPENSSL=ON needed]")
8889
log_set(CUSTOM_OPENSSL_LIB_DIR "lib" "OpenSSL lib directory relative to OPENSSL_ROOT_DIR [used only if find_package() failed]")
8990
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 "\
118119
-D PUBNUB_USE_FETCH_HISTORY=${USE_FETCH_HISTORY} \
119120
-D PUBNUB_CRYPTO_API=${USE_CRYPTO_API} \
120121
-D PUBNUB_RAND_INIT_VECTOR=${USE_LEGACY_CRYPTO_RANDOM_IV} \
122+
-D PUBNUB_USE_LOG_CALLBACK=${USE_LOG_CALLBACK} \
121123
-D PUBNUB_MBEDTLS=${MBEDTLS}")
122124

123125
#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
182184
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_netcore.c
183185
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_blocking_io.c
184186
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_timers.c
187+
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_log.c
185188
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_json_parse.c
186189
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_helper.c
187190
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_generate_uuid_v3_md5.c

core/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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
1+
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
22
# TODO: move coreapi_ex to new module
33

44
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
5757
#$(GCOVR) -r . --html --html-details -o coverage.html
5858

5959

60-
TIMER_LIST_SOURCEFILES = pubnub_alloc_static.c pubnub_assert_std.c pubnub_timers.c
60+
TIMER_LIST_SOURCEFILES = pubnub_alloc_static.c pubnub_assert_std.c pubnub_timers.c pubnub_log.c
6161

6262
pubnub_timer_list_unittest: pubnub_timer_list.c pubnub_timer_list_unit_test.c
6363
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

core/pubnub_internal_common.h

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#define PUBNUB_USE_IPV6 0
1818
#endif
1919

20+
#if !defined(PUBNUB_USE_LOG_CALLBACK)
21+
#define PUBNUB_USE_LOG_CALLBACK 0
22+
#endif
23+
2024
#if !defined(PUBNUB_SET_DNS_SERVERS)
2125
#define PUBNUB_SET_DNS_SERVERS 0
2226
#endif

core/pubnub_log.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
2+
#include "pubnub_log.h"
3+
4+
#if PUBNUB_USE_LOG_CALLBACK
5+
6+
#include <stddef.h>
7+
#include <stdarg.h>
8+
#include <stdlib.h>
9+
10+
void (*pubnub_log_callback)(enum pubnub_log_level log_level, const char* message) = NULL;
11+
12+
void pubnub_set_log_callback(void (*callback)(enum pubnub_log_level log_level, const char* message)) {
13+
pubnub_log_callback = callback;
14+
}
15+
16+
void log_with_callback(enum pubnub_log_level log_level, const char* format, ...) {
17+
//Check needed buffer size for the message
18+
va_list args;
19+
va_start(args, format);
20+
int needed_size = vsnprintf(NULL, 0, format, args) + 1;
21+
va_end(args);
22+
23+
if(needed_size <= 0) {return;}
24+
25+
//Allocate required size to the message
26+
char* logMessage = (char*)malloc(needed_size);
27+
if (!logMessage) {return;}
28+
29+
//Format string with message and all parameters
30+
va_start(args, format);
31+
vsnprintf(logMessage, needed_size, format, args);
32+
va_end(args);
33+
34+
//Execute the callback
35+
pubnub_log_callback(log_level, logMessage);
36+
free(logMessage);
37+
}
38+
39+
#endif /* PUBNUB_USE_LOG_CALLBACK */

core/pubnub_log.h

+37-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,45 @@ enum pubnub_log_level {
4343
#define PUBNUB_LOG_PRINTF(...) printf(__VA_ARGS__)
4444
#endif
4545

46+
#if PUBNUB_USE_LOG_CALLBACK
47+
48+
#include "../lib/pb_extern.h"
49+
50+
/** A global variable to store log function provided by the user.
51+
This shouldn't be accessed or set directly. Use pubnub_set_log_callback instead.
52+
*/
53+
extern void (*pubnub_log_callback)(enum pubnub_log_level log_level, const char* message);
54+
55+
/** Replaces default logging function "printf" with provided callback.
56+
57+
@param callback The callback that will be executed instead of default log
58+
*/
59+
PUBNUB_EXTERN void pubnub_set_log_callback(void (*callback)(enum pubnub_log_level log_level, const char* message));
60+
61+
void log_with_callback(enum pubnub_log_level log_level, const char* format, ...);
62+
63+
//Redefine this macro to include callback functionality
4664
/** Generic logging macro, logs to given @a LVL using a printf-like
47-
interface
65+
interface. Uses callback instead of printf if pubnub_log_callback
66+
is set.
67+
*/
68+
#define PUBNUB_LOG(LVL, ...) \
69+
do { \
70+
if (LVL <= PUBNUB_LOG_LEVEL) { \
71+
if (pubnub_log_callback) { \
72+
log_with_callback(LVL, __VA_ARGS__); \
73+
} else { \
74+
PUBNUB_LOG_PRINTF(__VA_ARGS__); \
75+
} \
76+
} \
77+
} while(0)
78+
#else
79+
/** Generic logging macro, logs to given @a LVL using a printf-like
80+
interface.
4881
*/
4982
#define PUBNUB_LOG(LVL, ...) do { if (LVL <= PUBNUB_LOG_LEVEL) PUBNUB_LOG_PRINTF(__VA_ARGS__); } while(0)
83+
#endif /* PUBNUB_USE_LOG_CALLBACK */
84+
5085

5186
/** Helper macro to log an error message */
5287
#define PUBNUB_LOG_ERROR(...) PUBNUB_LOG(PUBNUB_LOG_LEVEL_ERROR, __VA_ARGS__)
@@ -108,4 +143,4 @@ enum pubnub_log_level {
108143

109144

110145

111-
#endif /*!defined INC_PUBNUB_LOG*/
146+
#endif /* !defined INC_PUBNUB_LOG */

core/pubnub_version_internal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define INC_PUBNUB_VERSION_INTERNAL
44

55

6-
#define PUBNUB_SDK_VERSION "4.16.1"
6+
#define PUBNUB_SDK_VERSION "4.17.0"
77

88

99
#endif /* !defined INC_PUBNUB_VERSION_INTERNAL */

core/test/pubnub_config.h

+4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@
118118
#define PUBNUB_RECEIVE_GZIP_RESPONSE 1
119119
#endif
120120

121+
#if !defined(PUBNUB_USE_LOG_CALLBACK)
122+
#define PUBNUB_USE_LOG_CALLBACK 0
123+
#endif
124+
121125
#define PUBNUB_DEFAULT_TRANSACTION_TIMER 310000
122126

123127
#define PUBNUB_MIN_TRANSACTION_TIMER 200

make/common/preprocessing.mk

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ DEFINES_COMMON = \
3131
$(OPTION_PREFIX)D PUBNUB_USE_REVOKE_TOKEN_API=$(USE_REVOKE_TOKEN) \
3232
$(OPTION_PREFIX)D PUBNUB_USE_SSL=$(USE_SSL) \
3333
$(OPTION_PREFIX)D PUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=$(USE_SUBSCRIBE_EVENT_ENGINE) \
34-
$(OPTION_PREFIX)D PUBNUB_USE_SUBSCRIBE_V2=$(USE_SUBSCRIBE_V2)
34+
$(OPTION_PREFIX)D PUBNUB_USE_SUBSCRIBE_V2=$(USE_SUBSCRIBE_V2) \
35+
$(OPTION_PREFIX)D PUBNUB_USE_LOG_CALLBACK=$(USE_LOG_CALLBACK)
3536

3637
# Preprocessing flags for synchronous PubNub library version.
3738
CPPFLAGS_ = $(INCLUDES) $(DEFINES_PLATFORM) $(DEFINES_COMMON) $(DEFINES_EXTERN_C) $(DEFINES_RANDOM_IV)

make/common/preprocessing_defaults.mk

+4-1
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,7 @@ DEFAULT_USE_REVOKE_TOKEN = 0
8181
DEFAULT_USE_SUBSCRIBE_EVENT_ENGINE = 0
8282

8383
# Whether subscribe v2 feature should be enabled or not.
84-
DEFAULT_USE_SUBSCRIBE_V2 = 1
84+
DEFAULT_USE_SUBSCRIBE_V2 = 1
85+
86+
# Whether user defined callback for logging should be enabled or not.
87+
DEFAULT_USE_LOG_CALLBACK = 0

make/common/source_files.mk

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CORE_SOURCE_FILES = \
2222
../core/pubnub_generate_uuid_v3_md5.c \
2323
../core/pubnub_helper.c \
2424
../core/pubnub_json_parse.c \
25+
../core/pubnub_log.c \
2526
../core/pubnub_memory_block.c \
2627
../core/pubnub_netcore.c \
2728
../core/pubnub_pubsubapi.c \

make/posix_preprocessing.mk

+3
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ USE_SUBSCRIBE_EVENT_ENGINE ?= $(DEFAULT_USE_SUBSCRIBE_EVENT_ENGINE)
143143
# Whether subscribe v2 feature should be enabled or not.
144144
USE_SUBSCRIBE_V2 ?= $(DEFAULT_USE_SUBSCRIBE_V2)
145145

146+
# Whether user defined callback for logging should be enabled or not.
147+
USE_LOG_CALLBACK ?= $(DEFAULT_USE_LOG_CALLBACK)
148+
146149
# Additional user-provided compiler flags (C/C++).
147150
USER_C_FLAGS ?=
148151
USER_CXX_FLAGS ?=

make/windows_preprocessing.mk

+5
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ USE_SUBSCRIBE_EVENT_ENGINE = $(DEFAULT_USE_SUBSCRIBE_EVENT_ENGINE)
218218
USE_SUBSCRIBE_V2 = $(DEFAULT_USE_SUBSCRIBE_V2)
219219
!endif
220220

221+
# Whether user defined callback for logging should be enabled or not.
222+
!ifndef USE_LOG_CALLBACK
223+
USE_LOG_CALLBACK = $(DEFAULT_USE_LOG_CALLBACK)
224+
!endif
225+
221226
# Additional user-provided compiler flags (C/C++).
222227
!ifndef USER_C_FLAGS
223228
USER_C_FLAGS =

openssl/pubnub_config.h

+4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@
159159
#define PUBNUB_COMPRESSED_MAXLEN 32000
160160
#endif
161161

162+
#if !defined(PUBNUB_USE_LOG_CALLBACK)
163+
#define PUBNUB_USE_LOG_CALLBACK 1
164+
#endif
165+
162166
/** The maximum length (in characters) of the host name of the proxy
163167
that will be saved in the Pubnub context.
164168
*/

posix/pubnub_config.h

+3
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@
161161
#define PUBNUB_CRYPTO_API 0
162162
#endif
163163

164+
#if !defined(PUBNUB_USE_LOG_CALLBACK)
165+
#define PUBNUB_USE_LOG_CALLBACK 0
166+
#endif
164167

165168
#if !defined(PUBNUB_ONLY_PUBSUB_API)
166169
/** If true (!=0), will enable only publish and subscribe. All

windows/pubnub_config.h

+3
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@
162162
#define PUBNUB_CRYPTO_API 0
163163
#endif
164164

165+
#if !defined(PUBNUB_USE_LOG_CALLBACK)
166+
#define PUBNUB_USE_LOG_CALLBACK 0
167+
#endif
165168

166169
#if !defined(PUBNUB_ONLY_PUBSUB_API)
167170
/** If true (!=0), will enable only publish and subscribe. All

0 commit comments

Comments
 (0)