24#include <QApplication>
25#include <QCoreApplication>
26#include <QLocalSocket>
30#include "Application.h"
32#include "CubeManager.h"
34#include "IException.h"
38#include "Preference.h"
39#include "ProgramLauncher.h"
43#include "SessionLog.h"
45#include "UserInterface.h"
50 Application *iApp = NULL;
51 bool Application::p_applicationForceGuiApp =
false;
65 p_connectionToParent = NULL;
76 p_startPageFaults = 0;
77 p_startProcessSwaps = 0;
82 setlocale(LC_ALL,
"en_US");
85 strncpy(env,
"LANG=en_US", 1023);
89 QString pluginPath = getenv(
"ISISROOT");
90 pluginPath.append(
"/3rdParty/lib/");
91 QCoreApplication::addLibraryPath(pluginPath);
101 FileName f(QString(argv[0]) +
".xml");
104 Preference::Preferences(f.
name() ==
"unitTest.xml");
107 f =
"$ISISROOT/bin/xml/" + f.
name();
119 p_datetime =
DateTime(&p_startTime);
121 p_startClock = clock();
123 if (p_applicationForceGuiApp) {
127 setlocale(LC_ALL,
"en_US");
130 new QCoreApplication(argc, argv);
133 setlocale(LC_ALL,
"en_US");
136 QCoreApplication::setApplicationName(
FileName(p_appName).baseName());
148 p_connectionToParent =
new QLocalSocket;
150 QString serverName =
"isis_" +
UserName() +
153 p_connectionToParent->connectToServer(serverName);
154 if (!p_connectionToParent->waitForConnected()) {
155 delete p_connectionToParent;
156 p_connectionToParent = NULL;
189 p_datetime =
DateTime(&p_startTime);
191 p_startClock = clock();
195 SessionLog::TheLog(
true);
230 catch (exception &e) {
231 QString message = e.what();
232 Isis::iExceptionSystem i(message, _FILEINFO_);
236 QString message =
"Unknown error expection";
237 Isis::iExceptionSystem i(message, _FILEINFO_);
252 p_startClock = clock();
253 p_datetime =
DateTime(&p_startTime);
260 QString path = QCoreApplication::applicationDirPath();
262 history +=
PvlKeyword(
"ExecutionDateTime", p_datetime);
270 history.
addGroup(pvl.findGroup(
"UserParameters"));
282 int minutes = (int)(seconds / 60.0);
283 seconds = seconds - minutes * 60.0;
284 int hours = minutes / 60;
285 minutes = minutes - hours * 60;
287 snprintf(temp,
sizeof(temp),
"%02d:%02d:%04.1f", hours, minutes, seconds);
288 QString conTime = temp;
293 clock_t endClock = clock();
294 seconds = (double(endClock) - (double)p_startClock) / CLOCKS_PER_SEC;
295 minutes = (int)(seconds / 60.0);
296 seconds = seconds - minutes * 60.0;
297 hours = minutes / 60;
298 minutes = minutes - hours * 60;
299 snprintf(temp,
sizeof(temp),
"%02d:%02d:%04.1f", hours, minutes, seconds);
300 QString cpuTime = temp;
326 return 0 - p_startDirectIO;
335 return 0 - p_startPageFaults;
344 return 0 - p_startProcessSwaps;
354 static bool blankLine =
false;
356 SessionLog::TheLog().AddResults(results);
362 if (iApp && 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;
401 ostr << copy << endl;
402 QString data = ostr.str().c_str();
409 ostr << copy << endl;
426 ostr << copy << endl;
427 QString data = ostr.str().c_str();
434 ostr << copy << endl;
473 if (iApp == NULL)
return false;
474 if (iApp->
p_ui == NULL)
return false;
487 for (
int i = 0; i < errors.
groups(); i++) {
489 ostr << errors.
group(i) << endl;
490 QString data = ostr.str().c_str();
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 "
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 [" +
527 p_connectionToParent->waitForBytesWritten(-1);
538 SessionLog::TheLog().Write();
540 if (SessionLog::TheLog().TerminalOutput()) {
543 ostr << SessionLog::TheLog() << endl;
544 QString data = ostr.str().c_str();
549 ostr << SessionLog::TheLog() << endl;
554 cout << SessionLog::TheLog() << endl;
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 +
"]";
583 debugingLog << log << endl;
584 debugingLog <<
"\n############### User Preferences ################\n" << endl;
585 debugingLog << Preference::Preferences();
586 debugingLog <<
"\n############## System Disk Space ################\n" << endl;
588 debugingLog <<
"\n############ Executable Information #############\n" << 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;
605 cout <<
"\n############ Executable Information #############\n" << endl;
609 cout << SessionLog::TheLog() << endl;
627 Pvl errors = e.toPvl();
628 SessionLog::TheLog().AddError(errors);
629 SessionLog::TheLog().Write();
637 else if (SessionLog::TheLog().TerminalOutput()) {
638 cerr << SessionLog::TheLog() << endl;
641 cerr << e.toString() << endl;
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 +
"]";
668 debugingLog << log << endl;
669 debugingLog <<
"\n############### User Preferences ################\n" << endl;
670 debugingLog << Preference::Preferences();
671 debugingLog <<
"\n############ System Disk Space #############\n" << endl;
673 debugingLog <<
"\n############ Executable Information #############\n" << 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;
690 cout <<
"\n############ Executable Information #############\n" << endl;
694 cout << SessionLog::TheLog() << endl;
699 return (
int)e.errorType();
709 QString errorMessage = e.toString();
710 if (errorMessage ==
"") {
722 QString Application::p_appName(
"Unknown");
772 cout << percent <<
"% Processed\r" << flush;
775 cout << percent <<
"% Processed" << endl;
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;
847 QString tempFile = temp.expanded();
856#if defined(__linux__)
868 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
869 readTemp.getline(value, 256);
870 unameGroup.addKeyword(
PvlKeyword(
"MachineHardware", value));
871 readTemp.getline(value, 256);
872 unameGroup.addKeyword(
PvlKeyword(
"Processor", value));
873 readTemp.getline(value, 256);
874 unameGroup.addKeyword(
PvlKeyword(
"HardwarePlatform", value));
875 readTemp.getline(value, 256);
876 unameGroup.addKeyword(
PvlKeyword(
"OperatingSystem", value));
877 readTemp.getline(value, 256);
878 unameGroup.addKeyword(
PvlKeyword(
"KernelName", value));
879 readTemp.getline(value, 256);
880 unameGroup.addKeyword(
PvlKeyword(
"KernelVersion", value));
881 readTemp.getline(value, 256);
882 unameGroup.addKeyword(
PvlKeyword(
"KernelRelease", value));
883 readTemp.getline(value, 256);
884 unameGroup.addKeyword(
PvlKeyword(
"FullUnameString", value));
886#elif defined(__APPLE__)
897 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
898 readTemp.getline(value, 256);
899 unameGroup.addKeyword(
PvlKeyword(
"MachineHardware", value));
900 readTemp.getline(value, 256);
901 unameGroup.addKeyword(
PvlKeyword(
"Processor", value));
902 readTemp.getline(value, 256);
903 unameGroup.addKeyword(
PvlKeyword(
"OperatingSystem", value));
904 readTemp.getline(value, 256);
905 unameGroup.addKeyword(
PvlKeyword(
"OperatingSystemVersion", value));
906 readTemp.getline(value, 256);
907 unameGroup.addKeyword(
PvlKeyword(
"OperatingSystemRelease", value));
908 readTemp.getline(value, 256);
909 unameGroup.addKeyword(
PvlKeyword(
"FullUnameString", value));
913 remove(tempFile.toLatin1().data());
927 QString tempFile = temp.expanded();
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;
943 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
944 readTemp.getline(value, 255);
945 envGroup.addKeyword(
PvlKeyword(
"Shell", value));
946 readTemp.getline(value, 255);
947 envGroup.addKeyword(
PvlKeyword(
"Home", value));
948 readTemp.getline(value, 255);
949 envGroup.addKeyword(
PvlKeyword(
"Pwd", value));
950 readTemp.getline(value, 255);
951 envGroup.addKeyword(
PvlKeyword(
"ISISROOT", value));
952 readTemp.getline(value, 255);
953 envGroup.addKeyword(
PvlKeyword(
"ISISDATA", value));
956 QString cleanup =
"rm -f " + tempFile;
968 QString tempFile = temp.expanded();
970 QString diskspace =
"df >| " + tempFile;
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;
995 QString tempFile = temp.expanded();
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;
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;
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.
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.
int Exec(void(*funct)())
Start the Gui and enter the main loop This routine only returns when the program is ready to exit.
void Log(const QString &text)
Write text to the gui log.
void ProgressText(const QString &text)
Change progress text.
int ShowWarning()
Show an error message and return if the user wants to continue/abort.
void Progress(int percent)
Update the progress bar.
void LoadMessage(const QString &message)
Add more information to the error message.
bool ProcessEvents()
Let the event loop have some time to see if we need to cancel.
@ 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.
static void RunSystemCommand(QString commandLine)
This runs arbitrary system commands.
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 addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Command Line and Xml loader, validation, and access.
int ParentId()
Returns the parent id.
QString GetInfoFileName()
This method returns the filename where the debugging info is stored when the "-info" tag is used.
bool IsInteractive()
Indicates if the Isis Graphical User Interface is operating.
bool AbortOnError()
Returns true if the program should abort on error, and false if it should continue.
bool GetInfoFlag()
This method returns the flag state of info.
int BatchListSize()
Returns the size of the batchlist.
void SetErrorList(int i)
This method adds the line specified in the BatchList that the error occured on.
void SaveHistory()
Saves the user parameter information in the history of the program for later use.
void SetBatchList(int i)
Clears the gui parameters and sets the batch list information at line i as the new parameters.
void CommandLine(Isis::Pvl &lab) const
Creates a QString which could be used as a command line.
QString Version() const
Returns the application version date.
QString Brief() const
Returns the brief description of the program.
QString ProgramName() const
Returns the Program name.
void VerifyAll()
Verify all parameters.
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.