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