summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGavin Romig-Koch <gavin@localhost.localdomain>2009-08-14 17:12:28 -0400
committerGavin Romig-Koch <gavin@localhost.localdomain>2009-08-14 17:12:28 -0400
commit8e1ee5446ad9800e612f3e528da76816ca928b0c (patch)
treebcd9b5095c0aa4bd105e71aaa407734de52fdd89
parent6a47bca829a9da4a86dd2e341c8677c0cdcb2a97 (diff)
downloadfastback-8e1ee5446ad9800e612f3e528da76816ca928b0c.tar.gz
fastback-8e1ee5446ad9800e612f3e528da76816ca928b0c.tar.xz
fastback-8e1ee5446ad9800e612f3e528da76816ca928b0c.zip
create log file, and LOGFILE option
-rw-r--r--fastback.conf1
-rw-r--r--fastback.cpp115
2 files changed, 77 insertions, 39 deletions
diff --git a/fastback.conf b/fastback.conf
index 0fec5af..bb59955 100644
--- a/fastback.conf
+++ b/fastback.conf
@@ -1,6 +1,7 @@
# this is a config file for fastback
URLDIR = ftp://indus.usersys.redhat.com/incoming/
+LOGFILE = fastback-logfile
# ERROR = 103
# error
diff --git a/fastback.cpp b/fastback.cpp
index efe666e..49d8533 100644
--- a/fastback.cpp
+++ b/fastback.cpp
@@ -63,7 +63,7 @@ static const char fastback_name[] = "fastback";
static char* fastback_filename = 0; // local file to be uploaded
static char* fastback_ticket = 0; // ticket to upload to
static char* fastback_URLDIR = 0;
-
+static char* fastback_conf_LOGFILE = 0;
static bool fastback_encrypt = false; // encrypt file before upload
@@ -71,7 +71,8 @@ static bool fastback_encrypt = false; // encrypt file before upload
static bool fastback_newticket = false;
static bool fastback_verbose = false;
-static bool fastback_logging = false;
+
+static FILE* fastback_logfile = 0;
// if we need to create temporary files
@@ -101,6 +102,11 @@ show(FILE* file, string url)
else
fprintf(file,"fastback URLDIR not set\n");
+ if (fastback_conf_LOGFILE)
+ fprintf(file,"fastback LOGFILE: %s\n", fastback_conf_LOGFILE);
+ else
+ fprintf(file,"fastback LOGFILE not set\n");
+
if (fastback_filename)
fprintf(file,"fastback file: %s\n", fastback_filename);
@@ -167,9 +173,6 @@ fastback_argp_parser (int key, char *arg, struct argp_state *state)
case 'v':
fastback_verbose = true;
break;
- case 'l':
- fastback_logging = true;
- break;
default:
return ARGP_ERR_UNKNOWN;
}
@@ -181,7 +184,6 @@ static struct argp_option fastback_options[] = {
{0,'n',0,0,"create a new ticket for FILE"},
{"encrypt",'e',0,0,"encrypt FILE before uploading"},
{0,'v',0,0,"be verbose"},
- {"logging",'l',0,0,"print logfile to stderr"},
{ 0 }};
static struct argp fastback_argp = {
@@ -190,40 +192,53 @@ static struct argp fastback_argp = {
"FILE"
};
+struct config_option {
+ const char* option;
+ char** storage;
+};
+
+config_option all_config_options[] = {
+ { "URLDIR", &fastback_URLDIR },
+ { "LOGFILE", &fastback_conf_LOGFILE },
+ { 0, 0 }
+};
+
+
static bool
-option_parse(const char* filename,
+config_line_parse(const char* filename,
const char* line,
size_t var_start, size_t var_end,
size_t value_start, size_t value_end)
{
- bool config_file_error = false;
- const char option1[] = "URLDIR";
- size_t option_length = strlen(option1);
-
- if (option_length == (var_end - var_start)
- && memcmp(option1,line+var_start,option_length) == 0)
- {
- int value_length = value_end - value_start;
- fastback_URLDIR = (char*)malloc(value_length + 1);
- memcpy(fastback_URLDIR, line+value_start, value_length);
- fastback_URLDIR[value_length] = 0;
- }
+ config_option* each;
- else
+ for (each = all_config_options; each->option; each++)
{
- int var_length = var_end - var_start;
- char* var = (char*)malloc(var_length + 1);
- memcpy(var, line+var_start, var_length);
- var[var_length] = 0;
-
- fprintf(stderr,"%s: %s\n", filename, line);
- fprintf(stderr, "%s: error: unknown config option: %s\n",
- fastback_name, var);
- config_file_error = true;
- free(var);
+ size_t option_length = strlen(each->option);
+
+ if (option_length == (var_end - var_start)
+ && memcmp(each->option,line+var_start,option_length) == 0)
+ {
+ int value_length = value_end - value_start;
+ (*each->storage) = (char*)malloc(value_length + 1);
+ memcpy((*each->storage), line+value_start, value_length);
+ (*each->storage)[value_length] = 0;
+ return false;
+ }
}
- return config_file_error;
+ {
+ int var_length = var_end - var_start;
+ char* var = (char*)malloc(var_length + 1);
+ memcpy(var, line+var_start, var_length);
+ var[var_length] = 0;
+
+ fprintf(stderr,"%s: %s\n", filename, line);
+ fprintf(stderr, "%s: error: unknown config option: %s\n",
+ fastback_name, var);
+ free(var);
+ return true;
+ }
}
@@ -319,13 +334,13 @@ parse_config(const char* filename)
{
if (matchptr[4].rm_so != -1)
config_file_error |=
- option_parse(filename, line,
+ config_line_parse(filename, line,
matchptr[2].rm_so, matchptr[2].rm_eo,
matchptr[4].rm_so, matchptr[4].rm_eo);
else if (matchptr[5].rm_so != -1)
config_file_error |=
- option_parse(filename, line,
+ config_line_parse(filename, line,
matchptr[2].rm_so, matchptr[2].rm_eo,
matchptr[5].rm_so, matchptr[5].rm_eo);
}
@@ -478,9 +493,13 @@ cleanup()
free(fastback_filename);
free(fastback_ticket);
free(fastback_URLDIR);
+ free(fastback_conf_LOGFILE);
if (fastback_tmpdir != "")
RunCommand(string("rm -rf " + fastback_tmpdir));
+
+ if (fastback_logfile)
+ fclose(fastback_logfile);
}
@@ -518,6 +537,19 @@ main(int argc, char** argv)
if (parse_config("fastback.conf"))
exit(2);
+ if (fastback_conf_LOGFILE)
+ {
+ fastback_logfile = fopen(fastback_conf_LOGFILE,"a");
+ if (!fastback_logfile)
+ {
+ string msg = fastback_name;
+ msg += ": error: could not open logfile: ";
+ msg += fastback_conf_LOGFILE;
+ perror(msg.c_str());
+ exit(3);
+ }
+ }
+
if (!fastback_URLDIR)
fastback_URLDIR = strdup(fastback_default_URLDIR);
@@ -563,8 +595,8 @@ main(int argc, char** argv)
if (fastback_verbose)
show(stdout,url);
- if (fastback_logging)
- show(stderr,url);
+ if (fastback_logfile)
+ show(fastback_logfile,url);
if (curl_global_init(CURL_GLOBAL_ALL))
{
@@ -580,10 +612,15 @@ main(int argc, char** argv)
exit(3);
}
- // note that it is the fastback logging option which invokes curl verbose
- curl_err = curl_easy_setopt(handle, CURLOPT_VERBOSE, fastback_logging);
- check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_VERBOSE)");
-
+ if (fastback_logfile)
+ {
+ curl_err = curl_easy_setopt(handle, CURLOPT_VERBOSE, 1);
+ check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_VERBOSE)");
+
+ curl_err = curl_easy_setopt(handle, CURLOPT_STDERR, fastback_logfile);
+ check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_STDERR)");
+ }
+
curl_err = curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_URL)");