From 0e435683f7c31d0198d68e29461f754f00437fe5 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Tue, 20 Nov 2012 13:43:51 +1030 Subject: [PATCH] Move toprint functions from log.h to str.h Move alloca_sockaddr() to strbuf_helpers.h So that stand-alone executables can use them without pulling in the entire logging framework. --- Makefile.in | 4 ++-- conf.c | 1 + fdqueue.c | 3 ++- log.c | 43 +------------------------------------------ log.h | 12 +----------- net.c | 1 + rhizome_fetch.c | 1 + str.c | 43 +++++++++++++++++++++++++++++++++++++++++++ str.h | 8 ++++++++ strbuf_helpers.h | 2 ++ 10 files changed, 62 insertions(+), 56 deletions(-) diff --git a/Makefile.in b/Makefile.in index 1f98880f..fec9a7d1 100755 --- a/Makefile.in +++ b/Makefile.in @@ -72,9 +72,9 @@ directory_service: $(MDPCLIENTOBJS) directory_service.o @echo LINK $@ @$(CC) $(CFLAGS) -Wall -o $@ $(MDPCLIENTOBJS) directory_service.o $(LDFLAGS) -tfw_createfile: tfw_createfile.o str.o +tfw_createfile: tfw_createfile.o str.o strbuf.o strbuf_helpers.o @echo LINK $@ - @$(CC) $(CFLAGS) -Wall -o $@ tfw_createfile.o str.o + @$(CC) $(CFLAGS) -Wall -o $@ tfw_createfile.o str.o strbuf.o strbuf_helpers.o # This does not build on 64 bit elf platforms as NaCL isn't built with -fPIC # DOC 20120615 diff --git a/conf.c b/conf.c index 67be5f7c..285b6ccf 100644 --- a/conf.c +++ b/conf.c @@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include "conf.h" #include "log.h" +#include "str.h" /* This predicate function defines the constraints on configuration option names. Valid: diff --git a/fdqueue.c b/fdqueue.c index c179be8c..276f3b43 100644 --- a/fdqueue.c +++ b/fdqueue.c @@ -17,10 +17,11 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include #include "serval.h" +#include "str.h" #include "strbuf.h" #include "strbuf_helpers.h" -#include #define MAX_WATCHED_FDS 128 struct pollfd fds[MAX_WATCHED_FDS]; diff --git a/log.c b/log.c index 1620300c..9d8508b8 100644 --- a/log.c +++ b/log.c @@ -37,6 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "log.h" #include "net.h" #include "conf.h" +#include "str.h" #include "strbuf.h" #include "strbuf_helpers.h" @@ -338,48 +339,6 @@ debugflags_t debugFlagMask(const char *flagname) return 0; } -/* Format a buffer of data as a printable representation, eg: "Abc\x0b\n\0", for display - in log messages. - @author Andrew Bettison - */ -char *toprint(char *dstStr, ssize_t dstBufSiz, const char *srcBuf, size_t srcBytes, const char quotes[2]) -{ - strbuf b = strbuf_local(dstStr, dstBufSiz); - strbuf_toprint_quoted_len(b, quotes, srcBuf, srcBytes); - return dstStr; -} - -/* Compute the length of the string produced by toprint(). If dstStrLen == -1 then returns the - exact number of characters in the printable representation (excluding the terminating nul), - otherwise returns dstStrLen. - @author Andrew Bettison - */ -size_t toprint_len(const char *srcBuf, size_t srcBytes, const char quotes[2]) -{ - return strbuf_count(strbuf_toprint_quoted_len(strbuf_local(NULL, 0), quotes, srcBuf, srcBytes)); -} - -/* Format a null-terminated string as a printable representation, eg: "Abc\x0b\n", for display - in log messages. - @author Andrew Bettison - */ -char *toprint_str(char *dstStr, ssize_t dstBufSiz, const char *srcStr, const char quotes[2]) -{ - strbuf b = strbuf_local(dstStr, dstBufSiz); - strbuf_toprint_quoted(b, quotes, srcStr); - return dstStr; -} - -/* Compute the length of the string produced by toprint_str(). If dstStrLen == -1 then returns the - exact number of characters in the printable representation (excluding the terminating nul), - otherwise returns dstStrLen. - @author Andrew Bettison - */ -size_t toprint_str_len(const char *srcStr, const char quotes[2]) -{ - return strbuf_count(strbuf_toprint_quoted(strbuf_local(NULL, 0), quotes, srcStr)); -} - /* Read the symbolic link into the supplied buffer and add a terminating nul. Return -1 if the * buffer is too short to hold the link content and the nul. If readlink(2) returns an error, then * logs it and returns -1. Otherwise, returns the number of bytes read, including the terminating diff --git a/log.h b/log.h index 245c50dd..86a54d5a 100644 --- a/log.h +++ b/log.h @@ -21,7 +21,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include -#include "strbuf_helpers.h" typedef unsigned int debugflags_t; @@ -64,8 +63,6 @@ extern debugflags_t debug; #define LOG_LEVEL_ERROR (3) #define LOG_LEVEL_FATAL (4) -struct strbuf; - /* * Every log message identifies the location in the source code at which the * message was produced. This location is represented by a struct __sourceloc, @@ -127,18 +124,11 @@ void logMessage(int level, struct __sourceloc whence, const char *fmt, ...); void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list); debugflags_t debugFlagMask(const char *flagname); int logDump(int level, struct __sourceloc whence, char *name, const unsigned char *addr, size_t len); -char *toprint(char *dstStr, ssize_t dstBufSiz, const char *srcBuf, size_t srcBytes, const char quotes[2]); -char *toprint_str(char *dstStr, ssize_t dstBufSiz, const char *srcStr, const char quotes[2]); -size_t toprint_len(const char *srcBuf, size_t srcBytes, const char quotes[2]); -size_t toprint_str_len(const char *srcStr, const char quotes[2]); ssize_t get_self_executable_path(char *buf, size_t len); int log_backtrace(struct __sourceloc whence); +struct strbuf; void set_log_implementation(void (*log_function)(int level, struct strbuf *buf)); -#define alloca_toprint(dstlen,buf,len) toprint((char *)alloca((dstlen) == -1 ? toprint_len((const char *)(buf),(len), "``") + 1 : (dstlen)), (dstlen), (const char *)(buf), (len), "``") -#define alloca_str_toprint(str) toprint_str((char *)alloca(toprint_str_len(str, "``") + 1), -1, (str), "``") -#define alloca_sockaddr(addr) strbuf_str(strbuf_append_sockaddr(strbuf_alloca(40), (const struct sockaddr *)(addr))) - #define __HERE__ ((struct __sourceloc){ .file = __FILE__, .line = __LINE__, .function = __FUNCTION__ }) #define __NOWHERE__ ((struct __sourceloc){ .file = NULL, .line = 0, .function = NULL }) diff --git a/net.c b/net.c index 8b3d8eee..4b379e4c 100644 --- a/net.c +++ b/net.c @@ -24,6 +24,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include "net.h" +#include "str.h" #include "serval.h" int _set_nonblock(int fd, struct __sourceloc __whence) diff --git a/rhizome_fetch.c b/rhizome_fetch.c index cd266a39..374fd645 100644 --- a/rhizome_fetch.c +++ b/rhizome_fetch.c @@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "serval.h" #include "rhizome.h" #include "str.h" +#include "strbuf_helpers.h" /* Represents a queued fetch of a bundle payload, for which the manifest is already known. */ diff --git a/str.c b/str.c index 74c41b79..fd31caa6 100644 --- a/str.c +++ b/str.c @@ -19,6 +19,7 @@ #define __STR_INLINE #include "str.h" +#include "strbuf_helpers.h" #include #include @@ -164,6 +165,48 @@ int str_to_ll_scaled(const char *str, int base, long long *result, char **afterp return 1; } +/* Format a buffer of data as a printable representation, eg: "Abc\x0b\n\0", for display + in log messages. + @author Andrew Bettison + */ +char *toprint(char *dstStr, ssize_t dstBufSiz, const char *srcBuf, size_t srcBytes, const char quotes[2]) +{ + strbuf b = strbuf_local(dstStr, dstBufSiz); + strbuf_toprint_quoted_len(b, quotes, srcBuf, srcBytes); + return dstStr; +} + +/* Compute the length of the string produced by toprint(). If dstStrLen == -1 then returns the + exact number of characters in the printable representation (excluding the terminating nul), + otherwise returns dstStrLen. + @author Andrew Bettison + */ +size_t toprint_len(const char *srcBuf, size_t srcBytes, const char quotes[2]) +{ + return strbuf_count(strbuf_toprint_quoted_len(strbuf_local(NULL, 0), quotes, srcBuf, srcBytes)); +} + +/* Format a null-terminated string as a printable representation, eg: "Abc\x0b\n", for display + in log messages. + @author Andrew Bettison + */ +char *toprint_str(char *dstStr, ssize_t dstBufSiz, const char *srcStr, const char quotes[2]) +{ + strbuf b = strbuf_local(dstStr, dstBufSiz); + strbuf_toprint_quoted(b, quotes, srcStr); + return dstStr; +} + +/* Compute the length of the string produced by toprint_str(). If dstStrLen == -1 then returns the + exact number of characters in the printable representation (excluding the terminating nul), + otherwise returns dstStrLen. + @author Andrew Bettison + */ +size_t toprint_str_len(const char *srcStr, const char quotes[2]) +{ + return strbuf_count(strbuf_toprint_quoted(strbuf_local(NULL, 0), quotes, srcStr)); +} + size_t str_fromprint(unsigned char *dst, const char *src) { unsigned char *const odst = dst; diff --git a/str.h b/str.h index 35f0667d..cc8dfa41 100644 --- a/str.h +++ b/str.h @@ -21,6 +21,7 @@ #define __STR_H__ #include +#include #include #ifndef __STR_INLINE @@ -72,8 +73,15 @@ __STR_INLINE int hexvalue(char c) return -1; } +char *toprint(char *dstStr, ssize_t dstBufSiz, const char *srcBuf, size_t srcBytes, const char quotes[2]); +char *toprint_str(char *dstStr, ssize_t dstBufSiz, const char *srcStr, const char quotes[2]); +size_t toprint_len(const char *srcBuf, size_t srcBytes, const char quotes[2]); +size_t toprint_str_len(const char *srcStr, const char quotes[2]); size_t str_fromprint(unsigned char *dst, const char *src); +#define alloca_toprint(dstlen,buf,len) toprint((char *)alloca((dstlen) == -1 ? toprint_len((const char *)(buf),(len), "``") + 1 : (dstlen)), (dstlen), (const char *)(buf), (len), "``") +#define alloca_str_toprint(str) toprint_str((char *)alloca(toprint_str_len(str, "``") + 1), -1, (str), "``") + /* Check if a given string starts with a given sub-string. If so, return 1 and, if afterp is not * NULL, set *afterp to point to the character immediately following the substring. Otherwise * return 0. diff --git a/strbuf_helpers.h b/strbuf_helpers.h index 2b529c67..5fc49e81 100644 --- a/strbuf_helpers.h +++ b/strbuf_helpers.h @@ -86,4 +86,6 @@ strbuf strbuf_append_exit_status(strbuf sb, int status); struct sockaddr; strbuf strbuf_append_sockaddr(strbuf sb, const struct sockaddr *); +#define alloca_sockaddr(addr) strbuf_str(strbuf_append_sockaddr(strbuf_alloca(40), (const struct sockaddr *)(addr))) + #endif //__STRBUF_HELPERS_H__