From 4c31a79f1f76b6d80fffd05180dadcbb5a882293 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sun, 2 Mar 2008 11:44:52 -0500 Subject: Move logger/io routines to separate modules. --- src/Makefile | 2 +- src/io.c | 55 +++++++++++++++++++++++++++++++++++++ src/io.h | 9 ++++++ src/log.c | 37 +++++++++++++++++++++++++ src/log.h | 8 ++++++ src/mysqlerl.c | 87 ++-------------------------------------------------------- 6 files changed, 113 insertions(+), 85 deletions(-) create mode 100644 src/io.c create mode 100644 src/io.h create mode 100644 src/log.c create mode 100644 src/log.h diff --git a/src/Makefile b/src/Makefile index 4224443..5a9287d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,7 +10,7 @@ PRIVDIR = ../priv BEAMDIR = ../ebin BINS = $(PRIVDIR)/mysqlerl $(BEAMDIR)/mysqlerl.app -MYSQLERLOBJS = mysqlerl.o +MYSQLERLOBJS = io.o log.o mysqlerl.o BEAMS = mysqlerl.beam mysqlerl_app.beam mysqlerl_connection.beam \ mysqlerl_connection_sup.beam LIBS = -lmysqlclient diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..e255f61 --- /dev/null +++ b/src/io.c @@ -0,0 +1,55 @@ +#include "io.h" +#include "log.h" + +#include +#include +#include +#include + +int +restartable_read(unsigned char *buf, size_t buflen) +{ + ssize_t rc, readb; + + rc = 0; + READLOOP: + while (rc < buflen) { + readb = read(STDIN_FILENO, buf + rc, buflen - rc); + if (readb == -1) { + if (errno == EAGAIN || errno == EINTR) + goto READLOOP; + + return -1; + } else if (readb == 0) { + logmsg("ERROR: EOF trying to read additional %d bytes from " + "standard input", buflen - rc); + return -1; + } + + rc += readb; + } + + return rc; +} + +int +restartable_write(const unsigned char *buf, size_t buflen) +{ + ssize_t rc, wroteb; + + rc = 0; + WRITELOOP: + while (rc < buflen) { + wroteb = write(STDOUT_FILENO, buf + rc, buflen - rc); + if (wroteb == -1) { + if (errno == EAGAIN || errno == EINTR) + goto WRITELOOP; + + return -1; + } + + rc += wroteb; + } + + return rc; +} diff --git a/src/io.h b/src/io.h new file mode 100644 index 0000000..0db889b --- /dev/null +++ b/src/io.h @@ -0,0 +1,9 @@ +#ifndef _IO_H +#define _IO_H + +#include + +int restartable_read(unsigned char *buf, size_t buflen); +int restartable_write(const unsigned char *buf, size_t buflen); + +#endif diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..76ba469 --- /dev/null +++ b/src/log.c @@ -0,0 +1,37 @@ +#include "log.h" + +#include +#include + +const char *LOGPATH = "/tmp/mysqlerl.log"; +static FILE *logfile = NULL; + +void +openlog() +{ + logfile = fopen(LOGPATH, "a"); +} + +void +closelog() +{ + fclose(logfile); +} + +void +logmsg(const char *format, ...) +{ + FILE *out = logfile; + va_list args; + + if (logfile == NULL) + out = stderr; + + va_start(args, format); + (void)vfprintf(out, format, args); + (void)fprintf(out, "\n"); + va_end(args); + + fflush(out); +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..d09ca00 --- /dev/null +++ b/src/log.h @@ -0,0 +1,8 @@ +#ifndef _LOG_H +#define _LOG_H + +void openlog(); +void closelog(); +void logmsg(const char *format, ...); + +#endif diff --git a/src/mysqlerl.c b/src/mysqlerl.c index 9266baa..b0bbc87 100644 --- a/src/mysqlerl.c +++ b/src/mysqlerl.c @@ -4,20 +4,16 @@ * Copyright (C) 2008, Brian Cully */ +#include "io.h" +#include "log.h" + #include #include #include #include -#include -#include #include #include -#include -#include - -const char *LOGPATH = "/tmp/mysqlerl.log"; -static FILE *logfile = NULL; const char *QUERY_MSG = "sql_query"; @@ -29,83 +25,6 @@ struct msg { }; typedef struct msg msg_t; -void -openlog() -{ - logfile = fopen(LOGPATH, "a"); -} - -void -closelog() -{ - fclose(logfile); -} - -void -logmsg(const char *format, ...) -{ - FILE *out = logfile; - va_list args; - - if (logfile == NULL) - out = stderr; - - va_start(args, format); - (void)vfprintf(out, format, args); - (void)fprintf(out, "\n"); - va_end(args); - - fflush(out); -} - -int -restartable_read(unsigned char *buf, size_t buflen) -{ - ssize_t rc, readb; - - rc = 0; - READLOOP: - while (rc < buflen) { - readb = read(STDIN_FILENO, buf + rc, buflen - rc); - if (readb == -1) { - if (errno == EAGAIN || errno == EINTR) - goto READLOOP; - - return -1; - } else if (readb == 0) { - logmsg("ERROR: EOF trying to read additional %d bytes from " - "standard input", buflen - rc); - return -1; - } - - rc += readb; - } - - return rc; -} - -int -restartable_write(const unsigned char *buf, size_t buflen) -{ - ssize_t rc, wroteb; - - rc = 0; - WRITELOOP: - while (rc < buflen) { - wroteb = write(STDOUT_FILENO, buf + rc, buflen - rc); - if (wroteb == -1) { - if (errno == EAGAIN || errno == EINTR) - goto WRITELOOP; - - return -1; - } - - rc += wroteb; - } - - return rc; -} - msg_t * read_msg() { -- cgit v1.2.3