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