Improve "log.h", make logMessage() inline

Make logMessage() -- wrapper around vlogMessage() -- an inline
function and remove redundant conditional level != LOG_LEVEL_SILENT

Move logString() into log_util.c, refactor so outer loop now iterates
over lines in buffer, rather than over all log outputs

Rename log_backtrace() to logBacktrace() for consistency

Document __NOWHENCE__ value
This commit is contained in:
Andrew Bettison 2014-05-05 18:17:51 +09:30
parent 18c89b4b98
commit cd9f35f1c8
3 changed files with 68 additions and 74 deletions

41
log.c
View File

@ -36,6 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <dirent.h>
#include <assert.h>
#define __SERVAL_LOG_INLINE
#include "log.h"
#include "net.h"
#include "os.h"
@ -659,44 +660,6 @@ void logFlush()
_log_flush(&it);
}
void logString(int level, struct __sourceloc whence, const char *str)
{
if (level != LOG_LEVEL_SILENT) {
_log_iterator it;
_log_iterator_start(&it);
_rotate_log_file(&it);
const char *s = str;
const char *p;
for (p = str; *p; ++p) {
if (*p == '\n') {
_log_iterator_rewind(&it);
while (_log_iterator_next(&it, level)) {
_log_prefix_whence(&it, whence);
xprintf(it.xpf, "%.*s", (int)(p - s), s);
}
s = p + 1;
}
}
if (p > s) {
_log_iterator_rewind(&it);
while (_log_iterator_next(&it, level)) {
_log_prefix_whence(&it, whence);
xprintf(it.xpf, "%.*s", (int)(p - s), s);
}
}
}
}
void logMessage(int level, struct __sourceloc whence, const char *fmt, ...)
{
if (level != LOG_LEVEL_SILENT) {
va_list ap;
va_start(ap, fmt);
vlogMessage(level, whence, fmt, ap);
va_end(ap);
}
}
void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list ap)
{
if (level != LOG_LEVEL_SILENT) {
@ -722,7 +685,7 @@ void logConfigChanged()
logFlush();
}
int log_backtrace(int level, struct __sourceloc whence)
int logBacktrace(int level, struct __sourceloc whence)
{
#ifndef NO_BACKTRACE
_log_iterator it;

84
log.h
View File

@ -25,19 +25,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <sys/types.h>
#include <errno.h>
#define LOG_LEVEL_INVALID (-1)
#define LOG_LEVEL_SILENT (0)
#define LOG_LEVEL_DEBUG (1)
#define LOG_LEVEL_INFO (2)
#define LOG_LEVEL_HINT (3)
#define LOG_LEVEL_WARN (4)
#define LOG_LEVEL_ERROR (5)
#define LOG_LEVEL_FATAL (6)
#define LOG_LEVEL_NONE (127)
const char *log_level_as_string(int level);
int string_to_log_level(const char *text);
/*
* Every log message identifies the location in the source code at which the
* message was produced. This location is represented by a struct __sourceloc,
@ -47,7 +34,9 @@ int string_to_log_level(const char *text);
* the cpp(1) built-in macros __FILE__, __LINE__ and __FUNCTION__ to generate
* its elements. The __NOWHERE__ macro creates a struct __sourceloc with NULL
* and zero fields. If you pass __NOWHERE__ to logMessage(), it will omit
* location information from the log line.
* location information from the log line. The __NOWHENCE__ macro creates a
* special source __sourceloc that logging primitives should interpret to
* suppress the output of the usual source-code location information.
*
* Sometimes, a function wants to log a message as though its caller were the
* origin of the message. This is typical of "primitive" type functions that
@ -90,30 +79,52 @@ struct __sourceloc {
extern const struct __sourceloc __whence; // see above
extern int serverMode;
int create_log_file_directory();
void close_log_file();
void disable_log_stderr();
void logFlush();
void logArgv(int level, struct __sourceloc whence, const char *label, int argc, const char *const *argv);
void logString(int level, struct __sourceloc whence, const char *str);
void logMessage(int level, struct __sourceloc whence, const char *fmt, ...)
__attribute__ (( format(printf,3,4) ));
void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list);
void logConfigChanged();
int logDump(int level, struct __sourceloc whence, char *name, const unsigned char *addr, size_t len);
int log_backtrace(int level, struct __sourceloc whence);
struct strbuf;
#define __HERE__ ((struct __sourceloc){ .file = __FILE__, .line = __LINE__, .function = __FUNCTION__ })
#define __NOWHERE__ ((struct __sourceloc){ .file = NULL, .line = 0, .function = NULL })
#define __NOWHENCE__ ((struct __sourceloc){ .file = "", .line = 0, .function = NULL })
#define __WHENCE__ (__whence.file ? __whence : __HERE__)
#ifndef __SERVAL_LOG_INLINE
# if __GNUC__ && !__GNUC_STDC_INLINE__
# define __SERVAL_LOG_INLINE extern inline
# else
# define __SERVAL_LOG_INLINE inline
# endif
#endif
// Logging levels.
#define LOG_LEVEL_INVALID (-1)
#define LOG_LEVEL_SILENT (0)
#define LOG_LEVEL_DEBUG (1)
#define LOG_LEVEL_INFO (2)
#define LOG_LEVEL_HINT (3)
#define LOG_LEVEL_WARN (4)
#define LOG_LEVEL_ERROR (5)
#define LOG_LEVEL_FATAL (6)
#define LOG_LEVEL_NONE (127)
const char *log_level_as_string(int level);
int string_to_log_level(const char *text);
// Log output control.
extern int serverMode;
void close_log_file();
void disable_log_stderr();
void logFlush();
void logConfigChanged();
// Logging primitives.
void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list);
int logBacktrace(int level, struct __sourceloc whence);
__SERVAL_LOG_INLINE void logMessage(int level, struct __sourceloc whence, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vlogMessage(level, whence, fmt, ap);
va_end(ap);
}
// Useful logging primitive macros.
#define LOGF(L,F,...) logMessage(L, __WHENCE__, F, ##__VA_ARGS__)
#define LOGF_perror(L,F,...) logMessage_perror(L, __WHENCE__, F, ##__VA_ARGS__)
#define LOG_perror(L,X) LOGF_perror(L, "%s", (X))
@ -157,6 +168,11 @@ struct strbuf;
#define dump(X,A,N) logDump(LOG_LEVEL_DEBUG, __WHENCE__, (X), (const unsigned char *)(A), (size_t)(N))
#define BACKTRACE log_backtrace(LOG_LEVEL_FATAL, __WHENCE__)
#define BACKTRACE logBacktrace(LOG_LEVEL_FATAL, __WHENCE__)
// Utility functions, defined in terms of above primitives.
void logArgv(int level, struct __sourceloc whence, const char *label, int argc, const char *const *argv);
int logDump(int level, struct __sourceloc whence, char *name, const unsigned char *addr, size_t len);
void logString(int level, struct __sourceloc whence, const char *str);
#endif // __SERVAL_DNA__LOG_H

View File

@ -54,7 +54,6 @@ void logArgv(int level, struct __sourceloc whence, const char *label, int argc,
size_t len = strbuf_count(&b);
strbuf_init(&b, alloca(len + 1), len + 1);
strbuf_append_argv(&b, argc, argv);
if (label)
logMessage(level, whence, "%s %s", label, strbuf_str(&b));
else
@ -62,6 +61,22 @@ void logArgv(int level, struct __sourceloc whence, const char *label, int argc,
}
}
void logString(int level, struct __sourceloc whence, const char *str)
{
if (level != LOG_LEVEL_SILENT) {
const char *s = str;
const char *p;
for (p = str; *p; ++p) {
if (*p == '\n') {
logMessage(level, whence, "%.*s", (int)(p - s), s);
s = p + 1;
}
}
if (p > s)
logMessage(level, whence, "%.*s", (int)(p - s), s);
}
}
const char *log_level_as_string(int level)
{
switch (level) {