Skip to content

Commit

Permalink
FS-11867 [sofia] Send siptrace logs in a single log callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
crienzo committed May 31, 2019
1 parent 6782cc8 commit 57cb3b3
Showing 1 changed file with 65 additions and 69 deletions.
134 changes: 65 additions & 69 deletions libsofia-sip-ua/tport/tport_logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include <errno.h>
#include <limits.h>

#define TPORT_STAMP_SIZE 144

/**@var TPORT_LOG
*
* Environment variable determining if parsed message contents are logged.
Expand Down Expand Up @@ -315,7 +317,7 @@ int tport_open_log(tport_master_t *mr, tagi_t *tags)

/** Create log stamp */
void tport_stamp(tport_t const *self, msg_t *msg,
char stamp[512], char const *what,
char *stamp, char const *what,
size_t n, char const *via,
su_time_t now)
{
Expand Down Expand Up @@ -357,7 +359,7 @@ void tport_stamp(tport_t const *self, msg_t *msg,

su_inet_ntop(su->su_family, SU_ADDR(su), name, sizeof(name));

snprintf(stamp, 144,
snprintf(stamp, TPORT_STAMP_SIZE,
"%s "MOD_ZU" bytes %s %s/[%s]:%u%s%s at %02u:%02u:%02u.%06lu:\n",
what, (size_t)n, via, self->tp_name->tpn_proto,
name, ntohs(su->su_port), label[0] ? label : "", comp,
Expand All @@ -370,7 +372,7 @@ void tport_dump_iovec(tport_t const *self, msg_t *msg,
char const *what, char const *how)
{
tport_master_t *mr;
char stamp[128];
char stamp[TPORT_STAMP_SIZE];
size_t i;

assert(self); assert(msg);
Expand Down Expand Up @@ -814,95 +816,89 @@ int tport_capt_msg_hepv3 (tport_t const *self, msg_t *msg, size_t n,
if(hg) free(hg);
return 0;
}


/** Log the message. */
void tport_log_msg(tport_t *self, msg_t *msg,
char const *what, char const *via,
su_time_t now)
{
char stamp[128];
msg_iovec_t iov[80];
size_t i, iovlen = msg_iovec(msg, iov, 80);
size_t linelen = 0, n, logged = 0, truncated = 0;
size_t n;
int skip_lf = 0;
int j, unprintable = 0;
char *buffer = NULL;
size_t buffer_size = 0;
size_t buffer_pos = 0;
size_t bytes_written = 0;

#define MSG_SEPARATOR \
"------------------------------------------------------------------------\n"
#define MAX_LINELEN 2047

for (i = n = 0; i < iovlen && i < 80; i++)
n += iov[i].mv_len;

tport_stamp(self, msg, stamp, what, n, via, now);
su_log("%s " MSG_SEPARATOR, stamp);
buffer_size = sizeof(char) * n + 1 + TPORT_STAMP_SIZE + sizeof(MSG_SEPARATOR);
if (buffer_size > 16000) {
buffer_size = 16000;
}

buffer = malloc(buffer_size);
buffer[0] = '\0';

tport_stamp(self, msg, buffer, what, n, via, now);
buffer_pos = strlen(buffer);
if (buffer_pos < buffer_size) {
bytes_written = snprintf(buffer + buffer_pos, buffer_size - buffer_pos, "%s", MSG_SEPARATOR);
if (bytes_written > 0) {
buffer_pos += bytes_written;
}
}

for (i = 0; truncated == 0 && i < iovlen && i < 80; i++) {
for (i = 0; buffer_pos < buffer_size && i < iovlen && i < 80; i++) {
char *s = iov[i].mv_base, *end = s + iov[i].mv_len;

if (skip_lf && s < end && s[0] == '\n') { s++; logged++; skip_lf = 0; }
if (skip_lf && s < end && s[0] == '\n') { s++; skip_lf = 0; }

while (s < end) {
if (s[0] == '\0') {
truncated = logged;
break;
}

n = su_strncspn(s, end - s, "\r\n");
if (s[0] == '\0') {
break;
}

n = su_strncspn(s, end - s, "\r\n");

bytes_written = snprintf(buffer + buffer_pos, buffer_size - buffer_pos, "%.*s", (int)n, s);
if (bytes_written > 0) {
buffer_pos += bytes_written;
}

s += n;

if (s == end)
break;

if (buffer_pos < buffer_size) {
buffer[buffer_pos++] = '\n';
}
/* Skip eol */
if (s[0] == '\r') {
s++;
if (s == end) {
skip_lf = 1;
continue;
}
}

if (linelen + n > MAX_LINELEN) {
n = MAX_LINELEN - linelen;
truncated = logged + n;
}

if (!unprintable) {
for (j = 0; j < 4; j++) {
if (s[j] == 0) break;
if (s[j] != 9 && s[j] != 10 && s[j] != 13 && (s[j] < 32 || s[j] > 126)) {
unprintable++;
}
}
}

if (unprintable) {
if (unprintable == 1)
su_log("\n <ENCODED DATA>");
unprintable++;
} else {
su_log("%s%.*s", linelen > 0 ? "" : " ", (int)n, s);
}

s += n, linelen += n, logged += n;

if (truncated)
break;
if (s == end)
break;

linelen = 0;
su_log("\n");

/* Skip eol */
if (s[0] == '\r') {
s++, logged++;
if (s == end) {
skip_lf = 1;
continue;
}
}

if (s[0] == '\n') {
s++, logged++;
}
if (s[0] == '\n') {
s++;
}
}
}

su_log("%s " MSG_SEPARATOR, linelen > 0 ? "\n" : "");

if (!truncated && i == 80)
truncated = logged;

if (truncated)
su_log(" *** message truncated at "MOD_ZU" ***\n", truncated);
if (buffer_pos >= buffer_size) {
buffer_pos = buffer_size - 1;
}
buffer[buffer_pos] = '\0';
su_log("%s", buffer);
free(buffer);
}

0 comments on commit 57cb3b3

Please sign in to comment.