1 | /* cavernlog.cc |
---|
2 | * Run cavern inside an Aven window |
---|
3 | * |
---|
4 | * Copyright (C) 2005-2024 Olly Betts |
---|
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, write to the Free Software |
---|
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #include <config.h> |
---|
22 | |
---|
23 | #include "aven.h" |
---|
24 | #include "cavernlog.h" |
---|
25 | #include "filename.h" |
---|
26 | #include "mainfrm.h" |
---|
27 | #include "message.h" |
---|
28 | #include "osalloc.h" |
---|
29 | |
---|
30 | #include <algorithm> |
---|
31 | |
---|
32 | #include <errno.h> |
---|
33 | #include <stdio.h> |
---|
34 | #include <stdlib.h> |
---|
35 | |
---|
36 | #include <sys/time.h> |
---|
37 | #include <sys/types.h> |
---|
38 | #include <unistd.h> |
---|
39 | |
---|
40 | #include <wx/process.h> |
---|
41 | |
---|
42 | #define GVIM_COMMAND "gvim +'call cursor($l,$c)' $f" |
---|
43 | #define VIM_COMMAND "x-terminal-emulator -e vim +'call cursor($l,$c)' $f" |
---|
44 | #define NVIM_COMMAND "x-terminal-emulator -e nvim +'call cursor($l,$c)' $f" |
---|
45 | #define GEDIT_COMMAND "gedit $f +$l:$c" |
---|
46 | // Pluma currently ignores the column, but include it assuming some future |
---|
47 | // version will add support. |
---|
48 | #define PLUMA_COMMAND "pluma +$l:$c $f" |
---|
49 | #define EMACS_COMMAND "x-terminal-emulator -e emacs +$l:$c $f" |
---|
50 | #define NANO_COMMAND "x-terminal-emulator -e nano +$l,$c $f" |
---|
51 | #define JED_COMMAND "x-terminal-emulator -e jed $f -g $l" |
---|
52 | #define KATE_COMMAND "kate -l $l -c $c $f" |
---|
53 | |
---|
54 | #ifdef __WXMSW__ |
---|
55 | # define DEFAULT_EDITOR_COMMAND "notepad $f" |
---|
56 | #elif defined __WXMAC__ |
---|
57 | # define DEFAULT_EDITOR_COMMAND "open -t $f" |
---|
58 | #else |
---|
59 | # define DEFAULT_EDITOR_COMMAND VIM_COMMAND |
---|
60 | #endif |
---|
61 | |
---|
62 | enum { LOG_REPROCESS = 1234, LOG_SAVE = 1235 }; |
---|
63 | |
---|
64 | // New event type for signalling cavern output to process. |
---|
65 | wxDEFINE_EVENT(EVT_CAVERN_OUTPUT, wxCommandEvent); |
---|
66 | |
---|
67 | void |
---|
68 | CavernLogWindow::CheckForOutput(bool immediate) |
---|
69 | { |
---|
70 | timer.Stop(); |
---|
71 | if (cavern_out == NULL) return; |
---|
72 | |
---|
73 | wxInputStream * in = cavern_out->GetInputStream(); |
---|
74 | |
---|
75 | if (!in->CanRead()) { |
---|
76 | timer.StartOnce(); |
---|
77 | return; |
---|
78 | } |
---|
79 | |
---|
80 | size_t real_size = log_txt.size(); |
---|
81 | size_t allow = 1024; |
---|
82 | log_txt.resize(real_size + allow); |
---|
83 | in->Read(&log_txt[real_size], allow); |
---|
84 | size_t n = in->LastRead(); |
---|
85 | log_txt.resize(real_size + n); |
---|
86 | if (n) { |
---|
87 | if (immediate) { |
---|
88 | ProcessCavernOutput(); |
---|
89 | } else { |
---|
90 | QueueEvent(new wxCommandEvent(EVT_CAVERN_OUTPUT)); |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | int |
---|
96 | CavernLogWindow::OnPaintButton(wxButton* b, int x) |
---|
97 | { |
---|
98 | if (b) { |
---|
99 | x -= 4; |
---|
100 | const wxSize& bsize = b->GetSize(); |
---|
101 | x -= bsize.x; |
---|
102 | b->SetSize(x, 4, bsize.x, bsize.y); |
---|
103 | x -= 4; |
---|
104 | } |
---|
105 | return x; |
---|
106 | } |
---|
107 | |
---|
108 | void |
---|
109 | CavernLogWindow::OnPaint(wxPaintEvent&) |
---|
110 | { |
---|
111 | wxPaintDC dc(this); |
---|
112 | wxFont font = dc.GetFont(); |
---|
113 | wxFont bold_font = font.Bold(); |
---|
114 | wxFont underlined_font = font.Underlined(); |
---|
115 | const wxRegion& region = GetUpdateRegion(); |
---|
116 | const wxRect& rect = region.GetBox(); |
---|
117 | int scroll_x = 0, scroll_y = 0; |
---|
118 | GetViewStart(&scroll_x, &scroll_y); |
---|
119 | int fsize = dc.GetFont().GetPixelSize().GetHeight(); |
---|
120 | int limit = min((rect.y + rect.height + fsize - 1) / fsize + scroll_y, int(line_info.size()) - 1); |
---|
121 | for (int i = max(rect.y / fsize, scroll_y); i <= limit ; ++i) { |
---|
122 | LineInfo& info = line_info[i]; |
---|
123 | // Leave a small margin to the left. |
---|
124 | int x = fsize / 2 - scroll_x * fsize; |
---|
125 | int y = (i - scroll_y) * fsize; |
---|
126 | unsigned offset = info.start_offset; |
---|
127 | unsigned len = info.len; |
---|
128 | if (info.link_len) { |
---|
129 | dc.SetFont(underlined_font); |
---|
130 | dc.SetTextForeground(wxColour(192, 0, 192)); |
---|
131 | wxString link = wxString::FromUTF8(&log_txt[offset], info.link_len); |
---|
132 | offset += info.link_len; |
---|
133 | len -= info.link_len; |
---|
134 | dc.DrawText(link, x, y); |
---|
135 | x += info.link_pixel_width; |
---|
136 | dc.SetFont(font); |
---|
137 | } |
---|
138 | if (info.colour_len) { |
---|
139 | dc.SetTextForeground(dark_mode ? *wxWHITE : *wxBLACK); |
---|
140 | { |
---|
141 | size_t s_len = info.start_offset + info.colour_start - offset; |
---|
142 | wxString s = wxString::FromUTF8(&log_txt[offset], s_len); |
---|
143 | offset += s_len; |
---|
144 | len -= s_len; |
---|
145 | dc.DrawText(s, x, y); |
---|
146 | x += dc.GetTextExtent(s).GetWidth(); |
---|
147 | } |
---|
148 | switch (info.colour) { |
---|
149 | case LOG_ERROR: |
---|
150 | dc.SetTextForeground(*wxRED); |
---|
151 | break; |
---|
152 | case LOG_WARNING: |
---|
153 | dc.SetTextForeground(wxColour(0xf2, 0x8C, 0x28)); |
---|
154 | break; |
---|
155 | case LOG_INFO: |
---|
156 | dc.SetTextForeground(*wxBLUE); |
---|
157 | break; |
---|
158 | } |
---|
159 | dc.SetFont(bold_font); |
---|
160 | wxString d = wxString::FromUTF8(&log_txt[offset], info.colour_len); |
---|
161 | offset += info.colour_len; |
---|
162 | len -= info.colour_len; |
---|
163 | dc.DrawText(d, x, y); |
---|
164 | x += dc.GetTextExtent(d).GetWidth(); |
---|
165 | dc.SetFont(font); |
---|
166 | } |
---|
167 | dc.SetTextForeground(dark_mode ? *wxWHITE : *wxBLACK); |
---|
168 | dc.DrawText(wxString::FromUTF8(&log_txt[offset], len), x, y); |
---|
169 | } |
---|
170 | int x = GetClientSize().x; |
---|
171 | x = OnPaintButton(ok_button, x); |
---|
172 | x = OnPaintButton(reprocess_button, x); |
---|
173 | OnPaintButton(save_button, x); |
---|
174 | } |
---|
175 | |
---|
176 | BEGIN_EVENT_TABLE(CavernLogWindow, wxScrolledWindow) |
---|
177 | EVT_BUTTON(LOG_REPROCESS, CavernLogWindow::OnReprocess) |
---|
178 | EVT_BUTTON(LOG_SAVE, CavernLogWindow::OnSave) |
---|
179 | EVT_BUTTON(wxID_OK, CavernLogWindow::OnOK) |
---|
180 | EVT_COMMAND(wxID_ANY, EVT_CAVERN_OUTPUT, CavernLogWindow::OnCavernOutput) |
---|
181 | EVT_IDLE(CavernLogWindow::OnIdle) |
---|
182 | EVT_TIMER(wxID_ANY, CavernLogWindow::OnTimer) |
---|
183 | EVT_PAINT(CavernLogWindow::OnPaint) |
---|
184 | EVT_MOTION(CavernLogWindow::OnMouseMove) |
---|
185 | EVT_LEFT_UP(CavernLogWindow::OnLinkClicked) |
---|
186 | EVT_END_PROCESS(wxID_ANY, CavernLogWindow::OnEndProcess) |
---|
187 | END_EVENT_TABLE() |
---|
188 | |
---|
189 | wxString escape_for_shell(wxString s, bool protect_dash) |
---|
190 | { |
---|
191 | #ifdef __WXMSW__ |
---|
192 | // Correct quoting rules are insane: |
---|
193 | // |
---|
194 | // http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx |
---|
195 | // |
---|
196 | // Thankfully wxExecute passes the command string to CreateProcess(), so |
---|
197 | // at least we don't need to quote for cmd.exe too. |
---|
198 | if (protect_dash && !s.empty() && s[0u] == '-') { |
---|
199 | // If the filename starts with a '-', protect it from being |
---|
200 | // treated as an option by prepending ".\". |
---|
201 | s.insert(0, wxT(".\\")); |
---|
202 | } |
---|
203 | if (s.empty() || s.find_first_of(wxT(" \"\t\n\v")) != s.npos) { |
---|
204 | // Need to quote. |
---|
205 | s.insert(0, wxT('"')); |
---|
206 | for (size_t p = 1; p < s.size(); ++p) { |
---|
207 | size_t backslashes = 0; |
---|
208 | while (s[p] == wxT('\\')) { |
---|
209 | ++backslashes; |
---|
210 | if (++p == s.size()) { |
---|
211 | // Escape all the backslashes, since they're before |
---|
212 | // the closing quote we add below. |
---|
213 | s.append(backslashes, wxT('\\')); |
---|
214 | goto done; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | if (s[p] == wxT('"')) { |
---|
219 | // Escape any preceding backslashes and this quote. |
---|
220 | s.insert(p, backslashes + 1, wxT('\\')); |
---|
221 | p += backslashes + 1; |
---|
222 | } |
---|
223 | } |
---|
224 | done: |
---|
225 | s.append(wxT('"')); |
---|
226 | } |
---|
227 | #else |
---|
228 | size_t p = 0; |
---|
229 | if (protect_dash && !s.empty() && s[0u] == '-') { |
---|
230 | // If the filename starts with a '-', protect it from being |
---|
231 | // treated as an option by prepending "./". |
---|
232 | s.insert(0, wxT("./")); |
---|
233 | p = 2; |
---|
234 | } |
---|
235 | while (p < s.size()) { |
---|
236 | // Exclude a few safe characters which are common in filenames |
---|
237 | if (!isalnum((unsigned char)s[p]) && strchr("/._-", s[p]) == NULL) { |
---|
238 | s.insert(p, 1, wxT('\\')); |
---|
239 | ++p; |
---|
240 | } |
---|
241 | ++p; |
---|
242 | } |
---|
243 | #endif |
---|
244 | return s; |
---|
245 | } |
---|
246 | |
---|
247 | wxString get_command_path(const wxChar * command_name) |
---|
248 | { |
---|
249 | #ifdef __WXMSW__ |
---|
250 | wxString cmd; |
---|
251 | { |
---|
252 | DWORD len = 256; |
---|
253 | wchar_t *buf = NULL; |
---|
254 | while (1) { |
---|
255 | DWORD got; |
---|
256 | buf = (wchar_t*)osrealloc(buf, len * 2); |
---|
257 | got = GetModuleFileNameW(NULL, buf, len); |
---|
258 | if (got < len) break; |
---|
259 | len += len; |
---|
260 | } |
---|
261 | /* Strange Win32 nastiness - strip prefix "\\?\" if present */ |
---|
262 | wchar_t *start = buf; |
---|
263 | if (wcsncmp(start, L"\\\\?\\", 4) == 0) start += 4; |
---|
264 | wchar_t * slash = wcsrchr(start, L'\\'); |
---|
265 | if (slash) { |
---|
266 | cmd.assign(start, slash - start + 1); |
---|
267 | } |
---|
268 | free(buf); |
---|
269 | } |
---|
270 | #else |
---|
271 | wxString cmd = wxString::FromUTF8(msg_exepth()); |
---|
272 | #endif |
---|
273 | cmd += command_name; |
---|
274 | return cmd; |
---|
275 | } |
---|
276 | |
---|
277 | CavernLogWindow::CavernLogWindow(MainFrm * mainfrm_, const wxString & survey_, wxWindow * parent) |
---|
278 | : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, |
---|
279 | wxFULL_REPAINT_ON_RESIZE), |
---|
280 | mainfrm(mainfrm_), |
---|
281 | survey(survey_), |
---|
282 | timer(this) |
---|
283 | { |
---|
284 | if (wxSystemSettings::GetAppearance().IsDark()) { |
---|
285 | SetOwnBackgroundColour(*wxBLACK); |
---|
286 | dark_mode = true; |
---|
287 | } else { |
---|
288 | SetOwnBackgroundColour(*wxWHITE); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | CavernLogWindow::~CavernLogWindow() |
---|
293 | { |
---|
294 | timer.Stop(); |
---|
295 | if (cavern_out) { |
---|
296 | wxEndBusyCursor(); |
---|
297 | cavern_out->Detach(); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | void |
---|
302 | CavernLogWindow::OnMouseMove(wxMouseEvent& e) |
---|
303 | { |
---|
304 | const auto& pos = e.GetPosition(); |
---|
305 | int fsize = GetFont().GetPixelSize().GetHeight(); |
---|
306 | int scroll_x = 0, scroll_y = 0; |
---|
307 | GetViewStart(&scroll_x, &scroll_y); |
---|
308 | unsigned line = pos.y / fsize + scroll_y; |
---|
309 | unsigned x = pos.x + scroll_x * fsize - fsize / 2; |
---|
310 | if (line < line_info.size() && x <= line_info[line].link_pixel_width) { |
---|
311 | SetCursor(wxCursor(wxCURSOR_HAND)); |
---|
312 | } else { |
---|
313 | SetCursor(wxNullCursor); |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | void |
---|
318 | CavernLogWindow::OnLinkClicked(wxMouseEvent& e) |
---|
319 | { |
---|
320 | const auto& pos = e.GetPosition(); |
---|
321 | int fsize = GetFont().GetPixelSize().GetHeight(); |
---|
322 | int scroll_x = 0, scroll_y = 0; |
---|
323 | GetViewStart(&scroll_x, &scroll_y); |
---|
324 | unsigned line = pos.y / fsize + scroll_y; |
---|
325 | unsigned x = pos.x + scroll_x * fsize - fsize / 2; |
---|
326 | if (!(line < line_info.size() && x <= line_info[line].link_pixel_width)) |
---|
327 | return; |
---|
328 | |
---|
329 | const char* cur = &log_txt[line_info[line].start_offset]; |
---|
330 | size_t link_len = line_info[line].link_len; |
---|
331 | size_t colon = link_len; |
---|
332 | while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { } |
---|
333 | size_t colon2 = colon; |
---|
334 | while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { } |
---|
335 | if (cur[colon] != ':') { |
---|
336 | colon = colon2; |
---|
337 | colon2 = link_len; |
---|
338 | } |
---|
339 | |
---|
340 | wxString cmd; |
---|
341 | wxChar * p = wxGetenv(wxT("SURVEXEDITOR")); |
---|
342 | if (p) { |
---|
343 | cmd = p; |
---|
344 | if (!cmd.find(wxT("$f"))) { |
---|
345 | cmd += wxT(" $f"); |
---|
346 | } |
---|
347 | } else { |
---|
348 | p = wxGetenv(wxT("VISUAL")); |
---|
349 | if (!p) p = wxGetenv(wxT("EDITOR")); |
---|
350 | if (!p) { |
---|
351 | cmd = wxT(DEFAULT_EDITOR_COMMAND); |
---|
352 | } else { |
---|
353 | cmd = p; |
---|
354 | if (cmd == "gvim") { |
---|
355 | cmd = wxT(GVIM_COMMAND); |
---|
356 | } else if (cmd == "vim") { |
---|
357 | cmd = wxT(VIM_COMMAND); |
---|
358 | } else if (cmd == "nvim") { |
---|
359 | cmd = wxT(NVIM_COMMAND); |
---|
360 | } else if (cmd == "gedit") { |
---|
361 | cmd = wxT(GEDIT_COMMAND); |
---|
362 | } else if (cmd == "pluma") { |
---|
363 | cmd = wxT(PLUMA_COMMAND); |
---|
364 | } else if (cmd == "emacs") { |
---|
365 | cmd = wxT(EMACS_COMMAND); |
---|
366 | } else if (cmd == "nano") { |
---|
367 | cmd = wxT(NANO_COMMAND); |
---|
368 | } else if (cmd == "jed") { |
---|
369 | cmd = wxT(JED_COMMAND); |
---|
370 | } else if (cmd == "kate") { |
---|
371 | cmd = wxT(KATE_COMMAND); |
---|
372 | } else { |
---|
373 | // Escape any $. |
---|
374 | cmd.Replace(wxT("$"), wxT("$$")); |
---|
375 | cmd += wxT(" $f"); |
---|
376 | } |
---|
377 | } |
---|
378 | } |
---|
379 | size_t i = 0; |
---|
380 | while ((i = cmd.find(wxT('$'), i)) != wxString::npos) { |
---|
381 | if (++i >= cmd.size()) break; |
---|
382 | switch ((int)cmd[i]) { |
---|
383 | case wxT('$'): |
---|
384 | cmd.erase(i, 1); |
---|
385 | break; |
---|
386 | case wxT('f'): { |
---|
387 | wxString f = escape_for_shell(wxString(cur, colon), true); |
---|
388 | cmd.replace(i - 1, 2, f); |
---|
389 | i += f.size() - 1; |
---|
390 | break; |
---|
391 | } |
---|
392 | case wxT('l'): { |
---|
393 | wxString l = escape_for_shell(wxString(cur + colon + 1, colon2 - colon - 1)); |
---|
394 | cmd.replace(i - 1, 2, l); |
---|
395 | i += l.size() - 1; |
---|
396 | break; |
---|
397 | } |
---|
398 | case wxT('c'): { |
---|
399 | wxString l; |
---|
400 | if (colon2 == link_len) |
---|
401 | l = wxT("0"); |
---|
402 | else |
---|
403 | l = escape_for_shell(wxString(cur + colon2 + 1, link_len - colon2 - 1)); |
---|
404 | cmd.replace(i - 1, 2, l); |
---|
405 | i += l.size() - 1; |
---|
406 | break; |
---|
407 | } |
---|
408 | default: |
---|
409 | ++i; |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | if (wxExecute(cmd, wxEXEC_ASYNC|wxEXEC_MAKE_GROUP_LEADER) >= 0) |
---|
414 | return; |
---|
415 | |
---|
416 | wxString m; |
---|
417 | // TRANSLATORS: %s is replaced by the command we attempted to run. |
---|
418 | m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str()); |
---|
419 | m += wxT(" ("); |
---|
420 | m += wxString::FromUTF8(strerror(errno)); |
---|
421 | m += wxT(')'); |
---|
422 | wxGetApp().ReportError(m); |
---|
423 | } |
---|
424 | |
---|
425 | void |
---|
426 | CavernLogWindow::process(const wxString &file) |
---|
427 | { |
---|
428 | timer.Stop(); |
---|
429 | if (cavern_out) { |
---|
430 | cavern_out->Detach(); |
---|
431 | cavern_out = NULL; |
---|
432 | } else { |
---|
433 | wxBeginBusyCursor(); |
---|
434 | } |
---|
435 | |
---|
436 | SetFocus(); |
---|
437 | filename = file; |
---|
438 | |
---|
439 | info_count = 0; |
---|
440 | link_count = 0; |
---|
441 | log_txt.resize(0); |
---|
442 | line_info.resize(0); |
---|
443 | // Reserve enough that we won't need to grow the allocations in normal cases. |
---|
444 | log_txt.reserve(16384); |
---|
445 | line_info.reserve(256); |
---|
446 | ptr = 0; |
---|
447 | save_button = nullptr; |
---|
448 | reprocess_button = nullptr; |
---|
449 | ok_button = nullptr; |
---|
450 | DestroyChildren(); |
---|
451 | SetVirtualSize(0, 0); |
---|
452 | |
---|
453 | #ifdef __WXMSW__ |
---|
454 | SetEnvironmentVariable(wxT("SURVEX_UTF8"), wxT("1")); |
---|
455 | #else |
---|
456 | setenv("SURVEX_UTF8", "1", 1); |
---|
457 | #endif |
---|
458 | |
---|
459 | wxString escaped_file = escape_for_shell(file, true); |
---|
460 | wxString cmd = get_command_path(L"cavern"); |
---|
461 | cmd = escape_for_shell(cmd, false); |
---|
462 | cmd += wxT(" -o "); |
---|
463 | cmd += escaped_file; |
---|
464 | cmd += wxT(' '); |
---|
465 | cmd += escaped_file; |
---|
466 | |
---|
467 | cavern_out = wxProcess::Open(cmd); |
---|
468 | if (!cavern_out) { |
---|
469 | wxString m; |
---|
470 | m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str()); |
---|
471 | m += wxT(" ("); |
---|
472 | m += wxString::FromUTF8(strerror(errno)); |
---|
473 | m += wxT(')'); |
---|
474 | wxGetApp().ReportError(m); |
---|
475 | return; |
---|
476 | } |
---|
477 | |
---|
478 | // We want to receive the wxProcessEvent when cavern exits. |
---|
479 | cavern_out->SetNextHandler(this); |
---|
480 | |
---|
481 | // Check for output after 500ms if we don't get an idle event sooner. |
---|
482 | timer.StartOnce(500); |
---|
483 | } |
---|
484 | |
---|
485 | void |
---|
486 | CavernLogWindow::ProcessCavernOutput() |
---|
487 | { |
---|
488 | // ptr gives the start of the first line we've not yet processed. |
---|
489 | |
---|
490 | size_t nl; |
---|
491 | while ((nl = log_txt.find('\n', ptr)) != std::string::npos) { |
---|
492 | if (nl == ptr || (nl - ptr == 1 && log_txt[ptr] == '\r')) { |
---|
493 | // Don't show empty lines in the window. |
---|
494 | ptr = nl + 1; |
---|
495 | continue; |
---|
496 | } |
---|
497 | size_t line_len = nl - ptr - (log_txt[nl - 1] == '\r'); |
---|
498 | // FIXME: Avoid copy, use string_view? |
---|
499 | string cur(log_txt, ptr, line_len); |
---|
500 | if (log_txt[ptr] == ' ') { |
---|
501 | if (expecting_caret_line) { |
---|
502 | // FIXME: Check the line is only space, `^` and `~`? |
---|
503 | // Otherwise an error without caret info followed |
---|
504 | // by an error which contains a '^' gets |
---|
505 | // mishandled... |
---|
506 | size_t caret = cur.rfind('^'); |
---|
507 | if (caret != wxString::npos) { |
---|
508 | size_t tilde = cur.rfind('~'); |
---|
509 | if (tilde == wxString::npos || tilde < caret) { |
---|
510 | tilde = caret; |
---|
511 | } |
---|
512 | line_info.back().colour = line_info[line_info.size() - 2].colour; |
---|
513 | line_info.back().colour_start = caret; |
---|
514 | line_info.back().colour_len = tilde - caret + 1; |
---|
515 | expecting_caret_line = false; |
---|
516 | ptr = nl + 1; |
---|
517 | continue; |
---|
518 | } |
---|
519 | } |
---|
520 | expecting_caret_line = true; |
---|
521 | } |
---|
522 | line_info.emplace_back(ptr); |
---|
523 | line_info.back().len = line_len; |
---|
524 | size_t colon = cur.find(": "); |
---|
525 | if (colon != wxString::npos) { |
---|
526 | size_t link_len = colon; |
---|
527 | while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { } |
---|
528 | if (cur[colon] == ':') { |
---|
529 | line_info.back().link_len = link_len; |
---|
530 | |
---|
531 | static string info_marker = string(msg(/*info*/485)) + ':'; |
---|
532 | static string warning_marker = string(msg(/*warning*/106)) + ':'; |
---|
533 | static string error_marker = string(msg(/*error*/93)) + ':'; |
---|
534 | |
---|
535 | size_t offset = link_len + 2; |
---|
536 | if (cur.compare(offset, info_marker.size(), info_marker) == 0) { |
---|
537 | // Show "info" marker in blue. |
---|
538 | ++info_count; |
---|
539 | line_info.back().colour = LOG_INFO; |
---|
540 | line_info.back().colour_start = offset; |
---|
541 | line_info.back().colour_len = info_marker.size() - 1; |
---|
542 | } else if (cur.compare(offset, warning_marker.size(), warning_marker) == 0) { |
---|
543 | // Show "warning" marker in orange. |
---|
544 | line_info.back().colour = LOG_WARNING; |
---|
545 | line_info.back().colour_start = offset; |
---|
546 | line_info.back().colour_len = warning_marker.size() - 1; |
---|
547 | } else if (cur.compare(offset, error_marker.size(), error_marker) == 0) { |
---|
548 | // Show "error" marker in red. |
---|
549 | line_info.back().colour = LOG_ERROR; |
---|
550 | line_info.back().colour_start = offset; |
---|
551 | line_info.back().colour_len = error_marker.size() - 1; |
---|
552 | } |
---|
553 | ++link_count; |
---|
554 | } |
---|
555 | } |
---|
556 | |
---|
557 | int fsize = GetFont().GetPixelSize().GetHeight(); |
---|
558 | SetScrollRate(fsize, fsize); |
---|
559 | |
---|
560 | auto& info = line_info.back(); |
---|
561 | info.link_pixel_width = GetTextExtent(wxString(&log_txt[ptr], info.link_len)).GetWidth(); |
---|
562 | auto rest_pixel_width = GetTextExtent(wxString(&log_txt[ptr + info.link_len], info.len - info.link_len)).GetWidth(); |
---|
563 | int width = max(GetVirtualSize().GetWidth(), |
---|
564 | int(fsize + info.link_pixel_width + rest_pixel_width)); |
---|
565 | int height = line_info.size(); |
---|
566 | SetVirtualSize(width, height * fsize); |
---|
567 | if (!link_count) { |
---|
568 | // Auto-scroll until the first diagnostic. |
---|
569 | int scroll_x = 0, scroll_y = 0; |
---|
570 | GetViewStart(&scroll_x, &scroll_y); |
---|
571 | int xs, ys; |
---|
572 | GetClientSize(&xs, &ys); |
---|
573 | Scroll(scroll_x, line_info.size() * fsize - ys); |
---|
574 | } |
---|
575 | ptr = nl + 1; |
---|
576 | } |
---|
577 | } |
---|
578 | |
---|
579 | void |
---|
580 | CavernLogWindow::OnEndProcess(wxProcessEvent & evt) |
---|
581 | { |
---|
582 | bool cavern_success = evt.GetExitCode() == 0; |
---|
583 | |
---|
584 | // Read and process any remaining buffered output. |
---|
585 | wxInputStream* in = cavern_out->GetInputStream(); |
---|
586 | while (!in->Eof()) { |
---|
587 | CheckForOutput(true); |
---|
588 | } |
---|
589 | |
---|
590 | wxEndBusyCursor(); |
---|
591 | |
---|
592 | delete cavern_out; |
---|
593 | cavern_out = NULL; |
---|
594 | |
---|
595 | // Initially place buttons off the right of the window - they get moved to |
---|
596 | // the desired position by OnPaintButton(). |
---|
597 | wxPoint off_right(GetSize().x, 0); |
---|
598 | /* TRANSLATORS: Label for button in aven’s cavern log window which |
---|
599 | * allows the user to save the log to a file. */ |
---|
600 | save_button = new wxButton(this, LOG_SAVE, wmsg(/*&Save Log*/446), off_right); |
---|
601 | |
---|
602 | /* TRANSLATORS: Label for button in aven’s cavern log window which |
---|
603 | * causes the survey data to be reprocessed. */ |
---|
604 | reprocess_button = new wxButton(this, LOG_REPROCESS, wmsg(/*&Reprocess*/184), off_right); |
---|
605 | |
---|
606 | if (cavern_success) { |
---|
607 | ok_button = new wxButton(this, wxID_OK, wxString(), off_right); |
---|
608 | ok_button->SetDefault(); |
---|
609 | } |
---|
610 | |
---|
611 | Refresh(); |
---|
612 | if (!cavern_success) { |
---|
613 | return; |
---|
614 | } |
---|
615 | |
---|
616 | init_done = false; |
---|
617 | |
---|
618 | { |
---|
619 | wxString file3d(filename, 0, filename.length() - 3); |
---|
620 | file3d.append(wxT("3d")); |
---|
621 | if (!mainfrm->LoadData(file3d, survey)) { |
---|
622 | return; |
---|
623 | } |
---|
624 | } |
---|
625 | |
---|
626 | // Don't stay on log if there there are only "info" diagnostics. |
---|
627 | if (link_count == info_count) { |
---|
628 | wxCommandEvent dummy; |
---|
629 | OnOK(dummy); |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | void |
---|
634 | CavernLogWindow::OnReprocess(wxCommandEvent &) |
---|
635 | { |
---|
636 | process(filename); |
---|
637 | } |
---|
638 | |
---|
639 | void |
---|
640 | CavernLogWindow::OnSave(wxCommandEvent &) |
---|
641 | { |
---|
642 | wxString filelog(filename, 0, filename.length() - 3); |
---|
643 | #ifdef __WXMSW__ |
---|
644 | // We need to consistently use `\` here. |
---|
645 | filelog.Replace("/", "\\"); |
---|
646 | #endif |
---|
647 | filelog += wxT("log"); |
---|
648 | #ifdef __WXMOTIF__ |
---|
649 | wxString ext(wxT("*.log")); |
---|
650 | #else |
---|
651 | /* TRANSLATORS: Log files from running cavern (extension .log) */ |
---|
652 | wxString ext = wmsg(/*Log files*/447); |
---|
653 | ext += wxT("|*.log"); |
---|
654 | #endif |
---|
655 | wxFileDialog dlg(this, wmsg(/*Select an output filename*/319), |
---|
656 | wxString(), filelog, ext, |
---|
657 | wxFD_SAVE|wxFD_OVERWRITE_PROMPT); |
---|
658 | if (dlg.ShowModal() != wxID_OK) return; |
---|
659 | filelog = dlg.GetPath(); |
---|
660 | FILE * fh_log = wxFopen(filelog, wxT("w")); |
---|
661 | if (!fh_log) { |
---|
662 | wxGetApp().ReportError(wxString::Format(wmsg(/*Error writing to file “%s”*/7), filelog.c_str())); |
---|
663 | return; |
---|
664 | } |
---|
665 | FWRITE_(log_txt.data(), log_txt.size(), 1, fh_log); |
---|
666 | fclose(fh_log); |
---|
667 | } |
---|
668 | |
---|
669 | void |
---|
670 | CavernLogWindow::OnOK(wxCommandEvent &) |
---|
671 | { |
---|
672 | if (init_done) { |
---|
673 | mainfrm->HideLog(this); |
---|
674 | } else { |
---|
675 | mainfrm->InitialiseAfterLoad(filename, survey); |
---|
676 | init_done = true; |
---|
677 | } |
---|
678 | } |
---|