Skip to content

Commit

Permalink
coap-server.c: Add in support for output to syslog
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 committed Oct 19, 2023
1 parent 95faeac commit 26302a0
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 11 deletions.
43 changes: 40 additions & 3 deletions examples/coap-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2132,8 +2132,8 @@ usage(const char *program, const char *version) {
coap_string_tls_version(buffer, sizeof(buffer)));
fprintf(stderr, "%s\n", coap_string_tls_support(buffer, sizeof(buffer)));
fprintf(stderr, "\n"
"Usage: %s [-d max] [-e] [-g group] [-l loss] [-p port] [-r] [-v num]\n"
"\t\t[-w [port][,secure_port]] [-A address]\n"
"Usage: %s [-a priority] [-d max] [-e] [-g group] [-l loss] [-p port]\n"
"\t\t[-r] [-v num] [-w [port][,secure_port]] [-A address]\n"
"\t\t[-E oscore_conf_file[,seq_file]] [-G group_if] [-L value] [-N]\n"
"\t\t[-P scheme://address[:port],[name1[,name2..]]]\n"
"\t\t[-T max_token_size] [-U type] [-V num] [-X size]\n"
Expand All @@ -2143,6 +2143,7 @@ usage(const char *program, const char *version) {
"\t\t[-J pkcs11_pin] [-M rpk_file] [-R trust_casfile]\n"
"\t\t[-S match_pki_sni_file]]\n"
"General Options\n"
"\t-a priority\tSend logging output to syslog at priority (0-7) level\n"
"\t-d max \t\tAllow dynamic creation of up to a total of max\n"
"\t \t\tresources. If max is reached, a 4.06 code is returned\n"
"\t \t\tuntil one of the dynamic resources has been deleted\n"
Expand Down Expand Up @@ -2704,6 +2705,24 @@ cmdline_read_extended_token_size(char *arg) {
return 1;
}

#ifndef _WIN32

uint32_t syslog_pri = 0;

static void
syslog_handler(coap_log_t level, const char *message) {
char *cp = strchr(message, '\n');

if (cp) {
char *lcp = strchr(message, '\r');
if (lcp && lcp < cp)
cp = lcp;
}
syslog(syslog_pri, "%s %*.*s", coap_log_level_desc(level), (int)(cp-message),
(int)(cp-message), message);
}
#endif /* ! _WIN32 */

int
main(int argc, char **argv) {
coap_context_t *ctx = NULL;
Expand All @@ -2723,6 +2742,9 @@ main(int argc, char **argv) {
int nfds = 0;
size_t i;
int exit_code = 0;
#ifndef _WIN32
int use_syslog = 0;
#endif /* ! _WIN32 */
uint16_t cache_ignore_options[] = { COAP_OPTION_BLOCK1,
COAP_OPTION_BLOCK2,
/* See https://rfc-editor.org/rfc/rfc7959#section-2.10 */
Expand All @@ -2740,8 +2762,16 @@ main(int argc, char **argv) {
clock_offset = time(NULL);

while ((opt = getopt(argc, argv,
"c:d:eg:G:h:i:j:J:k:l:mnp:rs:tu:v:w:A:C:E:L:M:NP:R:S:T:U:V:X:")) != -1) {
"a:c:d:eg:G:h:i:j:J:k:l:mnp:rs:tu:v:w:A:C:E:L:M:NP:R:S:T:U:V:X:")) != -1) {
switch (opt) {
#ifndef _WIN32
case 'a':
use_syslog = 1;
syslog_pri = atoi(optarg);
if (syslog_pri > 7)
syslog_pri = 7;
break;
#endif /* ! _WIN32 */
case 'A' :
strncpy(addr_str, optarg, NI_MAXHOST-1);
addr_str[NI_MAXHOST - 1] = '\0';
Expand Down Expand Up @@ -2913,6 +2943,13 @@ main(int argc, char **argv) {
sigaction(SIGPIPE, &sa, NULL);
#endif

#ifndef _WIN32
if (use_syslog) {
openlog("coap-server", 0, LOG_DAEMON);
coap_set_show_pdu_output(0);
coap_set_log_handler(syslog_handler);
}
#endif /* ! _WIN32 */
coap_set_log_level(log_level);
coap_dtls_set_log_level(dtls_log_level);

Expand Down
7 changes: 7 additions & 0 deletions include/coap3/coap_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ void coap_dtls_set_log_level(coap_log_t level);
*/
coap_log_t coap_dtls_get_log_level(void);

/**
* Get the current logging description.
*
* @return Ascii representation of logging level.
*/
const char *coap_log_level_desc(coap_log_t level);

/**
* Logging callback handler definition.
*
Expand Down
1 change: 1 addition & 0 deletions libcoap-3.map
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ global:
coap_is_mcast;
coap_join_mcast_group_intf;
coap_log_impl;
coap_log_level_desc;
coap_make_str_const;
coap_malloc_type;
coap_mcast_per_resource;
Expand Down
1 change: 1 addition & 0 deletions libcoap-3.sym
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ coap_is_bcast
coap_is_mcast
coap_join_mcast_group_intf
coap_log_impl
coap_log_level_desc
coap_make_str_const
coap_malloc_type
coap_mcast_per_resource
Expand Down
1 change: 1 addition & 0 deletions man/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ install-man: install-man3 install-man5 install-man7
@echo ".so man3/coap_logging.3" > coap_dtls_log.3
@echo ".so man3/coap_logging.3" > coap_dtls_get_log_level.3
@echo ".so man3/coap_logging.3" > coap_dtls_set_log_level.3
@echo ".so man3/coap_logging.3" > coap_log_level_desc.3
@echo ".so man3/coap_logging.3" > coap_package_name.3
@echo ".so man3/coap_logging.3" > coap_package_version.3
@echo ".so man3/coap_logging.3" > coap_package_build.3
Expand Down
7 changes: 5 additions & 2 deletions man/coap-server.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ coap-server-notls

SYNOPSIS
--------
*coap-server* [*-d* max] [*-e*] [*-g* group] [*-l* loss] [*-p* port] [*-r*]
[*-t*] [*-v* num] [*-w* [port][,secure_port]]
*coap-server* [*-a* priority] [*-d* max] [*-e*] [*-g* group] [*-l* loss] [*-p* port]
[*-r*] [*-t*] [*-v* num] [*-w* [port][,secure_port]]
[*-A* address] [*-E* oscore_conf_file[,seq_file]]
[*-G* group_if] [*-L* value] [*-N*]
[*-P* scheme://addr[:port],[name1[,name2..]]]
Expand All @@ -43,6 +43,9 @@ DESCRIPTION

OPTIONS - General
-----------------
*-a* priority::
Send logging output to syslog at 'priority' (0-7) level.

*-d* max::
Enable support for creation of dynamic resources when doing a PUT up to a
limit of 'max'. If 'max' is reached, a 4.06 code is returned until one of
Expand Down
11 changes: 11 additions & 0 deletions man/coap_logging.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ coap_dtls_log,
coap_dtls_get_log_level,
coap_dtls_set_log_level,
coap_set_log_handler,
coap_log_level_desc,
coap_package_name,
coap_package_version,
coap_package_build,
Expand Down Expand Up @@ -74,6 +75,8 @@ SYNOPSIS

*void coap_set_log_handler(coap_log_handler_t _handler_);*

*const char *coap_log_level_desc(coap_log_t _level_);*

*const char *coap_package_name(void);*

*const char *coap_package_version(void);*
Expand Down Expand Up @@ -248,6 +251,11 @@ prototype is defined as:
typedef void (*coap_log_handler_t) (coap_log_t level, const char *message);
----

*Function: coap_log_level_desc()*

The *coap_log_level_desc*()* function can be used to return a 4 character ascii
description for the logging _level_.

*Function: coap_package_name()*

The *coap_package_name*() function returns the name of this library.
Expand Down Expand Up @@ -313,6 +321,9 @@ string.
*coap_dtls_get_log_level*() returns the current logging level
for the DTLS library specifics.

*coap_log_level_desc*() returns a description string of the logging
_level_.

*coap_endpoint_str*() returns a description string of the
_endpoint_.

Expand Down
19 changes: 13 additions & 6 deletions src/coap_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,21 @@ static const char *loglevels[] = {
"Emrg", "Alrt", "Crit", "Err ", "Warn", "Note", "Info", "Debg"
};

const char *
coap_log_level_desc(coap_log_t level) {
static char bad[8];
if (level >= sizeof(loglevels)/sizeof(loglevels[0])) {
snprintf(bad, sizeof(bad), "%4d", level);
return bad;
} else {
return loglevels[level];
}
}

#ifdef WITH_CONTIKI
void
coap_print_contiki_prefix(coap_log_t level) {
printf("[%s: COAP ] ", loglevels[level]);
printf("[%s: COAP ] ", coap_log_level_desc(level));
}
#endif /* WITH_CONTIKI */

Expand Down Expand Up @@ -1219,11 +1230,7 @@ coap_log_impl(coap_log_t level, const char *format, ...) {
if (len)
fprintf(log_fd, "%.*s ", (int)len, timebuf);

if (level >= sizeof(loglevels)/sizeof(loglevels[0])) {
fprintf(log_fd, "%4d ", level);
} else {
fprintf(log_fd, "%s ", loglevels[level]);
}
fprintf(log_fd, "%s ", coap_log_level_desc(level));

va_start(ap, format);
vfprintf(log_fd, format, ap);
Expand Down

0 comments on commit 26302a0

Please sign in to comment.