[asterisk-commits] jrose: branch jrose/call_identifiers r359343 - in /team/jrose/call_identifier...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Mar 14 11:06:17 CDT 2012
Author: jrose
Date: Wed Mar 14 11:06:13 2012
New Revision: 359343
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=359343
Log:
Committing Phase I Prototype
Modified:
team/jrose/call_identifiers/include/asterisk/logger.h
team/jrose/call_identifiers/main/logger.c
team/jrose/call_identifiers/main/pbx.c
Modified: team/jrose/call_identifiers/include/asterisk/logger.h
URL: http://svnview.digium.com/svn/asterisk/team/jrose/call_identifiers/include/asterisk/logger.h?view=diff&rev=359343&r1=359342&r2=359343
==============================================================================
--- team/jrose/call_identifiers/include/asterisk/logger.h (original)
+++ team/jrose/call_identifiers/include/asterisk/logger.h Wed Mar 14 11:06:13 2012
@@ -214,6 +214,50 @@
* \since 1.8
*/
void ast_logger_unregister_level(const char *name);
+
+/* XXX needs documentation */
+struct ast_callid;
+
+/*!
+ * \brief factory function to create a new uniquely identifying callid.
+ *
+ * \retval ast_callid struct pointer containing the call id (and other information if that route is taken)
+ *
+ * \note The newly created callid will be referenced upon creation and this function should be
+ * paired with a call to ast_callid_unref()
+ */
+struct ast_callid *ast_create_callid(void);
+
+/*!
+ * \brief Increase callid reference count
+ *
+ * \param c the ast_callid
+ *
+ * \retval c always
+ */
+#define ast_callid_ref(c) ({ ao2_ref(c, +1); (c); })
+
+/*!
+ * \brief Decrease callid reference count
+ *
+ * \param c the ast_callid
+ *
+ * \retval NULL always
+ */
+#define ast_callid_unref(c) ({ ao2_ref(c, -1); (NULL); })
+
+/*!
+ * \brief Adds a known callid to thread storage of the calling thread
+ *
+ * \retval 0 - success
+ * \retval 1 - failure due to thread already being bound to a callid
+ * \note possibly other retvals
+ */
+int ast_callid_threadassoc_add(struct ast_callid *callid);
+
+/*
+ * May need a function to clean the threadstorage if we want to repurpose a thread.
+ */
/*!
* \brief Send a log message to a dynamically registered log level
Modified: team/jrose/call_identifiers/main/logger.c
URL: http://svnview.digium.com/svn/asterisk/team/jrose/call_identifiers/main/logger.c?view=diff&rev=359343&r1=359342&r2=359343
==============================================================================
--- team/jrose/call_identifiers/main/logger.c (original)
+++ team/jrose/call_identifiers/main/logger.c Wed Mar 14 11:06:13 2012
@@ -43,6 +43,7 @@
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/manager.h"
+#include "asterisk/astobj2.h"
#include "asterisk/threadstorage.h"
#include "asterisk/strings.h"
#include "asterisk/pbx.h"
@@ -73,6 +74,14 @@
static unsigned int global_logmask = 0xFFFF;
static int queuelog_init;
static int logger_initialized;
+static volatile int next_unique_callid; /* Used to assign unique call_ids to calls */
+static void unique_callid_cleanup(void *data);
+
+struct ast_callid {
+ int call_identifier; /* Numerical value of the call displayed in the logs */
+};
+
+AST_THREADSTORAGE_CUSTOM(unique_callid, NULL, unique_callid_cleanup);
static enum rotatestrategy {
SEQUENTIAL = 1 << 0, /* Original method - create a new file, in order */
@@ -129,6 +138,7 @@
int level;
int line;
int lwp;
+ struct ast_callid *callid;
AST_DECLARE_STRING_FIELDS(
AST_STRING_FIELD(date);
AST_STRING_FIELD(file);
@@ -138,6 +148,14 @@
);
AST_LIST_ENTRY(logmsg) list;
};
+
+static void logmsg_free(struct logmsg *msg)
+{
+ if (msg->callid) {
+ ast_callid_unref(msg->callid);
+ }
+ ast_free(msg);
+}
static AST_LIST_HEAD_STATIC(logmsgs, logmsg);
static pthread_t logthread = AST_PTHREADT_NULL;
@@ -999,6 +1017,14 @@
if (!AST_RWLIST_EMPTY(&logchannels)) {
AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
+ /* XXX May need to grow larger later in order to accomodate call counts higher than 999999. */
+ char call_identifier_str[10] = "";
+
+ if (logmsg->callid) {
+ snprintf(call_identifier_str, sizeof(call_identifier_str), "[C%06d]", logmsg->callid->call_identifier);
+ }
+
+
/* If the channel is disabled, then move on to the next one */
if (chan->disabled) {
continue;
@@ -1022,10 +1048,11 @@
/* Turn the numerical line number into a string */
snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
/* Build string to print out */
- snprintf(buf, sizeof(buf), "[%s] %s[%d]: %s:%s %s: %s",
+ snprintf(buf, sizeof(buf), "[%s] %s[%d]%s: %s:%s %s: %s",
logmsg->date,
term_color(tmp1, logmsg->level_name, colors[logmsg->level], 0, sizeof(tmp1)),
logmsg->lwp,
+ call_identifier_str,
term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
@@ -1042,8 +1069,9 @@
}
/* Print out to the file */
- res = fprintf(chan->fileptr, "[%s] %s[%d] %s: %s",
- logmsg->date, logmsg->level_name, logmsg->lwp, logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
+ res = fprintf(chan->fileptr, "[%s] %s[%d]%s %s: %s",
+ logmsg->date, logmsg->level_name, logmsg->lwp, call_identifier_str,
+ logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
if (errno == ENOMEM || errno == ENOSPC)
@@ -1100,7 +1128,7 @@
logger_print_normal(msg);
/* Free the data since we are done */
- ast_free(msg);
+ logmsg_free(msg);
}
/* If we should stop, then stop */
@@ -1203,6 +1231,58 @@
return;
}
+struct ast_callid *ast_create_callid(void)
+{
+ struct ast_callid *call;
+ int using;
+
+ if (!(call = ao2_alloc(sizeof(struct ast_callid), NULL))) {
+ ast_log(LOG_ERROR, "Could not allocate callid struct.\n");
+ return NULL;
+ }
+
+ using = ast_atomic_fetchadd_int(&next_unique_callid, +1);
+
+ call->call_identifier = using;
+ return call;
+}
+
+int ast_callid_threadassoc_add(struct ast_callid *callid)
+{
+ struct ast_callid **pointing;
+ pointing = ast_threadstorage_get(&unique_callid, sizeof(struct ast_callid **));
+ if (!(pointing)) {
+ ast_log(LOG_ERROR, "Failed to allocate thread storage.\n");
+ return -1;
+ }
+
+ if (!(*pointing)) {
+ ast_callid_ref(callid);
+ *pointing = callid;
+ /* callid will be unreffed at thread destruction */
+ } else {
+ ast_log(LOG_WARNING, "Attempted to ast_callid_threadassoc_add on thread already associated with a callid.\n");
+ return 1;
+ }
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief thread storage cleanup function for unique_callid
+ */
+static void unique_callid_cleanup(void *data)
+{
+ struct ast_callid **callid = data;
+
+ if (*callid) {
+ ast_callid_unref(*callid);
+ }
+
+ ast_free(data);
+}
+
/*!
* \brief send log messages to syslog and/or the console
*/
@@ -1212,6 +1292,7 @@
struct ast_str *buf = NULL;
struct ast_tm tm;
struct timeval now = ast_tvnow();
+ struct ast_callid **callid;
int res = 0;
va_list ap;
char datestring[256];
@@ -1262,6 +1343,14 @@
logmsg->type = LOGMSG_NORMAL;
}
+ callid = ast_threadstorage_get(&unique_callid, sizeof(struct ast_callid **));
+
+ if (callid && *callid) {
+ ast_callid_ref(*callid);
+ logmsg->callid = (*callid);
+ /* callid will be unreffed at logmsg destruction */
+ }
+
/* Create our date/time */
ast_localtime(&now, &tm, NULL);
ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
@@ -1283,7 +1372,7 @@
AST_LIST_UNLOCK(&logmsgs);
} else {
logger_print_normal(logmsg);
- ast_free(logmsg);
+ logmsg_free(logmsg);
}
return;
Modified: team/jrose/call_identifiers/main/pbx.c
URL: http://svnview.digium.com/svn/asterisk/team/jrose/call_identifiers/main/pbx.c?view=diff&rev=359343&r1=359342&r2=359343
==============================================================================
--- team/jrose/call_identifiers/main/pbx.c (original)
+++ team/jrose/call_identifiers/main/pbx.c Wed Mar 14 11:06:13 2012
@@ -5486,6 +5486,11 @@
*/
struct ast_channel *c = data;
+ /* Associate new PBX thread with a call-id */
+ struct ast_callid *callid = ast_create_callid();
+ ast_callid_threadassoc_add(callid);
+ callid = ast_callid_unref(callid);
+
__ast_pbx_run(c, NULL);
decrease_call_count();
More information about the asterisk-commits
mailing list