2012-05-14 09:02:10 +00:00
|
|
|
/*
|
2012-07-30 03:28:21 +00:00
|
|
|
Serval logging.
|
|
|
|
Copyright (C) 2012 Serval Project Inc.
|
2012-05-14 09:02:10 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-07-25 07:32:15 +00:00
|
|
|
#include <stdlib.h>
|
2012-07-19 08:29:45 +00:00
|
|
|
#include <stdio.h>
|
2012-05-20 14:38:31 +00:00
|
|
|
#include <ctype.h>
|
2012-07-11 04:48:36 +00:00
|
|
|
#include <sys/time.h>
|
2012-07-25 07:32:15 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2012-09-05 09:23:22 +00:00
|
|
|
#include <sys/param.h>
|
2012-07-11 04:48:36 +00:00
|
|
|
#include <time.h>
|
2012-07-25 08:49:32 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#endif
|
2012-07-29 03:05:53 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2012-09-06 05:56:40 +00:00
|
|
|
#include <stdint.h>
|
2012-11-14 01:54:29 +00:00
|
|
|
#include <assert.h>
|
2012-05-14 09:02:10 +00:00
|
|
|
|
2012-08-06 07:30:33 +00:00
|
|
|
#include "log.h"
|
2012-08-23 03:01:07 +00:00
|
|
|
#include "net.h"
|
2013-02-25 04:55:53 +00:00
|
|
|
#include "os.h"
|
2012-08-06 07:30:33 +00:00
|
|
|
#include "conf.h"
|
2012-11-20 03:13:51 +00:00
|
|
|
#include "str.h"
|
2012-08-06 07:30:33 +00:00
|
|
|
#include "strbuf.h"
|
|
|
|
#include "strbuf_helpers.h"
|
2013-03-26 07:54:37 +00:00
|
|
|
#include "xprintf.h"
|
2012-08-06 07:30:33 +00:00
|
|
|
|
2013-03-18 05:04:33 +00:00
|
|
|
int serverMode = 0;
|
2012-10-16 06:16:52 +00:00
|
|
|
const struct __sourceloc __whence = __NOWHERE__;
|
|
|
|
|
2013-03-26 07:54:37 +00:00
|
|
|
#define NO_FILE ((FILE *)1)
|
2012-07-05 05:29:50 +00:00
|
|
|
|
2013-03-26 07:54:37 +00:00
|
|
|
struct _log_state {
|
|
|
|
|
|
|
|
/* This structure is initially zerofilled. */
|
|
|
|
|
|
|
|
/* Whether the software version has been logged in the current file yet.
|
|
|
|
*/
|
|
|
|
bool_t version_logged;
|
|
|
|
|
|
|
|
/* The time stamp of the last logged message, used to detect when the date advances so that
|
|
|
|
* the date can be logged.
|
|
|
|
*/
|
|
|
|
struct tm last_tm;
|
|
|
|
|
2013-03-28 13:52:08 +00:00
|
|
|
/* Whether the current configuration has been logged in the current file yet.
|
|
|
|
*/
|
|
|
|
bool_t config_logged;
|
|
|
|
|
2013-03-26 07:54:37 +00:00
|
|
|
};
|
|
|
|
|
2013-04-02 06:29:55 +00:00
|
|
|
/* Static variables for sending log output to a file.
|
|
|
|
*
|
|
|
|
* The logbuf is used to accumulate log messages before the log file is open and ready for
|
|
|
|
* writing.
|
|
|
|
*/
|
|
|
|
static FILE *logfile_file = NULL;
|
|
|
|
static void _open_log_file();
|
|
|
|
static void _rotate_log_file();
|
|
|
|
static void _flush_log_file();
|
2013-03-26 07:54:37 +00:00
|
|
|
struct _log_state state_file;
|
2013-04-02 06:29:55 +00:00
|
|
|
static char _log_buf[8192];
|
|
|
|
static struct strbuf logbuf = STRUCT_STRBUF_EMPTY;
|
|
|
|
|
2013-03-26 07:54:37 +00:00
|
|
|
#ifdef ANDROID
|
2013-04-02 06:29:55 +00:00
|
|
|
/* Static variables for sending log output to the Android log.
|
|
|
|
*
|
|
|
|
* The logbuf is used to accumulate a single log line before printing to Android's logging API.
|
|
|
|
*/
|
|
|
|
#include <android/log.h>
|
2013-03-26 07:54:37 +00:00
|
|
|
struct _log_state state_android;
|
2013-04-02 06:29:55 +00:00
|
|
|
static char _log_buf_android[1024];
|
|
|
|
static struct strbuf logbuf_android;
|
|
|
|
#endif // ANDROID
|
|
|
|
|
|
|
|
/* Static variables for sending log output to standard error.
|
|
|
|
*/
|
|
|
|
static FILE *logfile_stderr = NULL;
|
2013-03-26 07:54:37 +00:00
|
|
|
struct _log_state state_stderr;
|
2013-04-02 06:29:55 +00:00
|
|
|
static void _open_log_stderr();
|
|
|
|
static void _flush_log_stderr();
|
2013-03-26 07:54:37 +00:00
|
|
|
|
|
|
|
typedef struct _log_iterator {
|
2013-03-28 13:52:08 +00:00
|
|
|
const struct config_log_format *config;
|
|
|
|
struct _log_state *state;
|
2013-03-26 07:54:37 +00:00
|
|
|
struct timeval tv;
|
|
|
|
struct tm tm;
|
|
|
|
XPRINTF xpf;
|
|
|
|
} _log_iterator;
|
|
|
|
|
2013-03-28 13:52:08 +00:00
|
|
|
static void _log_iterator_start(_log_iterator *it)
|
2012-05-14 09:02:10 +00:00
|
|
|
{
|
2013-03-26 07:54:37 +00:00
|
|
|
memset(it, 0, sizeof *it);
|
|
|
|
gettimeofday(&it->tv, NULL);
|
|
|
|
localtime_r(&it->tv.tv_sec, &it->tm);
|
|
|
|
}
|
|
|
|
|
2013-04-02 06:29:55 +00:00
|
|
|
static void _log_iterator_advance_to_file(_log_iterator *it)
|
|
|
|
{
|
|
|
|
it->config = &config.log.file;
|
|
|
|
it->state = &state_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
static void _log_iterator_advance_to_android(_log_iterator *it)
|
|
|
|
{
|
|
|
|
it->config = &config.log.android;
|
|
|
|
it->state = &state_android;
|
|
|
|
}
|
|
|
|
#endif // ANDROID
|
|
|
|
|
|
|
|
static void _log_iterator_advance_to_stderr(_log_iterator *it)
|
|
|
|
{
|
|
|
|
it->config = &config.log.stderr;
|
|
|
|
it->state = &state_stderr;
|
|
|
|
}
|
|
|
|
|
2013-03-28 13:52:08 +00:00
|
|
|
static int _log_iterator_advance(_log_iterator *it)
|
|
|
|
{
|
|
|
|
if (it->config == NULL) {
|
2013-04-02 06:29:55 +00:00
|
|
|
_log_iterator_advance_to_file(it);
|
2013-03-28 13:52:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (it->config == &config.log.file) {
|
|
|
|
#ifdef ANDROID
|
2013-04-02 06:29:55 +00:00
|
|
|
_log_iterator_advance_to_android(it);
|
2013-03-28 13:52:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (it->config == &config.log.android) {
|
|
|
|
#endif // ANDROID
|
2013-04-02 06:29:55 +00:00
|
|
|
_log_iterator_advance_to_stderr(it);
|
2013-03-28 13:52:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _log_enabled(_log_iterator *it)
|
|
|
|
{
|
|
|
|
if (it->config == &config.log.file) {
|
2013-04-02 06:29:55 +00:00
|
|
|
_open_log_file(); // puts initial INFO message(s) at head of log file
|
2013-03-28 13:52:08 +00:00
|
|
|
if (logfile_file == NO_FILE)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (it->config == &config.log.stderr) {
|
|
|
|
_open_log_stderr();
|
|
|
|
if (logfile_stderr == NULL || logfile_stderr == NO_FILE)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _log_prefix_level(_log_iterator *it, int level)
|
2013-03-26 07:54:37 +00:00
|
|
|
{
|
|
|
|
const char *levelstr = "UNKWN:";
|
|
|
|
switch (level) {
|
|
|
|
case LOG_LEVEL_FATAL: levelstr = "FATAL:"; break;
|
|
|
|
case LOG_LEVEL_ERROR: levelstr = "ERROR:"; break;
|
|
|
|
case LOG_LEVEL_WARN: levelstr = "WARN:"; break;
|
|
|
|
case LOG_LEVEL_INFO: levelstr = "INFO:"; break;
|
|
|
|
case LOG_LEVEL_DEBUG: levelstr = "DEBUG:"; break;
|
|
|
|
}
|
|
|
|
xprintf(it->xpf, "%-6.6s", levelstr);
|
|
|
|
}
|
|
|
|
|
2013-03-28 13:52:08 +00:00
|
|
|
static void _log_prefix(_log_iterator *it, int level)
|
2013-03-26 07:54:37 +00:00
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
if (it->config == &config.log.file) {
|
2013-03-12 08:48:30 +00:00
|
|
|
if (strbuf_is_empty(&logbuf))
|
|
|
|
strbuf_init(&logbuf, _log_buf, sizeof _log_buf);
|
|
|
|
else if (strbuf_len(&logbuf))
|
|
|
|
strbuf_putc(&logbuf, '\n');
|
2013-03-26 07:54:37 +00:00
|
|
|
it->xpf = XPRINTF_STRBUF(&logbuf);
|
2013-03-28 13:52:08 +00:00
|
|
|
_log_prefix_level(it, level);
|
2013-03-26 07:54:37 +00:00
|
|
|
}
|
|
|
|
#ifdef ANDROID
|
2013-03-28 13:52:08 +00:00
|
|
|
else if (it->config == &config.log.android) {
|
2013-03-26 07:54:37 +00:00
|
|
|
strbuf_init(&logbuf_android, _log_buf_android, sizeof _log_buf_android);
|
|
|
|
it->xpf = XPRINTF_STRBUF(&logbuf_android);
|
|
|
|
}
|
|
|
|
#endif // ANDROID
|
2013-03-28 13:52:08 +00:00
|
|
|
else if (it->config == &config.log.stderr) {
|
2013-03-26 07:54:37 +00:00
|
|
|
it->xpf = XPRINTF_STDIO(logfile_stderr);
|
2013-03-28 13:52:08 +00:00
|
|
|
_log_prefix_level(it, level);
|
2012-07-27 08:20:40 +00:00
|
|
|
}
|
2013-03-26 07:54:37 +00:00
|
|
|
else
|
|
|
|
abort();
|
2013-03-28 13:52:08 +00:00
|
|
|
if (it->config->show_pid)
|
2013-03-26 07:54:37 +00:00
|
|
|
xprintf(it->xpf, "[%5u] ", getpid());
|
2013-03-28 13:52:08 +00:00
|
|
|
if (it->config->show_time) {
|
2013-03-26 07:54:37 +00:00
|
|
|
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, it->tv.tv_usec / 1000);
|
|
|
|
}
|
2012-07-27 08:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 06:29:55 +00:00
|
|
|
static const char *_trimbuildpath(const char *path)
|
|
|
|
{
|
|
|
|
/* Remove common path prefix */
|
|
|
|
int lastsep = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; __FILE__[i] && path[i]; ++i) {
|
|
|
|
if (i && path[i - 1] == '/')
|
|
|
|
lastsep = i;
|
|
|
|
if (__FILE__[i] != path[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return &path[lastsep];
|
|
|
|
}
|
|
|
|
|
2013-03-28 13:52:08 +00:00
|
|
|
static void _log_prefix_whence(_log_iterator *it, struct __sourceloc whence)
|
2012-08-23 03:01:07 +00:00
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
if (whence.file) {
|
|
|
|
xprintf(it->xpf, "%s", _trimbuildpath(whence.file));
|
|
|
|
if (whence.line)
|
|
|
|
xprintf(it->xpf, ":%u", whence.line);
|
|
|
|
if (whence.function)
|
|
|
|
xprintf(it->xpf, ":%s()", whence.function);
|
|
|
|
xputs(" ", it->xpf);
|
|
|
|
} else if (whence.function) {
|
|
|
|
xprintf(it->xpf, "%s() ", whence.function);
|
2013-03-26 07:54:37 +00:00
|
|
|
}
|
2013-03-28 13:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _log_end_line(_log_iterator *it, int level)
|
|
|
|
{
|
2012-05-14 09:02:10 +00:00
|
|
|
#ifdef ANDROID
|
2013-03-28 13:52:08 +00:00
|
|
|
if (it->config == &config.log.android) {
|
2013-03-26 07:54:37 +00:00
|
|
|
int alevel = ANDROID_LOG_UNKNOWN;
|
2013-03-28 13:52:08 +00:00
|
|
|
switch (level) {
|
2013-03-26 07:54:37 +00:00
|
|
|
case LOG_LEVEL_FATAL: alevel = ANDROID_LOG_FATAL; break;
|
|
|
|
case LOG_LEVEL_ERROR: alevel = ANDROID_LOG_ERROR; break;
|
|
|
|
case LOG_LEVEL_INFO: alevel = ANDROID_LOG_INFO; break;
|
|
|
|
case LOG_LEVEL_WARN: alevel = ANDROID_LOG_WARN; break;
|
|
|
|
case LOG_LEVEL_DEBUG: alevel = ANDROID_LOG_DEBUG; break;
|
|
|
|
default: abort();
|
|
|
|
}
|
|
|
|
__android_log_print(alevel, "servald", "%s", strbuf_str(_log_buf_android));
|
2012-07-27 08:20:40 +00:00
|
|
|
}
|
2013-03-28 13:52:08 +00:00
|
|
|
else
|
2013-03-26 07:54:37 +00:00
|
|
|
#endif // ANDROID
|
2013-03-28 13:52:08 +00:00
|
|
|
if (it->config == &config.log.stderr) {
|
2013-03-26 07:54:37 +00:00
|
|
|
fputc('\n', logfile_stderr);
|
2012-07-27 08:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 13:52:08 +00:00
|
|
|
static void _log_flush(_log_iterator *it)
|
2012-08-23 03:01:07 +00:00
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
if (it->config == &config.log.file) {
|
|
|
|
_flush_log_file();
|
2013-03-26 07:54:37 +00:00
|
|
|
}
|
2013-03-28 13:52:08 +00:00
|
|
|
else if (it->config == &config.log.stderr) {
|
|
|
|
_flush_log_stderr();
|
2013-03-26 07:54:37 +00:00
|
|
|
}
|
2013-03-28 13:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _log_vprintf_nl(_log_iterator *it, int level, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
_log_prefix(it, level);
|
|
|
|
vxprintf(it->xpf, fmt, ap);
|
|
|
|
_log_end_line(it, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _log_printf_nl(_log_iterator *it, int level, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
_log_vprintf_nl(it, level, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2013-04-02 06:29:55 +00:00
|
|
|
static void _log_current_datetime(_log_iterator *it, int level)
|
|
|
|
{
|
|
|
|
char buf[50];
|
|
|
|
if (strftime(buf, sizeof buf, "%F %T %z", &it->tm)) {
|
|
|
|
_log_printf_nl(it, level, "Local date/time: %s", buf);
|
|
|
|
it->state->last_tm = it->tm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _log_software_version(_log_iterator *it, int level)
|
|
|
|
{
|
|
|
|
_log_printf_nl(it, level, "Serval DNA version: %s", version_servald);
|
|
|
|
it->state->version_logged = 1;
|
|
|
|
}
|
|
|
|
|
2013-03-28 13:52:08 +00:00
|
|
|
static int _log_current_config(_log_iterator *it, int level)
|
|
|
|
{
|
|
|
|
if (!cf_limbo) {
|
|
|
|
struct cf_om_node *root = NULL;
|
|
|
|
int ret = cf_fmt_config_main(&root, &config);
|
|
|
|
if (ret == CFERROR) {
|
|
|
|
_log_printf_nl(it, level, "Cannot dump current configuration: cf_fmt_config_main() returned CFERROR");
|
|
|
|
} else {
|
|
|
|
_log_printf_nl(it, level, "Current configuration:");
|
|
|
|
struct cf_om_iterator oit;
|
|
|
|
int empty = 1;
|
|
|
|
for (cf_om_iter_start(&oit, root); oit.node; cf_om_iter_next(&oit)) {
|
|
|
|
if (oit.node->text && oit.node->line_number) {
|
|
|
|
empty = 0;
|
|
|
|
_log_printf_nl(it, level, " %s=%s", oit.node->fullkey, oit.node->text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty)
|
|
|
|
_log_printf_nl(it, level, " (empty)");
|
|
|
|
}
|
|
|
|
cf_om_free_node(&root);
|
|
|
|
it->state->config_logged = 1;
|
2013-03-26 07:54:37 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2012-08-08 01:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 13:52:08 +00:00
|
|
|
static void _log_update(_log_iterator *it)
|
2012-08-23 03:01:07 +00:00
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
if ( it->tm.tm_mday != it->state->last_tm.tm_mday
|
|
|
|
|| it->tm.tm_mon != it->state->last_tm.tm_mon
|
|
|
|
|| it->tm.tm_year != it->state->last_tm.tm_year
|
2013-04-02 06:29:55 +00:00
|
|
|
)
|
|
|
|
_log_current_datetime(it, LOG_LEVEL_INFO);
|
|
|
|
if (!it->state->version_logged)
|
|
|
|
_log_software_version(it, LOG_LEVEL_INFO);
|
|
|
|
if (it->config->dump_config && !it->state->config_logged)
|
2013-03-28 13:52:08 +00:00
|
|
|
_log_current_config(it, LOG_LEVEL_INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _log_iterator_next(_log_iterator *it, int level)
|
|
|
|
{
|
|
|
|
assert(level >= LOG_LEVEL_SILENT);
|
|
|
|
assert(level <= LOG_LEVEL_FATAL);
|
|
|
|
_log_end_line(it, level);
|
|
|
|
_log_flush(it);
|
|
|
|
while (_log_iterator_advance(it)) {
|
|
|
|
if (_log_enabled(it)) {
|
|
|
|
if (level >= it->config->level) {
|
2013-04-02 06:29:55 +00:00
|
|
|
_log_update(it);
|
2013-03-28 13:52:08 +00:00
|
|
|
_log_prefix(it, level);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
_log_flush(it);
|
2013-03-26 07:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2012-08-08 01:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 06:29:55 +00:00
|
|
|
static void _logs_vprintf_nl(int level, struct __sourceloc whence, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
_log_iterator it;
|
|
|
|
_log_iterator_start(&it);
|
|
|
|
while (_log_iterator_next(&it, level)) {
|
|
|
|
_log_prefix_whence(&it, whence);
|
|
|
|
_log_vprintf_nl(&it, level, fmt, ap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _logs_printf_nl(int level, struct __sourceloc whence, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
_logs_vprintf_nl(level, whence, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _open_log_file()
|
|
|
|
{
|
|
|
|
if (!logfile_file) {
|
|
|
|
const char *logpath = getenv("SERVALD_LOG_FILE");
|
|
|
|
if (!logpath) {
|
|
|
|
if (cf_limbo)
|
|
|
|
return;
|
|
|
|
logpath = config.log.file_path;
|
|
|
|
}
|
|
|
|
if (!logpath || !logpath[0]) {
|
|
|
|
logfile_file = NO_FILE;
|
|
|
|
if (serverMode)
|
|
|
|
_logs_printf_nl(LOG_LEVEL_INFO, __NOWHERE__, "No logfile_file configured");
|
|
|
|
} else {
|
|
|
|
char path[1024];
|
|
|
|
if (!FORM_SERVAL_INSTANCE_PATH(path, logpath)) {
|
|
|
|
logfile_file = NO_FILE;
|
|
|
|
_logs_printf_nl(LOG_LEVEL_WARN, __NOWHERE__, "Logfile path overrun");
|
|
|
|
} else if ((logfile_file = fopen(path, "a"))) {
|
|
|
|
setlinebuf(logfile_file);
|
|
|
|
memset(&state_file, 0, sizeof state_file);
|
|
|
|
// The first line in every log file must be the starting time stamp. (After that, it is up
|
|
|
|
// to _log_update() to insert other mandatory messages in any suitable order.)
|
|
|
|
_log_iterator it;
|
|
|
|
_log_iterator_start(&it);
|
|
|
|
_log_iterator_advance_to_file(&it);
|
|
|
|
_log_current_datetime(&it, LOG_LEVEL_INFO);
|
|
|
|
if (serverMode)
|
|
|
|
_logs_printf_nl(LOG_LEVEL_INFO, __NOWHERE__, "Logging to %s (fd %d)", path, fileno(logfile_file));
|
|
|
|
} else {
|
|
|
|
logfile_file = NO_FILE;
|
|
|
|
_logs_printf_nl(LOG_LEVEL_WARN, __NOWHERE__, "Cannot append to %s - %s [errno=%d]", path, strerror(errno), errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _rotate_log_file()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _flush_log_file()
|
|
|
|
{
|
|
|
|
if (logfile_file && logfile_file != NO_FILE) {
|
|
|
|
fprintf(logfile_file, "%s%s%s",
|
|
|
|
strbuf_len(&logbuf) ? strbuf_str(&logbuf) : "",
|
|
|
|
strbuf_len(&logbuf) ? "\n" : "",
|
|
|
|
strbuf_overrun(&logbuf) ? "LOG OVERRUN\n" : ""
|
|
|
|
);
|
|
|
|
strbuf_reset(&logbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_log_file()
|
|
|
|
{
|
|
|
|
if (logfile_file && logfile_file != NO_FILE)
|
|
|
|
fclose(logfile_file);
|
|
|
|
logfile_file = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _open_log_stderr()
|
|
|
|
{
|
|
|
|
if (!logfile_stderr) {
|
|
|
|
logfile_stderr = stderr;
|
|
|
|
setlinebuf(logfile_stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _flush_log_stderr()
|
|
|
|
{
|
|
|
|
if (logfile_stderr && logfile_stderr != NO_FILE)
|
|
|
|
fflush(logfile_stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable_log_stderr()
|
|
|
|
{
|
|
|
|
if (logfile_stderr && logfile_stderr != NO_FILE) {
|
|
|
|
fflush(logfile_stderr);
|
|
|
|
logfile_stderr = NO_FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-14 01:09:08 +00:00
|
|
|
void logFlush()
|
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
_log_iterator it;
|
|
|
|
_log_iterator_start(&it);
|
|
|
|
while (_log_iterator_next(&it, LOG_LEVEL_SILENT))
|
|
|
|
;
|
2012-12-14 01:09:08 +00:00
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
void logArgv(int level, struct __sourceloc whence, const char *label, int argc, const char *const *argv)
|
2012-07-27 08:20:40 +00:00
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
if (level != LOG_LEVEL_SILENT) {
|
|
|
|
struct strbuf b;
|
|
|
|
strbuf_init(&b, NULL, 0);
|
|
|
|
strbuf_append_argv(&b, argc, argv);
|
|
|
|
size_t len = strbuf_count(&b);
|
|
|
|
strbuf_init(&b, alloca(len + 1), len + 1);
|
|
|
|
strbuf_append_argv(&b, argc, argv);
|
2013-04-02 06:29:55 +00:00
|
|
|
_rotate_log_file();
|
2013-03-28 13:52:08 +00:00
|
|
|
_log_iterator it;
|
|
|
|
_log_iterator_start(&it);
|
|
|
|
while (_log_iterator_next(&it, level)) {
|
|
|
|
_log_prefix_whence(&it, whence);
|
|
|
|
if (label) {
|
|
|
|
xputs(label, it.xpf);
|
|
|
|
xputc(' ', it.xpf);
|
|
|
|
}
|
|
|
|
xputs(strbuf_str(&b), it.xpf);
|
2012-07-27 08:29:33 +00:00
|
|
|
}
|
2012-05-14 09:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
void logString(int level, struct __sourceloc whence, const char *str)
|
2012-08-01 08:22:38 +00:00
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
if (level != LOG_LEVEL_SILENT) {
|
2013-04-02 06:29:55 +00:00
|
|
|
_rotate_log_file();
|
2013-03-28 13:52:08 +00:00
|
|
|
_log_iterator it;
|
|
|
|
const char *s = str;
|
|
|
|
const char *p;
|
|
|
|
for (p = str; *p; ++p) {
|
|
|
|
if (*p == '\n') {
|
|
|
|
_log_iterator_start(&it);
|
|
|
|
while (_log_iterator_next(&it, level)) {
|
|
|
|
_log_prefix_whence(&it, whence);
|
|
|
|
xprintf(it.xpf, "%.*s", p - s, s);
|
|
|
|
}
|
|
|
|
s = p + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p > s) {
|
|
|
|
_log_iterator_start(&it);
|
|
|
|
while (_log_iterator_next(&it, level)) {
|
|
|
|
_log_prefix_whence(&it, whence);
|
2013-03-26 07:54:37 +00:00
|
|
|
xprintf(it.xpf, "%.*s", p - s, s);
|
2013-03-28 13:52:08 +00:00
|
|
|
}
|
2012-08-01 08:22:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
void logMessage(int level, struct __sourceloc whence, const char *fmt, ...)
|
2012-05-22 00:24:10 +00:00
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
if (level != LOG_LEVEL_SILENT) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vlogMessage(level, whence, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2012-07-27 08:20:40 +00:00
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list ap)
|
2012-07-27 08:20:40 +00:00
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
if (level != LOG_LEVEL_SILENT) {
|
2013-04-02 06:29:55 +00:00
|
|
|
_rotate_log_file();
|
2013-03-28 13:52:08 +00:00
|
|
|
_log_iterator it;
|
|
|
|
_log_iterator_start(&it);
|
|
|
|
while (_log_iterator_next(&it, level)) {
|
|
|
|
_log_prefix_whence(&it, whence);
|
|
|
|
vxprintf(it.xpf, fmt, ap);
|
|
|
|
}
|
|
|
|
}
|
2013-03-12 08:48:30 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 13:52:08 +00:00
|
|
|
void logConfigChanged()
|
2013-03-12 08:48:30 +00:00
|
|
|
{
|
2013-04-02 06:29:55 +00:00
|
|
|
_log_iterator it;
|
|
|
|
_log_iterator_start(&it);
|
|
|
|
while (_log_iterator_advance(&it))
|
|
|
|
it.state->config_logged = 0;
|
2013-03-28 13:52:08 +00:00
|
|
|
logFlush();
|
2013-03-12 08:48:30 +00:00
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
int logDump(int level, struct __sourceloc whence, char *name, const unsigned char *addr, size_t len)
|
2012-05-14 09:02:10 +00:00
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
if (level != LOG_LEVEL_SILENT) {
|
|
|
|
char buf[100];
|
|
|
|
size_t i;
|
|
|
|
if (name)
|
|
|
|
logMessage(level, whence, "Dump of %s", name);
|
|
|
|
for(i = 0; i < len; i += 16) {
|
|
|
|
strbuf b = strbuf_local(buf, sizeof buf);
|
|
|
|
strbuf_sprintf(b, " %04x :", i);
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < 16 && i + j < len; j++)
|
|
|
|
strbuf_sprintf(b, " %02x", addr[i + j]);
|
|
|
|
for (; j < 16; j++)
|
|
|
|
strbuf_puts(b, " ");
|
|
|
|
strbuf_puts(b, " ");
|
|
|
|
for (j = 0; j < 16 && i + j < len; j++)
|
|
|
|
strbuf_sprintf(b, "%c", addr[i+j] >= ' ' && addr[i+j] < 0x7f ? addr[i+j] : '.');
|
|
|
|
logMessage(level, whence, "%s", strbuf_str(b));
|
|
|
|
}
|
2012-06-22 07:49:14 +00:00
|
|
|
}
|
2012-05-14 09:02:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-25 07:32:15 +00:00
|
|
|
ssize_t get_self_executable_path(char *buf, size_t len)
|
|
|
|
{
|
2012-09-05 22:22:22 +00:00
|
|
|
#if defined(linux)
|
2012-07-25 07:32:15 +00:00
|
|
|
return read_symlink("/proc/self/exe", buf, len);
|
2012-09-05 22:22:22 +00:00
|
|
|
#elif defined (__sun__)
|
|
|
|
return read_symlink("/proc/self/path/a.out", buf, len);
|
|
|
|
#elif defined (__APPLE__)
|
2012-07-25 08:49:32 +00:00
|
|
|
uint32_t bufsize = len;
|
|
|
|
return _NSGetExecutablePath(buf, &bufsize) == -1 && len ? -1 : bufsize;
|
2012-09-05 22:22:22 +00:00
|
|
|
#else
|
|
|
|
#error Unable to find executable path
|
2012-07-25 07:32:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
int log_backtrace(struct __sourceloc whence)
|
2012-07-25 07:32:15 +00:00
|
|
|
{
|
2013-02-14 03:48:56 +00:00
|
|
|
#ifndef NO_BACKTRACE
|
2013-04-02 06:29:55 +00:00
|
|
|
_rotate_log_file();
|
2012-09-05 09:23:22 +00:00
|
|
|
char execpath[MAXPATHLEN];
|
2012-07-25 07:32:15 +00:00
|
|
|
if (get_self_executable_path(execpath, sizeof execpath) == -1)
|
|
|
|
return WHY("cannot log backtrace: own executable path unknown");
|
2012-09-05 09:23:22 +00:00
|
|
|
char tempfile[MAXPATHLEN];
|
2012-10-18 06:50:50 +00:00
|
|
|
if (!FORM_SERVAL_INSTANCE_PATH(tempfile, "servalgdb.XXXXXX"))
|
2012-07-27 10:29:16 +00:00
|
|
|
return -1;
|
2012-09-05 09:23:22 +00:00
|
|
|
int tmpfd = mkstemp(tempfile);
|
2012-07-25 07:32:15 +00:00
|
|
|
if (tmpfd == -1)
|
2012-10-18 06:50:50 +00:00
|
|
|
return WHYF_perror("mkstemp(%s)", alloca_str_toprint(tempfile));
|
2012-07-25 07:32:15 +00:00
|
|
|
if (write_str(tmpfd, "backtrace\n") == -1) {
|
|
|
|
close(tmpfd);
|
|
|
|
unlink(tempfile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (close(tmpfd) == -1) {
|
|
|
|
WHY_perror("close");
|
|
|
|
unlink(tempfile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char pidstr[12];
|
2012-09-05 09:23:22 +00:00
|
|
|
snprintf(pidstr, sizeof pidstr, "%jd", (intmax_t)getpid());
|
2012-07-25 07:32:15 +00:00
|
|
|
int stdout_fds[2];
|
|
|
|
if (pipe(stdout_fds) == -1)
|
|
|
|
return WHY_perror("pipe");
|
|
|
|
pid_t child_pid;
|
|
|
|
switch (child_pid = fork()) {
|
|
|
|
case -1: // error
|
|
|
|
WHY_perror("fork");
|
|
|
|
close(stdout_fds[0]);
|
|
|
|
close(stdout_fds[1]);
|
|
|
|
return WHY("cannot log backtrace: fork failed");
|
|
|
|
case 0: // child
|
2012-07-26 07:21:59 +00:00
|
|
|
if (dup2(stdout_fds[1], 1) == -1 || dup2(stdout_fds[1], 2) == -1) {
|
|
|
|
perror("dup2");
|
2012-07-25 07:32:15 +00:00
|
|
|
_exit(-1);
|
2012-07-26 07:21:59 +00:00
|
|
|
}
|
2012-07-25 07:32:15 +00:00
|
|
|
close(0);
|
2012-07-26 07:21:59 +00:00
|
|
|
if (open("/dev/null", O_RDONLY) != 0) {
|
|
|
|
perror("open(\"/dev/null\")");
|
2012-07-25 07:32:15 +00:00
|
|
|
_exit(-2);
|
2012-07-26 07:21:59 +00:00
|
|
|
}
|
2012-07-25 07:32:15 +00:00
|
|
|
close(stdout_fds[0]);
|
2012-09-05 09:23:22 +00:00
|
|
|
/* XXX: Need the cast on Solaris because it defins NULL as 0L and gcc doesn't
|
|
|
|
* see it as a sentinal */
|
|
|
|
execlp("gdb", "gdb", "-n", "-batch", "-x", tempfile, execpath, pidstr, (void*)NULL);
|
2012-07-26 07:21:59 +00:00
|
|
|
perror("execlp(\"gdb\")");
|
2012-07-25 07:32:15 +00:00
|
|
|
do { _exit(-3); } while (1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// parent
|
|
|
|
close(stdout_fds[1]);
|
2013-04-02 06:29:55 +00:00
|
|
|
_logs_printf_nl(LOG_LEVEL_DEBUG, whence, "GDB BACKTRACE");
|
2012-07-26 07:21:59 +00:00
|
|
|
char buf[1024];
|
|
|
|
char *const bufe = buf + sizeof buf;
|
|
|
|
char *linep = buf;
|
|
|
|
char *readp = buf;
|
2012-07-25 07:32:15 +00:00
|
|
|
ssize_t nr;
|
2012-07-26 07:21:59 +00:00
|
|
|
while ((nr = read(stdout_fds[0], readp, bufe - readp)) > 0) {
|
|
|
|
char *p = readp;
|
|
|
|
readp = readp + nr;
|
|
|
|
for (; p < readp; ++p)
|
|
|
|
if (*p == '\n' || *p == '\0') {
|
|
|
|
*p = '\0';
|
2013-04-02 06:29:55 +00:00
|
|
|
_logs_printf_nl(LOG_LEVEL_DEBUG, __NOWHERE__, "%s", linep);
|
2012-07-26 07:21:59 +00:00
|
|
|
linep = p + 1;
|
|
|
|
}
|
|
|
|
if (readp >= bufe && linep == buf) {
|
|
|
|
// Line does not fit into buffer.
|
|
|
|
char t = bufe[-1];
|
|
|
|
bufe[-1] = '\0';
|
2013-04-02 06:29:55 +00:00
|
|
|
_logs_printf_nl(LOG_LEVEL_DEBUG, __NOWHERE__, "%s", buf);
|
2012-07-26 07:21:59 +00:00
|
|
|
buf[0] = t;
|
|
|
|
readp = buf + 1;
|
|
|
|
} else if (readp + 120 >= bufe && linep != buf) {
|
|
|
|
// Buffer low on space.
|
|
|
|
if (linep < readp)
|
|
|
|
memmove(buf, linep, readp - linep);
|
|
|
|
readp -= linep - buf;
|
|
|
|
linep = buf;
|
|
|
|
}
|
|
|
|
// Invariant: readp < bufe
|
|
|
|
}
|
2012-07-25 07:32:15 +00:00
|
|
|
if (nr == -1)
|
|
|
|
WHY_perror("read");
|
2012-07-26 07:21:59 +00:00
|
|
|
if (readp > linep) {
|
|
|
|
*readp = '\0';
|
2013-04-02 06:29:55 +00:00
|
|
|
_logs_printf_nl(LOG_LEVEL_DEBUG, __NOWHERE__, "%s", linep);
|
2012-07-26 07:21:59 +00:00
|
|
|
}
|
2012-07-25 07:32:15 +00:00
|
|
|
close(stdout_fds[0]);
|
|
|
|
int status = 0;
|
|
|
|
if (waitpid(child_pid, &status, 0) == -1)
|
|
|
|
WHY_perror("waitpid");
|
2012-07-26 07:21:59 +00:00
|
|
|
strbuf b = strbuf_local(buf, sizeof buf);
|
|
|
|
strbuf_append_exit_status(b, status);
|
2013-04-02 06:29:55 +00:00
|
|
|
_logs_printf_nl(LOG_LEVEL_DEBUG, __NOWHERE__, "gdb %s", buf);
|
2012-07-25 07:32:15 +00:00
|
|
|
unlink(tempfile);
|
2013-02-14 03:48:56 +00:00
|
|
|
#endif
|
2012-07-25 07:32:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-04-02 06:29:55 +00:00
|
|
|
|
|
|
|
const char *log_level_as_string(int level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case LOG_LEVEL_SILENT: return "silent";
|
|
|
|
case LOG_LEVEL_DEBUG: return "debug";
|
|
|
|
case LOG_LEVEL_INFO: return "info";
|
|
|
|
case LOG_LEVEL_WARN: return "warn";
|
|
|
|
case LOG_LEVEL_ERROR: return "error";
|
|
|
|
case LOG_LEVEL_FATAL: return "fatal";
|
|
|
|
case LOG_LEVEL_NONE: return "none";
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int string_to_log_level(const char *text)
|
|
|
|
{
|
|
|
|
if (strcasecmp(text, "none") == 0) return LOG_LEVEL_NONE;
|
|
|
|
if (strcasecmp(text, "fatal") == 0) return LOG_LEVEL_FATAL;
|
|
|
|
if (strcasecmp(text, "error") == 0) return LOG_LEVEL_ERROR;
|
|
|
|
if (strcasecmp(text, "warn") == 0) return LOG_LEVEL_WARN;
|
|
|
|
if (strcasecmp(text, "info") == 0) return LOG_LEVEL_INFO;
|
|
|
|
if (strcasecmp(text, "debug") == 0) return LOG_LEVEL_DEBUG;
|
|
|
|
if (strcasecmp(text, "silent") == 0) return LOG_LEVEL_SILENT;
|
|
|
|
return LOG_LEVEL_INVALID;
|
|
|
|
}
|