From e4c216f105a5f8d2ce96505685395ea7d7c08dbe Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 16 Mar 2008 17:49:38 +0100 Subject: [PATCH 1/1] Implement logfile and daemon mode. --- daemon.c | 5 ----- daemon.h | 7 +++++++ dss.c | 55 ++++++++++++++++++++++++++++++++++++++++++------------- dss.ggo | 9 +++++++++ 4 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 daemon.h diff --git a/daemon.c b/daemon.c index ba890fb..e9cf7b0 100644 --- a/daemon.c +++ b/daemon.c @@ -108,8 +108,3 @@ void log_welcome(int loglevel) DSS_INFO_LOG("***** welcome to dss ******\n"); DSS_DEBUG_LOG("using loglevel %d\n", loglevel); } - -int com_daemon(int argc, char * const * argv) -{ - return 1; -} diff --git a/daemon.h b/daemon.h new file mode 100644 index 0000000..aead8e8 --- /dev/null +++ b/daemon.h @@ -0,0 +1,7 @@ + +/** \file daemon.h exported symbols from daemon.c */ + +void daemon_init(void); +FILE *open_log(const char *logfile_name); +void close_log(FILE* logfile); +void log_welcome(int loglevel); diff --git a/dss.c b/dss.c index bda603e..b619f7d 100644 --- a/dss.c +++ b/dss.c @@ -22,10 +22,12 @@ #include "error.h" #include "fd.h" #include "exec.h" +#include "daemon.h" struct gengetopt_args_info conf; char *dss_error_txt = NULL; +static FILE *logfile; DEFINE_DSS_ERRLIST; @@ -68,6 +70,37 @@ struct snapshot { unsigned interval; }; +__printf_2_3 void dss_log(int ll, const char* fmt,...) +{ + va_list argp; + FILE *outfd; + struct tm *tm; + time_t t1; + char str[255] = ""; + + if (ll < conf.loglevel_arg) + return; + outfd = logfile? logfile : stderr; + time(&t1); + tm = localtime(&t1); + strftime(str, sizeof(str), "%b %d %H:%M:%S", tm); + fprintf(outfd, "%s ", str); + if (conf.loglevel_arg <= INFO) + fprintf(outfd, "%i: ", ll); + va_start(argp, fmt); + vfprintf(outfd, fmt, argp); + va_end(argp); +} + +__printf_1_2 void msg(const char* fmt,...) +{ + FILE *outfd = conf.daemon_given? logfile : stdout; + va_list argp; + va_start(argp, fmt); + vfprintf(outfd, fmt, argp); + va_end(argp); +} + int is_snapshot(const char *dirname, int64_t now, struct snapshot *s) { int i, ret; @@ -357,7 +390,7 @@ int remove_redundant_snapshot(struct snapshot_list *sl, } assert(victim); if (dry_run) { - printf("%s would be removed (interval = %i)\n", + msg("%s would be removed (interval = %i)\n", victim->name, victim->interval); continue; } @@ -378,7 +411,7 @@ int remove_old_snapshot(struct snapshot_list *sl, int dry_run, pid_t *pid) if (s->interval <= conf.num_intervals_arg) continue; if (dry_run) { - printf("%s would be removed (interval = %i)\n", + msg("%s would be removed (interval = %i)\n", s->name, s->interval); continue; } @@ -589,7 +622,7 @@ int com_ls(void) struct snapshot *s; get_snapshot_list(&sl); FOR_EACH_SNAPSHOT(s, i, &sl) - printf("%u\t%s\n", s->interval, s->name); + msg("%u\t%s\n", s->interval, s->name); free_snapshot_list(&sl); return 1; } @@ -602,16 +635,6 @@ __noreturn void clean_exit(int status) exit(status); } -__printf_2_3 void dss_log(int ll, const char* fmt,...) -{ - va_list argp; - if (ll < conf.loglevel_arg) - return; - va_start(argp, fmt); - vfprintf(stderr, fmt, argp); - va_end(argp); -} - int read_config_file(void) { int ret; @@ -682,6 +705,12 @@ int main(int argc, char **argv) ret = check_config(); if (ret < 0) goto out; + if (conf.logfile_given) { + logfile = open_log(conf.logfile_arg); + log_welcome(conf.loglevel_arg); + } + if (conf.daemon_given) + daemon_init(); ret = dss_chdir(conf.dest_dir_arg); if (ret < 0) goto out; diff --git a/dss.ggo b/dss.ggo index 9af79d7..70f6434 100644 --- a/dss.ggo +++ b/dss.ggo @@ -31,6 +31,15 @@ option "logfile" - string typestr="filename" optional +option "daemon" d +#~~~~~~~~~~~~~~~~ +"run as background daemon" +flag off +dependon="logfile" +details=" + Note that dsss refuses to start in daemon mode if no logfile + was specified. +" defgroup "command" #================= -- 2.39.2