odyssey/core/od_log.c

87 lines
1.8 KiB
C
Raw Normal View History

2016-11-07 11:29:49 +00:00
/*
2016-11-08 11:18:58 +00:00
* odissey.
*
* PostgreSQL connection pooler and request router.
2016-11-07 11:29:49 +00:00
*/
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <machinarium.h>
#include <soprano.h>
2016-11-07 11:29:49 +00:00
#include "od_macro.h"
2016-11-28 13:03:09 +00:00
#include "od_pid.h"
2016-11-28 14:47:39 +00:00
#include "od_syslog.h"
2016-11-07 11:29:49 +00:00
#include "od_log.h"
#include "od_io.h"
2016-11-07 11:29:49 +00:00
2016-11-29 12:57:46 +00:00
int od_loginit(od_log_t *l, od_pid_t *pid, od_syslog_t *syslog)
2016-11-07 11:29:49 +00:00
{
2016-11-28 13:03:09 +00:00
l->pid = pid;
2016-11-28 14:47:39 +00:00
l->syslog = syslog;
l->fd = 0;
2016-11-07 11:29:49 +00:00
return 0;
}
2016-11-29 12:57:46 +00:00
int od_logopen(od_log_t *l, char *path)
2016-11-07 11:29:49 +00:00
{
int rc = open(path, O_RDWR|O_CREAT|O_APPEND, 0644);
if (rc == -1)
return -1;
l->fd = rc;
return 0;
}
2016-11-29 12:57:46 +00:00
int od_logclose(od_log_t *l)
2016-11-07 11:29:49 +00:00
{
if (l->fd == -1)
return 0;
int rc = close(l->fd);
l->fd = -1;
return rc;
}
2016-11-29 12:57:46 +00:00
int od_logv(od_log_t *l, od_syslogprio_t prio,
mm_io_t peer,
2016-11-28 14:47:39 +00:00
char *ident,
char *fmt, va_list args)
2016-11-07 11:29:49 +00:00
{
char buffer[512];
/* pid */
2016-11-28 13:03:09 +00:00
int len = snprintf(buffer, sizeof(buffer), "%d ", l->pid->pid);
2016-11-07 11:29:49 +00:00
/* time */
struct timeval tv;
gettimeofday(&tv, NULL);
len += strftime(buffer + len, sizeof(buffer) - len, "%d %b %H:%M:%S.",
localtime(&tv.tv_sec));
len += snprintf(buffer + len, sizeof(buffer) - len, "%03d ",
(int)tv.tv_usec / 1000);
2016-11-28 14:47:39 +00:00
/* message ident */
if (ident)
len += snprintf(buffer + len, sizeof(buffer) - len, "%s", ident);
/* peer */
if (peer) {
char *peer_name = od_getpeername(peer);
len += snprintf(buffer + len, sizeof(buffer) - len, "%s", peer_name);
}
2016-11-07 11:29:49 +00:00
/* message */
len += vsnprintf(buffer + len, sizeof(buffer) - len, fmt, args);
va_end(args);
len += snprintf(buffer + len, sizeof(buffer), "\n");
2016-11-28 14:47:39 +00:00
int rc = write(l->fd, buffer, len);
od_syslog(l->syslog, prio, buffer);
return rc > 0;
2016-11-07 11:29:49 +00:00
}