Main page in Markdown format
[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 // Always log to console in DEBUG mode
27 #if !defined(LOG_TO_CONSOLE) && !defined(NDEBUG)
28 #define LOG_TO_CONSOLE
29 #endif
30
31 #include "config.hpp"
32
33 #include "exception.hpp"
34 #include "runner.hpp"
35
36 #include "logger/logger.hpp"
37 #include "logger/backend-stderr.hpp"
38 #include "logger/backend-journal.hpp"
39 #include "utils/typeinfo.hpp"
40
41 #include <boost/program_options.hpp>
42 #include <iostream>
43
44 using namespace logger;
45 using namespace vasum;
46
47 namespace po = boost::program_options;
48
49
50 namespace {
51
52 const std::string PROGRAM_NAME_AND_VERSION =
53     "Vasum Zones Daemon " PROGRAM_VERSION;
54
55 } // namespace
56
57
58 int main(int argc, char* argv[])
59 {
60     try {
61         po::options_description desc("Allowed options");
62
63         desc.add_options()
64         ("help,h", "print this help")
65         ("version,v", "show application version")
66         ("log-level,l", po::value<std::string>()->default_value("DEBUG"), "set log level")
67         ;
68
69         po::variables_map vm;
70         po::basic_parsed_options< char > parsed =
71             po::command_line_parser(argc, argv).options(desc).allow_unregistered().run();
72
73         std::vector<std::string> unrecognized_options =
74             po::collect_unrecognized(parsed.options, po::include_positional);
75
76         if (!unrecognized_options.empty()) {
77             std::cerr << "Unrecognized options: ";
78
79             for (auto& uo : unrecognized_options) {
80                 std::cerr << ' ' << uo;
81             }
82
83             std::cerr << std::endl << std::endl;
84             std::cerr << desc << std::endl;
85
86             return 1;
87         }
88
89         po::store(parsed, vm);
90         po::notify(vm);
91
92         if (vm.count("help")) {
93             std::cout << desc << std::endl;
94             return 0;
95         } else if (vm.count("version")) {
96             std::cout << PROGRAM_NAME_AND_VERSION << std::endl;
97             return 0;
98         }
99
100         Logger::setLogLevel(vm["log-level"].as<std::string>());
101 #ifdef LOG_TO_CONSOLE
102         Logger::setLogBackend(new StderrBackend());
103 #else
104         Logger::setLogBackend(new SystemdJournalBackend());
105 #endif
106
107
108     } catch (std::exception& e) {
109         std::cerr << e.what() << std::endl;
110         return 1;
111     }
112
113     try {
114         zone_daemon::Runner daemon;
115         daemon.run();
116
117     } catch (std::exception& e) {
118         LOGE("Unexpected: " << utils::getTypeName(e) << ": " << e.what());
119         return 1;
120     }
121
122     return 0;
123 }
124