]> sigrok.org Git - pulseview.git/blame_incremental - main.cpp
Add new dependency: libgstreamermm >= 1.8.0.
[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 <fstream>
26#include <getopt.h>
27#include <vector>
28
29#include <gstreamermm.h>
30
31#include <libsigrokcxx/libsigrokcxx.hpp>
32
33#include <QCheckBox>
34#include <QDebug>
35#include <QFile>
36#include <QFileInfo>
37#include <QMessageBox>
38#include <QSettings>
39#include <QTextStream>
40
41#include "config.h"
42
43#ifdef ENABLE_SIGNALS
44#include "signalhandler.hpp"
45#endif
46
47#ifdef ENABLE_STACKTRACE
48#include <signal.h>
49#include <boost/stacktrace.hpp>
50#include <QStandardPaths>
51#endif
52
53#include "pv/application.hpp"
54#include "pv/devicemanager.hpp"
55#include "pv/globalsettings.hpp"
56#include "pv/logging.hpp"
57#include "pv/mainwindow.hpp"
58#include "pv/session.hpp"
59#include "pv/util.hpp"
60
61#ifdef ANDROID
62#include <libsigrokandroidutils/libsigrokandroidutils.h>
63#include "android/assetreader.hpp"
64#include "android/loghandler.hpp"
65#endif
66
67#ifdef _WIN32
68#include <QtPlugin>
69Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
70Q_IMPORT_PLUGIN(QSvgPlugin)
71#endif
72
73using std::exception;
74using std::ifstream;
75using std::ofstream;
76using std::shared_ptr;
77using std::string;
78
79#if ENABLE_STACKTRACE
80QString stacktrace_filename;
81
82void signal_handler(int signum)
83{
84 ::signal(signum, SIG_DFL);
85 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
86 ::raise(SIGABRT);
87}
88
89void process_stacktrace(QString temp_path)
90{
91 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
92
93 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
94 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
95 ofstream::out | ofstream::trunc);
96
97 boost::stacktrace::stacktrace st =
98 boost::stacktrace::stacktrace::from_dump(ifs);
99 ofs << st;
100
101 ofs.close();
102 ifs.close();
103
104 QFile f(stacktrace_outfile);
105 f.open(QFile::ReadOnly | QFile::Text);
106 QTextStream fs(&f);
107 QString stacktrace = fs.readAll();
108 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
109
110 qDebug() << QObject::tr("Stack trace of previous crash:");
111 qDebug() << "---------------------------------------------------------";
112 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
113 qDebug() << stacktrace.toLocal8Bit().data();
114 qDebug() << "---------------------------------------------------------";
115
116 f.close();
117
118 // Remove stack trace so we don't process it again the next time we run
119 QFile::remove(stacktrace_filename.toLocal8Bit().data());
120
121 // Show notification dialog if permitted
122 pv::GlobalSettings settings;
123 if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) {
124 QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again"));
125
126 QMessageBox msgbox;
127 msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
128 "A human-readable form has been saved to disk and was written to " \
129 "the log. You may access it from the settings dialog.").arg(PV_TITLE));
130 msgbox.setIcon(QMessageBox::Icon::Information);
131 msgbox.addButton(QMessageBox::Ok);
132 msgbox.setCheckBox(cb);
133
134 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
135 pv::GlobalSettings settings;
136 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
137 !state); });
138
139 msgbox.exec();
140 }
141}
142#endif
143
144void usage()
145{
146 fprintf(stdout,
147 "Usage:\n"
148 " %s [OPTIONS] [FILE]\n"
149 "\n"
150 "Help Options:\n"
151 " -h, -?, --help Show help option\n"
152 "\n"
153 "Application Options:\n"
154 " -V, --version Show release version\n"
155 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
156 " -d, --driver Specify the device driver to use\n"
157 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
158 " -i, --input-file Load input from file\n"
159 " -I, --input-format Input format\n"
160 " -c, --clean Don't restore previous sessions on startup\n"
161 "\n", PV_BIN_NAME);
162}
163
164int main(int argc, char *argv[])
165{
166 int ret = 0;
167 shared_ptr<sigrok::Context> context;
168 string open_file_format, driver;
169 vector<string> open_files;
170 bool restore_sessions = true;
171 bool do_scan = true;
172 bool show_version = false;
173
174 // Initialise gstreamermm. Must be called before any other GLib stuff.
175 Gst::init();
176
177 Application a(argc, argv);
178
179#ifdef ANDROID
180 srau_init_environment();
181 pv::AndroidLogHandler::install_callbacks();
182 pv::AndroidAssetReader asset_reader;
183#endif
184
185 // Parse arguments
186 while (true) {
187 static const struct option long_options[] = {
188 {"help", no_argument, nullptr, 'h'},
189 {"version", no_argument, nullptr, 'V'},
190 {"loglevel", required_argument, nullptr, 'l'},
191 {"driver", required_argument, nullptr, 'd'},
192 {"dont-scan", no_argument, nullptr, 'D'},
193 {"input-file", required_argument, nullptr, 'i'},
194 {"input-format", required_argument, nullptr, 'I'},
195 {"clean", no_argument, nullptr, 'c'},
196 {"log-to-stdout", no_argument, nullptr, 's'},
197 {nullptr, 0, nullptr, 0}
198 };
199
200 const int c = getopt_long(argc, argv,
201 "h?VDcl:d:i:I:", long_options, nullptr);
202 if (c == -1)
203 break;
204
205 switch (c) {
206 case 'h':
207 case '?':
208 usage();
209 return 0;
210
211 case 'V':
212 show_version = true;
213 break;
214
215 case 'l':
216 {
217 const int loglevel = atoi(optarg);
218 if (loglevel < 0 || loglevel > 5) {
219 qDebug() << "ERROR: invalid log level spec.";
220 break;
221 }
222 context->set_log_level(sigrok::LogLevel::get(loglevel));
223
224#ifdef ENABLE_DECODE
225 srd_log_loglevel_set(loglevel);
226#endif
227
228 if (loglevel >= 5) {
229 const QSettings settings;
230 qDebug() << "Settings:" << settings.fileName()
231 << "format" << settings.format();
232 }
233 break;
234 }
235
236 case 'd':
237 driver = optarg;
238 break;
239
240 case 'D':
241 do_scan = false;
242 break;
243
244 case 'i':
245 open_files.emplace_back(optarg);
246 break;
247
248 case 'I':
249 open_file_format = optarg;
250 break;
251
252 case 'c':
253 restore_sessions = false;
254 break;
255 }
256 }
257 argc -= optind;
258 argv += optind;
259
260 for (int i = 0; i < argc; i++)
261 open_files.emplace_back(argv[i]);
262
263 qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
264 qRegisterMetaType<uint64_t>("uint64_t");
265
266 // Prepare the global settings since logging needs them early on
267 pv::GlobalSettings settings;
268 settings.save_internal_defaults();
269 settings.set_defaults_where_needed();
270 settings.apply_theme();
271
272 pv::logging.init();
273
274 // Initialise libsigrok
275 context = sigrok::Context::create();
276 pv::Session::sr_context = context;
277
278#if ENABLE_STACKTRACE
279 QString temp_path = QStandardPaths::standardLocations(
280 QStandardPaths::TempLocation).at(0);
281 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
282 qDebug() << "Stack trace file is" << stacktrace_filename;
283
284 ::signal(SIGSEGV, &signal_handler);
285 ::signal(SIGABRT, &signal_handler);
286
287 if (QFileInfo::exists(stacktrace_filename))
288 process_stacktrace(temp_path);
289#endif
290
291#ifdef ANDROID
292 context->set_resource_reader(&asset_reader);
293#endif
294 do {
295
296#ifdef ENABLE_DECODE
297 // Initialise libsigrokdecode
298 if (srd_init(nullptr) != SRD_OK) {
299 qDebug() << "ERROR: libsigrokdecode init failed.";
300 break;
301 }
302
303 // Load the protocol decoders
304 srd_decoder_load_all();
305#endif
306
307#ifndef ENABLE_STACKTRACE
308 try {
309#endif
310
311 // Create the device manager, initialise the drivers
312 pv::DeviceManager device_manager(context, driver, do_scan);
313
314 a.collect_version_info(context);
315 if (show_version) {
316 a.print_version_info();
317 } else {
318 // Initialise the main window
319 pv::MainWindow w(device_manager);
320 w.show();
321
322 if (restore_sessions)
323 w.restore_sessions();
324
325 if (open_files.empty())
326 w.add_default_session();
327 else
328 for (string& open_file : open_files)
329 w.add_session_with_file(open_file, open_file_format);
330
331#ifdef ENABLE_SIGNALS
332 if (SignalHandler::prepare_signals()) {
333 SignalHandler *const handler = new SignalHandler(&w);
334 QObject::connect(handler, SIGNAL(int_received()),
335 &w, SLOT(close()));
336 QObject::connect(handler, SIGNAL(term_received()),
337 &w, SLOT(close()));
338 } else
339 qWarning() << "Could not prepare signal handler.";
340#endif
341
342 // Run the application
343 ret = a.exec();
344 }
345
346#ifndef ENABLE_STACKTRACE
347 } catch (exception& e) {
348 qDebug() << "Exception:" << e.what();
349 }
350#endif
351
352#ifdef ENABLE_DECODE
353 // Destroy libsigrokdecode
354 srd_exit();
355#endif
356
357 } while (false);
358
359 return ret;
360}