Commit a59ae69b authored by Stratos Psomadakis's avatar Stratos Psomadakis

Switch XSEGLOG2 to logging with syslog

parent 6261b41d
......@@ -68,6 +68,5 @@ EXPORT_SYMBOL(xseg_snprintf);
EXPORT_SYMBOL(__xseg_errbuf);
EXPORT_SYMBOL(__xseg_log);
EXPORT_SYMBOL(init_logctx);
EXPORT_SYMBOL(renew_logctx);
EXPORT_SYMBOL(__xseg_log2);
EXPORT_SYMBOL(xseg_printtrace);
......@@ -37,13 +37,10 @@ extern char __xseg_errbuf[4096];
void __xseg_log(const char *msg);
extern int (*xseg_snprintf)(char *str, size_t size, const char *format, ...);
struct log_ctx;
enum log_level { E = 0, W = 1, I = 2, D = 3};
extern int (*init_logctx)(struct log_ctx *lc, char *peer_name,
enum log_level log_level, char *logfile, uint32_t flags);
extern int (*renew_logctx)(struct log_ctx *lc, char *peer_name,
enum log_level log_level, char *logfile, uint32_t flags);
void __xseg_log2(struct log_ctx *lc, enum log_level level, char *fmt, ...);
extern void (*init_logctx)(char *peer_name, enum log_level level);
void __xseg_log2(enum log_level level, char *fmt, ...);
void xseg_printtrace(void);
#endif
......@@ -46,9 +46,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define XSEGLOG2(__ctx, __level, ...) \
do { \
if (__level <= ((__ctx)->log_level)) { \
__xseg_log2(__ctx, __level, FMTARG("%s: ", __func__, ## __VA_ARGS__ ,"")); \
} \
__xseg_log2(__level, FMTARG("%s: ", __func__, ## __VA_ARGS__ ,"")); \
} while(0)
/*
......@@ -121,19 +119,4 @@ typedef uint64_t xptr;
#define XPTR_TAKE(xptrval, base) \
( (void *) ( (unsigned long) base + (unsigned long) xptrval))
#define REOPEN_FILE (1 << 1)
#define REDIRECT_STDOUT (1 << 1)
#define REDIRECT_STDERR (1 << 2)
struct log_ctx {
// int stdout_orig;
// int stderr_orig;
char filename[MAX_LOGFILE_LEN];
volatile int logfile;
char peer_name[MAX_PEER_NAME];
unsigned int log_level;
uint32_t flags;
};
#endif
......@@ -31,6 +31,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <fcntl.h>
#include <sys/time.h>
#include <execinfo.h>
#include <syslog.h>
#include <xseg/util.h>
#include <xseg/xtypes.h>
#include <xseg/domain.h>
......@@ -107,170 +108,40 @@ void __get_current_time(struct timeval *tv) {
gettimeofday(tv, NULL);
}
int __renew_logctx(struct log_ctx *lc, char *peer_name,
enum log_level log_level, char *logfile, uint32_t flags)
{
int fd, tmp_fd;
mode_t old_mode;
if (peer_name){
strncpy(lc->peer_name, peer_name, MAX_PEER_NAME);
lc->peer_name[MAX_PEER_NAME -1] = 0;
}
lc->log_level = log_level;
if (logfile && logfile[0]) {
strncpy(lc->filename, logfile, MAX_LOGFILE_LEN);
lc->filename[MAX_LOGFILE_LEN - 1] = 0;
}
else if (!(flags & REOPEN_FILE) || lc->logfile == STDERR_FILENO)
return 0;
fd = open(lc->filename, O_WRONLY|O_CREAT|O_APPEND,
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
if (fd < 0){
return -1;
}
tmp_fd = lc->logfile;
lc->logfile = fd;
close(tmp_fd);
static inline int syslog_level(enum log_level level) {
int syslog_level;
flags &= ~REOPEN_FILE;
if ((flags|lc->flags) & REDIRECT_STDOUT){
fd = dup2(lc->logfile, STDOUT_FILENO);
if (fd < 0)
return -1;
}
if ((flags|lc->flags) & REDIRECT_STDERR){
fd = dup2(lc->logfile, STDERR_FILENO);
if (fd < 0)
return -1;
}
lc->flags |= flags;
switch (level) {
case E: syslog_level = LOG_ERROR; break;
case W: syslog_level = LOG_WARNING; break;
case I: syslog_level = LOG_INFO; break;
case D: syslog_level = LOG_DEBUG; break;
default: syslog_level = LOG_DEBUG; break;
}
return 0;
return syslog_level;
}
int (*renew_logctx)(struct log_ctx *lc, char *peer_name,
enum log_level log_level, char *logfile, uint32_t flags) = __renew_logctx;
int __init_logctx(struct log_ctx *lc, char *peer_name,
enum log_level log_level, char *logfile, uint32_t flags)
static void __init_logctx(char *peer_name, enum log_level level)
{
int fd;
int syslog_level;
if (peer_name){
strncpy(lc->peer_name, peer_name, MAX_PEER_NAME);
lc->peer_name[MAX_PEER_NAME -1] = 0;
}
else {
return -1;
}
openlog(peer_name, LOG_PID | LOG_CONS, LOG_LOCAL0);
/* set logfile to stderr by default */
lc->logfile = STDERR_FILENO;
#if 0
/* duplicate stdout, stderr */
fd = dup(STDOUT_FILENO);
if (fd < 0){
return -1;
}
lc->stdout_orig = fd;
fd = dup(STDERR_FILENO);
if (fd < 0){
return -1;
}
lc->stderr_orig = fd;
#endif
lc->log_level = log_level;
if (!logfile || !logfile[0]) {
// lc->logfile = lc->stderr_orig;
return 0;
}
strncpy(lc->filename, logfile, MAX_LOGFILE_LEN);
lc->filename[MAX_LOGFILE_LEN - 1] = 0;
fd = open(lc->filename, O_WRONLY|O_CREAT|O_APPEND,
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
if (fd < 1){
// lc->logfile = lc->stderr_orig;
return -1;
}
lc->logfile = fd;
/* FIXME: LOG_UPTO portability */
setlogmask(LOG_UPTO(syslog_level(level)));
if (flags & REDIRECT_STDOUT){
fd = dup2(lc->logfile, STDOUT_FILENO);
if (fd < 0)
return -1;
}
if (flags & REDIRECT_STDERR){
fd = dup2(lc->logfile, STDERR_FILENO);
if (fd < 0)
return -1;
}
lc->flags = flags;
return 0;
return;
}
int (*init_logctx)(struct log_ctx *lc, char *peer_name,
enum log_level log_level, char *logfile, uint32_t flags) = __init_logctx;
void __xseg_log2(struct log_ctx *lc, enum log_level level, char *fmt, ...)
void (*init_logctx)(char *peer_name, enum log_level level) = __init_logctx;
void __xseg_log2(enum log_level level, char *fmt, ...)
{
va_list ap;
time_t timeval;
char timebuf[1024], buffer[4096];
char *buf = buffer;
char *t = NULL, *pn = NULL;
ssize_t r, sum;
size_t count;
int fd;
va_start(ap, fmt);
switch (level) {
case E: t = "XSEG[EE]"; break;
case W: t = "XSEG[WW]"; break;
case I: t = "XSEG[II]"; break;
case D: t = "XSEG[DD]"; break;
default: t = "XSEG[UNKNONW]"; break;
}
pn = lc->peer_name;
if (!pn)
pn = "Invalid peer name";
time(&timeval);
ctime_r(&timeval, timebuf);
*strchr(timebuf, '\n') = '\0';
buf += sprintf(buf, "%s: ", t);
buf += snprintf(buf, MAX_PEER_NAME + 2, "%s: ", lc->peer_name);
buf += sprintf(buf, "%s (%ld):\n\t", timebuf, timeval);
unsigned long rem = sizeof(buffer) - (buf - buffer);
buf += vsnprintf(buf, rem, fmt, ap);
if (buf >= buffer + sizeof(buffer))
buf = buffer + sizeof(buffer) - 2;/* enough to hold \n and \0 */
buf += sprintf(buf, "\n");
count = buf-buffer;
sum = 0;
r = 0;
fd = *(volatile int *)&lc->logfile;
do {
r = write(fd, buffer + sum, count - sum);
if (r < 0){
if (errno == EBADF)
fd = *(volatile int *)&lc->logfile;
else {
//XSEGLOG("Error while writing log");
break;
}
} else {
sum += r;
}
} while (sum < count);
/* No need to check for error */
//fsync(fd);
vsyslog(syslog_level(level), fmt, ap);
va_end(ap);
return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment