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.
This commit is contained in:
Andrew Bettison 2012-11-20 13:43:51 +10:30
parent d6336597ee
commit 0e435683f7
10 changed files with 62 additions and 56 deletions

View File

@ -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

1
conf.c
View File

@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <ctype.h>
#include "conf.h"
#include "log.h"
#include "str.h"
/* This predicate function defines the constraints on configuration option names.
Valid:

View File

@ -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 <poll.h>
#include "serval.h"
#include "str.h"
#include "strbuf.h"
#include "strbuf_helpers.h"
#include <poll.h>
#define MAX_WATCHED_FDS 128
struct pollfd fds[MAX_WATCHED_FDS];

43
log.c
View File

@ -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 <andrew@servalproject.com>
*/
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 <andrew@servalproject.com>
*/
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 <andrew@servalproject.com>
*/
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 <andrew@servalproject.com>
*/
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

12
log.h
View File

@ -21,7 +21,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <stdio.h>
#include <stdarg.h>
#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 })

1
net.c
View File

@ -24,6 +24,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <netinet/in.h>
#include <time.h>
#include "net.h"
#include "str.h"
#include "serval.h"
int _set_nonblock(int fd, struct __sourceloc __whence)

View File

@ -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.
*/

43
str.c
View File

@ -19,6 +19,7 @@
#define __STR_INLINE
#include "str.h"
#include "strbuf_helpers.h"
#include <stdlib.h>
#include <stdio.h>
@ -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 <andrew@servalproject.com>
*/
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 <andrew@servalproject.com>
*/
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 <andrew@servalproject.com>
*/
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 <andrew@servalproject.com>
*/
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;

8
str.h
View File

@ -21,6 +21,7 @@
#define __STR_H__
#include <string.h>
#include <sys/types.h>
#include <ctype.h>
#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.

View File

@ -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__