Fix pkg-config files from returning wrong relative paths.
[platform/core/security/vasum.git] / zone-daemon / main.cpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Jan Olszak <j.olszak@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19
20 /**
21  * @file
22  * @author  Jan Olszak (j.olszak@samsung.com)
23  * @brief   Main file for the Vasum Daemon
24  */
25
26 #include "config.hpp"
27
28 #include "exception.hpp"
29 #include "runner.hpp"
30
31 #include "logger/logger.hpp"
32 #include "logger/backend-stderr.hpp"
33 #include "logger/backend-journal.hpp"
34 #include "logger/backend-syslog.hpp"
35 #include "utils/typeinfo.hpp"
36
37 #include <boost/program_options.hpp>
38 #include <iostream>
39
40 using namespace logger;
41 using namespace vasum;
42
43 namespace po = boost::program_options;
44
45
46 namespace {
47
48 const std::string PROGRAM_NAME_AND_VERSION =
49     "Vasum Zones Daemon " PROGRAM_VERSION;
50
51 } // namespace
52
53
54 int main(int argc, char* argv[])
55 {
56     try {
57         po::options_description desc("Allowed options");
58
59         desc.add_options()
60         ("help,h", "print this help")
61         ("version,v", "show application version")
62         ("log-level,l", po::value<std::string>()->default_value("DEBUG"), "set log level")
63         ;
64
65         po::variables_map vm;
66         po::basic_parsed_options< char > parsed =
67             po::command_line_parser(argc, argv).options(desc).allow_unregistered().run();
68
69         std::vector<std::string> unrecognized_options =
70             po::collect_unrecognized(parsed.options, po::include_positional);
71
72         if (!unrecognized_options.empty()) {
73             std::cerr << "Unrecognized options: ";
74
75             for (auto& uo : unrecognized_options) {
76                 std::cerr << ' ' << uo;
77             }
78
79             std::cerr << std::endl << std::endl;
80             std::cerr << desc << std::endl;
81
82             return 1;
83         }
84
85         po::store(parsed, vm);
86         po::notify(vm);
87
88         if (vm.count("help")) {
89             std::cout << desc << std::endl;
90             return 0;
91         } else if (vm.count("version")) {
92             std::cout << PROGRAM_NAME_AND_VERSION << std::endl;
93             return 0;
94         }
95
96         Logger::setLogLevel(vm["log-level"].as<std::string>());
97 #if !defined(NDEBUG)
98         Logger::setLogBackend(new StderrBackend());
99 #elif HAVE_SYSTEMD
100         Logger::setLogBackend(new SystemdJournalBackend());
101 #else
102         Logger::setLogBackend(new SyslogBackend());
103 #endif
104
105     } catch (std::exception& e) {
106         std::cerr << e.what() << std::endl;
107         return 1;
108     }
109
110     try {
111         zone_daemon::Runner daemon;
112         daemon.run();
113
114     } catch (std::exception& e) {
115         LOGE("Unexpected: " << utils::getTypeName(e) << ": " << e.what());
116         return 1;
117     }
118
119     return 0;
120 }
121