22#include <QApplication>
23#include <QCoreApplication>
24#include <QLocalSocket>
28#include "Application.h"
30#include "IException.h"
33#include "Preference.h"
34#include "ProgramLauncher.h"
38#include "SessionLog.h"
40#include "UserInterface.h"
46 bool Application::p_applicationForceGuiApp =
false;
60 p_connectionToParent = NULL;
71 p_startPageFaults = 0;
72 p_startProcessSwaps = 0;
77 setlocale(LC_ALL,
"en_US");
80 strncpy(env,
"LANG=en_US", 1023);
84 QString pluginPath = getenv(
"ISISROOT");
85 pluginPath.append(
"/3rdParty/lib/");
86 QCoreApplication::addLibraryPath(pluginPath);
96 FileName f(QString(argv[0]) +
".xml");
99 Preference::Preferences(f.
name() ==
"unitTest.xml");
102 f =
"$ISISROOT/bin/xml/" + f.
name();
112 if (!
p_ui->IsInteractive()) {
114 p_datetime =
DateTime(&p_startTime);
116 p_startClock = clock();
118 if (p_applicationForceGuiApp) {
122 setlocale(LC_ALL,
"en_US");
125 new QCoreApplication(argc, argv);
128 setlocale(LC_ALL,
"en_US");
131 QCoreApplication::setApplicationName(
FileName(p_appName).baseName());
143 p_connectionToParent =
new QLocalSocket;
145 QString serverName =
"isis_" +
UserName() +
146 "_" +
toString(iApp->GetUserInterface().ParentId());
148 p_connectionToParent->connectToServer(serverName);
149 if (!p_connectionToParent->waitForConnected()) {
150 delete p_connectionToParent;
151 p_connectionToParent = NULL;
174 if (
p_ui->IsInteractive()) {
175 p_ui->TheGui()->Exec(funct);
178 if (
p_ui->BatchListSize() > 0) {
179 for (
int i = 0; i <
p_ui->BatchListSize(); i++) {
181 p_ui->SetBatchList(i);
184 p_datetime =
DateTime(&p_startTime);
186 p_startClock = clock();
190 SessionLog::TheLog(
true);
198 p_ui->SetErrorList(i);
200 if (
p_ui->AbortOnError()) {
201 for (
int j = (i + 1); j <
p_ui->BatchListSize(); j++) {
202 p_ui->SetErrorList(j);
225 catch (exception &e) {
226 QString message = e.
what();
227 Isis::iExceptionSystem i(message, _FILEINFO_);
231 QString message =
"Unknown error expection";
232 Isis::iExceptionSystem i(message, _FILEINFO_);
246 if (
p_ui->IsInteractive()) {
247 p_startClock = clock();
248 p_datetime =
DateTime(&p_startTime);
255 QString path = QCoreApplication::applicationDirPath();
257 history +=
PvlKeyword(
"ExecutionDateTime", p_datetime);
264 p_ui->CommandLine(pvl);
277 int minutes = (int)(seconds / 60.0);
278 seconds = seconds - minutes * 60.0;
279 int hours = minutes / 60;
280 minutes = minutes - hours * 60;
282 snprintf(temp,
sizeof(temp),
"%02d:%02d:%04.1f", hours, minutes, seconds);
283 QString conTime = temp;
288 clock_t endClock = clock();
289 seconds = (double(endClock) - (double)p_startClock) / CLOCKS_PER_SEC;
290 minutes = (int)(seconds / 60.0);
291 seconds = seconds - minutes * 60.0;
292 hours = minutes / 60;
293 minutes = minutes - hours * 60;
294 snprintf(temp,
sizeof(temp),
"%02d:%02d:%04.1f", hours, minutes, seconds);
295 QString cpuTime = temp;
321 return 0 - p_startDirectIO;
330 return 0 - p_startPageFaults;
339 return 0 - p_startProcessSwaps;
349 static bool blankLine =
false;
351 SessionLog::TheLog().AddResults(results);
357 if (iApp && SessionLog::TheLog().TerminalOutput())
return;
362 if (blankLine) ostr << endl;
363 ostr << results << endl;
364 QString data = ostr.str().c_str();
365 iApp->SendParentData(
"LOG", data);
369 else if (iApp && iApp->GetUserInterface().IsInteractive()) {
371 if (blankLine) ostr << endl;
372 ostr << results << endl;
373 iApp->GetUserInterface().TheGui()->Log(ostr.str().c_str());
374 iApp->GetUserInterface().TheGui()->ShowLog();
379 if (blankLine) cout << endl;
380 cout << results << endl;
411 ostr << copy << endl;
412 QString data = ostr.str().c_str();
413 iApp->SendParentData(
"GUILOG", data);
417 else if (iApp->GetUserInterface().IsInteractive()) {
419 ostr << copy << endl;
420 iApp->GetUserInterface().TheGui()->Log(ostr.str().c_str());
421 iApp->GetUserInterface().TheGui()->ShowLog();
436 ostr << copy << endl;
437 QString data = ostr.str().c_str();
438 iApp->SendParentData(
"GUILOG", data);
442 else if (iApp->GetUserInterface().IsInteractive()) {
444 ostr << copy << endl;
445 iApp->GetUserInterface().TheGui()->Log(ostr.str().c_str());
446 iApp->GetUserInterface().TheGui()->ShowLog();
458 iApp->SendParentData(
"GUILOG", results);
462 else if (iApp->GetUserInterface().IsInteractive()) {
463 iApp->GetUserInterface().TheGui()->Log(results);
464 iApp->GetUserInterface().TheGui()->ShowLog();
483 if (iApp == NULL)
return false;
484 if (iApp->p_ui == NULL)
return false;
485 if (iApp->p_ui->ParentId() == 0)
return false;
497 for (
int i = 0; i < errors.
groups(); i++) {
499 ostr << errors.
group(i) << endl;
500 QString data = ostr.str().c_str();
501 iApp->SendParentData(
"ERROR", data);
511 const QString &message) {
513 if (p_connectionToParent == NULL) {
514 QString msg =
"This process (program) was executed by an existing Isis "
515 "process. However, we failed to establish a communication channel "
516 "with the parent (launcher) process. The parent process has a PID of "
517 "[" +
toString(iApp->GetUserInterface().ParentId()) +
"]";
528 if (p_connectionToParent->write(data.toLatin1().data(), data.toLatin1().size()) == -1) {
529 QString msg =
"This process (program) was executed by an exiting Isis "
530 "process. A communication channel was established with the parent "
531 "(launcher) process, but when we tried to send data to the parent "
532 "process an error occurred. The parent process has a PID of [" +
533 QString(iApp->GetUserInterface().ParentId()) +
"]";
537 p_connectionToParent->waitForBytesWritten(-1);
548 SessionLog::TheLog().Write();
550 if (SessionLog::TheLog().TerminalOutput()) {
553 ostr << SessionLog::TheLog() << endl;
554 QString data = ostr.str().c_str();
555 iApp->SendParentData(
"LOG", data);
557 else if (
p_ui->IsInteractive()) {
559 ostr << SessionLog::TheLog() << endl;
560 p_ui->TheGui()->Log(ostr.str().c_str());
561 p_ui->TheGui()->ShowLog();
564 cout << SessionLog::TheLog() << endl;
569 if (
p_ui->GetInfoFlag()) {
570 QString filename =
p_ui->GetInfoFileName();
572 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
573 if (p_BatchlistPass == 0) {
575 ss << SessionLog::TheLog();
585 if (filename.compare(
"") != 0) {
587 if (p_BatchlistPass == 0) {
588 ofstream debugingLog(filename.toLatin1().data());
589 if (!debugingLog.good()) {
590 QString msg =
"Error opening debugging log file [" + filename +
"]";
593 debugingLog << log << endl;
594 debugingLog <<
"\n############### User Preferences ################\n" << endl;
595 debugingLog << Preference::Preferences();
596 debugingLog <<
"\n############## System Disk Space ################\n" << endl;
598 debugingLog <<
"\n############ Executable Information #############\n" << endl;
603 ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
604 debugingLog << SessionLog::TheLog() << endl;
609 if (p_BatchlistPass == 0) {
611 cout <<
"\n############### User Preferences ################\n" << endl;
612 cout << Preference::Preferences();
613 cout <<
"\n############## System Disk Space ################\n" << endl;
615 cout <<
"\n############ Executable Information #############\n" << endl;
619 cout << SessionLog::TheLog() << endl;
638 for (
int i = 0; i < errors.
groups(); i++) {
644 SessionLog::TheLog().AddError(errors);
645 SessionLog::TheLog().Write();
650 else if (
p_ui->IsInteractive()) {
651 p_ui->TheGui()->LoadMessage(Application::formatError(e));
653 else if (SessionLog::TheLog().TerminalOutput()) {
654 cerr << SessionLog::TheLog() << endl;
657 cerr << Application::formatError(e) << endl;
661 if (
p_ui->GetInfoFlag()) {
662 QString filename =
p_ui->GetInfoFileName();
664 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
665 if (p_BatchlistPass == 0) {
667 ss << SessionLog::TheLog();
677 if (filename.compare(
"") != 0) {
678 if (p_BatchlistPass == 0) {
679 ofstream debugingLog(filename.toLatin1().data());
680 if (!debugingLog.good()) {
681 QString msg =
"Error opening debugging log file [" + filename +
"]";
684 debugingLog << log << endl;
685 debugingLog <<
"\n############### User Preferences ################\n" << endl;
686 debugingLog << Preference::Preferences();
687 debugingLog <<
"\n############ System Disk Space #############\n" << endl;
689 debugingLog <<
"\n############ Executable Information #############\n" << endl;
694 ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
695 debugingLog << SessionLog::TheLog() << endl;
700 if (p_BatchlistPass == 0) {
702 cout <<
"\n############### User Preferences ################\n" << endl;
703 cout << Preference::Preferences();
704 cout <<
"\n############ System Disk Space #############\n" << endl;
706 cout <<
"\n############ Executable Information #############\n" << endl;
710 cout << SessionLog::TheLog() << endl;
725 QString errorMessage = Application::formatError(e);
726 if (errorMessage ==
"") {
727 p_ui->TheGui()->ProgressText(
"Stopped");
730 p_ui->TheGui()->LoadMessage(errorMessage);
731 p_ui->TheGui()->ProgressText(
"Error");
734 if (
p_ui->TheGui()->ShowWarning())
738 QString Application::formatError(
IException &e) {
739 stringstream stringStream;
740 QString stringErrors = e.
toString(Preference::Preferences().reportFileLine());
742 if (Preference::Preferences().outputErrorAsPvl()) {
743 stringStream << stringErrors;
745 stringStream >> errors;
746 for (
int i = 0; i < errors.
groups(); i++) {
752 stringStream.str(std::string());
753 stringStream.
clear();
754 stringStream << errors;
755 stringErrors = stringStream.str().c_str();
761 QString Application::p_appName(
"Unknown");
781 iApp->SendParentData(QString(
"PROGRESSTEXT"), text);
783 else if (
p_ui->IsInteractive()) {
784 p_ui->TheGui()->ProgressText(text);
787 QString msg =
p_ui->ProgramName() +
": " + text;
804 iApp->SendParentData(QString(
"PROGRESS"), data);
806 else if (
p_ui->IsInteractive()) {
807 p_ui->TheGui()->Progress(percent);
811 cout << percent <<
"% Processed\r" << flush;
814 cout << percent <<
"% Processed" << endl;
826 if (
p_ui->IsInteractive()) {
827 if (
p_ui->TheGui()->ProcessEvents()) {
842 time_t startTime = time(NULL);
843 if (curtime != 0) *curtime = startTime;
844 struct tm *tmbuf = localtime(&startTime);
846 strftime(timestr, 80,
"%Y-%m-%dT%H:%M:%S", tmbuf);
847 return(QString) timestr;
895#if defined(__linux__)
907 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
908 readTemp.getline(value, 256);
910 readTemp.getline(value, 256);
912 readTemp.getline(value, 256);
914 readTemp.getline(value, 256);
916 readTemp.getline(value, 256);
918 readTemp.getline(value, 256);
920 readTemp.getline(value, 256);
922 readTemp.getline(value, 256);
925#elif defined(__APPLE__)
936 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
937 readTemp.getline(value, 256);
939 readTemp.getline(value, 256);
941 readTemp.getline(value, 256);
943 readTemp.getline(value, 256);
945 readTemp.getline(value, 256);
947 readTemp.getline(value, 256);
952 remove(tempFile.toLatin1().data());
967 PvlGroup envGroup(
"EnviromentVariables");
970 QString env1 =
"printenv SHELL >| " + tempFile;
971 QString env2 =
"printenv HOME >> " + tempFile;
972 QString env3 =
"printenv PWD >> " + tempFile;
973 QString env5 =
"printenv ISISROOT >> " + tempFile;
974 QString env6 =
"printenv ISISDATA >> " + tempFile;
982 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
983 readTemp.getline(value, 255);
985 readTemp.getline(value, 255);
987 readTemp.getline(value, 255);
989 readTemp.getline(value, 255);
991 readTemp.getline(value, 255);
995 QString cleanup =
"rm -f " + tempFile;
1007 QString tempFile = temp.
expanded();
1009 QString diskspace =
"df >| " + tempFile;
1011 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
1013 QString results =
"";
1015 while (!readTemp.eof()) {
1016 readTemp.getline(tmp, 512);
1022 QString cleanup =
"rm -f " + tempFile;
1034 QString tempFile = temp.
expanded();
1036 QString dependencies =
"";
1037#if defined(__linux__)
1038 dependencies =
"ldd -v " + file +
" >| " + tempFile;
1039#elif defined(__APPLE__)
1040 dependencies =
"otool -L " + file +
" >| " + tempFile;
1041#elif defined (__sun__)
1042 dependencies =
"ldd -v " + file +
" >| " + tempFile;
1045 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
1047 QString results =
"";
1049 while (!readTemp.eof()) {
1050 readTemp.getline(tmp, 512);
1056 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 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()
Runs some printenv commands that return 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 userName()
@Returns the user name.
File name manipulation and expansion.
bool fileExists() const
Returns true if the file exists; false otherwise.
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
static FileName createTempFile(FileName templateFileName="$TEMPORARY/temp")
Creates a temporary file and returns a FileName object created using the temporary file.
@ Unknown
A type of error that cannot be classified as any of the other error types.
@ Io
A type of error that occurred when performing an actual I/O operation.
const char * what() const
Returns a string representation of this exception in its current state.
ErrorType errorType() const
Returns the source of the error for this exception.
QString toString() const
Returns a string representation of this exception.
void print() const
Prints a string representation of this exception to stderr.
Pvl toPvl() const
Returns a PVL object representing the contents of this exception.
static void RunSystemCommand(QString commandLine)
This runs arbitrary system commands.
bool isNamed(const QString &match) const
Returns whether the given string is equal to the container name or not.
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
Contains multiple PvlContainers.
Container for cube-like labels.
A single keyword-value pair.
Contains Pvl Groups and Pvl Objects.
PvlGroup & group(const int index)
Return the group at the specified index.
int groups() const
Returns the number of groups contained.
void clear()
Remove everything from the current PvlObject.
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Command Line and Xml loader, validation, and access.
QString FileOpen(const QString &filename)
This error should be used when a file could not be opened.
This is free and unencumbered software released into the public domain.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Namespace for the standard library.