Isis 3.0 Programmer Reference
Back | Home
Application.cpp
Go to the documentation of this file.
1 
23 #include "IsisDebug.h"
24 
25 #include <cstdio>
26 #include <sstream>
27 #include <unistd.h>
28 #include <sys/wait.h>
29 #include <errno.h>
30 
31 extern int errno;
32 
33 #include <fstream>
34 //#include <stdlib.h>
35 //#include <QString>
36 
37 #include <iostream>
38 #include <sstream>
39 #include <locale.h>
40 
41 #include <QApplication>
42 #include <QCoreApplication>
43 #include <QLocalSocket>
44 #include <QString>
45 
46 #include "Application.h"
47 #include "Constants.h" //is this still used in this class?
48 #include "CubeManager.h"
49 #include "FileName.h"
50 #include "IException.h"
51 #include "IString.h"
52 #include "Gui.h" //is this still used?
53 #include "Message.h"
54 #include "Preference.h"
55 #include "ProgramLauncher.h"
56 #include "Pvl.h"
57 #include "PvlGroup.h"
58 #include "PvlObject.h"
59 #include "SessionLog.h"
60 #include "TextFile.h"
61 #include "UserInterface.h"
62 
63 using namespace std;
64 
65 namespace Isis {
66  Application *iApp = NULL;
67  bool Application::p_applicationForceGuiApp = false;
68 
79  Application::Application(int &argc, char *argv[]) {
80  p_ui = NULL;
81  p_connectionToParent = NULL;
82 
83  // Save the application name
84  p_appName = argv[0];
85 
86  // Get the starting wall clock time
87  // p_datetime = DateTime(&p_startTime);
88 
89  // Init
90  p_startClock = 0;
91  p_startDirectIO = 0;
92  p_startPageFaults = 0;
93  p_startProcessSwaps = 0;
94  p_BatchlistPass = 0;
95 
96  // try to use US locale for numbers so we don't end up printing "," instead
97  // of "." where it might count.
98  setlocale(LC_ALL, "en_US");
99 
100  char env[1024];
101  strncpy(env, "LANG=en_US", 1023);
102  putenv(env);
103 
104  // Verify ISISROOT was set
105  if (getenv("ISISROOT") == NULL || QString(getenv("ISISROOT")) == "") {
106  QString message = "Please set ISISROOT before running any Isis "
107  "applications";
108  cerr << message << endl;
109  abort();
110  }
111 
112  // Get the starting cpu time, direct I/Os, page faults, and swaps
113  //p_startClock = clock();
114  p_startDirectIO = DirectIO();
115  p_startPageFaults = PageFaults();
116  p_startProcessSwaps = ProcessSwaps();
117 
118  // Create user interface and log
119  try {
120  FileName f(QString(argv[0]) + ".xml");
121 
122  // Create preferences
123  Preference::Preferences(f.name() == "unitTest.xml");
124 
125  if (!f.fileExists()) {
126  f = "$ISISROOT/bin/xml/" + f.name();
127  if (!f.fileExists()) {
128  QString message = Message::FileOpen(f.expanded());
129  throw IException(IException::Io, message, _FILEINFO_);
130  }
131  }
132  QString xmlfile = f.expanded();
133 
134  p_ui = new UserInterface(xmlfile, argc, argv);
135 
136  if (!p_ui->IsInteractive()) {
137  // Get the starting wall clock time
138  p_datetime = DateTime(&p_startTime);
139  p_startClock = clock();
140 
141  if (p_applicationForceGuiApp) {
142  new QApplication(argc, argv);
143  // When QApplication is initialized, it will reset the locale to the shells locale. As a result
144  // the locale needs to be reset after QApplications initialization.
145  setlocale(LC_ALL, "en_US");
146  }
147  else {
148  new QCoreApplication(argc, argv);
149  // When QCoreApplication is initialized, it will reset the locale to the shells locale. As a result
150  // the locale needs to be reset after QCoreApplications initialization.
151  setlocale(LC_ALL, "en_US");
152  }
153 
154  QCoreApplication::setApplicationName(FileName(p_appName).baseName());
155  }
156  }
157  catch (IException &e) {
158  e.print();
159  exit(e.errorType());
160  }
161 
162  iApp = this;
163 
164  // If we were run by another Isis app, connect to it
165  if (GetUserInterface().ParentId()) {
166  p_connectionToParent = new QLocalSocket;
167 
168  QString serverName = "isis_" + UserName() +
169  "_" + toString(iApp->GetUserInterface().ParentId());
170 
171  p_connectionToParent->connectToServer(serverName);
172  if (!p_connectionToParent->waitForConnected()) {
173  delete p_connectionToParent;
174  p_connectionToParent = NULL;
175  }
176  }
177  }
178 
180  Application::~Application() {
181  if (p_ui) {
182  delete p_ui;
183  p_ui = NULL;
184  }
185  }
186 
194  int Application::Run(void (*funct)()) {
195  int status = 0;
196  try {
197  if (p_ui->IsInteractive()) {
198  p_ui->TheGui()->Exec(funct);
199  }
200  else {
201  if (p_ui->BatchListSize() > 0) {
202  for (int i = 0; i < p_ui->BatchListSize(); i++) {
203  try {
204  p_ui->SetBatchList(i);
205 
206  if (i != 0) {
207  p_datetime = DateTime(&p_startTime);
208  p_startClock = clock();
209  p_startDirectIO = DirectIO();
210  p_startPageFaults = PageFaults();
211  p_startProcessSwaps = ProcessSwaps();
212  SessionLog::TheLog(true);
213  }
214 
215  funct();
216  Application::FunctionCleanup();
217  p_BatchlistPass++;
218  }
219  catch (IException &e) {
220  p_ui->SetErrorList(i);
221  status = Application::FunctionError(e);
222  if (p_ui->AbortOnError()) {
223  for (int j = (i + 1); j < p_ui->BatchListSize(); j++) {
224  p_ui->SetErrorList(j);
225  p_BatchlistPass++;
226  }
227  break;
228  }
229  }
230  }
231  }
232  else {
233  p_ui->SaveHistory();
234  // The gui checks everything but not the command line mode so
235  // verify if necessary. Batchlist verifies on SetBatchList
236  p_ui->VerifyAll();
237  funct();
238  Application::FunctionCleanup();
239  }
240  }
241  }
242  catch (IException &e) {
243  status = Application::FunctionError(e);
244  }
245 
246 #if 0
247  catch (exception &e) {
248  QString message = e.what();
249  Isis::iExceptionSystem i(message, _FILEINFO_);
250  status = i.Report();
251  }
252  catch (...) {
253  QString message = "Unknown error expection";
254  Isis::iExceptionSystem i(message, _FILEINFO_);
255  status = i.Report();
256  }
257 #endif
258 
259  return status;
260  }
261 
267  PvlObject Application::History() {
268  if (p_ui->IsInteractive()) {
269  p_startClock = clock();
270  p_datetime = DateTime(&p_startTime);
271  //cerr << "History GUI start clock=" << p_startClock << " time=" << p_startTime << endl;
272  }
273  PvlObject history(p_ui->ProgramName());
274  history += PvlKeyword("IsisVersion", Version());
275  history += PvlKeyword("ProgramVersion", p_ui->Version());
276  QString path = QCoreApplication::applicationDirPath();
277  history += PvlKeyword("ProgramPath", path);
278  history += PvlKeyword("ExecutionDateTime", p_datetime);
279  history += PvlKeyword("HostName", HostName());
280  history += PvlKeyword("UserName", UserName());
281  history += PvlKeyword("Description", p_ui->Brief());
282 
283  // Add the user parameters
284  Pvl pvl;
285  p_ui->CommandLine(pvl);
286  history.addGroup(pvl.findGroup("UserParameters"));
287 
288  return history;
289  }
290 
296  PvlGroup Application::Accounting() {
297  // Grab the ending time to compute connect time
298  time_t endTime = time(NULL);
299  double seconds = difftime(endTime, p_startTime);
300  int minutes = (int)(seconds / 60.0);
301  seconds = seconds - minutes * 60.0;
302  int hours = minutes / 60;
303  minutes = minutes - hours * 60;
304  char temp[80];
305  sprintf(temp, "%02d:%02d:%04.1f", hours, minutes, seconds);
306  QString conTime = temp;
307 
308  //cerr << "Accounting GUI end time=" << endTime << " start time=" << p_startTime << " total=" << seconds << endl;
309 
310  // Grab the ending cpu time to compute total cpu time
311  clock_t endClock = clock();
312  seconds = (double(endClock) - (double)p_startClock) / CLOCKS_PER_SEC;
313  minutes = (int)(seconds / 60.0);
314  seconds = seconds - minutes * 60.0;
315  hours = minutes / 60;
316  minutes = minutes - hours * 60;
317  sprintf(temp, "%02d:%02d:%04.1f", hours, minutes, seconds);
318  QString cpuTime = temp;
319 
320  // Add this information to the log
321  PvlGroup acct("Accounting");
322  acct += PvlKeyword("ConnectTime", conTime);
323  acct += PvlKeyword("CpuTime", cpuTime);
324 
325  // Not sure if these are really valuable. If deemed so then
326  // uncomment and complete private methods (DirectIO, Pagefaults, and
327  // ProcessSwaps).
328  //int directIO = DirectIO();
329  //int pageFaults = PageFaults();
330  //int processSwaps = ProcessSwaps();
331  //acct += Isis::PvlKeyword("DirectIo",directIO);
332  //acct += Isis::PvlKeyword("PageFaults",pageFaults);
333  //acct += Isis::PvlKeyword("ProcessSwaps",processSwaps);
334 
335  return acct;
336  }
337 
343  int Application::DirectIO() {
344  return 0 - p_startDirectIO;
345  }
346 
352  int Application::PageFaults() {
353  return 0 - p_startPageFaults;
354  }
355 
361  int Application::ProcessSwaps() {
362  return 0 - p_startProcessSwaps;
363  }
364 
370  void Application::Log(PvlGroup &results) {
371  // Add it to the log file
372  static bool blankLine = false;
373 
374  SessionLog::TheLog().AddResults(results);
375 
376  // See if the log file will be written to the terminal/gui
377  if (SessionLog::TheLog().TerminalOutput()) return;
378 
379  // See if we should write the info to our parents gui
380  if (HasParent()) {
381  ostringstream ostr;
382  if (blankLine) ostr << endl;
383  ostr << results << endl;
384  QString data = ostr.str().c_str();
385  iApp->SendParentData("LOG", data);
386  }
387 
388  // Otherwise see if we need to write to our gui
389  else if (iApp->GetUserInterface().IsInteractive()) {
390  ostringstream ostr;
391  if (blankLine) ostr << endl;
392  ostr << results << endl;
393  iApp->GetUserInterface().TheGui()->Log(ostr.str().c_str());
394  iApp->GetUserInterface().TheGui()->ShowLog();
395  }
396 
397  // Otherwise its command line mode
398  else {
399  if (blankLine) cout << endl;
400  cout << results << endl;
401  }
402  blankLine = true;
403  }
404 
410  void Application::GuiLog(const Pvl &results) {
411  // See if we should write the info to our parents gui
412  Pvl copy(results);
413 
414  if (HasParent()) {
415  ostringstream ostr;
416  ostr << copy << endl;
417  QString data = ostr.str().c_str();
418  iApp->SendParentData("GUILOG", data);
419  }
420 
421  // Otherwise see if we need to write to our gui
422  else if (iApp->GetUserInterface().IsInteractive()) {
423  ostringstream ostr;
424  ostr << copy << endl;
425  iApp->GetUserInterface().TheGui()->Log(ostr.str().c_str());
426  iApp->GetUserInterface().TheGui()->ShowLog();
427  }
428  }
429 
435  void Application::GuiLog(const PvlGroup &results) {
436  // See if we should write the info to our parents gui
437  PvlGroup copy(results);
438 
439  if (HasParent()) {
440  ostringstream ostr;
441  ostr << copy << endl;
442  QString data = ostr.str().c_str();
443  iApp->SendParentData("GUILOG", data);
444  }
445 
446  // Otherwise see if we need to write to our gui
447  else if (iApp->GetUserInterface().IsInteractive()) {
448  ostringstream ostr;
449  ostr << copy << endl;
450  iApp->GetUserInterface().TheGui()->Log(ostr.str().c_str());
451  iApp->GetUserInterface().TheGui()->ShowLog();
452  }
453  }
454 
460  void Application::GuiLog(const QString &results) {
461  // See if we should write the info to our parents gui
462  if (HasParent()) {
463  iApp->SendParentData("GUILOG", results);
464  }
465 
466  // Otherwise see if we need to write to our gui
467  else if (iApp->GetUserInterface().IsInteractive()) {
468  iApp->GetUserInterface().TheGui()->Log(results);
469  iApp->GetUserInterface().TheGui()->ShowLog();
470  }
471  }
472 
478  Isis::UserInterface &Application::GetUserInterface() {
479  return *iApp->p_ui;
480  }
481 
487  bool Application::HasParent() {
488  if (iApp == NULL) return false;
489  if (iApp->p_ui == NULL) return false;
490  if (iApp->p_ui->ParentId() == 0) return false;
491  return true;
492  }
493 
499  void Application::SendParentErrors(Isis::PvlObject &errors) {
500  if (!HasParent()) return;
501 
502  for (int i = 0; i < errors.groups(); i++) {
503  ostringstream ostr;
504  ostr << errors.group(i) << endl;
505  QString data = ostr.str().c_str();
506  iApp->SendParentData("ERROR", data);
507  }
508  }
509 
510 
515  void Application::SendParentData(const QString code,
516  const QString &message) {
517  // See if we need to connect to the parent
518  if (p_connectionToParent == NULL) {
519  QString msg = "This process (program) was executed by an existing Isis 3 "
520  "process. However, we failed to establish a communication channel "
521  "with the parent (launcher) process. The parent process has a PID of "
522  "[" + toString(iApp->GetUserInterface().ParentId()) + "]";
523  throw IException(IException::Unknown, msg, _FILEINFO_);
524  }
525 
526  // Have connection so build data QString and send it
527  QString data = code;
528  data += char(27);
529  data += message;
530  data += char(27);
531  data += '\n';
532 
533  if (p_connectionToParent->write(data.toLatin1().data(), data.toLatin1().size()) == -1) {
534  QString msg = "This process (program) was executed by an exiting Isis 3 "
535  "process. A communication channel was established with the parent "
536  "(launcher) process, but when we tried to send data to the parent "
537  "process an error occurred. The parent process has a PID of [" +
538  QString(iApp->GetUserInterface().ParentId()) + "]";
539  throw IException(IException::Unknown, msg, _FILEINFO_);
540  }
541 
542  p_connectionToParent->waitForBytesWritten(-1);
543  }
544 
545 
551  void Application::FunctionCleanup() {
552 
553  SessionLog::TheLog().Write();
554 
555  if (SessionLog::TheLog().TerminalOutput()) {
556  if (HasParent()) {
557  ostringstream ostr;
558  ostr << SessionLog::TheLog() << endl;
559  QString data = ostr.str().c_str();
560  iApp->SendParentData("LOG", data);
561  }
562  else if (p_ui->IsInteractive()) {
563  ostringstream ostr;
564  ostr << SessionLog::TheLog() << endl;
565  p_ui->TheGui()->Log(ostr.str().c_str());
566  p_ui->TheGui()->ShowLog();
567  }
568  else {
569  cout << SessionLog::TheLog() << endl;
570  }
571  }
572 
573  // If debugging flag on write debugging log
574  if (p_ui->GetInfoFlag()) {
575  QString filename = p_ui->GetInfoFileName();
576  Pvl log;
577  QString app = (QString)QCoreApplication::applicationDirPath() + "/" + p_appName;
578  if (p_BatchlistPass == 0) {
579  stringstream ss ;
580  ss << SessionLog::TheLog();
581  ss.clear();
582  ss >> log;
583  PvlGroup uname = GetUnameInfo();
584  PvlGroup env = GetEnviromentInfo();
585  log.addGroup(uname);
586  log.addGroup(env);
587  }
588 
589  // Write to file
590  if (filename.compare("") != 0) {
591 
592  if (p_BatchlistPass == 0) {
593  ofstream debugingLog(filename.toLatin1().data());
594  if (!debugingLog.good()) {
595  QString msg = "Error opening debugging log file [" + filename + "]";
596  throw IException(IException::Io, msg, _FILEINFO_);
597  }
598  debugingLog << log << endl;
599  debugingLog << "\n############### User Preferences ################\n" << endl;
600  debugingLog << Preference::Preferences();
601  debugingLog << "\n############## System Disk Space ################\n" << endl;
602  debugingLog << GetSystemDiskSpace() << endl;
603  debugingLog << "\n############ Executable Information #############\n" << endl;
604  debugingLog << GetLibraryDependencies(app) << endl;
605  debugingLog.close();
606  }
607  else {
608  ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
609  debugingLog << SessionLog::TheLog() << endl;
610  debugingLog.close();
611  }
612  }
613  else { // Write to std out
614  if (p_BatchlistPass == 0) {
615  cout << log << endl;
616  cout << "\n############### User Preferences ################\n" << endl;
617  cout << Preference::Preferences();
618  cout << "\n############## System Disk Space ################\n" << endl;
619  cout << GetSystemDiskSpace() << endl;
620  cout << "\n############ Executable Information #############\n" << endl;
621  cout << GetLibraryDependencies(app) << endl;
622  }
623  else {
624  cout << SessionLog::TheLog() << endl;
625  }
626  }
627  }
628 
629 
630  }
631 
641  int Application::FunctionError(IException &e) {
642  Pvl errors = e.toPvl();
643  SessionLog::TheLog().AddError(errors);
644  SessionLog::TheLog().Write();
645 
646  if (HasParent()) {
647  SendParentErrors(errors);
648  }
649  else if (p_ui->IsInteractive()) {
650  p_ui->TheGui()->LoadMessage(e.toString());
651  }
652  else if (SessionLog::TheLog().TerminalOutput()) {
653  cerr << SessionLog::TheLog() << endl;
654  }
655  else {
656  cerr << e.toString() << endl;
657  }
658 
659  // If debugging flag on write debugging log
660  if (p_ui->GetInfoFlag()) {
661  QString filename = p_ui->GetInfoFileName();
662  Pvl log;
663  QString app = (QString)QCoreApplication::applicationDirPath() + "/" + p_appName;
664  if (p_BatchlistPass == 0) {
665  stringstream ss ;
666  ss << SessionLog::TheLog();
667  ss.clear();
668  ss >> log;
669  PvlGroup uname = GetUnameInfo();
670  PvlGroup env = GetEnviromentInfo();
671  log.addGroup(uname);
672  log.addGroup(env);
673  }
674 
675  // Write to file
676  if (filename.compare("") != 0) {
677  if (p_BatchlistPass == 0) {
678  ofstream debugingLog(filename.toLatin1().data());
679  if (!debugingLog.good()) {
680  QString msg = "Error opening debugging log file [" + filename + "]";
681  throw IException(IException::Io, msg, _FILEINFO_);
682  }
683  debugingLog << log << endl;
684  debugingLog << "\n############### User Preferences ################\n" << endl;
685  debugingLog << Preference::Preferences();
686  debugingLog << "\n############ System Disk Space #############\n" << endl;
687  debugingLog << GetSystemDiskSpace() << endl;
688  debugingLog << "\n############ Executable Information #############\n" << endl;
689  debugingLog << GetLibraryDependencies(app) << endl;
690  debugingLog.close();
691  }
692  else {
693  ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
694  debugingLog << SessionLog::TheLog() << endl;
695  debugingLog.close();
696  }
697  }
698  else { // Write to std out
699  if (p_BatchlistPass == 0) {
700  cout << log << endl;
701  cout << "\n############### User Preferences ################\n" << endl;
702  cout << Preference::Preferences();
703  cout << "\n############ System Disk Space #############\n" << endl;
704  cout << GetSystemDiskSpace() << endl;
705  cout << "\n############ Executable Information #############\n" << endl;
706  cout << GetLibraryDependencies(app) << endl;
707  }
708  else {
709  cout << SessionLog::TheLog() << endl;
710  }
711  }
712  }
713 
714  return (int)e.errorType();
715  }
716 
723  void Application::GuiReportError(IException &e) {
724  QString errorMessage = e.toString();
725  if (errorMessage == "") {
726  p_ui->TheGui()->ProgressText("Stopped");
727  }
728  else {
729  p_ui->TheGui()->LoadMessage(errorMessage);
730  p_ui->TheGui()->ProgressText("Error");
731  }
732 
733  if (p_ui->TheGui()->ShowWarning())
734  exit(0);
735  }
736 
737  QString Application::p_appName("Unknown");
738 
744  QString Application::Name() {
745  return p_appName;
746  }
747 
755  void Application::UpdateProgress(const QString &text, bool print) {
756  if (HasParent() && print) {
757  iApp->SendParentData(QString("PROGRESSTEXT"), text);
758  }
759  else if (p_ui->IsInteractive()) {
760  p_ui->TheGui()->ProgressText(text);
761  }
762  else if (print) {
763  QString msg = p_ui->ProgramName() + ": " + text;
764  cout << msg << endl;
765  }
766 
767  ProcessGuiEvents();
768  }
769 
777  void Application::UpdateProgress(int percent, bool print) {
778  if (HasParent() && print) {
779  QString data = toString(percent);
780  iApp->SendParentData(QString("PROGRESS"), data);
781  }
782  else if (p_ui->IsInteractive()) {
783  p_ui->TheGui()->Progress(percent);
784  }
785  else if (print) {
786  if (percent < 100) {
787  cout << percent << "% Processed\r" << flush;
788  }
789  else {
790  cout << percent << "% Processed" << endl;
791  }
792  }
793  }
794 
801  void Application::ProcessGuiEvents() {
802  if (p_ui->IsInteractive()) {
803  if (p_ui->TheGui()->ProcessEvents()) {
804  throw IException();
805  }
806  }
807  }
808 
809 
817  QString Application::DateTime(time_t *curtime) {
818  time_t startTime = time(NULL);
819  if (curtime != 0) *curtime = startTime;
820  struct tm *tmbuf = localtime(&startTime);
821  char timestr[80];
822  strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
823  return(QString) timestr;
824  }
825 
831  QString Application::UserName() {
832  return userName();
833  }
834 
840  QString Application::HostName() {
841  return hostName();
842  }
843 
849  QString Application::Version() {
850  return isisVersion();
851  }
852 
853 
859  PvlGroup Application::GetUnameInfo() {
860  // Create a temporary file to store console output to
861  FileName temp = FileName::createTempFile("$temporary/UnameConsoleInfo.txt");
862  QString tempFile = temp.expanded();
863 
864  // Uname commands output to temp file with each of the following
865  // values on its own line in this order:
866  // machine hardware name, processor, hardware platform name,
867  // operating system, kernel name, kernel version, kernel release, all
868  PvlGroup unameGroup("UNAME");
869  ifstream readTemp;
870 
871 #if defined(__linux__)
872  // Write uname outputs to temp file
873  ProgramLauncher::RunSystemCommand("uname -m > " + tempFile);
874  ProgramLauncher::RunSystemCommand("uname -p > " + tempFile);
875  ProgramLauncher::RunSystemCommand("uname -i > " + tempFile);
876  ProgramLauncher::RunSystemCommand("uname -o > " + tempFile);
877  ProgramLauncher::RunSystemCommand("uname -s > " + tempFile);
878  ProgramLauncher::RunSystemCommand("uname -v > " + tempFile);
879  ProgramLauncher::RunSystemCommand("uname -r > " + tempFile);
880  ProgramLauncher::RunSystemCommand("uname -a > " + tempFile);
881  // Read data from temp file
882  char value[256];
883  readTemp.open(tempFile.toLatin1().data(), ifstream::in);
884  readTemp.getline(value, 256);
885  unameGroup.addKeyword(PvlKeyword("MachineHardware", value));
886  readTemp.getline(value, 256);
887  unameGroup.addKeyword(PvlKeyword("Processor", value));
888  readTemp.getline(value, 256);
889  unameGroup.addKeyword(PvlKeyword("HardwarePlatform", value));
890  readTemp.getline(value, 256);
891  unameGroup.addKeyword(PvlKeyword("OperatingSystem", value));
892  readTemp.getline(value, 256);
893  unameGroup.addKeyword(PvlKeyword("KernelName", value));
894  readTemp.getline(value, 256);
895  unameGroup.addKeyword(PvlKeyword("KernelVersion", value));
896  readTemp.getline(value, 256);
897  unameGroup.addKeyword(PvlKeyword("KernelRelease", value));
898  readTemp.getline(value, 256);
899  unameGroup.addKeyword(PvlKeyword("FullUnameString", value));
900 
901 #elif defined(__APPLE__)
902  // Write uname outputs to temp file
903  ProgramLauncher::RunSystemCommand("uname -m > " + tempFile);
904  ProgramLauncher::RunSystemCommand("uname -p > " + tempFile);
905  ProgramLauncher::RunSystemCommand("uname -s > " + tempFile);
906  ProgramLauncher::RunSystemCommand("uname -v > " + tempFile);
907  ProgramLauncher::RunSystemCommand("uname -r > " + tempFile);
908  ProgramLauncher::RunSystemCommand("uname -a > " + tempFile);
909 
910  // Read data from temp file
911  char value[256];
912  readTemp.open(tempFile.toLatin1().data(), ifstream::in);
913  readTemp.getline(value, 256);
914  unameGroup.addKeyword(PvlKeyword("MachineHardware", value));
915  readTemp.getline(value, 256);
916  unameGroup.addKeyword(PvlKeyword("Processor", value));
917  readTemp.getline(value, 256);
918  unameGroup.addKeyword(PvlKeyword("OperatingSystem", value));
919  readTemp.getline(value, 256);
920  unameGroup.addKeyword(PvlKeyword("OperatingSystemVersion", value));
921  readTemp.getline(value, 256);
922  unameGroup.addKeyword(PvlKeyword("OperatingSystemRelease", value));
923  readTemp.getline(value, 256);
924  unameGroup.addKeyword(PvlKeyword("FullUnameString", value));
925 #endif
926 
927  // remove temp file and return
928  remove(tempFile.toLatin1().data());
929  return unameGroup;
930  }
931 
939  PvlGroup Application::GetEnviromentInfo() {
940  // Create a temporary file to store console output to
941  FileName temp = FileName::createTempFile("$temporary/EnviromentInfo.txt");
942  QString tempFile = temp.expanded();
943  PvlGroup envGroup("EnviromentVariables");
944  ifstream readTemp;
945 
946  QString env1 = "printenv SHELL >| " + tempFile;
947  QString env2 = "printenv HOME >> " + tempFile;
948  QString env3 = "printenv PWD >> " + tempFile;
949  QString env5 = "printenv ISISROOT >> " + tempFile;
950  QString env6 = "printenv ISIS3DATA >> " + tempFile;
951  ProgramLauncher::RunSystemCommand(env1);
952  ProgramLauncher::RunSystemCommand(env2);
953  ProgramLauncher::RunSystemCommand(env3);
954  ProgramLauncher::RunSystemCommand(env5);
955  ProgramLauncher::RunSystemCommand(env6);
956  // Read data from temp file
957  char value[511];
958  readTemp.open(tempFile.toLatin1().data(), ifstream::in);
959  readTemp.getline(value, 255);
960  envGroup.addKeyword(PvlKeyword("Shell", value));
961  readTemp.getline(value, 255);
962  envGroup.addKeyword(PvlKeyword("Home", value));
963  readTemp.getline(value, 255);
964  envGroup.addKeyword(PvlKeyword("Pwd", value));
965  readTemp.getline(value, 255);
966  envGroup.addKeyword(PvlKeyword("ISISROOT", value));
967  readTemp.getline(value, 255);
968  envGroup.addKeyword(PvlKeyword("ISIS3DATA", value));
969 
970  // remove temp file and return
971  QString cleanup = "rm -f " + tempFile;
972  ProgramLauncher::RunSystemCommand(cleanup);
973  return envGroup;
974  }
975 
981  QString Application::GetSystemDiskSpace() {
982  FileName temp = FileName::createTempFile("$temporary/SystemDiskSpace.txt");
983  QString tempFile = temp.expanded();
984  ifstream readTemp;
985  QString diskspace = "df >| " + tempFile;
986  ProgramLauncher::RunSystemCommand(diskspace);
987  readTemp.open(tempFile.toLatin1().data(), ifstream::in);
988 
989  QString results = "";
990  char tmp[512];
991  while (!readTemp.eof()) {
992  readTemp.getline(tmp, 512);
993  results += tmp;
994  results += "\n";
995  }
996 
997  // remove temp file and return
998  QString cleanup = "rm -f " + tempFile;
999  ProgramLauncher::RunSystemCommand(cleanup);
1000  return results;
1001  }
1002 
1008  QString Application::GetLibraryDependencies(QString file) {
1009  FileName temp = FileName::createTempFile("$temporary/LibraryDependencies.txt");
1010  QString tempFile = temp.expanded();
1011  ifstream readTemp;
1012  QString dependencies = "";
1013 #if defined(__linux__)
1014  dependencies = "ldd -v " + file + " >| " + tempFile;
1015 #elif defined(__APPLE__)
1016  dependencies = "otool -L " + file + " >| " + tempFile;
1017 #elif defined (__sun__)
1018  dependencies = "ldd -v " + file + " >| " + tempFile;
1019 #endif
1020  ProgramLauncher::RunSystemCommand(dependencies);
1021  readTemp.open(tempFile.toLatin1().data(), ifstream::in);
1022 
1023  QString results = "";
1024  char tmp[512];
1025  while (!readTemp.eof()) {
1026  readTemp.getline(tmp, 512);
1027  results += tmp;
1028  results += "\n";
1029  }
1030 
1031  // remove temp file and return
1032  QString cleanup = "rm -f " + tempFile;
1033  ProgramLauncher::RunSystemCommand(cleanup);
1034  return results;
1035  }
1036 } //end namespace isis
Pvl toPvl() const
Returns a PVL object representing the contents of this exception.
Definition: IException.cpp:491
static UserInterface & GetUserInterface()
Returns the UserInterface object.
File name manipulation and expansion.
Definition: FileName.h:111
bool IsInteractive()
Indicates if the Isis Graphical User Interface is operating.
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:198
ErrorType errorType() const
Returns the source of the error for this exception.
Definition: IException.cpp:446
UserInterface * p_ui
Pointer to a User Interface object.
Definition: Application.h:171
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
void SendParentData(QString, const QString &)
void Log(const QString &text)
Write text to the gui log.
Definition: Gui.cpp:589
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:38
void print() const
Prints a string representation of this exception to stderr.
Definition: IException.cpp:461
int ParentId()
Returns the parent id.
A single keyword-value pair.
Definition: PvlKeyword.h:98
Container for cube-like labels.
Definition: Pvl.h:135
int groups() const
Returns the number of groups contained.
Definition: PvlObject.h:87
PvlGroup & group(const int index)
Return the group at the specified index.
Definition: PvlObject.cpp:423
QString toString() const
Returns a string representation of this exception.
Definition: IException.cpp:553
Isis exception class.
Definition: IException.h:99
QString FileOpen(const QString &filename)
This error should be used when a file could not be opened.
Definition: FileOpen.cpp:28
Command Line and Xml loader, validation, and access.
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:14:25