2013-02-06 13:20:21 +01:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright 2008-2009, Freie Universitaet Berlin (FUB). All rights reserved.
|
|
|
|
|
|
|
|
These sources were developed at the Freie Universitaet Berlin, Computer Systems
|
|
|
|
and Telematics group (http://cst.mi.fu-berlin.de).
|
|
|
|
-------------------------------------------------------------------------------
|
2013-03-07 20:51:26 +01:00
|
|
|
This file is part of RIOT.
|
2013-02-06 13:20:21 +01: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 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
2013-03-07 20:51:26 +01:00
|
|
|
RIOT is distributed in the hope that it will be useful, but WITHOUT
|
2013-02-06 13:20:21 +01:00
|
|
|
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, see http://www.gnu.org/licenses/ .
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
For further information and questions please use the web site
|
|
|
|
http://scatterweb.mi.fu-berlin.de
|
|
|
|
and the mailinglist (subscription via web site)
|
|
|
|
scatterweb@lists.spline.inf.fu-berlin.de
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @ingroup syslog
|
|
|
|
* @brief System Logging Service output implementation
|
|
|
|
*
|
2013-03-08 11:30:23 +01:00
|
|
|
* @author Freie Universität Berlin, Computer Systems & Telematics
|
2013-02-06 13:20:21 +01:00
|
|
|
* @version $Revision: 3854 $
|
|
|
|
*
|
|
|
|
* @note $Id: syslog-out.c 3854 2011-12-06 15:27:01Z hwill $
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef MODULE_FAT
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#endif
|
|
|
|
// sys
|
|
|
|
#include "cfg-feuerware.h"
|
|
|
|
#include "syslog.h"
|
|
|
|
#include "clock.h"
|
|
|
|
#include "device-rs232.h"
|
|
|
|
#if FEUERWARE_CONF_CORE_SUPPORTS_TIME
|
|
|
|
#include "timelib.h"
|
|
|
|
#endif
|
|
|
|
// net
|
|
|
|
#include "net-types.h"
|
|
|
|
#ifdef MODULE_TRACELOG
|
|
|
|
#include "tracelog.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2013-06-22 05:11:53 +02:00
|
|
|
void syslog_format_ascii(struct syslog_args *args, struct syslog_chainlink *chainlink)
|
2013-02-06 13:20:21 +01:00
|
|
|
{
|
2013-06-22 05:11:53 +02:00
|
|
|
char buffer[SYSLOG_CONF_BUFSIZE + 25];
|
|
|
|
int msglen = 0;
|
|
|
|
|
|
|
|
if(args->message[0] != '\t') {
|
|
|
|
const char *strlevel = syslog_leveltostring(args->level);
|
|
|
|
msglen = snprintf(buffer, SYSLOG_CONF_BUFSIZE + 23,
|
|
|
|
"#[%u.%u:%u=%s] %s:",
|
|
|
|
NETWORK_ADDR_NET(NET_LOCAL_ADDRESS), NETWORK_ADDR_HOST(NET_LOCAL_ADDRESS),
|
|
|
|
args->level, strlevel, args->module
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
msglen += snprintf(buffer + msglen, SYSLOG_CONF_BUFSIZE + 23 - msglen, "%s", args->message);
|
|
|
|
buffer[msglen++] = '\n';
|
|
|
|
buffer[msglen] = '\0';
|
|
|
|
|
|
|
|
args->message = buffer;
|
|
|
|
|
|
|
|
if(chainlink != NULL && chainlink->fpout != NULL) {
|
|
|
|
chainlink->fpout(args, chainlink->next);
|
|
|
|
}
|
2013-02-06 13:20:21 +01:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2013-06-22 05:11:53 +02:00
|
|
|
void syslog_format_xml(struct syslog_args *args, struct syslog_chainlink *chainlink)
|
2013-02-06 13:20:21 +01:00
|
|
|
{
|
2013-06-22 05:11:53 +02:00
|
|
|
const size_t bufsize = SYSLOG_CONF_BUFSIZE + 80;
|
|
|
|
char buffer[bufsize];
|
|
|
|
char tbuf[20];
|
|
|
|
int msglen = 0;
|
|
|
|
|
|
|
|
#if FEUERWARE_CONF_CORE_SUPPORTS_TIME
|
|
|
|
time_get_string(tbuf, sizeof(tbuf));
|
|
|
|
#else
|
|
|
|
sprintf(tbuf, "%lu", clock_time(NULL));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
msglen = snprintf(buffer, bufsize,
|
|
|
|
"<log lvl=%u src=\"%u.%u\" id=%u ts=\"%s\" mod=\"%s\">%s</log>\n",
|
|
|
|
args->level, NETWORK_ADDR_NET(NET_LOCAL_ADDRESS), NETWORK_ADDR_HOST(NET_LOCAL_ADDRESS),
|
|
|
|
args->msgnum, tbuf, args->module, args->message
|
|
|
|
);
|
|
|
|
|
|
|
|
args->message = buffer;
|
|
|
|
|
|
|
|
if(chainlink != NULL && chainlink->fpout != NULL) {
|
|
|
|
chainlink->fpout(args, chainlink->next);
|
|
|
|
}
|
2013-02-06 13:20:21 +01:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2013-06-22 05:11:53 +02:00
|
|
|
void syslog_copy_stdout(struct syslog_args *args, struct syslog_chainlink *chainlink)
|
2013-02-06 13:20:21 +01:00
|
|
|
{
|
2013-06-22 05:11:53 +02:00
|
|
|
if(args->message[0] != '\t') {
|
|
|
|
const char *strlevel = syslog_leveltostring(args->level);
|
|
|
|
printf("#[%u.%u:%u=%s] %s:",
|
|
|
|
NETWORK_ADDR_NET(NET_LOCAL_ADDRESS), NETWORK_ADDR_HOST(NET_LOCAL_ADDRESS),
|
|
|
|
args->level, strlevel, args->module
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s\n", args->message);
|
|
|
|
|
|
|
|
if(chainlink != NULL && chainlink->fpout != NULL) {
|
|
|
|
chainlink->fpout(args, chainlink->next);
|
|
|
|
}
|
2013-02-06 13:20:21 +01:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2013-06-22 05:11:53 +02:00
|
|
|
void syslog_out_stdout(struct syslog_args *args, struct syslog_chainlink *chainlink)
|
2013-02-06 13:20:21 +01:00
|
|
|
{
|
2013-06-22 05:11:53 +02:00
|
|
|
printf(args->message);
|
2013-02-06 13:20:21 +01:00
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
if(chainlink != NULL && chainlink->fpout != NULL) {
|
|
|
|
chainlink->fpout(args, chainlink->next);
|
|
|
|
}
|
2013-02-06 13:20:21 +01:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
#ifdef MODULE_FAT
|
|
|
|
static int syslog_file = -1;
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
static int fat_open_logfile(const char *path);
|
|
|
|
static int fat_open_logfile(const char *path)
|
2013-02-06 13:20:21 +01:00
|
|
|
{
|
2013-06-22 05:11:53 +02:00
|
|
|
char t[20];
|
|
|
|
int file;
|
|
|
|
|
|
|
|
file = open(path, O_WRONLY | O_APPEND | O_CREAT);
|
2013-02-06 13:20:21 +01:00
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
time_get_string(t, sizeof(t));
|
2013-02-06 13:20:21 +01:00
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
if(file >= 0) {
|
|
|
|
syslog_notice("sys", "%s log %s opened", t, path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
syslog_warn("sys", "%s log %s failed", t, path);
|
|
|
|
}
|
2013-02-06 13:20:21 +01:00
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
return file;
|
2013-02-06 13:20:21 +01:00
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
void syslog_out_file(struct syslog_args *args, struct syslog_chainlink *chainlink)
|
2013-02-06 13:20:21 +01:00
|
|
|
{
|
2013-06-22 05:11:53 +02:00
|
|
|
if(syslog_file >= 0) {
|
|
|
|
size_t length = (size_t)strlen(args->message);
|
|
|
|
int ret = write(syslog_file, args->message, length);
|
|
|
|
|
|
|
|
if(ret == 1) {
|
2013-02-06 13:20:21 +01:00
|
|
|
#ifdef MODULE_TRACELOG
|
2013-06-22 05:11:53 +02:00
|
|
|
trace_string(TRACELOG_EV_MEMORY, "fat");
|
2013-02-06 13:20:21 +01:00
|
|
|
#endif
|
2013-06-22 05:11:53 +02:00
|
|
|
}
|
|
|
|
}
|
2013-02-06 13:20:21 +01:00
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
if(chainlink != NULL && chainlink->fpout != NULL) {
|
|
|
|
chainlink->fpout(args, chainlink->next);
|
|
|
|
}
|
2013-02-06 13:20:21 +01:00
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
bool syslog_open_file(void)
|
|
|
|
{
|
|
|
|
syslog_file = fat_open_logfile("SYSLOG.LOG");
|
|
|
|
return (syslog_file >= 0);
|
2013-02-06 13:20:21 +01:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
void syslog_close_file(void)
|
|
|
|
{
|
|
|
|
close(syslog_file);
|
|
|
|
syslog_file = -1;
|
2013-02-06 13:20:21 +01:00
|
|
|
}
|
|
|
|
#endif
|