Commit b6fad25e authored by Pekka Pessi's avatar Pekka Pessi

nta: updated sl_utils_log.c and sl_utils_print.c. Updated sl_header_log()...

nta: updated sl_utils_log.c and sl_utils_print.c. Updated sl_header_log() prototype and implemented it.

darcs-hash:20061214122432-65a35-c62ffdc80668b136f8c80545b5f5fc5054a1c1a8.gz
parent 7fbff9ad
......@@ -35,6 +35,7 @@
#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
......@@ -123,24 +124,21 @@ void sl_sip_log(su_log_t *log,
* @param log output log
* @param level logging level of output
* @param fmt output format
* @param from header object
*
* @param from @From header
*/
void sl_from_log(su_log_t *log, int level,
char const *fmt, sip_from_t const *from)
{
char s[1024];
char const *d = from->a_display;
snprintf(s, sizeof(s), "%s%s<" URL_FORMAT_STRING ">",
d ? d : "", d ? " " : "",
URL_PRINT_ARGS(from->a_url));
sip_addr_t a[1];
if (log == NULL)
log = su_log_default;
if (from == NULL)
return;
su_llog(log, level, fmt, s);
memcpy(a, from, sizeof a);
a->a_params = NULL;
if (!a->a_display) a->a_display = "";
return sl_header_log(log, level, fmt, (sip_header_t *)a);
}
/**Log a @To header.
......@@ -153,7 +151,7 @@ void sl_from_log(su_log_t *log, int level,
* @param log output log
* @param level logging level of output
* @param fmt output format
* @param to header object
* @param to @To header
*/
void sl_to_log(su_log_t *log, int level, char const *fmt, sip_to_t const *to)
{
......@@ -170,7 +168,7 @@ void sl_to_log(su_log_t *log, int level, char const *fmt, sip_to_t const *to)
* @param log output log
* @param level logging level of output
* @param fmt output format
* @param contact header object
* @param contact @Contact header
*/
void sl_contact_log(su_log_t *log, int level,
char const *fmt, sip_contact_t const *m)
......@@ -188,29 +186,13 @@ void sl_contact_log(su_log_t *log, int level,
* @param log output log
* @param level logging level of output
* @param fmt output format
* @param allow header object
* @param allow @Allow header
*
*/
void sl_allow_log(su_log_t *log, int level,
char const *fmt, sip_allow_t const *allow)
{
char *s, b[1024], *end = b + sizeof(b) - 1;
msg_param_t const *p;
sip_allow_t const *k = allow;
s = b; *end = '\0';
for (; k; k = k->k_next) {
for (p = k->k_items; p && *p; p++) {
strncpy(s, s == b ? "" : ",", end - s);
strncat(s, *p, end - s);
s += strlen(s);
}
}
if (log == NULL)
log = su_log_default;
su_llog(log, level, fmt, b);
sl_header_log(log, level, fmt, (sip_header_t *)allow);
}
......@@ -221,25 +203,13 @@ void sl_allow_log(su_log_t *log, int level,
* is replaced with header contents. If @a fmt is @c NULL, only the header
* contents are logged.
*
* @param log output log
* @param fmt output format
* @param v header object
*
* @return
* The function sl_via_log() returns number of bytes logged,
* or -1 upon an error.
* @param log output log
* @param fmt format used when logging
* @param v via header
*/
void sl_via_log(su_log_t *log, int level, char const *fmt, sip_via_t const *v)
{
char s[256];
sip_header_field_e(s, sizeof(s), (sip_header_t const *)v, 0);
s[sizeof(s) - 1] = '\0';
if (!fmt)
fmt = "%s";
su_llog(log, level, fmt, s);
sl_header_log(log, level, fmt, (sip_header_t *)v);
}
......@@ -281,3 +251,37 @@ void sl_payload_log(su_log_t *log, int level,
s += n + crlf;
}
}
/** Log a header.
*
* Logs the contents of an header to the output @a stream. The @a fmt
* specifies the output format, where %s is replaced with header contents.
* If @a fmt is @c NULL, only the header contents are logged.
*
* @param stream output stream
* @param fmt output format
* @param h a SIP header object
*/
void sl_header_log(su_log_t *log, int level, char const *fmt,
sip_header_t const *h)
{
char *s, b[1024];
issize_t len;
len = sip_header_field_e(s = b, sizeof b, h, 0);
if (len == -1)
return;
if ((size_t)len >= sizeof b) {
s = malloc(len + 1); if (!s) return -1;
sip_header_field_e(s, len + 1, h, 0);
}
s[len] = '\0';
if (fmt == NULL)
fmt = "%s\n";
su_llog(log, level, fmt, s);
if (s != b)
free(s);
}
......@@ -35,6 +35,7 @@
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
......@@ -116,19 +117,16 @@ void sl_message_log(FILE *stream,
*/
issize_t sl_from_print(FILE *stream, char const *fmt, sip_from_t const *from)
{
char s[1024];
char const *d = from->a_display;
sip_addr_t a[1];
snprintf(s, sizeof(s), "%s%s<" URL_FORMAT_STRING ">",
d ? d : "", d ? " " : "",
URL_PRINT_ARGS(from->a_url));
if (from == NULL)
return -1;
if (fmt && strcmp(fmt, "%s"))
return fprintf(stream, fmt, s);
if (fputs(s, stream) >= 0)
return (issize_t)strlen(s);
return -1;
memcpy(a, from, sizeof a);
a->a_params = NULL;
if (!a->a_display) a->a_display = "";
return sl_header_print(stream, fmt, (sip_header_t *)a);
}
/** Print @To header.
......@@ -166,7 +164,8 @@ issize_t sl_to_print(FILE *stream, char const *fmt, sip_to_t const *to)
* The function sl_contact_print() returns number of bytes printed,
* or -1 upon an error.
*/
issize_t sl_contact_print(FILE *stream, char const *fmt, sip_contact_t const *m)
issize_t sl_contact_print(FILE *stream, char const *fmt,
sip_contact_t const *m)
{
return sl_from_print(stream, fmt, (sip_from_t const *)m);
}
......@@ -186,27 +185,11 @@ issize_t sl_contact_print(FILE *stream, char const *fmt, sip_contact_t const *m)
* The function sl_allow_print() returns number of bytes printed,
* or -1 upon an error.
*/
issize_t sl_allow_print(FILE *stream, char const *fmt, sip_allow_t const *allow)
issize_t sl_allow_print(FILE *stream,
char const *fmt,
sip_allow_t const *allow)
{
char *s, b[1024], *end = b + sizeof(b) - 1;
msg_param_t const *p;
sip_allow_t const *k = allow;
s = b; *end = '\0';
for (; k; k = k->k_next) {
for (p = k->k_items; p && *p; p++) {
strncpy(s, s == b ? "" : ",", end - s);
strncat(s, *p, end - s);
s += strlen(s);
}
}
if (fmt && strcmp(fmt, "%s"))
return fprintf(stream, fmt, b);
if (fputs(b, stream) >= 0)
return (issize_t)strlen(b);
return -1;
return sl_header_print(stream, fmt, (sip_header_t *)allow);
}
......@@ -274,31 +257,41 @@ issize_t sl_via_print(FILE *stream, char const *fmt, sip_via_t const *v)
return -1;
}
/** Print an header.
/** Print a header.
*
* The function sl_header_print() prints the contents of an header to the
* output @a stream. The @a fmt specifies the output format, where %s is
* replaced with header contents. If @a fmt is @c NULL, only the header
* contents are printed.
* Prints the contents of an header to the output @a stream. The @a fmt
* specifies the output format, where %s is replaced with header contents.
* If @a fmt is @c NULL, only the header contents are printed.
*
* @param stream output stream
* @param fmt output format
* @param v header object
*
* @return
* The function sl_header_print() returns number of bytes printed,
* or -1 upon an error.
* Number of bytes logged, or -1 upon an error.
*/
issize_t sl_header_print(FILE *stream, char const *fmt, sip_header_t const *h)
{
char s[1024];
char *s, b[1024];
issize_t len;
sip_header_field_e(s, sizeof(s), h, 0);
s[sizeof(s) - 1] = '\0';
len = sip_header_field_e(s = b, sizeof b, h, 0);
if (len == -1)
return len;
if (fmt && strcmp(fmt, "%s"))
return fprintf(stream, fmt, s);
if (fputs(s, stream) >= 0)
return (issize_t)strlen(s);
return -1;
if ((size_t)len >= sizeof b) {
s = malloc(len + 1); if (!s) return -1;
sip_header_field_e(s, len + 1, h, 0);
}
s[len] = '\0';
if (fmt != NULL && strcmp(fmt, "%s") != 0)
len = fprintf(stream, fmt, s);
else if (fputs(s, stream) < 0)
len = -1;
if (s != b)
free(s);
return len;
}
......@@ -71,7 +71,7 @@ sl_via_print(FILE *, char const *fmt, sip_via_t const *v);
/* Logging functions */
SOFIAPUBFUN void
sl_sip_log(su_log_t*, int lvl, char const *, sip_t const *, int details),
sl_header_log(su_log_t *, char const *prefix, sip_header_t const *h),
sl_header_log(su_log_t *, int lvl, char const *, sip_header_t const *h),
sl_from_log(su_log_t *, int lvl, char const *, sip_from_t const *from),
sl_to_log(su_log_t *, int lvl, char const *, sip_to_t const *to),
sl_contact_log(su_log_t *, int lvl, char const *, sip_contact_t const *m),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment