KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_export_step_process.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 Kicad Developers, see AUTHORS.txt for contributors.
5 * Copyright (C) 2020 New Pagodi(https://stackoverflow.com/users/6846682/new-pagodi)
6 * from https://stackoverflow.com/a/63289812/1522001
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
27#include <wx/textctrl.h>
28#include <wx/process.h>
29#include <wx/log.h>
30#include <wx/timer.h>
31#include <wx/txtstrm.h>
32
33wxDEFINE_EVENT( wxEVT_THREAD_STDIN, wxThreadEvent );
34wxDEFINE_EVENT( wxEVT_THREAD_STDERR, wxThreadEvent );
35
40class STDSTREAM_THREAD : public wxThread
41{
42public:
43 STDSTREAM_THREAD( wxEvtHandler* aEventHandler, wxProcess* aProcess,
44 wxMessageQueue<DIALOG_EXPORT_STEP_LOG::STATE_MESSAGE>& aMsgQueue ) :
45 wxThread( wxTHREAD_JOINABLE ),
46 m_queue( aMsgQueue )
47 {
48 m_process = aProcess;
49 m_handler = aEventHandler;
50 m_bufferSize = 1024 * 1024;
51 m_buffer = new char[m_bufferSize];
52 }
53
55 {
56 delete[] m_buffer;
57 delete m_process;
58 }
59
60private:
61 ExitCode Entry() override;
62 void DrainInput();
63
64 wxMessageQueue<DIALOG_EXPORT_STEP_LOG::STATE_MESSAGE>& m_queue;
65 wxEvtHandler* m_handler;
66 wxProcess* m_process;
67 char* m_buffer;
69};
70
71
72wxThread::ExitCode STDSTREAM_THREAD::Entry()
73{
74 ExitCode c;
75
76 while( 1 )
77 {
78 // Check if termination was requested.
79 if( TestDestroy() )
80 {
81 wxProcess::Kill( m_process->GetPid() );
82 c = reinterpret_cast<ExitCode>( 1 );
83 break;
84 }
85
87 wxMessageQueueError e = m_queue.ReceiveTimeout( 10, m );
88
89 // Check if a message was received or we timed out.
90 if( e == wxMSGQUEUE_NO_ERROR )
91 {
93 {
94 DrainInput();
95 c = reinterpret_cast<ExitCode>( 0 );
96 break;
97 }
99 {
100 wxProcess::Kill( m_process->GetPid() );
101 c = reinterpret_cast<ExitCode>( 1 );
102 break;
103 }
104 }
105 else if( e == wxMSGQUEUE_TIMEOUT )
106 {
107 DrainInput();
108 }
109 }
110
111 return c;
112}
113
114
116{
117 if( !m_process->IsInputOpened() )
118 {
119 return;
120 }
121
122 wxString fromInputStream, fromErrorStream;
123 wxInputStream* stream;
124
125 while( m_process->IsInputAvailable() )
126 {
127 stream = m_process->GetInputStream();
128 stream->Read( m_buffer, m_bufferSize );
129 fromInputStream << wxString( m_buffer, stream->LastRead() );
130 }
131
132 while( m_process->IsErrorAvailable() )
133 {
134 stream = m_process->GetErrorStream();
135 stream->Read( m_buffer, m_bufferSize );
136 fromErrorStream << wxString( m_buffer, stream->LastRead() );
137 }
138
139 if( !fromInputStream.IsEmpty() )
140 {
141 wxThreadEvent* event = new wxThreadEvent( wxEVT_THREAD_STDIN );
142 event->SetString( fromInputStream );
143 m_handler->QueueEvent( event );
144 }
145
146 if( !fromErrorStream.IsEmpty() )
147 {
148 wxThreadEvent* event = new wxThreadEvent( wxEVT_THREAD_STDERR );
149 event->SetString( fromErrorStream );
150 m_handler->QueueEvent( event );
151 }
152}
153
154
155void DIALOG_EXPORT_STEP_LOG::appendMessage( const wxString& aMessage )
156{
157 m_textCtrlLog->AppendText( aMessage );
158}
159
160
161void DIALOG_EXPORT_STEP_LOG::onProcessTerminate( wxProcessEvent& aEvent )
162{
163 // We need to inform the thread that the process has died
164 // Since it can't receive the event from wx
165 if( m_stdioThread && m_stdioThread->IsRunning() )
166 {
168 m_stdioThread->Wait();
169 delete m_stdioThread;
170 m_stdioThread = nullptr;
171 m_sdbSizerOK->Enable( true );
172
173 // set the progress bar to complete/incomplete base don status
174 m_activityGauge->SetRange( 1 );
175 if( aEvent.GetExitCode() )
176 m_activityGauge->SetValue( 0 );
177 else
178 m_activityGauge->SetValue( 1 );
179 }
180}
181
182void DIALOG_EXPORT_STEP_LOG::onThreadInput( wxThreadEvent& aEvent )
183{
184 m_textCtrlLog->AppendText( aEvent.GetString() );
185 m_activityGauge->Pulse();
186}
187
188
189void DIALOG_EXPORT_STEP_LOG::onClose( wxCloseEvent& aEvent )
190{
191 if( m_stdioThread && m_stdioThread->IsRunning() )
192 {
194 m_stdioThread->Wait();
195 delete m_stdioThread;
196 }
197
198 Destroy();
199}
200
201
202DIALOG_EXPORT_STEP_LOG::DIALOG_EXPORT_STEP_LOG( wxWindow* aParent, wxString aStepCmd ) :
204{
205 m_sdbSizerOK->Enable( false );
206
207 m_process = new wxProcess( this );
208 m_process->Redirect();
209
210 Bind( wxEVT_END_PROCESS, &DIALOG_EXPORT_STEP_LOG::onProcessTerminate, this );
211
212 Bind( wxEVT_THREAD_STDIN, &DIALOG_EXPORT_STEP_LOG::onThreadInput, this );
213 Bind( wxEVT_THREAD_STDERR, &DIALOG_EXPORT_STEP_LOG::onThreadInput, this );
214 Bind( wxEVT_CLOSE_WINDOW, &DIALOG_EXPORT_STEP_LOG::onClose, this );
215
216 // Print the command line used to run kicad-cli.
217 // it can be useful if kicad-cli as a problem.
218 m_textCtrlLog->AppendText( _( "Command line:\n" ) );
219 m_textCtrlLog->AppendText( aStepCmd );
220 m_textCtrlLog->AppendText( wxT( "\n\n" ) );
221
223 m_stdioThread->Run();
224
225 if( !m_stdioThread->IsRunning() )
226 {
227 m_textCtrlLog->AppendText( "Unable to launch stdstream thread.\n" );
228 delete m_stdioThread;
229 return;
230 }
231
232 m_activityGauge->Pulse();
233
234 wxExecute( aStepCmd, wxEXEC_ASYNC, m_process );
235}
void appendMessage(const wxString &aMessage)
DIALOG_EXPORT_STEP_LOG(wxWindow *aParent, wxString aStepCmd)
void onProcessTerminate(wxProcessEvent &aEvent)
@ PROCESS_COMPLETE
Informs the thread the process terminate event was received from wx.
@ REQUEST_EXIT
Asks the thread to exit and kill the process.
@ SENTINEL
Just a dummy entry for end of list.
void onClose(wxCloseEvent &event)
wxMessageQueue< STATE_MESSAGE > m_msgQueue
Class DIALOG_EXPORT_STEP_PROCESS_BASE.
This thread handles consuming the input streams from the launched process.
STDSTREAM_THREAD(wxEvtHandler *aEventHandler, wxProcess *aProcess, wxMessageQueue< DIALOG_EXPORT_STEP_LOG::STATE_MESSAGE > &aMsgQueue)
wxMessageQueue< DIALOG_EXPORT_STEP_LOG::STATE_MESSAGE > & m_queue
wxDEFINE_EVENT(wxEVT_THREAD_STDIN, wxThreadEvent)
#define _(s)