Add log_context

This commit is contained in:
Andrew Bettison 2015-06-22 18:07:50 +09:30
parent 8d799840df
commit cf0e1aa8d6
6 changed files with 77 additions and 13 deletions

View File

@ -314,6 +314,8 @@ static void call_alarm(struct sched_ent *alarm, int revents)
alarm->poll.revents = revents;
alarm->function(alarm);
strbuf_reset(&log_context);
if (call_stats.totals)
fd_func_exit(__HERE__, &call_stats);

41
log.c
View File

@ -101,6 +101,11 @@ static time_t _log_file_start_time;
static char _log_file_buf[8192];
static struct strbuf _log_file_strbuf = STRUCT_STRBUF_EMPTY;
/* The log context is a string that can be set as a prefix to all subsequent log messages.
*/
static char _log_context[16];
struct strbuf log_context = STRUCT_STRBUF_INIT_STATIC(_log_context);
#ifdef ANDROID
/* Static variables for sending log output to the Android log.
*
@ -206,6 +211,28 @@ static void _log_prefix_level(_log_iterator *it, int level)
xprintf(it->xpf, "%-6.6s", levelstr);
}
static void _log_prefix_context(_log_iterator *it)
{
if (it->config->show_pid)
xprintf(it->xpf, "[%5u] ", getpid());
if (it->config->show_time) {
if (it->tv.tv_sec == 0) {
xputs("NOTIME______ ", it->xpf);
} else {
char buf[50];
if (strftime(buf, sizeof buf, "%T", &it->tm) == 0)
xputs("EMPTYTIME___ ", it->xpf);
else
xprintf(it->xpf, "%s.%03u ", buf, (unsigned int)it->tv.tv_usec / 1000);
}
}
if (_log_context[0]) {
xputs("[", it->xpf);
xputs(_log_context, it->xpf);
xputs("] ", it->xpf);
}
}
static void _log_prefix(_log_iterator *it, int level)
{
if (it->config == &config_file) {
@ -228,19 +255,7 @@ static void _log_prefix(_log_iterator *it, int level)
}
else
abort();
if (it->config->show_pid)
xprintf(it->xpf, "[%5u] ", getpid());
if (it->config->show_time) {
if (it->tv.tv_sec == 0) {
xputs("NOTIME______ ", it->xpf);
} else {
char buf[50];
if (strftime(buf, sizeof buf, "%T", &it->tm) == 0)
xputs("EMPTYTIME___ ", it->xpf);
else
xprintf(it->xpf, "%s.%03u ", buf, (unsigned int)it->tv.tv_usec / 1000);
}
}
_log_prefix_context(it);
}
static void _log_prefix_whence(_log_iterator *it, struct __sourceloc whence)

4
log.h
View File

@ -54,6 +54,10 @@ void disable_log_stderr();
void logFlush();
void logConfigChanged();
// Logging context string.
struct strbuf;
extern struct strbuf log_context;
// Logging primitives.
void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list);
int logBacktrace(int level, struct __sourceloc whence);

27
log_context.c Normal file
View File

@ -0,0 +1,27 @@
/*
Serval DNA logging
Copyright 2015 Serval Project Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// This compilation unit provides an initialised log_context strbuf for clients that do not provide
// their own.
#include "log.h"
#include "strbuf.h"
static char _log_context[16];
struct strbuf log_context = STRUCT_STRBUF_INIT_STATIC(_log_context);

View File

@ -34,6 +34,7 @@ SERVAL_CLIENT_SOURCES = \
# These objects do not belong in the Serval DNA daemon but are available for
# client applications.
SERVAL_LIB_SOURCES = \
log_context.c \
log_stderr.c
# These source files are imported and do not depend on any local header files.
@ -107,6 +108,7 @@ TEST_SOURCES = \
commandline.c \
main.c \
test_cli.c \
log_context.c \
log_stderr.c \
context1.c

View File

@ -110,6 +110,20 @@ struct strbuf {
*/
#define STRUCT_STRBUF_EMPTY ((struct strbuf){NULL, NULL, NULL})
/* Constant for initialising a struct strbuf to a static backing buffer:
* char buf[n];
* struct strbuf ssb = STRUCT_STRBUF_INIT_STATIC(buf);
* Immediately following this assignment, the following properties hold:
* strbuf_is_empty(&ssb)
* strbuf_len(&ssb) == 0
* strbuf_count(&ssb) == 0
* strbuf_str(&ssb) == buf
* strbuf_size(sb) == n
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
#define STRUCT_STRBUF_INIT_STATIC(B) ((struct strbuf){(B), (B) + sizeof(B) - 1, (B)})
typedef struct strbuf *strbuf;
typedef const struct strbuf *const_strbuf;