25 #include <QApplication>
26 #include <QCoreApplication>
27 #include <QLocalSocket>
31 #include "Application.h"
32 #include "Constants.h"
33 #include "CubeManager.h"
35 #include "IException.h"
39 #include "Preference.h"
40 #include "ProgramLauncher.h"
43 #include "PvlObject.h"
44 #include "SessionLog.h"
46 #include "UserInterface.h"
51 Application *iApp = NULL;
52 bool Application::p_applicationForceGuiApp =
false;
64 Application::Application(
int &argc,
char *argv[]) {
66 p_connectionToParent = NULL;
77 p_startPageFaults = 0;
78 p_startProcessSwaps = 0;
83 setlocale(LC_ALL,
"en_US");
86 strncpy(env,
"LANG=en_US", 1023);
90 QString pluginPath = getenv(
"ISISROOT");
91 pluginPath.append(
"/3rdParty/lib/");
92 QCoreApplication::addLibraryPath(pluginPath);
96 p_startDirectIO = DirectIO();
97 p_startPageFaults = PageFaults();
98 p_startProcessSwaps = ProcessSwaps();
102 FileName f(QString(argv[0]) +
".xml");
105 Preference::Preferences(f.
name() ==
"unitTest.xml");
108 f =
"$ISISROOT/bin/xml/" + f.
name();
110 QString message = Message::FileOpen(f.
expanded());
111 throw IException(IException::Io, message, _FILEINFO_);
118 if (!p_ui->IsInteractive()) {
120 p_datetime = DateTime(&p_startTime);
121 m_connectTime.start();
122 p_startClock = clock();
124 if (p_applicationForceGuiApp) {
128 setlocale(LC_ALL,
"en_US");
131 new QCoreApplication(argc, argv);
134 setlocale(LC_ALL,
"en_US");
137 QCoreApplication::setApplicationName(
FileName(p_appName).baseName());
148 if (GetUserInterface().ParentId()) {
149 p_connectionToParent =
new QLocalSocket;
151 QString serverName =
"isis_" + UserName() +
154 p_connectionToParent->connectToServer(serverName);
155 if (!p_connectionToParent->waitForConnected()) {
156 delete p_connectionToParent;
157 p_connectionToParent = NULL;
163 Application::~Application() {
177 int Application::Run(
void (*funct)()) {
180 if (p_ui->IsInteractive()) {
181 p_ui->TheGui()->Exec(funct);
184 if (p_ui->BatchListSize() > 0) {
185 for (
int i = 0; i < p_ui->BatchListSize(); i++) {
187 p_ui->SetBatchList(i);
190 p_datetime = DateTime(&p_startTime);
191 m_connectTime.start();
192 p_startClock = clock();
193 p_startDirectIO = DirectIO();
194 p_startPageFaults = PageFaults();
195 p_startProcessSwaps = ProcessSwaps();
196 SessionLog::TheLog(
true);
200 Application::FunctionCleanup();
204 p_ui->SetErrorList(i);
205 status = Application::FunctionError(e);
206 if (p_ui->AbortOnError()) {
207 for (
int j = (i + 1); j < p_ui->BatchListSize(); j++) {
208 p_ui->SetErrorList(j);
222 Application::FunctionCleanup();
227 status = Application::FunctionError(e);
231 catch (exception &e) {
232 QString message = e.what();
233 Isis::iExceptionSystem i(message, _FILEINFO_);
237 QString message =
"Unknown error expection";
238 Isis::iExceptionSystem i(message, _FILEINFO_);
252 if (p_ui->IsInteractive()) {
253 p_startClock = clock();
254 p_datetime = DateTime(&p_startTime);
255 m_connectTime.start();
259 history +=
PvlKeyword(
"IsisVersion", Version());
260 history +=
PvlKeyword(
"ProgramVersion", p_ui->Version());
261 QString path = QCoreApplication::applicationDirPath();
263 history +=
PvlKeyword(
"ExecutionDateTime", p_datetime);
264 history +=
PvlKeyword(
"HostName", HostName());
265 history +=
PvlKeyword(
"UserName", UserName());
266 history +=
PvlKeyword(
"Description", p_ui->Brief());
270 p_ui->CommandLine(pvl);
282 double seconds = m_connectTime.elapsed() / 1000.0;
283 int minutes = (int)(seconds / 60.0);
284 seconds = seconds - minutes * 60.0;
285 int hours = minutes / 60;
286 minutes = minutes - hours * 60;
288 sprintf(temp,
"%02d:%02d:%04.1f", hours, minutes, seconds);
289 QString conTime = temp;
294 clock_t endClock = clock();
295 seconds = (double(endClock) - (double)p_startClock) / CLOCKS_PER_SEC;
296 minutes = (int)(seconds / 60.0);
297 seconds = seconds - minutes * 60.0;
298 hours = minutes / 60;
299 minutes = minutes - hours * 60;
300 sprintf(temp,
"%02d:%02d:%04.1f", hours, minutes, seconds);
301 QString cpuTime = temp;
326 int Application::DirectIO() {
327 return 0 - p_startDirectIO;
335 int Application::PageFaults() {
336 return 0 - p_startPageFaults;
344 int Application::ProcessSwaps() {
345 return 0 - p_startProcessSwaps;
355 static bool blankLine =
false;
357 SessionLog::TheLog().AddResults(results);
362 if (SessionLog::TheLog().TerminalOutput())
return;
367 if (blankLine) ostr << endl;
368 ostr << results << endl;
369 QString data = ostr.str().c_str();
376 if (blankLine) ostr << endl;
377 ostr << results << endl;
384 if (blankLine) cout << endl;
385 cout << results << endl;
395 void Application::GuiLog(
const Pvl &results) {
401 ostr << copy << endl;
402 QString data = ostr.str().c_str();
409 ostr << copy << endl;
420 void Application::GuiLog(
const PvlGroup &results) {
426 ostr << copy << endl;
427 QString data = ostr.str().c_str();
434 ostr << copy << endl;
445 void Application::GuiLog(
const QString &results) {
472 bool Application::HasParent() {
473 if (iApp == NULL)
return false;
474 if (iApp->
p_ui == NULL)
return false;
485 if (!HasParent())
return;
487 for (
int i = 0; i < errors.
groups(); i++) {
489 ostr << errors.
group(i) << endl;
490 QString data = ostr.str().c_str();
500 void Application::SendParentData(
const QString code,
501 const QString &message) {
503 if (p_connectionToParent == NULL) {
504 QString msg =
"This process (program) was executed by an existing Isis "
505 "process. However, we failed to establish a communication channel "
506 "with the parent (launcher) process. The parent process has a PID of "
508 throw IException(IException::Unknown, msg, _FILEINFO_);
518 if (p_connectionToParent->write(data.toLatin1().data(), data.toLatin1().size()) == -1) {
519 QString msg =
"This process (program) was executed by an exiting Isis "
520 "process. A communication channel was established with the parent "
521 "(launcher) process, but when we tried to send data to the parent "
522 "process an error occurred. The parent process has a PID of [" +
524 throw IException(IException::Unknown, msg, _FILEINFO_);
527 p_connectionToParent->waitForBytesWritten(-1);
536 void Application::FunctionCleanup() {
538 SessionLog::TheLog().Write();
540 if (SessionLog::TheLog().TerminalOutput()) {
543 ostr << SessionLog::TheLog() << endl;
544 QString data = ostr.str().c_str();
547 else if (p_ui->IsInteractive()) {
549 ostr << SessionLog::TheLog() << endl;
550 p_ui->TheGui()->Log(ostr.str().c_str());
551 p_ui->TheGui()->ShowLog();
554 cout << SessionLog::TheLog() << endl;
559 if (p_ui->GetInfoFlag()) {
560 QString filename = p_ui->GetInfoFileName();
562 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
563 if (p_BatchlistPass == 0) {
565 ss << SessionLog::TheLog();
575 if (filename.compare(
"") != 0) {
577 if (p_BatchlistPass == 0) {
578 ofstream debugingLog(filename.toLatin1().data());
579 if (!debugingLog.good()) {
580 QString msg =
"Error opening debugging log file [" + filename +
"]";
581 throw IException(IException::Io, msg, _FILEINFO_);
583 debugingLog << log << endl;
584 debugingLog <<
"\n############### User Preferences ################\n" << endl;
585 debugingLog << Preference::Preferences();
586 debugingLog <<
"\n############## System Disk Space ################\n" << endl;
587 debugingLog << GetSystemDiskSpace() << endl;
588 debugingLog <<
"\n############ Executable Information #############\n" << endl;
589 debugingLog << GetLibraryDependencies(app) << endl;
593 ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
594 debugingLog << SessionLog::TheLog() << endl;
599 if (p_BatchlistPass == 0) {
601 cout <<
"\n############### User Preferences ################\n" << endl;
602 cout << Preference::Preferences();
603 cout <<
"\n############## System Disk Space ################\n" << endl;
604 cout << GetSystemDiskSpace() << endl;
605 cout <<
"\n############ Executable Information #############\n" << endl;
606 cout << GetLibraryDependencies(app) << endl;
609 cout << SessionLog::TheLog() << endl;
628 SessionLog::TheLog().AddError(errors);
629 SessionLog::TheLog().Write();
632 SendParentErrors(errors);
634 else if (p_ui->IsInteractive()) {
635 p_ui->TheGui()->LoadMessage(e.
toString());
637 else if (SessionLog::TheLog().TerminalOutput()) {
638 cerr << SessionLog::TheLog() << endl;
645 if (p_ui->GetInfoFlag()) {
646 QString filename = p_ui->GetInfoFileName();
648 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
649 if (p_BatchlistPass == 0) {
651 ss << SessionLog::TheLog();
661 if (filename.compare(
"") != 0) {
662 if (p_BatchlistPass == 0) {
663 ofstream debugingLog(filename.toLatin1().data());
664 if (!debugingLog.good()) {
665 QString msg =
"Error opening debugging log file [" + filename +
"]";
666 throw IException(IException::Io, msg, _FILEINFO_);
668 debugingLog << log << endl;
669 debugingLog <<
"\n############### User Preferences ################\n" << endl;
670 debugingLog << Preference::Preferences();
671 debugingLog <<
"\n############ System Disk Space #############\n" << endl;
672 debugingLog << GetSystemDiskSpace() << endl;
673 debugingLog <<
"\n############ Executable Information #############\n" << endl;
674 debugingLog << GetLibraryDependencies(app) << endl;
678 ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
679 debugingLog << SessionLog::TheLog() << endl;
684 if (p_BatchlistPass == 0) {
686 cout <<
"\n############### User Preferences ################\n" << endl;
687 cout << Preference::Preferences();
688 cout <<
"\n############ System Disk Space #############\n" << endl;
689 cout << GetSystemDiskSpace() << endl;
690 cout <<
"\n############ Executable Information #############\n" << endl;
691 cout << GetLibraryDependencies(app) << endl;
694 cout << SessionLog::TheLog() << endl;
709 QString errorMessage = e.
toString();
710 if (errorMessage ==
"") {
711 p_ui->TheGui()->ProgressText(
"Stopped");
714 p_ui->TheGui()->LoadMessage(errorMessage);
715 p_ui->TheGui()->ProgressText(
"Error");
718 if (p_ui->TheGui()->ShowWarning())
722 QString Application::p_appName(
"Unknown");
729 QString Application::Name() {
740 void Application::UpdateProgress(
const QString &text,
bool print) {
741 if (HasParent() && print) {
744 else if (p_ui->IsInteractive()) {
745 p_ui->TheGui()->ProgressText(text);
748 QString msg = p_ui->ProgramName() +
": " + text;
762 void Application::UpdateProgress(
int percent,
bool print) {
763 if (HasParent() && print) {
767 else if (p_ui->IsInteractive()) {
768 p_ui->TheGui()->Progress(percent);
772 cout << percent <<
"% Processed\r" << flush;
775 cout << percent <<
"% Processed" << endl;
786 void Application::ProcessGuiEvents() {
787 if (p_ui->IsInteractive()) {
788 if (p_ui->TheGui()->ProcessEvents()) {
802 QString Application::DateTime(time_t *curtime) {
803 time_t startTime = time(NULL);
804 if (curtime != 0) *curtime = startTime;
805 struct tm *tmbuf = localtime(&startTime);
807 strftime(timestr, 80,
"%Y-%m-%dT%H:%M:%S", tmbuf);
808 return(QString) timestr;
816 QString Application::UserName() {
825 QString Application::HostName() {
834 QString Application::Version() {
835 return isisVersion();
846 FileName temp = FileName::createTempFile(
"$temporary/UnameConsoleInfo.txt");
856 #if defined(__linux__)
858 ProgramLauncher::RunSystemCommand(
"uname -m > " + tempFile);
859 ProgramLauncher::RunSystemCommand(
"uname -p > " + tempFile);
860 ProgramLauncher::RunSystemCommand(
"uname -i > " + tempFile);
861 ProgramLauncher::RunSystemCommand(
"uname -o > " + tempFile);
862 ProgramLauncher::RunSystemCommand(
"uname -s > " + tempFile);
863 ProgramLauncher::RunSystemCommand(
"uname -v > " + tempFile);
864 ProgramLauncher::RunSystemCommand(
"uname -r > " + tempFile);
865 ProgramLauncher::RunSystemCommand(
"uname -a > " + tempFile);
868 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
869 readTemp.getline(value, 256);
871 readTemp.getline(value, 256);
873 readTemp.getline(value, 256);
875 readTemp.getline(value, 256);
877 readTemp.getline(value, 256);
879 readTemp.getline(value, 256);
881 readTemp.getline(value, 256);
883 readTemp.getline(value, 256);
886 #elif defined(__APPLE__)
888 ProgramLauncher::RunSystemCommand(
"uname -m > " + tempFile);
889 ProgramLauncher::RunSystemCommand(
"uname -p > " + tempFile);
890 ProgramLauncher::RunSystemCommand(
"uname -s > " + tempFile);
891 ProgramLauncher::RunSystemCommand(
"uname -v > " + tempFile);
892 ProgramLauncher::RunSystemCommand(
"uname -r > " + tempFile);
893 ProgramLauncher::RunSystemCommand(
"uname -a > " + tempFile);
897 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
898 readTemp.getline(value, 256);
900 readTemp.getline(value, 256);
902 readTemp.getline(value, 256);
904 readTemp.getline(value, 256);
906 readTemp.getline(value, 256);
908 readTemp.getline(value, 256);
913 remove(tempFile.toLatin1().data());
926 FileName temp = FileName::createTempFile(
"$temporary/EnviromentInfo.txt");
928 PvlGroup envGroup(
"EnviromentVariables");
931 QString env1 =
"printenv SHELL >| " + tempFile;
932 QString env2 =
"printenv HOME >> " + tempFile;
933 QString env3 =
"printenv PWD >> " + tempFile;
934 QString env5 =
"printenv ISISROOT >> " + tempFile;
935 QString env6 =
"printenv ISISDATA >> " + tempFile;
936 ProgramLauncher::RunSystemCommand(env1);
937 ProgramLauncher::RunSystemCommand(env2);
938 ProgramLauncher::RunSystemCommand(env3);
939 ProgramLauncher::RunSystemCommand(env5);
940 ProgramLauncher::RunSystemCommand(env6);
943 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
944 readTemp.getline(value, 255);
946 readTemp.getline(value, 255);
948 readTemp.getline(value, 255);
950 readTemp.getline(value, 255);
952 readTemp.getline(value, 255);
956 QString cleanup =
"rm -f " + tempFile;
957 ProgramLauncher::RunSystemCommand(cleanup);
966 QString Application::GetSystemDiskSpace() {
967 FileName temp = FileName::createTempFile(
"$temporary/SystemDiskSpace.txt");
970 QString diskspace =
"df >| " + tempFile;
971 ProgramLauncher::RunSystemCommand(diskspace);
972 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
974 QString results =
"";
976 while (!readTemp.eof()) {
977 readTemp.getline(tmp, 512);
983 QString cleanup =
"rm -f " + tempFile;
984 ProgramLauncher::RunSystemCommand(cleanup);
993 QString Application::GetLibraryDependencies(QString file) {
994 FileName temp = FileName::createTempFile(
"$temporary/LibraryDependencies.txt");
997 QString dependencies =
"";
998 #if defined(__linux__)
999 dependencies =
"ldd -v " + file +
" >| " + tempFile;
1000 #elif defined(__APPLE__)
1001 dependencies =
"otool -L " + file +
" >| " + tempFile;
1002 #elif defined (__sun__)
1003 dependencies =
"ldd -v " + file +
" >| " + tempFile;
1005 ProgramLauncher::RunSystemCommand(dependencies);
1006 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
1008 QString results =
"";
1010 while (!readTemp.eof()) {
1011 readTemp.getline(tmp, 512);
1017 QString cleanup =
"rm -f " + tempFile;
1018 ProgramLauncher::RunSystemCommand(cleanup);