Skip to content

Commit

Permalink
Use log4cplus(coturn#1344)
Browse files Browse the repository at this point in the history
  • Loading branch information
KangLin committed Dec 18, 2023
1 parent 1c7171b commit cebcefb
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 22 deletions.
16 changes: 16 additions & 0 deletions examples/etc/log.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
logpath=../log

log4cplus.appender.CONSOLE=log4cplus::ConsoleAppender
log4cplus.appender.CONSOLE.Append=true
log4cplus.appender.CONSOLE.layout=log4cplus::PatternLayout
log4cplus.appender.CONSOLE.layout.ConversionPattern=[%t] %-5p %c - %m
#log4cplus.appender.CONSOLE.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S,%Q} %l [%t] %-5p %c - %m

log4cplus.appender.FILE=log4cplus::DailyRollingFileAppender
log4cplus.appender.FILE.File=${logpath}/error.log
log4cplus.appender.FILE.Schedule=HOURLY
log4cplus.appender.FILE.Append=true
log4cplus.appender.FILE.layout=log4cplus::PatternLayout
log4cplus.appender.FILE.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S,%Q} [%t] %-5p %c - %m

log4cplus.rootLogger=ALL, CONSOLE, FILE
3 changes: 3 additions & 0 deletions examples/etc/turnserver.conf
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@
#
#dh-file=<DH-PEM-file-name>

# log configure file
#log-conf-file=/etc/turnserver/log.conf

# Flag to prevent stdout log messages.
# By default, all log messages go to both stdout and to
# the configured log file. With this option everything will
Expand Down
7 changes: 7 additions & 0 deletions src/apps/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ else()
endif()
endif()

find_package(log4cplus)
if(log4cplus_FOUND)
list(APPEND COMMON_LIBS log4cplus::log4cplus)
list(APPEND COMMON_DEFINED HAS_LOG4CPLUS)
list(APPEND SOURCE_FILES log4cplus.cpp)
endif()

find_package(hiredis)
if(hiredis_FOUND)
list(APPEND SOURCE_FILES hiredis_libevent2.c)
Expand Down
85 changes: 85 additions & 0 deletions src/apps/common/log4cplus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <log4cplus/logger.h>
#include <log4cplus/clogger.h>
#include <log4cplus/appender.h>
#include <log4cplus/hierarchylocker.h>
#include <log4cplus/hierarchy.h>
#include <log4cplus/helpers/loglog.h>
#include <log4cplus/configurator.h>
#include <log4cplus/streams.h>
#include <log4cplus/helpers/snprintf.h>
#include <log4cplus/initializer.h>
#include "ns_turn_utils.h"

using namespace log4cplus;
using namespace log4cplus::helpers;

void* turn_log_init(){
int ret = -1;
void* log = log4cplus_initialize();
const char* file = "../etc/log4cplus.conf";
FILE* pf = fopen(file, "a");
if(pf){
fclose(pf);
ret = log4cplus_file_configure(file);
}
if(ret)
log4cplus_basic_reconfigure(1);
return log;
}

void turn_log_clean(void* log){
log4cplus_deinitialize(log);
}

int turn_log_set_conf_file(const char* file){
return log4cplus_file_reconfigure(file);
}

LogLevel turn_level_to_loglevel(TURN_LOG_LEVEL level)
{
switch((int)level){
case TURN_LOG_LEVEL_DEBUG:
return DEBUG_LOG_LEVEL;
case TURN_LOG_LEVEL_INFO:
return INFO_LOG_LEVEL;
case TURN_LOG_LEVEL_WARNING:
return WARN_LOG_LEVEL;
case TURN_LOG_LEVEL_ERROR:
return ERROR_LOG_LEVEL;
}
return DEBUG_LOG_LEVEL;
}

