提交 754fd7d7 authored 作者: William King's avatar William King

Merge pull request #666 in FS/freeswitch from…

Merge pull request #666 in FS/freeswitch from feature/FS-8728-mod_amqp-add-logging-ability to master

* commit 'e7d20832':
  FS-8728 default configs
  FS-8728 Adding logging profile and functionality
......@@ -60,4 +60,24 @@
</params>
</profile>
</commands>
<logging>
<profile name="default">
<connections>
<connection name="primary">
<param name="hostname" value="localhost"/>
<param name="virtualhost" value="/"/>
<param name="username" value="guest"/>
<param name="password" value="guest"/>
<param name="port" value="5672"/>
<param name="heartbeat" value="0"/>
</connection>
</connections>
<params>
<param name="exchange-name" value="TAP.Logging"/>
<param name="send_queue_size" value="5000"/>
<param name="reconnect_interval_ms" value="1000"/>
<param name="log-levels" value="debug,info,notice,warning,err,crit,alert"/>
</params>
</profile>
</logging>
</configuration>
......@@ -4,7 +4,7 @@ MODNAME=mod_amqp
if HAVE_AMQP
mod_LTLIBRARIES = mod_amqp.la
mod_amqp_la_SOURCES = mod_amqp_utils.c mod_amqp_connection.c mod_amqp_producer.c mod_amqp_command.c mod_amqp.c
mod_amqp_la_SOURCES = mod_amqp_utils.c mod_amqp_connection.c mod_amqp_producer.c mod_amqp_command.c mod_amqp_logging.c mod_amqp.c
mod_amqp_la_CFLAGS = $(AM_CFLAGS) $(AMQP_CFLAGS)
mod_amqp_la_LIBADD = $(switch_builddir)/libfreeswitch.la
mod_amqp_la_LDFLAGS = -avoid-version -module -no-undefined -shared $(AMQP_LIBS) $(SWITCH_AM_LDFLAGS)
......
......@@ -62,6 +62,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_amqp_load)
globals.pool = pool;
switch_core_hash_init(&(globals.producer_hash));
switch_core_hash_init(&(globals.command_hash));
switch_core_hash_init(&(globals.logging_hash));
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "mod_apqp loading: Version %s\n", switch_version_full());
......@@ -72,6 +73,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_amqp_load)
SWITCH_ADD_API(api_interface, "amqp", "amqp API", amqp_reload, "syntax");
switch_log_bind_logger(mod_amqp_logging_recv, SWITCH_LOG_DEBUG, SWITCH_FALSE);
return SWITCH_STATUS_SUCCESS;
}
......@@ -84,6 +87,7 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_amqp_shutdown)
switch_hash_index_t *hi;
mod_amqp_producer_profile_t *producer;
mod_amqp_command_profile_t *command;
mod_amqp_logging_profile_t *logging;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Mod starting shutting down\n");
switch_event_unbind_callback(mod_amqp_producer_event_handler);
......@@ -98,6 +102,13 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_amqp_shutdown)
mod_amqp_command_destroy(&command);
}
switch_log_unbind_logger(mod_amqp_logging_recv);
while ((hi = switch_core_hash_first(globals.logging_hash))) {
switch_core_hash_this(hi, NULL, NULL, (void **)&logging);
mod_amqp_logging_destroy(&logging);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Mod finished shutting down\n");
return SWITCH_STATUS_SUCCESS;
}
......
......@@ -146,17 +146,46 @@ typedef struct {
char *custom_attr;
} mod_amqp_command_profile_t;
typedef struct {
char *name;
char *exchange;
char *exchange_type;
int exchange_durable;
int exchange_auto_delete;
uint32_t log_level_mask;
/* Note: The AMQP channel is not reentrant this MUTEX serializes sending events. */
mod_amqp_connection_t *conn_root;
mod_amqp_connection_t *conn_active;
int reconnect_interval_ms;
/* Logging thread */
switch_thread_t *logging_thread;
switch_queue_t *send_queue;
unsigned int send_queue_size;
switch_mutex_t *mutex;
switch_bool_t running;
char *custom_attr;
switch_memory_pool_t *pool;
} mod_amqp_logging_profile_t;
struct {
switch_memory_pool_t *pool;
switch_hash_t *producer_hash;
switch_hash_t *command_hash;
switch_hash_t *logging_hash;
} globals;
/* utils */
switch_status_t mod_amqp_do_config(switch_bool_t reload);
int mod_amqp_log_if_amqp_error(amqp_rpc_reply_t x, char const *context);
int mod_amqp_count_chars(const char* string, char ch);
void mod_amqp_util_msg_destroy(mod_amqp_message_t **msg);
/* connection */
switch_status_t mod_amqp_connection_create(mod_amqp_connection_t **conn, switch_xml_t cfg, switch_memory_pool_t *pool);
......@@ -179,5 +208,11 @@ void * SWITCH_THREAD_FUNC mod_amqp_producer_thread(switch_thread_t *thread, void
char *amqp_util_encode(char *key, char *dest);
/* logging */
switch_status_t mod_amqp_logging_recv(const switch_log_node_t *node, switch_log_level_t level);
switch_status_t mod_amqp_logging_create(char *name, switch_xml_t cfg);
switch_status_t mod_amqp_logging_destroy(mod_amqp_logging_profile_t **prof);
void * SWITCH_THREAD_FUNC mod_amqp_logging_thread(switch_thread_t *thread, void *data);
#endif /* MOD_AMQP_H */
......@@ -38,13 +38,6 @@
#include "mod_amqp.h"
void mod_amqp_producer_msg_destroy(mod_amqp_message_t **msg)
{
if (!msg || !*msg) return;
switch_safe_free((*msg)->pjson);
switch_safe_free(*msg);
}
switch_status_t mod_amqp_producer_routing_key(mod_amqp_producer_profile_t *profile, char routingKey[MAX_AMQP_ROUTING_KEY_LENGTH],
switch_event_t* evt, mod_amqp_keypart_t routingKeyEventHeaderNames[])
{
......@@ -115,7 +108,7 @@ void mod_amqp_producer_event_handler(switch_event_t* evt)
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "AMQP message queue full. Messages will be dropped for %.1fs! (Queue capacity %d)",
profile->circuit_breaker_ms / 1000.0, queue_size);
mod_amqp_producer_msg_destroy(&amqp_message);
mod_amqp_util_msg_destroy(&amqp_message);
}
}
......@@ -155,7 +148,7 @@ switch_status_t mod_amqp_producer_destroy(mod_amqp_producer_profile_t **prof) {
profile->conn_root = NULL;
while (profile->send_queue && switch_queue_trypop(profile->send_queue, (void**)&msg) == SWITCH_STATUS_SUCCESS) {
mod_amqp_producer_msg_destroy(&msg);
mod_amqp_util_msg_destroy(&msg);
}
if (pool) {
......@@ -497,7 +490,7 @@ void * SWITCH_THREAD_FUNC mod_amqp_producer_thread(switch_thread_t *thread, void
switch (mod_amqp_producer_send(profile, msg)) {
case SWITCH_STATUS_SUCCESS:
/* Success: prepare for next message */
mod_amqp_producer_msg_destroy(&msg);
mod_amqp_util_msg_destroy(&msg);
break;
case SWITCH_STATUS_NOT_INITALIZED:
......@@ -541,7 +534,7 @@ void * SWITCH_THREAD_FUNC mod_amqp_producer_thread(switch_thread_t *thread, void
}
/* Abort the current message */
mod_amqp_producer_msg_destroy(&msg);
mod_amqp_util_msg_destroy(&msg);
// Terminate the thread
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Event sender thread stopped\n");
......
......@@ -144,6 +144,30 @@ switch_status_t mod_amqp_do_config(switch_bool_t reload)
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unable to locate commands section for mod_amqp\n" );
}
if ((profiles = switch_xml_child(cfg, "logging"))) {
if ((profile = switch_xml_child(profiles, "profile"))) {
for (; profile; profile = profile->next) {
char *name = (char *) switch_xml_attr_soft(profile, "name");
if (zstr(name)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to load mod_amqp profile. Check configs missing name attr\n");
continue;
}
name = switch_core_strdup(globals.pool, name);
if ( mod_amqp_logging_create(name, profile) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to load mod_amqp profile [%s]. Check configs\n", name);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Loaded mod_amqp profile [%s] successfully\n", name);
}
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Unable to locate a profile for mod_amqp\n" );
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unable to locate logging section for mod_amqp\n" );
}
return SWITCH_STATUS_SUCCESS;
}
......@@ -183,6 +207,14 @@ char *amqp_util_encode(char *key, char *dest) {
return dest;
}
void mod_amqp_util_msg_destroy(mod_amqp_message_t **msg)
{
if (!msg || !*msg) return;
switch_safe_free((*msg)->pjson);
switch_safe_free(*msg);
}
/* For Emacs:
* Local Variables:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论