Small fixes and tidy-ups spotted by cppcheck.
[dcpomatic.git] / src / tools / dcpomatic_server_cli.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/server.h"
21 #include <iostream>
22 #include <stdexcept>
23 #include <cstring>
24 #include <vector>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <boost/array.hpp>
29 #include <boost/asio.hpp>
30 #include <boost/algorithm/string.hpp>
31 #include <boost/thread.hpp>
32 #include <boost/thread/mutex.hpp>
33 #include <boost/thread/condition.hpp>
34 #include "lib/config.h"
35 #include "lib/dcp_video.h"
36 #include "lib/exceptions.h"
37 #include "lib/util.h"
38 #include "lib/config.h"
39 #include "lib/scaler.h"
40 #include "lib/image.h"
41 #include "lib/log.h"
42 #include "lib/version.h"
43
44 using std::cerr;
45 using std::string;
46 using std::cout;
47 using boost::shared_ptr;
48
49 static void
50 help (string n)
51 {
52         cerr << "Syntax: " << n << " [OPTION]\n"
53              << "  -v, --version      show DCP-o-matic version\n"
54              << "  -h, --help         show this help\n"
55              << "  -t, --threads      number of parallel encoding threads to use\n"
56              << "  --verbose          be verbose to stdout\n"
57              << "  --log              write a log file of activity\n";
58 }
59
60 int
61 main (int argc, char* argv[])
62 {
63         int num_threads = Config::instance()->num_local_encoding_threads ();
64         bool verbose = false;
65         bool write_log = false;
66
67         int option_index = 0;
68         while (true) {
69                 static struct option long_options[] = {
70                         { "version", no_argument, 0, 'v'},
71                         { "help", no_argument, 0, 'h'},
72                         { "threads", required_argument, 0, 't'},
73                         { "verbose", no_argument, 0, 'A'},
74                         { "log", no_argument, 0, 'B'},
75                         { 0, 0, 0, 0 }
76                 };
77
78                 int c = getopt_long (argc, argv, "vht:AB", long_options, &option_index);
79
80                 if (c == -1) {
81                         break;
82                 }
83
84                 switch (c) {
85                 case 'v':
86                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
87                         exit (EXIT_SUCCESS);
88                 case 'h':
89                         help (argv[0]);
90                         exit (EXIT_SUCCESS);
91                 case 't':
92                         num_threads = atoi (optarg);
93                         break;
94                 case 'A':
95                         verbose = true;
96                         break;
97                 case 'B':
98                         write_log = true;
99                         break;
100                 }
101         }
102
103         Scaler::setup_scalers ();
104         shared_ptr<Log> log;
105         if (write_log) {
106                 log.reset (new FileLog ("dcpomatic_server_cli.log"));
107         } else {
108                 log.reset (new NullLog);
109         }
110         
111         Server server (log, verbose);
112         
113         try {
114                 server.run (num_threads);
115         } catch (boost::system::system_error& e) {
116                 if (e.code() == boost::system::errc::address_in_use) {
117                         cerr << argv[0] << ": address already in use.  Is another DCP-o-matic server instance already running?\n";
118                         exit (EXIT_FAILURE);
119                 }
120                 cerr << argv[0] << ": " << e.what() << "\n";
121         }
122         return 0;
123 }