odyssey/core/od_log.c

73 lines
1.4 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 "od_macro.h"
#include "od_log.h"
int od_loginit(odlog_t *l)
{
l->pid = getpid();
l->fd = 0;
2016-11-07 11:29:49 +00:00
return 0;
}
int od_logopen(odlog_t *l, char *path)
{
int rc = open(path, O_RDWR|O_CREAT|O_APPEND, 0644);
if (rc == -1)
return -1;
l->fd = rc;
return 0;
}
int od_logclose(odlog_t *l)
{
if (l->fd == -1)
return 0;
int rc = close(l->fd);
l->fd = -1;
return rc;
}
2016-11-15 11:38:31 +00:00
int od_logv(odlog_t *l, char *prefix, char *fmt, va_list args)
2016-11-07 11:29:49 +00:00
{
char buffer[512];
/* pid */
int len = snprintf(buffer, sizeof(buffer), "%d ", l->pid);
/* 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-08 11:12:05 +00:00
/* message prefix */
if (prefix)
len += snprintf(buffer + len, sizeof(buffer) - len, "%s", prefix);
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");
if (write(l->fd, buffer, len) == -1)
return -1;
2016-11-07 11:29:49 +00:00
return 0;
}
2016-11-08 11:12:05 +00:00