22#include <QApplication>
23#include <QCoreApplication>
24#include <QLocalSocket>
30#include "Application.h"
32#include "IException.h"
35#include "Preference.h"
36#include "ProgramLauncher.h"
40#include "SessionLog.h"
42#include "UserInterface.h"
48 bool Application::p_applicationForceGuiApp =
false;
62 p_connectionToParent = NULL;
73 p_startPageFaults = 0;
74 p_startProcessSwaps = 0;
79 setlocale(LC_ALL,
"en_US.UTF-8");
82 strncpy(env,
"LANG=en_US.UTF-8", 1023);
86 QString pluginPath = getenv(
"ISISROOT");
87 pluginPath.append(
"/3rdParty/lib/");
88 QCoreApplication::addLibraryPath(pluginPath);
99 CPLSetErrorHandler(CPLQuietErrorHandler);
101 catch(exception &e) {
102 QString msg =
"Failed to load GDAL Drivers, with error [" + QString(e.what()) +
"]";
103 throw IException(IException::Unknown, msg, _FILEINFO_);
108 FileName f(QString(argv[0]) +
".xml");
111 Preference::Preferences(f.name() ==
"unitTest.xml");
113 if (!f.fileExists()) {
114 f =
"$ISISROOT/bin/xml/" + f.name();
115 if (!f.fileExists()) {
116 QString message = Message::FileOpen(f.expanded());
117 throw IException(IException::Io, message, _FILEINFO_);
120 QString xmlfile = f.expanded();
124 if (!
p_ui->IsInteractive()) {
126 p_datetime =
DateTime(&p_startTime);
128 p_startClock = clock();
130 if (p_applicationForceGuiApp) {
134 setlocale(LC_ALL,
"en_US.UTF-8");
137 new QCoreApplication(argc, argv);
140 setlocale(LC_ALL,
"en_US.UTF-8");
143 QCoreApplication::setApplicationName(FileName(p_appName).baseName());
146 catch (IException &e) {
155 p_connectionToParent =
new QLocalSocket;
157 QString serverName =
"isis_" +
UserName() +
158 "_" +
toString(iApp->GetUserInterface().ParentId());
160 p_connectionToParent->connectToServer(serverName);
161 if (!p_connectionToParent->waitForConnected()) {
162 delete p_connectionToParent;
163 p_connectionToParent = NULL;
186 if (
p_ui->IsInteractive()) {
187 p_ui->TheGui()->Exec(funct);
190 if (
p_ui->BatchListSize() > 0) {
191 for (
int i = 0; i <
p_ui->BatchListSize(); i++) {
193 p_ui->SetBatchList(i);
196 p_datetime =
DateTime(&p_startTime);
198 p_startClock = clock();
202 SessionLog::TheLog(
true);
209 catch (IException &e) {
210 p_ui->SetErrorList(i);
212 if (
p_ui->AbortOnError()) {
213 for (
int j = (i + 1); j <
p_ui->BatchListSize(); j++) {
214 p_ui->SetErrorList(j);
232 catch (IException &e) {
237 catch (exception &e) {
238 QString message = e.what();
239 Isis::iExceptionSystem i(message, _FILEINFO_);
243 QString message =
"Unknown error expection";
244 Isis::iExceptionSystem i(message, _FILEINFO_);
258 if (
p_ui->IsInteractive()) {
259 p_startClock = clock();
260 p_datetime =
DateTime(&p_startTime);
264 PvlObject history(
p_ui->ProgramName());
265 history += PvlKeyword(
"IsisVersion",
Version());
266 history += PvlKeyword(
"AleVersion",
AleVersion());
267 history += PvlKeyword(
"ProgramVersion",
p_ui->Version());
268 QString path = QCoreApplication::applicationDirPath();
269 history += PvlKeyword(
"ProgramPath", path);
270 history += PvlKeyword(
"ExecutionDateTime", p_datetime);
271 history += PvlKeyword(
"HostName",
HostName());
272 history += PvlKeyword(
"UserName",
UserName());
273 history += PvlKeyword(
"Description",
p_ui->Brief());
277 p_ui->CommandLine(pvl);
278 history.addGroup(pvl.findGroup(
"UserParameters"));
290 int minutes = (int)(seconds / 60.0);
291 seconds = seconds - minutes * 60.0;
292 int hours = minutes / 60;
293 minutes = minutes - hours * 60;
295 snprintf(temp,
sizeof(temp),
"%02d:%02d:%04.1f", hours, minutes, seconds);
296 QString conTime = temp;
301 clock_t endClock = clock();
302 seconds = (double(endClock) - (double)p_startClock) / CLOCKS_PER_SEC;
303 minutes = (int)(seconds / 60.0);
304 seconds = seconds - minutes * 60.0;
305 hours = minutes / 60;
306 minutes = minutes - hours * 60;
307 snprintf(temp,
sizeof(temp),
"%02d:%02d:%04.1f", hours, minutes, seconds);
308 QString cpuTime = temp;
311 PvlGroup acct(
"Accounting");
312 acct += PvlKeyword(
"ConnectTime", conTime);
313 acct += PvlKeyword(
"CpuTime", cpuTime);
334 return 0 - p_startDirectIO;
343 return 0 - p_startPageFaults;
352 return 0 - p_startProcessSwaps;
362 static bool blankLine =
false;
364 SessionLog::TheLog().AddResults(results);
370 if (iApp && SessionLog::TheLog().TerminalOutput())
return;
375 if (blankLine) ostr << endl;
376 ostr << results << endl;
377 QString data = ostr.str().c_str();
378 iApp->SendParentData(
"LOG", data);
382 else if (iApp && iApp->GetUserInterface().IsInteractive()) {
384 if (blankLine) ostr << endl;
385 ostr << results << endl;
386 iApp->GetUserInterface().TheGui()->Log(ostr.str().c_str());
387 iApp->GetUserInterface().TheGui()->ShowLog();
392 if (blankLine) cout << endl;
393 cout << results << endl;
409 log->addGroup(results);
424 ostr << copy << endl;
425 QString data = ostr.str().c_str();
426 iApp->SendParentData(
"GUILOG", data);
430 else if (iApp->GetUserInterface().IsInteractive()) {
432 ostr << copy << endl;
433 iApp->GetUserInterface().TheGui()->Log(ostr.str().c_str());
434 iApp->GetUserInterface().TheGui()->ShowLog();
445 PvlGroup copy(results);
449 ostr << copy << endl;
450 QString data = ostr.str().c_str();
451 iApp->SendParentData(
"GUILOG", data);
455 else if (iApp->GetUserInterface().IsInteractive()) {
457 ostr << copy << endl;
458 iApp->GetUserInterface().TheGui()->Log(ostr.str().c_str());
459 iApp->GetUserInterface().TheGui()->ShowLog();
471 iApp->SendParentData(
"GUILOG", results);
475 else if (iApp->GetUserInterface().IsInteractive()) {
476 iApp->GetUserInterface().TheGui()->Log(results);
477 iApp->GetUserInterface().TheGui()->ShowLog();
496 if (iApp == NULL)
return false;
497 if (iApp->p_ui == NULL)
return false;
498 if (iApp->p_ui->ParentId() == 0)
return false;
510 for (
int i = 0; i < errors.groups(); i++) {
512 ostr << errors.group(i) << endl;
513 QString data = ostr.str().c_str();
514 iApp->SendParentData(
"ERROR", data);
524 const QString &message) {
526 if (p_connectionToParent == NULL) {
527 QString msg =
"This process (program) was executed by an existing Isis "
528 "process. However, we failed to establish a communication channel "
529 "with the parent (launcher) process. The parent process has a PID of "
530 "[" +
toString(iApp->GetUserInterface().ParentId()) +
"]";
531 throw IException(IException::Unknown, msg, _FILEINFO_);
541 if (p_connectionToParent->write(data.toLatin1().data(), data.toLatin1().size()) == -1) {
542 QString msg =
"This process (program) was executed by an exiting Isis "
543 "process. A communication channel was established with the parent "
544 "(launcher) process, but when we tried to send data to the parent "
545 "process an error occurred. The parent process has a PID of [" +
546 QString::number(iApp->GetUserInterface().ParentId()) +
"]";
547 throw IException(IException::Unknown, msg, _FILEINFO_);
550 p_connectionToParent->waitForBytesWritten(-1);
561 SessionLog::TheLog().Write();
563 if (SessionLog::TheLog().TerminalOutput()) {
566 ostr << SessionLog::TheLog() << endl;
567 QString data = ostr.str().c_str();
568 iApp->SendParentData(
"LOG", data);
570 else if (
p_ui->IsInteractive()) {
572 ostr << SessionLog::TheLog() << endl;
573 p_ui->TheGui()->Log(ostr.str().c_str());
574 p_ui->TheGui()->ShowLog();
577 cout << SessionLog::TheLog() << endl;
582 if (
p_ui->GetInfoFlag()) {
583 QString filename =
p_ui->GetInfoFileName();
585 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
586 if (p_BatchlistPass == 0) {
588 ss << SessionLog::TheLog();
598 if (filename.compare(
"") != 0) {
600 if (p_BatchlistPass == 0) {
601 ofstream debugingLog(filename.toLatin1().data());
602 if (!debugingLog.good()) {
603 QString msg =
"Error opening debugging log file [" + filename +
"]";
604 throw IException(IException::Io, msg, _FILEINFO_);
606 debugingLog << log << endl;
607 debugingLog <<
"\n############### User Preferences ################\n" << endl;
608 debugingLog << Preference::Preferences();
609 debugingLog <<
"\n############## System Disk Space ################\n" << endl;
611 debugingLog <<
"\n############ Executable Information #############\n" << endl;
616 ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
617 debugingLog << SessionLog::TheLog() << endl;
622 if (p_BatchlistPass == 0) {
624 cout <<
"\n############### User Preferences ################\n" << endl;
625 cout << Preference::Preferences();
626 cout <<
"\n############## System Disk Space ################\n" << endl;
628 cout <<
"\n############ Executable Information #############\n" << endl;
632 cout << SessionLog::TheLog() << endl;
650 Pvl errors = e.toPvl();
651 for (
int i = 0; i < errors.groups(); i++) {
652 PvlGroup &group = errors.group(i);
653 if (group.isNamed(
"Error")) {
657 SessionLog::TheLog().AddError(errors);
658 SessionLog::TheLog().Write();
663 else if (
p_ui->IsInteractive()) {
664 p_ui->TheGui()->LoadMessage(Application::formatError(e));
666 else if (SessionLog::TheLog().TerminalOutput()) {
667 cerr << SessionLog::TheLog() << endl;
670 cerr << Application::formatError(e) << endl;
674 if (
p_ui->GetInfoFlag()) {
675 QString filename =
p_ui->GetInfoFileName();
677 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
678 if (p_BatchlistPass == 0) {
680 ss << SessionLog::TheLog();
690 if (filename.compare(
"") != 0) {
691 if (p_BatchlistPass == 0) {
692 ofstream debugingLog(filename.toLatin1().data());
693 if (!debugingLog.good()) {
694 QString msg =
"Error opening debugging log file [" + filename +
"]";
695 throw IException(IException::Io, msg, _FILEINFO_);
697 debugingLog << log << endl;
698 debugingLog <<
"\n############### User Preferences ################\n" << endl;
699 debugingLog << Preference::Preferences();
700 debugingLog <<
"\n############ System Disk Space #############\n" << endl;
702 debugingLog <<
"\n############ Executable Information #############\n" << endl;
707 ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
708 debugingLog << SessionLog::TheLog() << endl;
713 if (p_BatchlistPass == 0) {
715 cout <<
"\n############### User Preferences ################\n" << endl;
716 cout << Preference::Preferences();
717 cout <<
"\n############ System Disk Space #############\n" << endl;
719 cout <<
"\n############ Executable Information #############\n" << endl;
723 cout << SessionLog::TheLog() << endl;
728 return (
int)e.errorType();
738 QString errorMessage = Application::formatError(e);
739 if (errorMessage ==
"") {
740 p_ui->TheGui()->ProgressText(
"Stopped");
743 p_ui->TheGui()->LoadMessage(errorMessage);
744 p_ui->TheGui()->ProgressText(
"Error");
747 if (
p_ui->TheGui()->ShowWarning())
751 QString Application::formatError(IException &e) {
752 stringstream stringStream;
753 QString stringErrors = e.toString(Preference::Preferences().reportFileLine());
755 if (Preference::Preferences().outputErrorAsPvl()) {
756 stringStream << stringErrors;
758 stringStream >> errors;
759 for (
int i = 0; i < errors.groups(); i++) {
760 PvlGroup &group = errors.group(i);
761 if (group.isNamed(
"Error")) {
765 stringStream.str(std::string());
766 stringStream.clear();
767 stringStream << errors;
768 stringErrors = stringStream.str().c_str();
774 QString Application::p_appName(
"Unknown");
794 iApp->SendParentData(QString(
"PROGRESSTEXT"), text);
796 else if (
p_ui->IsInteractive()) {
797 p_ui->TheGui()->ProgressText(text);
800 QString msg =
p_ui->ProgramName() +
": " + text;
817 iApp->SendParentData(QString(
"PROGRESS"), data);
819 else if (
p_ui->IsInteractive()) {
820 p_ui->TheGui()->Progress(percent);
824 cout << percent <<
"% Processed\r" << flush;
827 cout << percent <<
"% Processed" << endl;
839 if (
p_ui->IsInteractive()) {
840 if (
p_ui->TheGui()->ProcessEvents()) {
855 time_t startTime = time(NULL);
856 if (curtime != 0) *curtime = startTime;
857 struct tm *tmbuf = localtime(&startTime);
859 strftime(timestr, 80,
"%Y-%m-%dT%H:%M:%S", tmbuf);
860 return(QString) timestr;
907 FileName temp = FileName::createTempFile(
"$temporary/UnameConsoleInfo.txt");
908 QString tempFile = temp.expanded();
914 PvlGroup unameGroup(
"UNAME");
917#if defined(__linux__)
929 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
930 readTemp.getline(value, 256);
931 unameGroup.addKeyword(PvlKeyword(
"MachineHardware", value));
932 readTemp.getline(value, 256);
933 unameGroup.addKeyword(PvlKeyword(
"Processor", value));
934 readTemp.getline(value, 256);
935 unameGroup.addKeyword(PvlKeyword(
"HardwarePlatform", value));
936 readTemp.getline(value, 256);
937 unameGroup.addKeyword(PvlKeyword(
"OperatingSystem", value));
938 readTemp.getline(value, 256);
939 unameGroup.addKeyword(PvlKeyword(
"KernelName", value));
940 readTemp.getline(value, 256);
941 unameGroup.addKeyword(PvlKeyword(
"KernelVersion", value));
942 readTemp.getline(value, 256);
943 unameGroup.addKeyword(PvlKeyword(
"KernelRelease", value));
944 readTemp.getline(value, 256);
945 unameGroup.addKeyword(PvlKeyword(
"FullUnameString", value));
947#elif defined(__APPLE__)
958 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
959 readTemp.getline(value, 256);
960 unameGroup.addKeyword(PvlKeyword(
"MachineHardware", value));
961 readTemp.getline(value, 256);
962 unameGroup.addKeyword(PvlKeyword(
"Processor", value));
963 readTemp.getline(value, 256);
964 unameGroup.addKeyword(PvlKeyword(
"OperatingSystem", value));
965 readTemp.getline(value, 256);
966 unameGroup.addKeyword(PvlKeyword(
"OperatingSystemVersion", value));
967 readTemp.getline(value, 256);
968 unameGroup.addKeyword(PvlKeyword(
"OperatingSystemRelease", value));
969 readTemp.getline(value, 256);
970 unameGroup.addKeyword(PvlKeyword(
"FullUnameString", value));
974 remove(tempFile.toLatin1().data());
985 PvlGroup envGroup(
"EnviromentVariables");
987 envGroup.addKeyword(PvlKeyword(
"Shell", getenv(
"SHELL")));
988 envGroup.addKeyword(PvlKeyword(
"Home", getenv(
"HOME")));
989 envGroup.addKeyword(PvlKeyword(
"PWD", getenv(
"PWD")));
990 envGroup.addKeyword(PvlKeyword(
"ISISROOT", getenv(
"ISISROOT")));
991 envGroup.addKeyword(PvlKeyword(
"ISISDATA", getenv(
"ISISDATA")));
992 envGroup.addKeyword(PvlKeyword(
"SPICEQL_CACHE_DIR", getenv(
"SPICEQL_CACHE_DIR")));
1003 FileName temp = FileName::createTempFile(
"$temporary/SystemDiskSpace.txt");
1004 QString tempFile = temp.expanded();
1006 QString diskspace =
"df >| " + tempFile;
1008 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
1010 QString results =
"";
1012 while (!readTemp.eof()) {
1013 readTemp.getline(tmp, 512);
1019 QString cleanup =
"rm -f " + tempFile;
1030 FileName temp = FileName::createTempFile(
"$temporary/LibraryDependencies.txt");
1031 QString tempFile = temp.expanded();
1033 QString dependencies =
"";
1034#if defined(__linux__)
1035 dependencies =
"ldd -v " + file +
" >| " + tempFile;
1036#elif defined(__APPLE__)
1037 dependencies =
"otool -L " + file +
" >| " + tempFile;
1038#elif defined (__sun__)
1039 dependencies =
"ldd -v " + file +
" >| " + tempFile;
1042 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
1044 QString results =
"";
1046 while (!readTemp.eof()) {
1047 readTemp.getline(tmp, 512);
1053 QString cleanup =
"rm -f " + tempFile;
int Run(void(*funct)())
Runs the program defined in the function funct.
static QString DateTime(time_t *curtime=0)
Returns the date and time as a QString.
PvlObject History()
Creates an application history PvlObject.
void FunctionCleanup()
Cleans up after the function by writing the log, saving the history, and either sending the log to th...
static void Log(PvlGroup &results)
Writes Pvl results to sessionlog and printfile.
PvlGroup Accounting()
Creates accounting PvlGroup.
void SendParentData(QString, const QString &)
static QString AleVersion()
The Ale Version for this application.
static void GuiLog(const Pvl &results)
Writes the Pvl results to the sessionlog, but not to the printfile.
static QString HostName()
Returns the host name.
void GuiReportError(IException &e)
Loads the error message into the gui, but does not write it to the session log.
int DirectIO()
Returns the current number of I/O's.
QElapsedTimer m_connectTime
Used to calculate program's run time.
static QString Version()
The Isis Version for this application.
static QString Name()
Returns the name of the application.
int PageFaults()
Returns the current number of faults.
static void AppendAndLog(PvlGroup &results, Pvl *log)
Writes the pvl group results to both a passed in Pvl log and the applications log (either GUI or comm...
void ProcessGuiEvents()
Processes the gui events.
int ProcessSwaps()
Returns the current number of swaps.
static QString GetLibraryDependencies(QString file)
Runs ldd on linux and sun and otool on macs to get information about the applicaiton run.
static bool HasParent()
Returns whether the application has a parent or not.
static QString GetSystemDiskSpace()
Runs df to see the disk space availability.
void UpdateProgress(const QString &text, bool print)
Updates the progress bar in the gui.
void SendParentErrors(PvlObject &errors)
Sends errors to the parent.
UserInterface * p_ui
Pointer to a User Interface object.
static PvlGroup GetEnviromentInfo()
Returns ISIS-related Enviroment Variables.
static QString UserName()
Returns the user name.
static UserInterface & GetUserInterface()
Returns the UserInterface object.
Application(int &argc, char *argv[])
Constuctor for the Application object.
int FunctionError(IException &e)
Adds the error to the session log, sends the error to the parent if it has one, loads the error messa...
~Application()
Destroys the Application object.
static PvlGroup GetUnameInfo()
Runs various system specific uname commands and returns the results.
static QString isisVersion()
static QString hostName()
static QString aleVersion()
static QString userName()
@Returns the user name.
static void RunSystemCommand(QString commandLine)
This runs arbitrary system commands.
Command Line and Xml loader, validation, and access.
This is free and unencumbered software released into the public domain.
QString toString(const LinearAlgebra::Vector &vector, int precision)
A global function to format LinearAlgebra::Vector as a QString with the given precision.
Namespace for the standard library.