summaryrefslogtreecommitdiffstats
path: root/server/parser/argparser.c
blob: 6ad4c69a136861366aeaa00d9ecb3c4f098a80a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2009 Red Hat Inc.
 *
 * This application is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; version 2.
 *
 * This application is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

/**
 * @file   argparser.c
 * @author David Sommerseth <davids@redhat.com>
 * @date   Thu Oct 22 13:58:46 2009
 *
 * @brief  Generic argument parser
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <eurephia_values.h>
#include <eurephia_nullsafe.h>


/**
 * Print a help screen to stdout
 */
void usage() {
	printf("rteval-parserd:  Parses new reports recieved via XML-RPC\n"
	       "\n"
	       "This program will wait for changes to the rteval 'submissionqueue' table.\n"
	       "When a new report is registered here, it will send this report to one of\n"
	       "the worker threads which will insert the parsed result into the database.\n"
	       "\n"
	       "** Program arguments:\n"
	       "  -d | --daemon                    Run as a daemon\n"
	       "  -l | --log        <log dest>     Where to put log data\n"
	       "  -L | --log-level  <verbosity>    What to log\n"
	       "  -f | --config     <config file>  Which configuration file to use\n"
	       "  -t | --threads    <num. threads> How many worker threads to start (def: 4)\n"
	       "  -h | --help                      This help screen\n"
	       "\n"
	       "** Configuration file\n"
	       "By default the program will look for /etc/rteval.conf.  This can be\n"
	       "overriden by using --config <config file>.\n"
	       "\n"
	       "** Logging\n"
	       "When the program is started as a daemon, it will log to syslog by default.\n"
	       "The default log level is 'info'.  When not started as a daemon, all logging\n"
	       "will go to stderr by default.\n"
	       "\n"
	       "The --log argument takes either 'destination' or a file name.  Unknown\n"
	       "destinations are treated as filenames.  Valid 'destinations' are:\n"
	       "\n"
	       "    stderr:             - Log to stderr\n"
	       "    stdout:             - Log to stdout\n"
	       "    syslog:[facility]   - Log to syslog\n"
	       "    <file name>         - Log to given file\n"
	       "\n"
	       "For syslog the default facility is 'daemon', but can be overriden by using\n"
	       "one of the following facility values:\n"
	       "    daemon, user and local0 to local7\n"
	       "\n"
	       "Log verbosity is set by the --log-level.  The valid values here are:\n"
	       "\n"
	       "    emerg, emergency    - Only log errors which causes the program to stop\n"
	       "    alert               - Incidents which needs immediate attention\n"
	       "    crit, critical      - Unexpected incidents which is not urgent\n"
	       "    err, error          - Parsing errors.  Issues with input data\n"
	       "    warn, warning       - Incidents which may influence performance\n"
	       "    notice              - Less important warnings\n"
	       "    info                - General run information\n"
	       "    debug               - Detailed run information, incl. thread operations\n"
	       "\n"
	       );
}


/**
 * Parses program arguments and puts the recognised arguments into an eurephiaVALUES struct.
 *
 * @param argc   argument counter
 * @param argv   argument string table
 *
 * @return Returns a pointer to an eurephiaVALUES struct.  On failure, the program halts.
 */
eurephiaVALUES *parse_arguments(int argc, char **argv) {
	eurephiaVALUES *args = NULL;
	int optidx, c;
	static struct option long_opts[] = {
		{"log", 1, 0, 'l'},
		{"log-level", 1, 0, 'L'},
		{"config", 1, 0, 'f'},
		{"threads", 1, 0, 't'},
		{"daemon", 0, 0, 'd'},
		{"help", 0, 0, 'h'},
		{0, 0, 0, 0}
	};

	args = eCreate_value_space(NULL, 21);
	eAdd_value(args, "daemon", "0");
	eAdd_value(args, "configfile", "/etc/rteval.conf");
	eAdd_value(args, "threads", "4");

	while( 1 ) {
		optidx = 0;
		c = getopt_long(argc, argv, "l:L:f:t:dh", long_opts, &optidx);
		if( c == -1 ) {
			break;
		}

		switch( c ) {
		case 'l':
			eUpdate_value(args, "log", optarg, 1);
			break;
		case 'L':
			eUpdate_value(args, "loglevel", optarg, 1);
			break;
		case 'f':
			eUpdate_value(args, "configfile", optarg, 0);
			break;
		case 't':
			eUpdate_value(args, "threads", optarg, 0);
			break;
		case 'd':
			eUpdate_value(args, "daemon", "1", 0);
			break;
		case 'h':
			usage();
			exit(0);
		}
	}

	// If logging is not configured, and it is not run as a daemon
	// -> log to stderr:
	if( (eGet_value(args, "log") == NULL)
	    && (atoi_nullsafe(eGet_value(args, "daemon")) == 0) ) {
		eAdd_value(args, "log", "stderr:");
	}

	return args;
}