[Sheepdog] [PATCH] collie: stop using syslog

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Thu Apr 8 02:11:04 CEST 2010


On Wed, Apr 7, 2010 at 6:11 PM, FUJITA Tomonori
<fujita.tomonori at lab.ntt.co.jp> wrote:
> - distributions save a syslog in different places.
>
> - only root can access to a syslog
>
> - when you run multiple collie daemons on the single host, it's
>  difficult to read a single log file.
>
> So this patch makes collie daemon use the own log file like:
>
> fujita at rose:~/git/sheepdog$ ./collie/collie /tmp/u/
> fujita at rose:~/git/sheepdog$ ls -l /tmp/u
> total 12
> -rw-r--r-- 1 fujita fujita 1103 2010-04-07 18:09 collie.log
> drwxr-x--- 2 fujita fujita 4096 2010-04-07 18:09 epoch
> drwxr-x--- 2 fujita fujita 4096 2010-04-07 18:09 obj
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp>
> ---
>  collie/collie.c  |   15 ++++++++++-----
>  collie/store.c   |   10 ++++++----
>  include/logger.h |    3 ++-
>  lib/logger.c     |   34 ++++++++++++++++++++++++++++++----
>  4 files changed, 48 insertions(+), 14 deletions(-)
>
> diff --git a/collie/collie.c b/collie/collie.c
> index 8367487..eed948b 100644
> --- a/collie/collie.c
> +++ b/collie/collie.c
> @@ -18,6 +18,7 @@
>
>  #define EPOLL_SIZE 4096
>  #define DEFAULT_OBJECT_DIR "/tmp"
> +#define LOG_FILE_NAME "collie.log"
>
>  static char program_name[] = "collie";
>
> @@ -60,6 +61,7 @@ int main(int argc, char **argv)
>        char *dir = DEFAULT_OBJECT_DIR;
>        int is_daemon = 1;
>        int log_level = LOG_INFO;
> +       char path[PATH_MAX];
>
>        while ((ch = getopt_long(argc, argv, short_options, long_options,
>                                 &longindex)) >= 0) {
> @@ -72,6 +74,7 @@ int main(int argc, char **argv)
>                        break;
>                case 'l':
>                        log_level = atoi(optarg);
> +                       break;
>                case 'd':
>                        /* removed soon. use loglevel instead */
>                        log_level = LOG_DEBUG;
> @@ -88,18 +91,20 @@ int main(int argc, char **argv)
>        if (optind != argc)
>                dir = argv[optind];
>
> -       ret = log_init(program_name, LOG_SPACE_SIZE, is_daemon, log_level);
> +       snprintf(path, sizeof(path), "%s/" LOG_FILE_NAME, dir);
> +
> +       ret = init_store(dir);
>        if (ret)
>                exit(1);
>
> -       if (is_daemon && daemon(0, 0))
> +       ret = log_init(program_name, LOG_SPACE_SIZE, is_daemon, log_level, path);
> +       if (ret)
>                exit(1);
>
> -       ret = init_event(EPOLL_SIZE);
> -       if (ret)
> +       if (is_daemon && daemon(0, 0))
>                exit(1);
>
> -       ret = init_store(dir);
> +       ret = init_event(EPOLL_SIZE);
>        if (ret)
>                exit(1);
>
> diff --git a/collie/store.c b/collie/store.c
> index b35bbf5..9de86e1 100644
> --- a/collie/store.c
> +++ b/collie/store.c
> @@ -1373,9 +1373,11 @@ int get_nodeid(uint64_t *nodeid)
>        return attr(epoch_path, ANAME_NODEID, nodeid, sizeof(*nodeid), 0);
>  }
>
> -static int init_base_path(char *d, int *new)
> +int init_base_path(char *d)
>  {
> -       return init_path(d, new);
> +       int new = 0;
> +
> +       return init_path(d, &new);
>  }
>
>  #define OBJ_PATH "/obj/"
> @@ -1473,9 +1475,9 @@ static int init_mnt_path(char *base_path)
>
>  int init_store(char *d)
>  {
> -       int ret, new = 0;
> +       int ret;
>
> -       ret = init_base_path(d, &new);
> +       ret = init_base_path(d);
>        if (ret)
>                return ret;
>
> diff --git a/include/logger.h b/include/logger.h
> index 7fa4cf1..6b4a399 100644
> --- a/include/logger.h
> +++ b/include/logger.h
> @@ -55,9 +55,10 @@ struct logarea {
>        char *buff;
>        int semid;
>        union semun semarg;
> +       int fd;
>  };
>
> -extern int log_init(char * progname, int size, int daemon, int level);
> +extern int log_init(char * progname, int size, int daemon, int level, char *outfile);
>  extern void log_close (void);
>  extern void dump_logmsg (void *);
>  extern void log_write(int prio, const char *func, int line, const char *fmt, ...)
> diff --git a/lib/logger.c b/lib/logger.c
> index 41dc0af..e30b698 100644
> --- a/lib/logger.c
> +++ b/lib/logger.c
> @@ -26,6 +26,7 @@
>  #include <syslog.h>
>  #include <signal.h>
>  #include <errno.h>
> +#include <time.h>
>  #include <sys/shm.h>
>  #include <sys/ipc.h>
>  #include <sys/stat.h>
> @@ -132,6 +133,8 @@ static int logarea_init (int size)
>
>  static void free_logarea (void)
>  {
> +       if (la->fd >= 0)
> +               close(la->fd);
>        semctl(la->semid, 0, IPC_RMID, la->semarg);
>        shmdt(la->buff);
>        shmdt(la->start);
> @@ -176,6 +179,18 @@ static int log_enqueue(int prio, const char *func, int line, const char *fmt,
>        }
>
>        p = buff;
> +
> +       if (la->fd != -1) {
> +               time_t t;
> +               struct tm *tmp;
> +
> +               t = time(NULL);
> +               tmp = localtime(&t);
> +
> +               strftime(p, MAX_MSG_SIZE, "%b %2d %I:%M:%S ", tmp);
> +               p += strlen(p);
> +       }
> +
>        snprintf(p, MAX_MSG_SIZE, "%s(%d) ", func, line);
>
>        p += strlen(p);
> @@ -260,7 +275,10 @@ static void log_syslog (void * buff)
>  {
>        struct logmsg * msg = (struct logmsg *)buff;
>
> -       syslog(msg->prio, "%s", (char *)&msg->str);
> +       if (la->fd >= 0)
> +               write(la->fd, (char *)&msg->str, strlen((char *)&msg->str));
> +       else
> +               syslog(msg->prio, "%s", (char *)&msg->str);
>  }
>
>  static void dolog(int prio, const char *func, int line, const char *fmt, va_list ap)
> @@ -342,7 +360,7 @@ static void log_flush(void)
>        }
>  }
>
> -int log_init(char *program_name, int size, int daemon, int level)
> +int log_init(char *program_name, int size, int daemon, int level, char *outfile)
>  {
>        log_level = level;
>
> @@ -356,8 +374,15 @@ int log_init(char *program_name, int size, int daemon, int level)
>                struct sigaction sa_new;
>                int fd;
>
> -               openlog(log_name, 0, LOG_DAEMON);
> -               setlogmask (LOG_UPTO (LOG_DEBUG));
> +               if (outfile) {
> +                       fd = open(outfile, O_CREAT | O_RDWR | O_APPEND, 0644);
> +                       if (fd < 0)
> +                               syslog(LOG_ERR, "failed to open %s\n", outfile);
> +               } else {
> +                       fd = -1;
> +                       openlog(log_name, 0, LOG_DAEMON);
> +                       setlogmask (LOG_UPTO (LOG_DEBUG));
> +               }

It seems that outfile is always non-NULL.
How about removing all the syslog stuff?

Thanks,

Kazutaka Morita



More information about the sheepdog mailing list