2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
21 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
25 #include <libsigrokcxx/libsigrokcxx.hpp>
33 #include "signalhandler.hpp"
36 #ifdef ENABLE_STACKTRACE
38 #include <boost/stacktrace.hpp>
39 #include <QStandardPaths>
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"
50 #include <libsigrokandroidutils/libsigrokandroidutils.h>
51 #include "android/assetreader.hpp"
52 #include "android/loghandler.hpp"
59 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
60 Q_IMPORT_PLUGIN(QSvgPlugin)
64 using std::shared_ptr;
68 QString stacktrace_filename;
70 void signal_handler(int signum)
72 ::signal(signum, SIG_DFL);
73 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
82 " %s [OPTIONS] [FILE]\n"
85 " -h, -?, --help Show help option\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"
99 int main(int argc, char *argv[])
102 shared_ptr<sigrok::Context> context;
103 string open_file, open_file_format, driver;
104 bool restore_sessions = true;
106 bool do_logging = true;
108 Application a(argc, argv);
111 srau_init_environment();
112 pv::AndroidLogHandler::install_callbacks();
113 pv::AndroidAssetReader asset_reader;
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 {"no-scan", no_argument, nullptr, 'D'},
124 {"input-file", required_argument, nullptr, 'i'},
125 {"input-format", required_argument, nullptr, 'I'},
126 {"clean", no_argument, nullptr, 'c'},
127 {"log-to-stdout", no_argument, nullptr, 's'},
128 {nullptr, 0, nullptr, 0}
131 const int c = getopt_long(argc, argv,
132 "h?VDcsl:d:i:I:", long_options, nullptr);
143 // Print version info
144 fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
149 const int loglevel = atoi(optarg);
150 if (loglevel < 0 || loglevel > 5) {
151 qDebug() << "ERROR: invalid log level spec.";
154 context->set_log_level(sigrok::LogLevel::get(loglevel));
157 srd_log_loglevel_set(loglevel);
161 const QSettings settings;
162 qDebug() << "Settings:" << settings.fileName()
163 << "format" << settings.format();
181 open_file_format = optarg;
185 restore_sessions = false;
194 if (argc - optind > 1) {
195 fprintf(stderr, "Only one file can be opened.\n");
199 if (argc - optind == 1)
200 open_file = argv[argc - 1];
202 // Prepare the global settings since logging needs them early on
203 pv::GlobalSettings settings;
204 settings.set_defaults_where_needed();
209 // Initialise libsigrok
210 context = sigrok::Context::create();
211 pv::Session::sr_context = context;
213 #if ENABLE_STACKTRACE
214 QString temp_path = QStandardPaths::standardLocations(
215 QStandardPaths::TempLocation).at(0);
216 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
217 qDebug() << "Stack trace file is" << stacktrace_filename;
219 ::signal(SIGSEGV, &signal_handler);
220 ::signal(SIGABRT, &signal_handler);
224 context->set_resource_reader(&asset_reader);
229 // Initialise libsigrokdecode
230 if (srd_init(nullptr) != SRD_OK) {
231 qDebug() << "ERROR: libsigrokdecode init failed.";
235 // Load the protocol decoders
236 srd_decoder_load_all();
239 #ifndef ENABLE_STACKTRACE
243 // Create the device manager, initialise the drivers
244 pv::DeviceManager device_manager(context, driver, do_scan);
246 // Initialise the main window
247 pv::MainWindow w(device_manager);
250 if (restore_sessions)
251 w.restore_sessions();
253 if (!open_file.empty())
254 w.add_session_with_file(open_file, open_file_format);
256 w.add_default_session();
258 #ifdef ENABLE_SIGNALS
259 if (SignalHandler::prepare_signals()) {
260 SignalHandler *const handler = new SignalHandler(&w);
261 QObject::connect(handler, SIGNAL(int_received()),
263 QObject::connect(handler, SIGNAL(term_received()),
266 qWarning() << "Could not prepare signal handler.";
269 // Run the application
272 #ifndef ENABLE_STACKTRACE
273 } catch (exception& e) {
274 qDebug() << "Exception:" << e.what();
279 // Destroy libsigrokdecode