CommandLineParams.cpp 3.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  CERN
 *
 * This program 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "tapeserver/daemon/CommandLineParams.hpp"
#include "common/exception/Exception.hpp"
#include <getopt.h>
#include <string.h>

24
25
26
namespace cta { namespace daemon {

CommandLineParams::CommandLineParams(int argc, char** argv):
27
  foreground(false), logToStdout(false), logToFile(false),
28
29
  configFileLocation("/etc/cta/cta.conf"),
  helpRequested(false){
30
  struct ::option longopts[6];
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

  longopts[0].name = "foreground";
  longopts[0].has_arg = no_argument;
  longopts[0].flag = NULL;
  longopts[0].val = 'f';

  longopts[1].name = "config";
  longopts[1].has_arg = required_argument;
  longopts[1].flag = NULL;
  longopts[1].val = 'c';

  longopts[2].name = "help";
  longopts[2].has_arg = no_argument;
  longopts[2].flag = NULL;
  longopts[2].val = 'h';
  
  longopts[3].name = "stdout";
  longopts[3].has_arg = no_argument;
  longopts[3].flag = NULL;
  longopts[3].val = 's';
  
52
  longopts[4].name = "log-to-file";
53
54
55
  longopts[4].has_arg = required_argument;
  longopts[4].flag = NULL;
  longopts[4].val = 'l';
56
  
57
58
59
60
61
62
63
64
  memset(&longopts[4], 0, sizeof(struct ::option));

  char c;
  // Reset getopt's global variables to make sure we start fresh
  optind=0;
  // Prevent getopt from printing out errors on stdout
  opterr=0;
  // We ask getopt to not reshuffle argv ('+')
65
  while ((c = getopt_long(argc, argv, "+fsc:l:h", longopts, NULL)) != -1) {
66
67
68
69
70
71
72
73
74
75
76
77
78
    switch (c) {
    case 'f':
      foreground = true;
      break;
    case 's':
      logToStdout = true;
      break;
    case 'c':
      configFileLocation = optarg;
      break;
    case 'h':
      helpRequested = true;
      break;
79
80
81
82
    case 'l':
      logFilePath = optarg;
      logToFile = true;
      break;
83
84
85
86
87
88
89
    default:
      break;
    }
  }
  if (logToStdout && !foreground) {
    throw cta::exception::Exception("In CommandLineParams::CommandLineParams(): cannot log to stdout without running in the foreground");
  }
90
91
92
  if (logToFile && logToStdout) {
    throw cta::exception::Exception("In CommandLineParams::CommandLineParams(): cannot log to both stdout and file");
  }
93
}
94

Victor Kotlyar's avatar
Victor Kotlyar committed
95
96
std::list<cta::log::Param> CommandLineParams::toLogParams() const {
  std::list<cta::log::Param> ret;
97
98
99
100
101
102
  ret.push_back({"foreground", foreground});
  ret.push_back({"logToStdout", logToStdout});
  ret.push_back({"logToFile", logToFile});
  ret.push_back({"logFilePath", logFilePath});
  ret.push_back({"configFileLocation", configFileLocation});
  ret.push_back({"helpRequested", helpRequested});
103
104
105
106
107
  return ret;
}

}} //  namespace cta::daemon