23 #include "IsisDebug.h"
41 #include <QApplication>
42 #include <QLocalSocket>
47 #include "CubeManager.h"
65 Application *iApp = NULL;
66 bool Application::p_applicationForceGuiApp =
false;
78 Application::Application(
int &argc,
char *argv[]) {
80 p_connectionToParent = NULL;
91 p_startPageFaults = 0;
92 p_startProcessSwaps = 0;
97 setlocale(LC_ALL,
"en_US");
100 strncpy(env,
"LANG=en_US", 1023);
104 if (getenv(
"ISISROOT") == NULL || QString(getenv(
"ISISROOT")) ==
"") {
105 QString message =
"Please set ISISROOT before running any Isis "
107 cerr << message << endl;
113 p_startDirectIO = DirectIO();
114 p_startPageFaults = PageFaults();
115 p_startProcessSwaps = ProcessSwaps();
119 FileName f(QString(argv[0]) +
".xml");
122 Preference::Preferences(f.name() ==
"unitTest.xml");
124 if (!f.fileExists()) {
125 f =
"$ISISROOT/bin/xml/" + f.name();
126 if (!f.fileExists()) {
131 QString xmlfile = f.expanded();
134 if (!p_ui->IsInteractive()) {
136 p_datetime = DateTime(&p_startTime);
137 p_startClock = clock();
139 if (p_applicationForceGuiApp) {
143 new QCoreApplication(argc, argv);
146 QCoreApplication::setApplicationName(
FileName(p_appName).baseName());
157 if (GetUserInterface().ParentId()) {
158 p_connectionToParent =
new QLocalSocket;
160 QString serverName =
"isis_" + UserName() +
163 p_connectionToParent->connectToServer(serverName);
164 if (!p_connectionToParent->waitForConnected()) {
165 delete p_connectionToParent;
166 p_connectionToParent = NULL;
172 Application::~Application() {
185 int Application::Run(
void (*funct)()) {
188 if (p_ui->IsInteractive()) {
189 p_ui->TheGui()->Exec(funct);
192 if (p_ui->BatchListSize() > 0) {
193 for (
int i = 0; i < p_ui->BatchListSize(); i++) {
195 p_ui->SetBatchList(i);
198 p_datetime = DateTime(&p_startTime);
199 p_startClock = clock();
200 p_startDirectIO = DirectIO();
201 p_startPageFaults = PageFaults();
202 p_startProcessSwaps = ProcessSwaps();
203 SessionLog::TheLog(
true);
207 Application::FunctionCleanup();
211 p_ui->SetErrorList(i);
212 status = Application::FunctionError(e);
213 if (p_ui->AbortOnError()) {
214 for (
int j = (i + 1); j < p_ui->BatchListSize(); j++) {
215 p_ui->SetErrorList(j);
229 Application::FunctionCleanup();
234 status = Application::FunctionError(e);
238 catch (exception &e) {
239 QString message = e.what();
240 Isis::iExceptionSystem i(message,
_FILEINFO_);
244 QString message =
"Unknown error expection";
245 Isis::iExceptionSystem i(message,
_FILEINFO_);
259 if (p_ui->IsInteractive()) {
260 p_startClock = clock();
261 p_datetime = DateTime(&p_startTime);
265 history +=
PvlKeyword(
"IsisVersion", Version());
266 history +=
PvlKeyword(
"ProgramVersion", p_ui->Version());
267 QString path = QCoreApplication::applicationDirPath();
269 history +=
PvlKeyword(
"ExecutionDateTime", p_datetime);
270 history +=
PvlKeyword(
"HostName", HostName());
271 history +=
PvlKeyword(
"UserName", UserName());
272 history +=
PvlKeyword(
"Description", p_ui->Brief());
276 p_ui->CommandLine(pvl);
277 history.
addGroup(pvl.findGroup(
"UserParameters"));
289 time_t endTime = time(NULL);
290 double seconds = difftime(endTime, p_startTime);
291 int minutes = (int)(seconds / 60.0);
292 seconds = seconds - minutes * 60.0;
293 int hours = minutes / 60;
294 minutes = minutes - hours * 60;
296 sprintf(temp,
"%02d:%02d:%04.1f", hours, minutes, seconds);
297 QString conTime = temp;
302 clock_t endClock = clock();
303 seconds = (double(endClock) - (double)p_startClock) / CLOCKS_PER_SEC;
304 minutes = (int)(seconds / 60.0);
305 seconds = seconds - minutes * 60.0;
306 hours = minutes / 60;
307 minutes = minutes - hours * 60;
308 sprintf(temp,
"%02d:%02d:%04.1f", hours, minutes, seconds);
309 QString cpuTime = temp;
334 int Application::DirectIO() {
335 return 0 - p_startDirectIO;
343 int Application::PageFaults() {
344 return 0 - p_startPageFaults;
352 int Application::ProcessSwaps() {
353 return 0 - p_startProcessSwaps;
363 static bool blankLine =
false;
365 SessionLog::TheLog().AddResults(results);
368 if (SessionLog::TheLog().TerminalOutput())
return;
373 if (blankLine) ostr << endl;
374 ostr << results << endl;
375 QString data = ostr.str().c_str();
382 if (blankLine) ostr << endl;
383 ostr << results << endl;
390 if (blankLine) cout << endl;
391 cout << results << endl;
401 void Application::GuiLog(
const Pvl &results) {
407 ostr << copy << endl;
408 QString data = ostr.str().c_str();
415 ostr << copy << endl;
426 void Application::GuiLog(
const PvlGroup &results) {
432 ostr << copy << endl;
433 QString data = ostr.str().c_str();
440 ostr << copy << endl;
451 void Application::GuiLog(
const QString &results) {
478 bool Application::HasParent() {
479 if (iApp == NULL)
return false;
480 if (iApp->
p_ui == NULL)
return false;
491 if (!HasParent())
return;
493 for (
int i = 0; i < errors.
groups(); i++) {
495 ostr << errors.
group(i) << endl;
496 QString data = ostr.str().c_str();
506 void Application::SendParentData(
const QString code,
507 const QString &message) {
509 if (p_connectionToParent == NULL) {
510 QString msg =
"This process (program) was executed by an existing Isis 3 "
511 "process. However, we failed to establish a communication channel "
512 "with the parent (launcher) process. The parent process has a PID of "
524 if (p_connectionToParent->write(data.toAscii().data(), data.toAscii().size()) == -1) {
525 QString msg =
"This process (program) was executed by an exiting Isis 3 "
526 "process. A communication channel was established with the parent "
527 "(launcher) process, but when we tried to send data to the parent "
528 "process an error occurred. The parent process has a PID of [" +
533 p_connectionToParent->waitForBytesWritten(-1);
542 void Application::FunctionCleanup() {
543 CubeManager::CleanUp();
545 SessionLog::TheLog().Write();
547 if (SessionLog::TheLog().TerminalOutput()) {
550 ostr << SessionLog::TheLog() << endl;
551 QString data = ostr.str().c_str();
554 else if (p_ui->IsInteractive()) {
556 ostr << SessionLog::TheLog() << endl;
557 p_ui->TheGui()->Log(ostr.str().c_str());
558 p_ui->TheGui()->ShowLog();
561 cout << SessionLog::TheLog() << endl;
566 if (p_ui->GetInfoFlag()) {
567 QString filename = p_ui->GetInfoFileName();
569 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
570 if (p_BatchlistPass == 0) {
572 ss << SessionLog::TheLog();
582 if (filename.compare(
"") != 0) {
584 if (p_BatchlistPass == 0) {
585 ofstream debugingLog(filename.toAscii().data());
586 if (!debugingLog.good()) {
587 QString msg =
"Error opening debugging log file [" + filename +
"]";
590 debugingLog << log << endl;
591 debugingLog <<
"\n############### User Preferences ################\n" << endl;
592 debugingLog << Preference::Preferences();
593 debugingLog <<
"\n############## System Disk Space ################\n" << endl;
594 debugingLog << GetSystemDiskSpace() << endl;
595 debugingLog <<
"\n############ Executable Information #############\n" << endl;
596 debugingLog << GetLibraryDependencies(app) << endl;
600 ofstream debugingLog(filename.toAscii().data(), ios_base::app);
601 debugingLog << SessionLog::TheLog() << endl;
606 if (p_BatchlistPass == 0) {
608 cout <<
"\n############### User Preferences ################\n" << endl;
609 cout << Preference::Preferences();
610 cout <<
"\n############## System Disk Space ################\n" << endl;
611 cout << GetSystemDiskSpace() << endl;
612 cout <<
"\n############ Executable Information #############\n" << endl;
613 cout << GetLibraryDependencies(app) << endl;
616 cout << SessionLog::TheLog() << endl;
635 SessionLog::TheLog().AddError(errors);
636 SessionLog::TheLog().Write();
639 SendParentErrors(errors);
641 else if (p_ui->IsInteractive()) {
642 p_ui->TheGui()->LoadMessage(e.
toString());
644 else if (SessionLog::TheLog().TerminalOutput()) {
645 cerr << SessionLog::TheLog() << endl;
652 if (p_ui->GetInfoFlag()) {
653 QString filename = p_ui->GetInfoFileName();
655 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
656 if (p_BatchlistPass == 0) {
658 ss << SessionLog::TheLog();
668 if (filename.compare(
"") != 0) {
669 if (p_BatchlistPass == 0) {
670 ofstream debugingLog(filename.toAscii().data());
671 if (!debugingLog.good()) {
672 QString msg =
"Error opening debugging log file [" + filename +
"]";
675 debugingLog << log << endl;
676 debugingLog <<
"\n############### User Preferences ################\n" << endl;
677 debugingLog << Preference::Preferences();
678 debugingLog <<
"\n############ System Disk Space #############\n" << endl;
679 debugingLog << GetSystemDiskSpace() << endl;
680 debugingLog <<
"\n############ Executable Information #############\n" << endl;
681 debugingLog << GetLibraryDependencies(app) << endl;
685 ofstream debugingLog(filename.toAscii().data(), ios_base::app);
686 debugingLog << SessionLog::TheLog() << endl;
691 if (p_BatchlistPass == 0) {
693 cout <<
"\n############### User Preferences ################\n" << endl;
694 cout << Preference::Preferences();
695 cout <<
"\n############ System Disk Space #############\n" << endl;
696 cout << GetSystemDiskSpace() << endl;
697 cout <<
"\n############ Executable Information #############\n" << endl;
698 cout << GetLibraryDependencies(app) << endl;
701 cout << SessionLog::TheLog() << endl;
716 QString errorMessage = e.
toString();
717 if (errorMessage ==
"") {
718 p_ui->TheGui()->ProgressText(
"Stopped");
721 p_ui->TheGui()->LoadMessage(errorMessage);
722 p_ui->TheGui()->ProgressText(
"Error");
725 if (p_ui->TheGui()->ShowWarning())
729 QString Application::p_appName(
"Unknown");
736 QString Application::Name() {
747 void Application::UpdateProgress(
const QString &text,
bool print) {
748 if (HasParent() && print) {
751 else if (p_ui->IsInteractive()) {
752 p_ui->TheGui()->ProgressText(text);
755 QString msg = p_ui->ProgramName() +
": " + text;
769 void Application::UpdateProgress(
int percent,
bool print) {
770 if (HasParent() && print) {
774 else if (p_ui->IsInteractive()) {
775 p_ui->TheGui()->Progress(percent);
779 cout << percent <<
"% Processed\r" << flush;
782 cout << percent <<
"% Processed" << endl;
793 void Application::ProcessGuiEvents() {
794 if (p_ui->IsInteractive()) {
795 if (p_ui->TheGui()->ProcessEvents()) {
809 QString Application::DateTime(time_t *curtime) {
810 time_t startTime = time(NULL);
811 if (curtime != 0) *curtime = startTime;
812 struct tm *tmbuf = localtime(&startTime);
814 strftime(timestr, 80,
"%Y-%m-%dT%H:%M:%S", tmbuf);
815 return(QString) timestr;
823 QString Application::UserName() {
832 QString Application::HostName() {
841 QString Application::Version() {
842 return isisVersion();
853 FileName temp = FileName::createTempFile(
"$temporary/UnameConsoleInfo.txt");
854 QString tempFile = temp.expanded();
863 #if defined(__linux__)
865 ProgramLauncher::RunSystemCommand(
"uname -m > " + tempFile);
866 ProgramLauncher::RunSystemCommand(
"uname -p > " + tempFile);
867 ProgramLauncher::RunSystemCommand(
"uname -i > " + tempFile);
868 ProgramLauncher::RunSystemCommand(
"uname -o > " + tempFile);
869 ProgramLauncher::RunSystemCommand(
"uname -s > " + tempFile);
870 ProgramLauncher::RunSystemCommand(
"uname -v > " + tempFile);
871 ProgramLauncher::RunSystemCommand(
"uname -r > " + tempFile);
872 ProgramLauncher::RunSystemCommand(
"uname -a > " + tempFile);
875 readTemp.open(tempFile.toAscii().data(), ifstream::in);
876 readTemp.getline(value, 256);
878 readTemp.getline(value, 256);
880 readTemp.getline(value, 256);
882 readTemp.getline(value, 256);
884 readTemp.getline(value, 256);
886 readTemp.getline(value, 256);
888 readTemp.getline(value, 256);
890 readTemp.getline(value, 256);
893 #elif defined(__APPLE__)
895 ProgramLauncher::RunSystemCommand(
"uname -m > " + tempFile);
896 ProgramLauncher::RunSystemCommand(
"uname -p > " + tempFile);
897 ProgramLauncher::RunSystemCommand(
"uname -s > " + tempFile);
898 ProgramLauncher::RunSystemCommand(
"uname -v > " + tempFile);
899 ProgramLauncher::RunSystemCommand(
"uname -r > " + tempFile);
900 ProgramLauncher::RunSystemCommand(
"uname -a > " + tempFile);
904 readTemp.open(tempFile.toAscii().data(), ifstream::in);
905 readTemp.getline(value, 256);
907 readTemp.getline(value, 256);
909 readTemp.getline(value, 256);
911 readTemp.getline(value, 256);
913 readTemp.getline(value, 256);
915 readTemp.getline(value, 256);
920 remove(tempFile.toAscii().data());
933 FileName temp = FileName::createTempFile(
"$temporary/EnviromentInfo.txt");
934 QString tempFile = temp.expanded();
935 PvlGroup envGroup(
"EnviromentVariables");
938 QString env1 =
"printenv SHELL >| " + tempFile;
939 QString env2 =
"printenv HOME >> " + tempFile;
940 QString env3 =
"printenv PWD >> " + tempFile;
941 QString env5 =
"printenv ISISROOT >> " + tempFile;
942 QString env6 =
"printenv ISIS3DATA >> " + tempFile;
943 ProgramLauncher::RunSystemCommand(env1);
944 ProgramLauncher::RunSystemCommand(env2);
945 ProgramLauncher::RunSystemCommand(env3);
946 ProgramLauncher::RunSystemCommand(env5);
947 ProgramLauncher::RunSystemCommand(env6);
950 readTemp.open(tempFile.toAscii().data(), ifstream::in);
951 readTemp.getline(value, 255);
953 readTemp.getline(value, 255);
955 readTemp.getline(value, 255);
957 readTemp.getline(value, 255);
959 readTemp.getline(value, 255);
963 QString cleanup =
"rm -f " + tempFile;
964 ProgramLauncher::RunSystemCommand(cleanup);
973 QString Application::GetSystemDiskSpace() {
974 FileName temp = FileName::createTempFile(
"$temporary/SystemDiskSpace.txt");
975 QString tempFile = temp.expanded();
977 QString diskspace =
"df >| " + tempFile;
978 ProgramLauncher::RunSystemCommand(diskspace);
979 readTemp.open(tempFile.toAscii().data(), ifstream::in);
981 QString results =
"";
983 while (!readTemp.eof()) {
984 readTemp.getline(tmp, 512);
990 QString cleanup =
"rm -f " + tempFile;
991 ProgramLauncher::RunSystemCommand(cleanup);
1000 QString Application::GetLibraryDependencies(QString file) {
1001 FileName temp = FileName::createTempFile(
"$temporary/LibraryDependencies.txt");
1002 QString tempFile = temp.expanded();
1004 QString dependencies =
"";
1005 #if defined(__linux__)
1006 dependencies =
"ldd -v " + file +
" >| " + tempFile;
1007 #elif defined(__APPLE__)
1008 dependencies =
"otool -L " + file +
" >| " + tempFile;
1009 #elif defined (__sun__)
1010 dependencies =
"ldd -v " + file +
" >| " + tempFile;
1012 ProgramLauncher::RunSystemCommand(dependencies);
1013 readTemp.open(tempFile.toAscii().data(), ifstream::in);
1015 QString results =
"";
1017 while (!readTemp.eof()) {
1018 readTemp.getline(tmp, 512);
1024 QString cleanup =
"rm -f " + tempFile;
1025 ProgramLauncher::RunSystemCommand(cleanup);