void turn_log_func_default(char *file, int line, char* function, char *category, TURN_LOG_LEVEL level, const char *msgfmt, ...) {
int retval = -1;

try
{
Logger logger = category ? Logger::getInstance(category) : Logger::getRoot();
LogLevel ll = turn_level_to_loglevel(level);
if( logger.isEnabledFor(ll) )
{
const tchar * msg = nullptr;
snprintf_buf buf;
std::va_list ap;

do
{
va_start(ap, msgfmt);
retval = buf.print_va_list(msg, msgfmt, ap);
va_end(ap);
}
while (retval == -1);

logger.forcedLog(ll, msg, file, line, function);
}

retval = 0;
}
catch(std::exception const &)
{
// Fall through.
}

return;
}
42 changes: 27 additions & 15 deletions src/apps/common/ns_turn_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,6 @@ void rollover_logfile(void) {
static int get_syslog_level(TURN_LOG_LEVEL level) {
#if defined(__unix__) || defined(unix) || defined(__APPLE__)
switch (level) {
case TURN_LOG_LEVEL_CONTROL:
return LOG_NOTICE;
case TURN_LOG_LEVEL_WARNING:
return LOG_WARNING;
case TURN_LOG_LEVEL_ERROR:
Expand All @@ -526,7 +524,11 @@ void err(int eval, const char *format, ...) {
}
#endif

void turn_log_func_default(char *file, int line, TURN_LOG_LEVEL level, const char *format, ...) {
#if !defined(HAS_LOG4CPLUS)
void* turn_log_init(){return NULL;}
void turn_log_clean(void*){}
int turn_log_set_conf_file(const char* file){}
void turn_log_func_default(char *file, int line, char *category, TURN_LOG_LEVEL level, const char *format, ...) {
va_list args;
va_start(args, format);
#if defined(TURN_LOG_FUNC_IMPL)
Expand All @@ -543,32 +545,41 @@ void turn_log_func_default(char *file, int line, TURN_LOG_LEVEL level, const cha
so_far += snprintf(s, sizeof(s), "%lu: ", (unsigned long)log_time());
}

#if defined(WINDOWS) || defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__CYGWIN64__)
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "[%lu:%lu] ", GetCurrentProcessId(),
GetCurrentThreadId());
#else

#ifdef SYS_gettid
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "(%lu): ", (unsigned long)gettid());
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "[%lu] ", (unsigned long)gettid());
#endif

#endif

if (_log_file_line_set)
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "%s(%d):", file, line);

so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "%s(%d)", file, line);
switch (level) {
case TURN_LOG_LEVEL_DEBUG:
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "DEBUG: ");
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "DEBUG");
break;
case TURN_LOG_LEVEL_INFO:
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "INFO: ");
break;
case TURN_LOG_LEVEL_CONTROL:
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "CONTROL: ");
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "INFO");
break;
case TURN_LOG_LEVEL_WARNING:
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "WARNING: ");
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "WARNING");
break;
case TURN_LOG_LEVEL_ERROR:
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "ERROR: ");
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "ERROR");
break;
}

if (category && strcmp(category, ""))
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), " %s", category);
so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), ": ");

so_far += vsnprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), format, args);

