Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ endif()
find_package(Libevent REQUIRED COMPONENTS ${LIBEVENT_COMPONENTS})
find_package(ZLIB REQUIRED)

# Find libwebsockets for WebSocketClientWrapper
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBWEBSOCKETS REQUIRED libwebsockets)

if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
Expand All @@ -37,10 +41,12 @@ endif()

set (LIBWSC_SOURCES
libwsc/WebSocketClient.cpp
libwsc/WebSocketClientWrapper.cpp
libwsc/Utf8Validator.cpp
libwsc/base64.cpp)
set (LIBWSC_HEADERS
libwsc/WebSocketClient.h
libwsc/WebSocketClientWrapper.h
libwsc/Logger.h
libwsc/Utf8Validator.h
libwsc/base64.h
Expand All @@ -57,12 +63,14 @@ target_include_directories(libwsc
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/libwsc>
PRIVATE
${LIBEVENT_INCLUDE_DIRS}
${LIBWEBSOCKETS_INCLUDE_DIRS}
)

target_link_libraries(libwsc
PRIVATE
${LIBEVENT_LIBRARIES}
ZLIB::ZLIB
${LIBWEBSOCKETS_LIBRARIES}
)

if (LIBWSC_USE_DEBUG)
Expand Down
51 changes: 48 additions & 3 deletions libwsc/Logger.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Logger.h
* Dual-mode logger: console or syslog
* Dual-mode logger: FreeSWITCH, console or syslog
*
* Author: Milan M.
* Copyright (c) 2025 AMSOFTSWITCH LTD. All rights reserved.
Expand All @@ -16,6 +16,14 @@
#include <sys/time.h>
#include <ctime>

// Check if FreeSWITCH headers are available
#ifdef SWITCH_LOG_DEBUG
#include <switch.h>
#define LIBWSC_USE_FREESWITCH_LOG 1
#else
#define LIBWSC_USE_FREESWITCH_LOG 0
#endif

/**
* \brief Determines if logs should go to syslog (non-interactive) or console.
*/
Expand Down Expand Up @@ -45,33 +53,59 @@ inline void current_timestamp(char* buf, size_t len) {
}

/**
* \brief Internal debug logger: writes to stdout or syslog.
* \brief Internal debug logger: writes to FreeSWITCH, stdout or syslog.
*/
inline void log_debug_impl(const char* message) {
#ifdef LIBWSC_USE_DEBUG
#if LIBWSC_USE_FREESWITCH_LOG
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "mod_audio_stream[libwsc]: %s\n", message);
#else
char ts[32]; current_timestamp(ts, sizeof(ts));
if (logger_use_syslog()) {
syslog(LOG_DEBUG, "%s", message);
} else {
fprintf(stdout, "[DEBUG %s] %s\n", ts, message);
fflush(stdout);
}
#endif
#else
(void)message;
#endif
}

/**
* \brief Internal error logger: writes to stderr or syslog.
* \brief Internal error logger: writes to FreeSWITCH, stderr or syslog.
*/
inline void log_error_impl(const char* message) {
#if LIBWSC_USE_FREESWITCH_LOG
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_audio_stream[libwsc]: %s\n", message);
#else
char ts[32]; current_timestamp(ts, sizeof(ts));
if (logger_use_syslog()) {
syslog(LOG_ERR, "%s", message);
} else {
fprintf(stderr, "[ERROR %s] %s\n", ts, message);
fflush(stderr);
}
#endif
}

/**
* \brief Internal info logger: writes to FreeSWITCH, stdout or syslog.
* Always enabled (not dependent on LIBWSC_USE_DEBUG).
*/
inline void log_info_impl(const char* message) {
#if LIBWSC_USE_FREESWITCH_LOG
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "mod_audio_stream[libwsc]: %s\n", message);
#else
char ts[32]; current_timestamp(ts, sizeof(ts));
if (logger_use_syslog()) {
syslog(LOG_INFO, "%s", message);
} else {
fprintf(stdout, "[INFO %s] %s\n", ts, message);
fflush(stdout);
}
#endif
}

// Zero-arg overloads to avoid format-security warnings
Expand All @@ -81,6 +115,9 @@ inline void log_debug_fmt_impl(const char* fmt) {
inline void log_error_fmt_impl(const char* fmt) {
log_error_impl(fmt);
}
inline void log_info_fmt_impl(const char* fmt) {
log_info_impl(fmt);
}

// Templated overloads for formatting
template<typename... Args>
Expand All @@ -97,6 +134,13 @@ inline void log_error_fmt_impl(const char* fmt, Args... args) {
log_error_impl(buf);
}

template<typename... Args>
inline void log_info_fmt_impl(const char* fmt, Args... args) {
char buf[256];
snprintf(buf, sizeof(buf), fmt, args...);
log_info_impl(buf);
}

// Public macros
#ifdef LIBWSC_USE_DEBUG
#define log_debug(...) log_debug_fmt_impl(__VA_ARGS__)
Expand All @@ -105,5 +149,6 @@ inline void log_error_fmt_impl(const char* fmt, Args... args) {
#endif

#define log_error(...) log_error_fmt_impl(__VA_ARGS__)
#define log_info(...) log_info_fmt_impl(__VA_ARGS__)

#endif // LOGGER_H
Loading