23 #include "IsisDebug.h"
41 #include <QApplication>
42 #include <QCoreApplication>
43 #include <QLocalSocket>
48 #include "CubeManager.h"
66 Application *iApp = NULL;
67 bool Application::p_applicationForceGuiApp =
false;
79 Application::Application(
int &argc,
char *argv[]) {
81 p_connectionToParent = NULL;
92 p_startPageFaults = 0;
93 p_startProcessSwaps = 0;
98 setlocale(LC_ALL,
"en_US");
101 strncpy(env,
"LANG=en_US", 1023);
105 if (getenv(
"ISISROOT") == NULL || QString(getenv(
"ISISROOT")) ==
"") {
106 QString message =
"Please set ISISROOT before running any Isis "
108 cerr << message << endl;
114 p_startDirectIO = DirectIO();
115 p_startPageFaults = PageFaults();
116 p_startProcessSwaps = ProcessSwaps();
120 FileName f(QString(argv[0]) +
".xml");
123 Preference::Preferences(f.name() ==
"unitTest.xml");
125 if (!f.fileExists()) {
126 f =
"$ISISROOT/bin/xml/" + f.name();
127 if (!f.fileExists()) {
132 QString xmlfile = f.expanded();
136 if (!p_ui->IsInteractive()) {
138 p_datetime = DateTime(&p_startTime);
139 p_startClock = clock();
141 if (p_applicationForceGuiApp) {
145 setlocale(LC_ALL,
"en_US");
148 new QCoreApplication(argc, argv);
151 setlocale(LC_ALL,
"en_US");
154 QCoreApplication::setApplicationName(
FileName(p_appName).baseName());
165 if (GetUserInterface().ParentId()) {
166 p_connectionToParent =
new QLocalSocket;
168 QString serverName =
"isis_" + UserName() +
171 p_connectionToParent->connectToServer(serverName);
172 if (!p_connectionToParent->waitForConnected()) {
173 delete p_connectionToParent;
174 p_connectionToParent = NULL;
180 Application::~Application() {
194 int Application::Run(
void (*funct)()) {
197 if (p_ui->IsInteractive()) {
198 p_ui->TheGui()->Exec(funct);
201 if (p_ui->BatchListSize() > 0) {
202 for (
int i = 0; i < p_ui->BatchListSize(); i++) {
204 p_ui->SetBatchList(i);
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);
216 Application::FunctionCleanup();
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);
238 Application::FunctionCleanup();
243 status = Application::FunctionError(e);
247 catch (exception &e) {
248 QString message = e.what();
249 Isis::iExceptionSystem i(message,
_FILEINFO_);
253 QString message =
"Unknown error expection";
254 Isis::iExceptionSystem i(message,
_FILEINFO_);
268 if (p_ui->IsInteractive()) {
269 p_startClock = clock();
270 p_datetime = DateTime(&p_startTime);
274 history +=
PvlKeyword(
"IsisVersion", Version());
275 history +=
PvlKeyword(
"ProgramVersion", p_ui->Version());
276 QString path = QCoreApplication::applicationDirPath();
278 history +=
PvlKeyword(
"ExecutionDateTime", p_datetime);
279 history +=
PvlKeyword(
"HostName", HostName());
280 history +=
PvlKeyword(
"UserName", UserName());
281 history +=
PvlKeyword(
"Description", p_ui->Brief());
285 p_ui->CommandLine(pvl);
286 history.
addGroup(pvl.findGroup(
"UserParameters"));
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;
305 sprintf(temp,
"%02d:%02d:%04.1f", hours, minutes, seconds);
306 QString conTime = temp;
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;
343 int Application::DirectIO() {
344 return 0 - p_startDirectIO;
352 int Application::PageFaults() {
353 return 0 - p_startPageFaults;
361 int Application::ProcessSwaps() {
362 return 0 - p_startProcessSwaps;
372 static bool blankLine =
false;
374 SessionLog::TheLog().AddResults(results);
377 if (SessionLog::TheLog().TerminalOutput())
return;
382 if (blankLine) ostr << endl;
383 ostr << results << endl;
384 QString data = ostr.str().c_str();
391 if (blankLine) ostr << endl;
392 ostr << results << endl;
399 if (blankLine) cout << endl;
400 cout << results << endl;
410 void Application::GuiLog(
const Pvl &results) {
416 ostr << copy << endl;
417 QString data = ostr.str().c_str();
424 ostr << copy << endl;
435 void Application::GuiLog(
const PvlGroup &results) {
441 ostr << copy << endl;
442 QString data = ostr.str().c_str();
449 ostr << copy << endl;
460 void Application::GuiLog(
const QString &results) {
487 bool Application::HasParent() {
488 if (iApp == NULL)
return false;
489 if (iApp->
p_ui == NULL)
return false;
500 if (!HasParent())
return;
502 for (
int i = 0; i < errors.
groups(); i++) {
504 ostr << errors.
group(i) << endl;
505 QString data = ostr.str().c_str();
515 void Application::SendParentData(
const QString code,
516 const QString &message) {
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 "
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 [" +
542 p_connectionToParent->waitForBytesWritten(-1);
551 void Application::FunctionCleanup() {
553 SessionLog::TheLog().Write();
555 if (SessionLog::TheLog().TerminalOutput()) {
558 ostr << SessionLog::TheLog() << endl;
559 QString data = ostr.str().c_str();
562 else if (p_ui->IsInteractive()) {
564 ostr << SessionLog::TheLog() << endl;
565 p_ui->TheGui()->Log(ostr.str().c_str());
566 p_ui->TheGui()->ShowLog();
569 cout << SessionLog::TheLog() << endl;
574 if (p_ui->GetInfoFlag()) {
575 QString filename = p_ui->GetInfoFileName();
577 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
578 if (p_BatchlistPass == 0) {
580 ss << SessionLog::TheLog();
590 if (filename.compare(
"") != 0) {
592 if (p_BatchlistPass == 0) {
593 ofstream debugingLog(filename.toLatin1().data());
594 if (!debugingLog.good()) {
595 QString msg =
"Error opening debugging log file [" + filename +
"]";
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;
608 ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
609 debugingLog << SessionLog::TheLog() << endl;
614 if (p_BatchlistPass == 0) {
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;
624 cout << SessionLog::TheLog() << endl;
643 SessionLog::TheLog().AddError(errors);
644 SessionLog::TheLog().Write();
647 SendParentErrors(errors);
649 else if (p_ui->IsInteractive()) {
650 p_ui->TheGui()->LoadMessage(e.
toString());
652 else if (SessionLog::TheLog().TerminalOutput()) {
653 cerr << SessionLog::TheLog() << endl;
660 if (p_ui->GetInfoFlag()) {
661 QString filename = p_ui->GetInfoFileName();
663 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
664 if (p_BatchlistPass == 0) {
666 ss << SessionLog::TheLog();
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 +
"]";
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;
693 ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
694 debugingLog << SessionLog::TheLog() << endl;
699 if (p_BatchlistPass == 0) {
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;
709 cout << SessionLog::TheLog() << endl;
724 QString errorMessage = e.
toString();
725 if (errorMessage ==
"") {
726 p_ui->TheGui()->ProgressText(
"Stopped");
729 p_ui->TheGui()->LoadMessage(errorMessage);
730 p_ui->TheGui()->ProgressText(
"Error");
733 if (p_ui->TheGui()->ShowWarning())
737 QString Application::p_appName(
"Unknown");
744 QString Application::Name() {
755 void Application::UpdateProgress(
const QString &text,
bool print) {
756 if (HasParent() && print) {
759 else if (p_ui->IsInteractive()) {
760 p_ui->TheGui()->ProgressText(text);
763 QString msg = p_ui->ProgramName() +
": " + text;
777 void Application::UpdateProgress(
int percent,
bool print) {
778 if (HasParent() && print) {
782 else if (p_ui->IsInteractive()) {
783 p_ui->TheGui()->Progress(percent);
787 cout << percent <<
"% Processed\r" << flush;
790 cout << percent <<
"% Processed" << endl;
801 void Application::ProcessGuiEvents() {
802 if (p_ui->IsInteractive()) {
803 if (p_ui->TheGui()->ProcessEvents()) {
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);
822 strftime(timestr, 80,
"%Y-%m-%dT%H:%M:%S", tmbuf);
823 return(QString) timestr;
831 QString Application::UserName() {
840 QString Application::HostName() {
849 QString Application::Version() {
850 return isisVersion();
861 FileName temp = FileName::createTempFile(
"$temporary/UnameConsoleInfo.txt");
862 QString tempFile = temp.expanded();
871 #if defined(__linux__)
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);
883 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
884 readTemp.getline(value, 256);
886 readTemp.getline(value, 256);
888 readTemp.getline(value, 256);
890 readTemp.getline(value, 256);
892 readTemp.getline(value, 256);
894 readTemp.getline(value, 256);
896 readTemp.getline(value, 256);
898 readTemp.getline(value, 256);
901 #elif defined(__APPLE__)
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);
912 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
913 readTemp.getline(value, 256);
915 readTemp.getline(value, 256);
917 readTemp.getline(value, 256);
919 readTemp.getline(value, 256);
921 readTemp.getline(value, 256);
923 readTemp.getline(value, 256);
928 remove(tempFile.toLatin1().data());
941 FileName temp = FileName::createTempFile(
"$temporary/EnviromentInfo.txt");
942 QString tempFile = temp.expanded();
943 PvlGroup envGroup(
"EnviromentVariables");
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);
958 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
959 readTemp.getline(value, 255);
961 readTemp.getline(value, 255);
963 readTemp.getline(value, 255);
965 readTemp.getline(value, 255);
967 readTemp.getline(value, 255);
971 QString cleanup =
"rm -f " + tempFile;
972 ProgramLauncher::RunSystemCommand(cleanup);
981 QString Application::GetSystemDiskSpace() {
982 FileName temp = FileName::createTempFile(
"$temporary/SystemDiskSpace.txt");
983 QString tempFile = temp.expanded();
985 QString diskspace =
"df >| " + tempFile;
986 ProgramLauncher::RunSystemCommand(diskspace);
987 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
989 QString results =
"";
991 while (!readTemp.eof()) {
992 readTemp.getline(tmp, 512);
998 QString cleanup =
"rm -f " + tempFile;
999 ProgramLauncher::RunSystemCommand(cleanup);
1008 QString Application::GetLibraryDependencies(QString file) {
1009 FileName temp = FileName::createTempFile(
"$temporary/LibraryDependencies.txt");
1010 QString tempFile = temp.expanded();
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;
1020 ProgramLauncher::RunSystemCommand(dependencies);
1021 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
1023 QString results =
"";
1025 while (!readTemp.eof()) {
1026 readTemp.getline(tmp, 512);
1032 QString cleanup =
"rm -f " + tempFile;
1033 ProgramLauncher::RunSystemCommand(cleanup);
Pvl toPvl() const
Returns a PVL object representing the contents of this exception.
static UserInterface & GetUserInterface()
Returns the UserInterface object.
File name manipulation and expansion.
bool IsInteractive()
Indicates if the Isis Graphical User Interface is operating.
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
ErrorType errorType() const
Returns the source of the error for this exception.
UserInterface * p_ui
Pointer to a User Interface object.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
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.
Contains multiple PvlContainers.
#define _FILEINFO_
Macro for the filename and line number.
void print() const
Prints a string representation of this exception to stderr.
int ParentId()
Returns the parent id.
A single keyword-value pair.
Container for cube-like labels.
int groups() const
Returns the number of groups contained.
PvlGroup & group(const int index)
Return the group at the specified index.
QString toString() const
Returns a string representation of this exception.
QString FileOpen(const QString &filename)
This error should be used when a file could not be opened.
Command Line and Xml loader, validation, and access.
Contains Pvl Groups and Pvl Objects.