if (so_far > MAX_RTPPRINTF_BUFFER_SIZE + 1) {
so_far = MAX_RTPPRINTF_BUFFER_SIZE + 1;
}
Expand All @@ -584,7 +595,7 @@ void turn_log_func_default(char *file, int line, TURN_LOG_LEVEL level, const cha
#else
syslog(syslog_facility | get_syslog_level(level), "%s", s);
#endif

} else {
log_lock();
set_rtpfile();
Expand All @@ -598,6 +609,7 @@ void turn_log_func_default(char *file, int line, TURN_LOG_LEVEL level, const cha
#endif
va_end(args);
}
#endif // !defined(HAS_LOG4CPLUS)

///////////// ORIGIN ///////////////////

Expand Down
15 changes: 11 additions & 4 deletions src/apps/common/ns_turn_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
#ifndef __TURN_ULIB__
#define __TURN_ULIB__

#define TURN_LOG_CATEGORY(category, level, ...) turn_log_func_default(__FILE__, __LINE__, __FUNCTION__, category, level, __VA_ARGS__)
#if !defined(TURN_LOG_FUNC)
#define TURN_LOG_FUNC(level, ...) turn_log_func_default(__FILE__, __LINE__, level, __VA_ARGS__)
#define TURN_LOG_FUNC(level, ...) TURN_LOG_CATEGORY(NULL, level, __VA_ARGS__)
#endif

#if defined(WINDOWS)
Expand All @@ -52,7 +53,6 @@ extern "C" {
typedef enum {
TURN_LOG_LEVEL_DEBUG = 0,
TURN_LOG_LEVEL_INFO,
TURN_LOG_LEVEL_CONTROL,
TURN_LOG_LEVEL_WARNING,
TURN_LOG_LEVEL_ERROR
} TURN_LOG_LEVEL;
Expand All @@ -71,9 +71,12 @@ void set_syslog_facility(char *val);

void set_turn_log_timestamp_format(char *new_format);

void turn_log_func_default(char *file, int line, TURN_LOG_LEVEL level, const char *format, ...)
/*!
* \note User don't use it. please use TURN_LOG_CATEGORY
*/
void turn_log_func_default(char *file, int line, char* function, char *category, TURN_LOG_LEVEL level, const char *format, ...)
#ifdef __GNUC__
__attribute__((format(printf, 4, 5)))
__attribute__((format(printf, 6, 7)))
#endif
;

Expand All @@ -84,6 +87,10 @@ extern volatile int _log_time_value_set;
extern volatile turn_time_t _log_time_value;
extern int use_new_log_timestamp_format;

void* turn_log_init();
void turn_log_clean(void*);
int turn_log_set_conf_file(const char* file);

void rtpprintf(const char *format, ...);
void reset_rtpprintf(void);
void set_logfile(const char *fn);
Expand Down
5 changes: 4 additions & 1 deletion src/apps/natdiscovery/natdiscovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,8 @@ int main(int argc, char **argv) {
int rfc5780;
int first = 1;
ioa_addr other_addr, reflexive_addr, tmp_addr, remote_addr, local_addr, local2_addr;


void* log = turn_log_init();
if (socket_init())
return -1;

Expand Down Expand Up @@ -814,5 +815,7 @@ int main(int argc, char **argv) {
socket_closesocket(udp_fd);
socket_closesocket(udp_fd2);

turn_log_clean(log);

return 0;
}
18 changes: 16 additions & 2 deletions src/apps/relay/mainrelay.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "dbdrivers/dbdriver.h"

#include "prom_server.h"
#include <math.h>

#if defined(WINDOWS)
#include <iphlpapi.h>
Expand Down Expand Up @@ -1141,6 +1142,9 @@ static char Usage[] =
" -l, --log-file <filename> Option to set the full path name of the log file.\n"
" By default, the turnserver tries to open a log file in\n"
" /var/log/turnserver/, /var/log, /var/tmp, /tmp and . (current) "
" --log-conf-file <filename> Option to set the full path name of the configure log file.\n"
" By default, the turnserver tries to open a configure log file in\n"
" /etc/turnserver/log.conf and ./log.conf (current) "
"directories\n"
" (which open operation succeeds first that file will be used).\n"
" With this option you can set the definite log file name.\n"
Expand Down Expand Up @@ -1374,6 +1378,7 @@ enum EXTRA_OPTS {
DEL_ALL_AUTH_SECRETS_OPT,
STATIC_AUTH_SECRET_VAL_OPT,
AUTH_SECRET_TS_EXP, /* deprecated */
LOG_CONF_FILE,
NO_STDOUT_LOG_OPT,
SYSLOG_OPT,
SYSLOG_FACILITY_OPT,
Expand Down Expand Up @@ -1524,6 +1529,7 @@ static const struct myoption long_options[] = {
{"pkey", required_argument, NULL, PKEY_FILE_OPT},
{"pkey-pwd", required_argument, NULL, PKEY_PWD_OPT},
{"log-file", required_argument, NULL, 'l'},
{"log-conf-file", optional_argument, NULL, LOG_CONF_FILE},
{"no-stdout-log", optional_argument, NULL, NO_STDOUT_LOG_OPT},
{"syslog", optional_argument, NULL, SYSLOG_OPT},
{"simple-log", optional_argument, NULL, SIMPLE_LOG_OPT},
Expand Down Expand Up @@ -2276,6 +2282,7 @@ static void set_option(int c, char *value) {

/* these options have been already taken care of before: */
case 'l':
case LOG_CONF_FILE:
case NO_STDOUT_LOG_OPT:
case SYSLOG_OPT:
case SIMPLE_LOG_OPT:
Expand Down Expand Up @@ -2408,6 +2415,8 @@ static void read_config_file(int argc, char **argv, int pass) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "Bad configuration format: %s\n", sarg);
} else if ((pass == 0) && (c == 'l')) {
set_logfile(value);
} else if((pass == 0) && (c == LOG_CONF_FILE)) {
turn_log_set_conf_file(value);
} else if ((pass == 0) && (c == NO_STDOUT_LOG_OPT)) {
set_no_stdout_log(get_bool_value(value));
} else if ((pass == 0) && (c == SYSLOG_OPT)) {
Expand Down Expand Up @@ -2858,7 +2867,8 @@ static void init_domain(void) {

int main(int argc, char **argv) {
int c = 0;


void *log = turn_log_init();
IS_TURN_SERVER = 1;

TURN_MUTEX_INIT(&turn_params.tls_mutex);
Expand Down Expand Up @@ -2890,6 +2900,9 @@ int main(int argc, char **argv) {
case 'l':
set_logfile(optarg);
break;
case LOG_CONF_FILE:
turn_log_set_conf_file(optarg);
break;
case NO_STDOUT_LOG_OPT:
set_no_stdout_log(get_bool_value(optarg));
break;
Expand Down Expand Up @@ -3238,7 +3251,8 @@ int main(int argc, char **argv) {
run_listener_server(&(turn_params.listener));

disconnect_database();


turn_log_clean(log);
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"features": [ "openssl", "thread" ]
},
"openssl",
"log4cplus",
"sqlite3",
"libpq",
"hiredis",
Expand Down

0 comments on commit cebcefb

Please sign in to comment.