Skip to content

Commit 96e063e

Browse files
Add logger that prints c-core logs. (#25)
Add possibility to print c-core and cpp chat logs in Unreal. Make get_user happen only once during Chat_Init. Clean up cmake. Small fixes for Linux compilation.
1 parent ce78512 commit 96e063e

17 files changed

+145
-31
lines changed

CMakeLists.txt

+12-9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ set(USE_CALLBACK_API ON)
5454
set(USE_SET_DNS_SERVERS ON)
5555
set(USE_IPV6 ON)
5656
set(USE_SUBSCRIBE_EVENT_ENGINE ON)
57+
set(USE_LOG_CALLBACK ON)
5758
set(USE_NTF_RUNTIME_SELECTION ON)
5859

5960
if(WIN32 OR WIN64 OR MSVC)
@@ -63,7 +64,7 @@ endif()
6364
FetchContent_Declare(
6465
pubnub
6566
GIT_REPOSITORY https://github.com/pubnub/c-core.git
66-
GIT_TAG feat/runtime-api-enforcement
67+
GIT_TAG v5.0.0
6768
GIT_SHALLOW TRUE
6869
GIT_PROGRESS ON
6970
SYSTEM
@@ -88,9 +89,11 @@ endif()
8889
if(${ENABLE_C_ABI})
8990
target_compile_options(pubnub PUBLIC -DPUBNUB_SDK_VERSION_SUFFIX=\"/CA-Unity/0.4.3\")
9091
else()
91-
target_compile_options(pubnub PUBLIC -DPUBNUB_SDK_VERSION_SUFFIX=\"/CA-Unreal/0.3.0\")
92+
target_compile_options(pubnub PUBLIC -DPUBNUB_SDK_VERSION_SUFFIX=\"/CA-Unreal/0.3.3\")
9293
endif()
93-
target_compile_options(pubnub PUBLIC -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
94+
95+
set(CCORE_COMPILE_OPTIONS -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1 -DPUBNUB_USE_LOG_CALLBACK=1)
96+
target_compile_options(pubnub PUBLIC ${CCORE_COMPILE_OPTIONS})
9497

9598
FetchContent_Declare(
9699
json
@@ -133,7 +136,8 @@ set(INFRA_SOURCES
133136
${CMAKE_CURRENT_SOURCE_DIR}/src/infra/timer.cpp
134137
${CMAKE_CURRENT_SOURCE_DIR}/src/infra/pubnub.cpp
135138
${CMAKE_CURRENT_SOURCE_DIR}/src/infra/interval_task.cpp
136-
${CMAKE_CURRENT_SOURCE_DIR}/src/infra/rate_limiter.cpp)
139+
${CMAKE_CURRENT_SOURCE_DIR}/src/infra/rate_limiter.cpp
140+
${CMAKE_CURRENT_SOURCE_DIR}/src/infra/logger.cpp)
137141

138142
set(DOMAIN_SOURCES
139143
${CMAKE_CURRENT_SOURCE_DIR}/src/domain/chat_entity.cpp
@@ -181,8 +185,7 @@ set(SOURCES
181185
${CMAKE_CURRENT_SOURCE_DIR}/src/presentation/access_manager.cpp
182186
${CMAKE_CURRENT_SOURCE_DIR}/src/presentation/callback_handle.cpp
183187
${CMAKE_CURRENT_SOURCE_DIR}/src/chat_helpers.cpp
184-
${CMAKE_CURRENT_SOURCE_DIR}/src/string.cpp
185-
)
188+
${CMAKE_CURRENT_SOURCE_DIR}/src/string.cpp)
186189

187190
if(${ENABLE_C_ABI})
188191
add_compile_definitions(PN_CHAT_C_ABI)
@@ -212,19 +215,19 @@ set(SOURCES
212215

213216
add_library(pubnub-chat SHARED ${SOURCES})
214217

215-
target_compile_options(pubnub-chat PRIVATE -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
218+
target_compile_options(pubnub-chat PRIVATE ${CCORE_COMPILE_OPTIONS})
216219

217220
target_link_libraries(pubnub-chat PRIVATE -lpthread pubnub)
218221

219222
# TODO: kept for debugging purposes - should be deleted before release
220223
add_executable(pubnub-chat-example example/main.cpp)
221-
target_compile_options(pubnub-chat-example PRIVATE -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
224+
target_compile_options(pubnub-chat-example PRIVATE ${CCORE_COMPILE_OPTIONS})
222225
target_link_libraries(pubnub-chat-example PUBLIC -lpthread pubnub-chat)
223226

224227
if(${COMPILE_EXAMPLES})
225228
function(example name)
226229
add_executable(pubnub_${name} example/${name}.cpp)
227-
target_compile_options(pubnub_${name} PRIVATE -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
230+
target_compile_options(pubnub_${name} PRIVATE ${CCORE_COMPILE_OPTIONS})
228231
target_link_libraries(pubnub_${name} PRIVATE pubnub-chat)
229232
endfunction()
230233

include/pubnub_chat/chat.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,13 @@ namespace Pubnub {
170170
/* PAM */
171171
PN_CHAT_EXPORT Pubnub::AccessManager access_manager() const;
172172

173+
/* LOG */
174+
PN_CHAT_EXPORT void register_logger_callback(std::function<void(Pubnub::pn_log_level, const char*)> callback);
175+
173176
private:
174177
Chat(const Pubnub::String& publish_key, const Pubnub::String& subscribe_key, const Pubnub::String& user_id, const ChatConfig& config);
175178
void store_user_activity_timestamp() const;
179+
Pubnub::User create_user_for_init_chat(const Pubnub::String& user_id, const Pubnub::ChatUserData& user_data) const;
176180

177181
std::shared_ptr<const ChatService> chat_service;
178182
std::shared_ptr<const UserService> user_service;

include/pubnub_chat/enums.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ enum EventMethod {
3838
Signal
3939
};
4040

41+
enum pn_log_level {
42+
None,
43+
Error,
44+
Warning,
45+
Info,
46+
Debug,
47+
Trace
48+
};
49+
4150
}
4251

4352
#endif // PN_ENUMS_HPP

include/pubnub_chat/string.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cstddef>
66
#include <ostream>
77
#include <string>
8+
#include <cstring>
89

910
namespace Pubnub {
1011
/**

src/application/chat_service.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ void ChatService::init_services(const ChatConfig& config) {
5757
presence_service,
5858
pubnub
5959
);
60+
61+
this->pubnub->lock()->set_logging_callback(Logger::log_ccore_message);
6062
}
6163

6264
ThreadSafePtr<PubNub> ChatService::create_pubnub(const String& publish_key, const String& subscribe_key, const String& user_id, const String& auth_key) {
@@ -228,3 +230,8 @@ std::shared_ptr<Subscription> ChatService::listen_for_events(const Pubnub::Strin
228230

229231
return subscription;
230232
}
233+
234+
void ChatService::register_logger_callback(std::function<void(Pubnub::pn_log_level, const char*)> callback) const
235+
{
236+
logger.register_logging_callback(callback);
237+
}

src/application/chat_service.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "mentions.hpp"
1010
#include "string.hpp"
1111
#include "enums.hpp"
12+
#include "infra/logger.hpp"
1213
#include <functional>
1314
#include <memory>
1415
#include <vector>
@@ -45,6 +46,8 @@ class ChatService : public std::enable_shared_from_this<ChatService>
4546
std::tuple<std::vector<Pubnub::UserMentionData>, bool> get_current_user_mentions(const Pubnub::String& start_timetoken, const Pubnub::String& end_timetoken, int count) const;
4647
std::shared_ptr<Subscription> listen_for_events(const Pubnub::String& channel_id, Pubnub::pubnub_chat_event_type chat_event_type, std::function<void(const Pubnub::Event&)> event_callback) const;
4748

49+
void register_logger_callback(std::function<void(Pubnub::pn_log_level, const char*)> callback) const;
50+
4851
std::shared_ptr<const UserService> user_service;
4952
std::shared_ptr<const MessageService> message_service;
5053
std::shared_ptr<const MembershipService> membership_service;
@@ -53,6 +56,7 @@ class ChatService : public std::enable_shared_from_this<ChatService>
5356
std::shared_ptr<const AccessManagerService> access_manager_service;
5457
std::shared_ptr<const ChannelService> channel_service;
5558
Pubnub::ChatConfig chat_config;
59+
mutable Logger logger;
5660

5761
std::shared_ptr<CallbackService> callback_service;
5862

src/application/user_service.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,31 @@ User UserService::get_current_user() const
3131
return this->get_user(user_id);
3232
}
3333

34-
User UserService::create_user(const String& user_id, const UserDAO& user_data) const
34+
User UserService::create_user(const String& user_id, const UserDAO& user_data, bool skip_get_user) const
3535
{
3636
if(user_id.empty())
3737
{
3838
throw std::invalid_argument("Failed to create user, user_id is empty");
3939
}
4040

41-
bool user_exists = true;
42-
43-
try
44-
{
45-
get_user(user_id);
46-
47-
}
48-
catch(...)
41+
if(!skip_get_user)
4942
{
50-
user_exists = false;
51-
}
43+
bool user_exists = true;
5244

53-
if(user_exists)
54-
{
55-
throw std::invalid_argument("User with this ID already exists");
45+
try
46+
{
47+
get_user(user_id);
48+
49+
}
50+
catch(...)
51+
{
52+
user_exists = false;
53+
}
54+
55+
if(user_exists)
56+
{
57+
throw std::invalid_argument("User with this ID already exists");
58+
}
5659
}
5760

5861
auto new_user_entity = user_data.to_entity();

src/application/user_service.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class UserService : public std::enable_shared_from_this<UserService>
2626

2727
Pubnub::User get_current_user() const;
2828

29-
Pubnub::User create_user(const Pubnub::String& user_id, const UserDAO& user_data) const;
29+
Pubnub::User create_user(const Pubnub::String& user_id, const UserDAO& user_data, bool skip_get_user = false) const;
3030
Pubnub::User get_user(const Pubnub::String& user_id) const;
3131
std::tuple<std::vector<Pubnub::User>, Pubnub::Page, int> get_users(const Pubnub::String& filter = "", const Pubnub::String& sort = "", int limit = 0, const Pubnub::Page& page = Pubnub::Page()) const;
3232
Pubnub::User update_user(const Pubnub::String& user_id, const UserDAO& user_data) const;

src/chat_helpers.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ namespace Pubnub
1616
String create_set_memberships_object(String channel_id, String custom_params_json)
1717
{
1818
String custom_parameter_string;
19-
custom_params_json.empty() ? custom_parameter_string="{}" : custom_parameter_string = custom_params_json;
20-
return String("[{\"channel\": {\"id\": \"") + channel_id + String("\"}, \"custom\": ") + custom_parameter_string + String("}]");
19+
custom_params_json.empty() ? custom_parameter_string = "" : custom_parameter_string = String(", \"custom\": ") + custom_params_json;
20+
return String("[{\"channel\": {\"id\": \"") + channel_id + String("\"}") + custom_parameter_string + String("}]");
2121
}
2222

2323
String create_set_members_object(String user_id, String custom_params_json)
2424
{
2525
String custom_parameter_string;
26-
custom_params_json.empty() ? custom_parameter_string="{}" : custom_parameter_string = custom_params_json;
27-
return String("[{\"uuid\": {\"id\": \"") + user_id + String("\"}, \"custom\": ") + custom_parameter_string + String("}]");
26+
custom_params_json.empty() ? custom_parameter_string="" : custom_parameter_string = String(", \"custom\": ") + custom_params_json;
27+
return String("[{\"uuid\": {\"id\": \"") + user_id + String("\"}") + custom_parameter_string + String("}]");
2828
}
2929

3030
String create_set_members_object(std::vector<String> users_ids, String custom_params_json)
3131
{
3232
String custom_parameter_string;
33-
custom_params_json.empty() ? custom_parameter_string="{}" : custom_parameter_string = custom_params_json;
33+
custom_params_json.empty() ? custom_parameter_string = "" : custom_parameter_string = String(", \"custom\": ") + custom_params_json;
3434

3535
//Start json array
3636
String final_object = "[";
@@ -39,7 +39,7 @@ String create_set_members_object(std::vector<String> users_ids, String custom_pa
3939
for(auto &user_id : users_ids)
4040
{
4141
if(user_id.empty()) {continue;}
42-
String user_object = String("{\"uuid\": {\"id\": \"") + user_id + String("\"}, \"custom\": ") + custom_parameter_string + String("},");
42+
String user_object = String("{\"uuid\": {\"id\": \"") + user_id + String("\"}") + custom_parameter_string + String("},");
4343
final_object += user_object;
4444
}
4545

src/infra/logger.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "logger.hpp"
2+
#include <iostream>
3+
4+
std::function<void(Pubnub::pn_log_level, const char*)> Logger::cpp_log_callback = nullptr;
5+
6+
void Logger::log_message(Pubnub::pn_log_level log_level, Pubnub::String message)
7+
{
8+
if(cpp_log_callback)
9+
{
10+
cpp_log_callback(log_level, message);
11+
}
12+
else
13+
{
14+
std::cout << message << std::endl;
15+
}
16+
}
17+
18+
void Logger::register_logging_callback(std::function<void(Pubnub::pn_log_level, const char*)> callback)
19+
{
20+
cpp_log_callback = callback;
21+
}
22+
23+
void Logger::log_ccore_message(enum pubnub_log_level log_level, const char* message)
24+
{
25+
if(cpp_log_callback)
26+
{
27+
cpp_log_callback((Pubnub::pn_log_level)log_level, message);
28+
}
29+
}

src/infra/logger.hpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef PN_CHAT_LOG_HPP
2+
#define PN_CHAT_LOG_HPP
3+
4+
#include "enums.hpp"
5+
#include "string.hpp"
6+
#include <functional>
7+
#include <sstream>
8+
9+
extern "C" {
10+
#include <pubnub_log.h>
11+
}
12+
13+
14+
class Logger
15+
{
16+
public:
17+
18+
void log_message(Pubnub::pn_log_level log_level, Pubnub::String message);
19+
20+
21+
void register_logging_callback(std::function<void(Pubnub::pn_log_level, const char*)> callback);
22+
23+
static void log_ccore_message(enum pubnub_log_level log_level, const char* message);
24+
25+
private:
26+
27+
static std::function<void(Pubnub::pn_log_level, const char*)> cpp_log_callback;
28+
29+
};
30+
31+
#endif // PN_CHAT_LOG_HPP

src/infra/pubnub.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" {
3030
#include <pubnub_ntf_enforcement.h>
3131
#include <pubnub_entities.h>
3232
#include <pubnub_subscribe_event_listener.h>
33+
#include <pubnub_log.h>
3334
}
3435

3536
using json = nlohmann::json;
@@ -840,4 +841,9 @@ int PubNub::set_pubnub_origin(const Pubnub::String origin)
840841
custom_origin = origin;
841842
return pubnub_origin_set(this->main_context.get(), custom_origin.c_str());
842843
return pubnub_origin_set(this->long_poll_context.get(), custom_origin.c_str());
844+
}
845+
846+
void PubNub::set_logging_callback(void (*callback)(enum pubnub_log_level log_level, const char* message))
847+
{
848+
pubnub_set_log_callback(callback);
843849
}

src/infra/pubnub.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "application/subscription.hpp"
55
#include "string.hpp"
66
#include "enums.hpp"
7+
#include "logger.hpp"
78
#include <memory>
89
#include <thread>
910
#include <vector>
@@ -76,6 +77,8 @@ class PubNub {
7677
void set_auth_token(const Pubnub::String token);
7778
int set_pubnub_origin(const Pubnub::String origin);
7879

80+
void set_logging_callback(void (*callback)(enum pubnub_log_level log_level, const char* message));
81+
7982
private:
8083
void await_and_handle_error(pubnub_res result);
8184
bool is_subscribed_to_channel(const Pubnub::String channel);

src/infra/rate_limiter.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <algorithm>
66
#include <chrono>
77
#include <memory>
8+
#include <cmath>
89

910
#define THREADS_MAX_SPEEL_MS 1000
1011
#define NO_SLEEP_REQUIRED -1

src/infra/rate_limiter.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <map>
1313
#include <memory>
1414
#include <vector>
15+
#include <thread>
1516

1617
struct RateLimiterElement {
1718
std::function<Pubnub::String()> task;

src/infra/timer.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <chrono>
66
#include <future>
77
#include <functional>
8+
#include <thread>
89

910
class Timer
1011
{

0 commit comments

Comments
 (0)