]> sigrok.org Git - pulseview.git/blame_incremental - main.cpp
Main: Add -s option to allow logging to console as before
[pulseview.git] / main.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifdef ENABLE_DECODE
21#include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#endif
23
24#include <cstdint>
25#include <libsigrokcxx/libsigrokcxx.hpp>
26
27#include <getopt.h>
28
29#include <QDebug>
30#include <QSettings>
31
32#ifdef ENABLE_SIGNALS
33#include "signalhandler.hpp"
34#endif
35
36#ifdef ENABLE_STACKTRACE
37#include <signal.h>
38#include <boost/stacktrace.hpp>
39#include <QStandardPaths>
40#endif
41
42#include "pv/application.hpp"
43#include "pv/devicemanager.hpp"
44#include "pv/globalsettings.hpp"
45#include "pv/logging.hpp"
46#include "pv/mainwindow.hpp"
47#include "pv/session.hpp"
48
49#ifdef ANDROID
50#include <libsigrokandroidutils/libsigrokandroidutils.h>
51#include "android/assetreader.hpp"
52#include "android/loghandler.hpp"
53#endif
54
55#include "config.h"
56
57#ifdef _WIN32
58#include <QtPlugin>
59Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
60Q_IMPORT_PLUGIN(QSvgPlugin)
61#endif
62
63using std::exception;
64using std::shared_ptr;
65using std::string;
66
67#if ENABLE_STACKTRACE
68QString stacktrace_filename;
69
70void signal_handler(int signum)
71{
72 ::signal(signum, SIG_DFL);
73 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
74 ::raise(SIGABRT);
75}
76#endif
77
78void usage()
79{
80 fprintf(stdout,
81 "Usage:\n"
82 " %s [OPTIONS] [FILE]\n"
83 "\n"
84 "Help Options:\n"
85 " -h, -?, --help Show help option\n"
86 "\n"
87 "Application Options:\n"
88 " -V, --version Show release version\n"
89 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
90 " -d, --driver Specify the device driver to use\n"
91 " -D, --no-scan Don't auto-scan for devices, use -d spec only\n"
92 " -i, --input-file Load input from file\n"
93 " -I, --input-format Input format\n"
94 " -c, --clean Don't restore previous sessions on startup\n"
95 " -s, --log-to-stdout Don't use logging, output to stdout instead\n"
96 "\n", PV_BIN_NAME);
97}
98
99int main(int argc, char *argv[])
100{
101 int ret = 0;
102 shared_ptr<sigrok::Context> context;
103 string open_file, open_file_format, driver;
104 bool restore_sessions = true;
105 bool do_scan = true;
106 bool do_logging = true;
107
108 Application a(argc, argv);
109
110#ifdef ANDROID
111 srau_init_environment();
112 pv::AndroidLogHandler::install_callbacks();
113 pv::AndroidAssetReader asset_reader;
114#endif
115
116 // Parse arguments
117 while (true) {
118 static const struct option long_options[] = {
119 {"help", no_argument, nullptr, 'h'},
120 {"version", no_argument, nullptr, 'V'},
121 {"loglevel", required_argument, nullptr, 'l'},
122 {"driver", required_argument, nullptr, 'd'},
123 {"input-file", required_argument, nullptr, 'i'},
124 {"input-format", required_argument, nullptr, 'I'},
125 {"clean", no_argument, nullptr, 'c'},
126 {"log-to-stdout", no_argument, nullptr, 's'},
127 {nullptr, 0, nullptr, 0}
128 };
129
130 const int c = getopt_long(argc, argv,
131 "h?VDcsl:d:i:I:", long_options, nullptr);
132 if (c == -1)
133 break;
134
135 switch (c) {
136 case 'h':
137 case '?':
138 usage();
139 return 0;
140
141 case 'V':
142 // Print version info
143 fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
144 return 0;
145
146 case 'l':
147 {
148 const int loglevel = atoi(optarg);
149 if (loglevel < 0 || loglevel > 5) {
150 qDebug() << "ERROR: invalid log level spec.";
151 break;
152 }
153 context->set_log_level(sigrok::LogLevel::get(loglevel));
154
155#ifdef ENABLE_DECODE
156 srd_log_loglevel_set(loglevel);
157#endif
158
159 if (loglevel >= 5) {
160 const QSettings settings;
161 qDebug() << "Settings:" << settings.fileName()
162 << "format" << settings.format();
163 }
164 break;
165 }
166
167 case 'd':
168 driver = optarg;
169 break;
170
171 case 'D':
172 do_scan = false;
173 break;
174
175 case 'i':
176 open_file = optarg;
177 break;
178
179 case 'I':
180 open_file_format = optarg;
181 break;
182
183 case 'c':
184 restore_sessions = false;
185 break;
186
187 case 's':
188 do_logging = false;
189 break;
190 }
191 }
192
193 if (argc - optind > 1) {
194 fprintf(stderr, "Only one file can be opened.\n");
195 return 1;
196 }
197
198 if (argc - optind == 1)
199 open_file = argv[argc - 1];
200
201 // Prepare the global settings since logging needs them early on
202 pv::GlobalSettings settings;
203 settings.set_defaults_where_needed();
204
205 if (do_logging)
206 pv::logging.init();
207
208 // Initialise libsigrok
209 context = sigrok::Context::create();
210 pv::Session::sr_context = context;
211
212#if ENABLE_STACKTRACE
213 QString temp_path = QStandardPaths::standardLocations(
214 QStandardPaths::TempLocation).at(0);
215 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
216 qDebug() << "Stack trace file is" << stacktrace_filename;
217
218 ::signal(SIGSEGV, &signal_handler);
219 ::signal(SIGABRT, &signal_handler);
220#endif
221
222#ifdef ANDROID
223 context->set_resource_reader(&asset_reader);
224#endif
225 do {
226
227#ifdef ENABLE_DECODE
228 // Initialise libsigrokdecode
229 if (srd_init(nullptr) != SRD_OK) {
230 qDebug() << "ERROR: libsigrokdecode init failed.";
231 break;
232 }
233
234 // Load the protocol decoders
235 srd_decoder_load_all();
236#endif
237
238#ifndef ENABLE_STACKTRACE
239 try {
240#endif
241
242 // Create the device manager, initialise the drivers
243 pv::DeviceManager device_manager(context, driver, do_scan);
244
245 // Initialise the main window
246 pv::MainWindow w(device_manager);
247 w.show();
248
249 if (restore_sessions)
250 w.restore_sessions();
251
252 if (!open_file.empty())
253 w.add_session_with_file(open_file, open_file_format);
254 else
255 w.add_default_session();
256
257#ifdef ENABLE_SIGNALS
258 if (SignalHandler::prepare_signals()) {
259 SignalHandler *const handler = new SignalHandler(&w);
260 QObject::connect(handler, SIGNAL(int_received()),
261 &w, SLOT(close()));
262 QObject::connect(handler, SIGNAL(term_received()),
263 &w, SLOT(close()));
264 } else
265 qWarning() << "Could not prepare signal handler.";
266#endif
267
268 // Run the application
269 ret = a.exec();
270
271#ifndef ENABLE_STACKTRACE
272 } catch (exception& e) {
273 qDebug() << "Exception:" << e.what();
274 }
275#endif
276
277#ifdef ENABLE_DECODE
278 // Destroy libsigrokdecode
279 srd_exit();
280#endif
281
282 } while (false);
283
284 return ret;
285}