OpendTect-6_4  6.4
oscommand.h
Go to the documentation of this file.
1 #ifndef oscommand_h
2 #define oscommand_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Bert
9  Date: Nov 2013
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "basicmod.h"
16 #include "bufstring.h"
17 #include "uistring.h"
18 
19 mFDQtclass(QProcess);
20 class od_istream;
21 class od_ostream;
22 class qstreambuf;
23 
24 namespace OS
25 {
26 
27 
29 
30 
35 {
36 public:
37  CommandExecPars( bool isbatchprog=false )
38  : needmonitor_(false)
39  , createstreams_(false)
40  , prioritylevel_(isbatchprog ? -1.0f : 0.0f)
41  , launchtype_(isbatchprog ? RunInBG : Wait4Finish)
42  , isconsoleuiprog_(false) {}
43 
44  mDefSetupClssMemb(CommandExecPars,bool,createstreams);
45  mDefSetupClssMemb(CommandExecPars,bool,needmonitor);
46  mDefSetupClssMemb(CommandExecPars,BufferString,monitorfnm);
48 
49  mDefSetupClssMemb(CommandExecPars,LaunchType,launchtype);
50 
51  mDefSetupClssMemb(CommandExecPars,float,prioritylevel);
53 
54  mDefSetupClssMemb(CommandExecPars,bool,isconsoleuiprog);
57 
58  void usePar(const IOPar&);
59  void fillPar(IOPar&) const;
60  void removeFromPar(IOPar&) const;
61 
62  static const char* sKeyPriority() { return "priority"; }
63 
64  static const StepInterval<int> cMachineUserPriorityRange(bool iswin);
70  static int getMachinePriority(float priolevel,bool iswin);
71 };
72 
73 
81 {
82 public:
83 
84  MachineCommand(const char* comm=0);
89  MachineCommand(const char* comm,const char* hnm);
90 
91  inline const char* command() const { return comm_; }
92  inline void setCommand( const char* cm ) { comm_ = cm; }
93  inline const char* hostName() const { return hname_; }
94  inline void setHostName( const char* hnm ) { hname_ = hnm; }
95  inline const char* remExec() const { return remexec_; }
96  inline void setRemExec( const char* sh ) { remexec_ = sh; }
97 
98  inline bool isBad() const { return comm_.isEmpty(); }
99 
100  bool setFromSingleStringRep(const char*,
101  bool ignorehostname=false);
107  const char* getSingleStringRep() const;
108 
109  bool hasHostName() const { return !hname_.isEmpty(); }
110 
111  static const char* defaultRemExec() { return defremexec_; }
112  static void setDefaultRemExec( const char* s ) { defremexec_ = s; }
113 
114  static const char* extractHostName(const char*,BufferString&);
116  BufferString getLocalCommand() const;
117 
118  BufferString runAndCollectOutput(BufferString* errmsg=nullptr);
120 
121  static const char* odRemExecCmd() { return "od_remexec"; }
122  static const char* sKeyRemoteHost() { return "machine"; }
123  static const char* sKeyRemoteCmd() { return "cmd"; }
124  static const char* sKeyMasterHost() { return "masterhost"; }
125  static const char* sKeyMasterPort() { return "masterport"; }
126  static const char* sKeyBG() { return "bg"; }
127  static const char* sKeyFG() { return "fg"; }
128  static const char* sKeyJobID() { return "jobid"; }
129 
130 protected:
131 
135 
137 
138 };
139 
140 
144 { mODTextTranslationClass(CommandLauncher);
145 public:
146  CommandLauncher(const MachineCommand&);
147  CommandLauncher(const MachineCommand&,bool isolated);
148  ~CommandLauncher();
149 
150  void set(const MachineCommand&);
151  void set(const MachineCommand&,bool isolated);
152 
153  bool execute(BufferString& output_stdout,
154  BufferString* output_stderr=0);
156  bool execute(const CommandExecPars& pars=CommandExecPars());
157 
158  int processID() const;
159  const char* monitorFileName() const { return monitorfnm_; }
160  uiString errorMsg() const { return errmsg_; }
161 
162  od_istream* getStdOutput() { return stdoutput_; }
163  od_istream* getStdError() { return stderror_; }
164  od_ostream* getStdInput() { return stdinput_; }
165 
166 protected:
167 
168  void reset();
169  bool doExecute(const char* comm,bool wait4finish,
170  bool inconsole = false,
171  bool createstreams=false );
172  int catchError();
173  bool startDetached(const char*,bool inconsole=false);
174 
175  MachineCommand machcmd_;
178 
182 
183  QProcess* process_;
185 
189 
193 
194 private:
195  void setIsolated();
196 
197 public: //Extra utilities, not for general use
198  static void addShellIfNeeded(BufferString& cmd);
202  static void addQuotesIfNeeded(BufferString& cmd);
205  static void manageQProcess(QProcess*);
207 };
208 
209 
211 mGlobal(Basic) bool ExecCommand(const char* cmd,LaunchType lt=Wait4Finish,
212  BufferString* stdoutput=0,
213  BufferString* stderror=0);
214 
215 
216 } // namespace OS
217 
219 mGlobal(Basic) bool ExecODProgram(const char* prognm,const char* args=0,
220  OS::LaunchType lt=OS::RunInBG);
221 
222 
223 #endif
int64_t pid_
Definition: oscommand.h:184
#define mExpClass(module)
Definition: commondefs.h:160
OD class for stream write common access to the user log file, or std::cout in batch progs...
Definition: od_ostream.h:26
static const char * defaultRemExec()
Definition: oscommand.h:111
const char * remExec() const
Definition: oscommand.h:95
BufferString remexec_
Definition: oscommand.h:134
#define mODTextTranslationClass(clss)
Definition: uistring.h:38
Specifies how to execute a command.
Definition: oscommand.h:34
#define mGlobal(module)
Definition: commondefs.h:163
uiString errorMsg() const
Definition: oscommand.h:160
uiString errmsg_
Definition: oscommand.h:180
static const char * sKeyJobID()
Definition: oscommand.h:128
Definition: oscommand.h:24
void usePar(const IOPar &iop, ODPolygon< T > &poly, const char *inpkey)
Definition: polygon.h:188
#define od_int64
Definition: plftypes.h:36
#define mDefSetupClssMemb(clss, typ, memb)
Definition: commondefs.h:133
const char * command() const
Definition: oscommand.h:91
Encapsulates an actual command to execute + the machine to run it on.
Definition: oscommand.h:80
Launches machine commands.
Definition: oscommand.h:143
MachineCommand machcmd_
Definition: oscommand.h:175
od_istream * stderror_
Definition: oscommand.h:187
Definition: uistring.h:89
static const char * sKeyRemoteCmd()
Definition: oscommand.h:123
qstreambuf * stdinputbuf_
Definition: oscommand.h:192
static void setDefaultRemExec(const char *s)
Definition: oscommand.h:112
CommandExecPars(bool isbatchprog=false)
Definition: oscommand.h:37
Adapter to use a qprocess as a stream.
Definition: qstreambuf.h:37
OD class for stream read.
Definition: od_istream.h:24
static const char * sKeyMasterPort()
Definition: oscommand.h:125
void setRemExec(const char *sh)
Definition: oscommand.h:96
bool isBad() const
Definition: oscommand.h:98
QProcess * process_
Definition: oscommand.h:183
static BufferString defremexec_
Definition: oscommand.h:136
bool hasHostName() const
Definition: oscommand.h:109
od_ostream * stdinput_
Definition: oscommand.h:188
bool redirectoutput_
Definition: oscommand.h:177
Generalized set of parameters of the keyword-value type.
Definition: iopar.h:47
BufferString hname_
Definition: oscommand.h:133
static const char * sKeyRemoteHost()
Definition: oscommand.h:122
Definition: oscommand.h:28
LaunchType
Definition: oscommand.h:28
void setHostName(const char *hnm)
Definition: oscommand.h:94
void setCommand(const char *cm)
Definition: oscommand.h:92
#define mFDQtclass(cls)
Definition: commondefs.h:228
void fillPar(IOPar &iop, const ODPolygon< T > &poly, const char *inpkey)
Definition: polygon.h:175
const char * monitorFileName() const
Definition: oscommand.h:159
Definition: oscommand.h:28
const BufferString odprogressviewer_
Definition: oscommand.h:181
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size...
Definition: bufstring.h:40
BufferString progvwrcmd_
Definition: oscommand.h:179
static const char * odRemExecCmd()
Definition: oscommand.h:121
BufferString errmsg_
Definition: horizontracker.h:119
static const char * sKeyPriority()
Definition: oscommand.h:62
static const char * sKeyMasterHost()
Definition: oscommand.h:124
const char * hostName() const
Definition: oscommand.h:93
qstreambuf * stderrorbuf_
Definition: oscommand.h:191
od_ostream * getStdInput()
Definition: oscommand.h:164
bool ExecCommand(const char *cmd, LaunchType lt=Wait4Finish, BufferString *stdoutput=0, BufferString *stderror=0)
od_istream * getStdOutput()
Definition: oscommand.h:162
static const char * sKeyBG()
Definition: oscommand.h:126
BufferString comm_
Definition: oscommand.h:132
od_istream * getStdError()
Definition: oscommand.h:163
BufferString monitorfnm_
Definition: oscommand.h:176
bool ExecODProgram(const char *prognm, const char *args=0, OS::LaunchType lt=OS::RunInBG)
od_istream * stdoutput_
Definition: oscommand.h:186
static const char * sKeyFG()
Definition: oscommand.h:127
qstreambuf * stdoutputbuf_
Definition: oscommand.h:190

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019