[libpri-commits] rmudgett: branch 1.4 r1351 - in /branches/1.4: pri.c pri_internal.h

SVN commits to the libpri project libpri-commits at lists.digium.com
Fri Nov 20 20:40:24 CST 2009


Author: rmudgett
Date: Fri Nov 20 20:40:23 2009
New Revision: 1351

URL: http://svnview.digium.com/svn/libpri?view=rev&rev=1351
Log:
Fix debug output so built up output lines are readable again.

A recent change to Asterisk put the span number at the begining of each
line.  This is a good thing if you need to debug multiple spans or forget
which span you are debugging.  Unfortunately, any pri_message() output
that is not a complete line is messed up.

The pri_message() function now will accumulate line output until a '\n' is
seen on the end.

Modified:
    branches/1.4/pri.c
    branches/1.4/pri_internal.h

Modified: branches/1.4/pri.c
URL: http://svnview.digium.com/svn/libpri/branches/1.4/pri.c?view=diff&rev=1351&r1=1350&r2=1351
==============================================================================
--- branches/1.4/pri.c (original)
+++ branches/1.4/pri.c Fri Nov 20 20:40:23 2009
@@ -239,6 +239,7 @@
 			pri_schedule_del(call->pri, call->retranstimer);
 			pri_call_apdu_queue_cleanup(call);
 		}
+		free(p->msg_line);
 		free(p);
 	}
 }
@@ -266,6 +267,14 @@
 		}
 		p = &dummy_ctrl->ctrl;
 		break;
+	}
+	if (!master) {
+		/* This is the master record. */
+		p->msg_line = calloc(1, sizeof(*p->msg_line));
+		if (!p->msg_line) {
+			free(p);
+			return NULL;
+		}
 	}
 
 	p->bri = bri;
@@ -1121,20 +1130,77 @@
 	__pri_error = func;
 }
 
-void pri_message(struct pri *pri, char *fmt, ...)
+static void pri_old_message(struct pri *ctrl, const char *fmt, va_list *ap)
 {
 	char tmp[1024];
-	va_list ap;
-	va_start(ap, fmt);
-	vsnprintf(tmp, sizeof(tmp), fmt, ap);
-	va_end(ap);
+
+	vsnprintf(tmp, sizeof(tmp), fmt, *ap);
 	if (__pri_message)
-		__pri_message(PRI_MASTER(pri), tmp);
+		__pri_message(ctrl, tmp);
 	else
 		fputs(tmp, stdout);
 }
 
-void pri_error(struct pri *pri, char *fmt, ...)
+void pri_message(struct pri *ctrl, const char *fmt, ...)
+{
+	int added_length;
+	va_list ap;
+
+	ctrl = PRI_MASTER(ctrl);
+	if (!ctrl || !ctrl->msg_line) {
+		/* Just have to do it the old way. */
+		va_start(ap, fmt);
+		pri_old_message(ctrl, fmt, &ap);
+		va_end(ap);
+		return;
+	}
+
+	va_start(ap, fmt);
+	added_length = vsnprintf(ctrl->msg_line->str + ctrl->msg_line->length,
+		sizeof(ctrl->msg_line->str) - ctrl->msg_line->length, fmt, ap);
+	va_end(ap);
+	if (added_length < 0
+		|| sizeof(ctrl->msg_line->str) <= ctrl->msg_line->length + added_length) {
+		static char truncated_output[] =
+			"v-- Error building output or output was truncated. (Next line) --v\n";
+
+		/*
+		 * This clause should never need to run because the
+		 * output line accumulation buffer is quite large.
+		 */
+
+		/* vsnprintf() error or output string was truncated. */
+		if (__pri_message) {
+			__pri_message(ctrl, truncated_output);
+		} else {
+			fputs(truncated_output, stdout);
+		}
+
+		/* Add a terminating '\n' to force a flush of the line. */
+		ctrl->msg_line->length = strlen(ctrl->msg_line->str);
+		if (ctrl->msg_line->length) {
+			ctrl->msg_line->str[ctrl->msg_line->length - 1] = '\n';
+		} else {
+			ctrl->msg_line->str[0] = '\n';
+			ctrl->msg_line->str[1] = '\0';
+		}
+	} else {
+		ctrl->msg_line->length += added_length;
+	}
+
+	if (ctrl->msg_line->length
+		&& ctrl->msg_line->str[ctrl->msg_line->length - 1] == '\n') {
+		/* The accumulated output line was terminated so send it out. */
+		ctrl->msg_line->length = 0;
+		if (__pri_message) {
+			__pri_message(ctrl, ctrl->msg_line->str);
+		} else {
+			fputs(ctrl->msg_line->str, stdout);
+		}
+	}
+}
+
+void pri_error(struct pri *pri, const char *fmt, ...)
 {
 	char tmp[1024];
 	va_list ap;

Modified: branches/1.4/pri_internal.h
URL: http://svnview.digium.com/svn/libpri/branches/1.4/pri_internal.h?view=diff&rev=1351&r1=1350&r2=1351
==============================================================================
--- branches/1.4/pri_internal.h (original)
+++ branches/1.4/pri_internal.h Fri Nov 20 20:40:23 2009
@@ -55,12 +55,22 @@
 /*! Maximum number of facility ie's to handle per incoming message. */
 #define MAX_FACILITY_IES	8
 
+/*! Accumulated pri_message() line until a '\n' is seen on the end. */
+struct pri_msg_line {
+	/*! Accumulated buffer used. */
+	unsigned length;
+	/*! Accumulated pri_message() contents. */
+	char str[2048];
+};
+
 /*! \brief D channel controller structure */
 struct pri {
 	int fd;				/* File descriptor for D-Channel */
 	pri_io_cb read_func;		/* Read data callback */
 	pri_io_cb write_func;		/* Write data callback */
 	void *userdata;
+	/*! Accumulated pri_message() line. (Valid in master record only) */
+	struct pri_msg_line *msg_line;
 	struct pri *subchannel;	/* Sub-channel if appropriate */
 	struct pri *master;		/* Master channel if appropriate */
 	struct pri_sched pri_sched[MAX_SCHED];	/* Scheduled events */
@@ -525,7 +535,7 @@
 
 /*! D channel control structure with associated dummy call reference record. */
 struct d_ctrl_dummy {
-	/*! D channel control structure. */
+	/*! D channel control structure. Must be first in the structure. */
 	struct pri ctrl;
 	/*! Dummy call reference call record. */
 	struct q931_call dummy_call;
@@ -539,8 +549,8 @@
 
 extern pri_event *pri_mkerror(struct pri *pri, char *errstr);
 
-void pri_message(struct pri *ctrl, char *fmt, ...) __attribute__((format(printf, 2, 3)));
-void pri_error(struct pri *ctrl, char *fmt, ...) __attribute__((format(printf, 2, 3)));
+void pri_message(struct pri *ctrl, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
+void pri_error(struct pri *ctrl, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 
 void libpri_copy_string(char *dst, const char *src, size_t size);
 




More information about the libpri-commits mailing list