23 #include "IsisDebug.h" 41 #include <QApplication> 42 #include <QCoreApplication> 43 #include <QLocalSocket> 49 #include "CubeManager.h" 67 Application *iApp = NULL;
68 bool Application::p_applicationForceGuiApp =
false;
80 Application::Application(
int &argc,
char *argv[]) {
82 p_connectionToParent = NULL;
93 p_startPageFaults = 0;
94 p_startProcessSwaps = 0;
99 setlocale(LC_ALL,
"en_US");
102 strncpy(env,
"LANG=en_US", 1023);
106 QString pluginPath = getenv(
"ISISROOT");
107 pluginPath.append(
"/3rdParty/lib/");
108 QCoreApplication::addLibraryPath(pluginPath);
112 p_startDirectIO = DirectIO();
113 p_startPageFaults = PageFaults();
114 p_startProcessSwaps = ProcessSwaps();
118 FileName f(QString(argv[0]) +
".xml");
121 Preference::Preferences(f.
name() ==
"unitTest.xml");
124 f =
"$ISISROOT/bin/xml/" + f.
name();
134 if (!p_ui->IsInteractive()) {
136 p_datetime = DateTime(&p_startTime);
137 m_connectTime.start();
138 p_startClock = clock();
140 if (p_applicationForceGuiApp) {
144 setlocale(LC_ALL,
"en_US");
147 new QCoreApplication(argc, argv);
150 setlocale(LC_ALL,
"en_US");
153 QCoreApplication::setApplicationName(
FileName(p_appName).baseName());
164 if (GetUserInterface().ParentId()) {
165 p_connectionToParent =
new QLocalSocket;
167 QString serverName =
"isis_" + UserName() +
170 p_connectionToParent->connectToServer(serverName);
171 if (!p_connectionToParent->waitForConnected()) {
172 delete p_connectionToParent;
173 p_connectionToParent = NULL;
179 Application::~Application() {
193 int Application::Run(
void (*funct)()) {
196 if (p_ui->IsInteractive()) {
197 p_ui->TheGui()->Exec(funct);
200 if (p_ui->BatchListSize() > 0) {
201 for (
int i = 0; i < p_ui->BatchListSize(); i++) {
203 p_ui->SetBatchList(i);
206 p_datetime = DateTime(&p_startTime);
207 m_connectTime.start();
208 p_startClock = clock();
209 p_startDirectIO = DirectIO();
210 p_startPageFaults = PageFaults();
211 p_startProcessSwaps = ProcessSwaps();
212 SessionLog::TheLog(
true);
216 Application::FunctionCleanup();
220 p_ui->SetErrorList(i);
221 status = Application::FunctionError(e);
222 if (p_ui->AbortOnError()) {
223 for (
int j = (i + 1); j < p_ui->BatchListSize(); j++) {
224 p_ui->SetErrorList(j);
238 Application::FunctionCleanup();
243 status = Application::FunctionError(e);
247 catch (exception &e) {
248 QString message = e.what();
249 Isis::iExceptionSystem i(message,
_FILEINFO_);
253 QString message =
"Unknown error expection";
254 Isis::iExceptionSystem i(message,
_FILEINFO_);
268 if (p_ui->IsInteractive()) {
269 p_startClock = clock();
270 p_datetime = DateTime(&p_startTime);
271 m_connectTime.start();
275 history +=
PvlKeyword(
"IsisVersion", Version());
276 history +=
PvlKeyword(
"ProgramVersion", p_ui->Version());
277 QString path = QCoreApplication::applicationDirPath();
279 history +=
PvlKeyword(
"ExecutionDateTime", p_datetime);
280 history +=
PvlKeyword(
"HostName", HostName());
281 history +=
PvlKeyword(
"UserName", UserName());
282 history +=
PvlKeyword(
"Description", p_ui->Brief());
286 p_ui->CommandLine(pvl);
287 history.
addGroup(pvl.findGroup(
"UserParameters"));
298 double seconds = m_connectTime.elapsed() / 1000.0;
299 int minutes = (int)(seconds / 60.0);
300 seconds = seconds - minutes * 60.0;
301 int hours = minutes / 60;
302 minutes = minutes - hours * 60;
304 sprintf(temp,
"%02d:%02d:%04.1f", hours, minutes, seconds);
305 QString conTime = temp;
310 clock_t endClock = clock();
311 seconds = (double(endClock) - (double)p_startClock) / CLOCKS_PER_SEC;
312 minutes = (int)(seconds / 60.0);
313 seconds = seconds - minutes * 60.0;
314 hours = minutes / 60;
315 minutes = minutes - hours * 60;
316 sprintf(temp,
"%02d:%02d:%04.1f", hours, minutes, seconds);
317 QString cpuTime = temp;
342 int Application::DirectIO() {
343 return 0 - p_startDirectIO;
351 int Application::PageFaults() {
352 return 0 - p_startPageFaults;
360 int Application::ProcessSwaps() {
361 return 0 - p_startProcessSwaps;
371 static bool blankLine =
false;
373 SessionLog::TheLog().AddResults(results);
378 if (SessionLog::TheLog().TerminalOutput())
return;
383 if (blankLine) ostr << endl;
384 ostr << results << endl;
385 QString data = ostr.str().c_str();
392 if (blankLine) ostr << endl;
393 ostr << results << endl;
400 if (blankLine) cout << endl;
401 cout << results << endl;
411 void Application::GuiLog(
const Pvl &results) {
417 ostr << copy << endl;
418 QString data = ostr.str().c_str();
425 ostr << copy << endl;
436 void Application::GuiLog(
const PvlGroup &results) {
442 ostr << copy << endl;
443 QString data = ostr.str().c_str();
450 ostr << copy << endl;
461 void Application::GuiLog(
const QString &results) {
488 bool Application::HasParent() {
489 if (iApp == NULL)
return false;
490 if (iApp->
p_ui == NULL)
return false;
501 if (!HasParent())
return;
503 for (
int i = 0; i < errors.
groups(); i++) {
505 ostr << errors.
group(i) << endl;
506 QString data = ostr.str().c_str();
516 void Application::SendParentData(
const QString code,
517 const QString &message) {
519 if (p_connectionToParent == NULL) {
520 QString msg =
"This process (program) was executed by an existing Isis 3 " 521 "process. However, we failed to establish a communication channel " 522 "with the parent (launcher) process. The parent process has a PID of " 534 if (p_connectionToParent->write(data.toLatin1().data(), data.toLatin1().size()) == -1) {
535 QString msg =
"This process (program) was executed by an exiting Isis 3 " 536 "process. A communication channel was established with the parent " 537 "(launcher) process, but when we tried to send data to the parent " 538 "process an error occurred. The parent process has a PID of [" +
543 p_connectionToParent->waitForBytesWritten(-1);
552 void Application::FunctionCleanup() {
554 SessionLog::TheLog().Write();
556 if (SessionLog::TheLog().TerminalOutput()) {
559 ostr << SessionLog::TheLog() << endl;
560 QString data = ostr.str().c_str();
563 else if (p_ui->IsInteractive()) {
565 ostr << SessionLog::TheLog() << endl;
566 p_ui->TheGui()->Log(ostr.str().c_str());
567 p_ui->TheGui()->ShowLog();
570 cout << SessionLog::TheLog() << endl;
575 if (p_ui->GetInfoFlag()) {
576 QString filename = p_ui->GetInfoFileName();
578 QString app = (QString)QCoreApplication::applicationDirPath() +
"/" + p_appName;
579 if (p_BatchlistPass == 0) {
581 ss << SessionLog::TheLog();
591 if (filename.compare(
"") != 0) {
593 if (p_BatchlistPass == 0) {
594 ofstream debugingLog(filename.toLatin1().data());
595 if (!debugingLog.good()) {
596 QString msg =
"Error opening debugging log file [" + filename +
"]";
599 debugingLog << log << endl;
600 debugingLog <<
"\n############### User Preferences ################\n" << endl;
601 debugingLog << Preference::Preferences();
602 debugingLog <<
"\n############## System Disk Space ################\n" << endl;
603 debugingLog << GetSystemDiskSpace() << endl;
604 debugingLog <<
"\n############ Executable Information #############\n" << endl;
605 debugingLog << GetLibraryDependencies(app) << endl;
609 ofstream debugingLog(filename.toLatin1().data(), ios_base::app);
610 debugingLog << SessionLog::TheLog() << endl;
615 if (p_BatchlistPass == 0) {
617 cout <<
"\n############### User Preferences ################\n" << endl;
618 cout << Preference::Preferences();
619 cout <<
"\n############## System Disk Space ################\n" << endl;
620 cout << GetSystemDiskSpace() << endl;
621 cout <<
"\n############ Executable Information #############\n" << endl;
622 cout << GetLibraryDependencies(app) << endl;
625 cout << SessionLog::TheLog() << endl;
644 SessionLog::TheLog().AddError(errors);
645 SessionLog::TheLog().Write();
648 SendParentErrors(errors);
650 else if (p_ui->IsInteractive()) {
651 p_ui->TheGui()->LoadMessage(e.
toString());
653 else if (SessionLog::TheLog().TerminalOutput()) {
654 cerr << SessionLog::TheLog() << 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;
688 debugingLog << GetSystemDiskSpace() << endl;
689 debugingLog <<
"\n############ Executable Information #############\n" << endl;
690 debugingLog << GetLibraryDependencies(app) << 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;
705 cout << GetSystemDiskSpace() << endl;
706 cout <<
"\n############ Executable Information #############\n" << endl;
707 cout << GetLibraryDependencies(app) << endl;
710 cout << SessionLog::TheLog() << endl;
725 QString errorMessage = e.
toString();
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::p_appName(
"Unknown");
745 QString Application::Name() {
756 void Application::UpdateProgress(
const QString &text,
bool print) {
757 if (HasParent() && print) {
760 else if (p_ui->IsInteractive()) {
761 p_ui->TheGui()->ProgressText(text);
764 QString msg = p_ui->ProgramName() +
": " + text;
778 void Application::UpdateProgress(
int percent,
bool print) {
779 if (HasParent() && print) {
783 else if (p_ui->IsInteractive()) {
784 p_ui->TheGui()->Progress(percent);
788 cout << percent <<
"% Processed\r" << flush;
791 cout << percent <<
"% Processed" << endl;
802 void Application::ProcessGuiEvents() {
803 if (p_ui->IsInteractive()) {
804 if (p_ui->TheGui()->ProcessEvents()) {
818 QString Application::DateTime(time_t *curtime) {
819 time_t startTime = time(NULL);
820 if (curtime != 0) *curtime = startTime;
821 struct tm *tmbuf = localtime(&startTime);
823 strftime(timestr, 80,
"%Y-%m-%dT%H:%M:%S", tmbuf);
824 return(QString) timestr;
832 QString Application::UserName() {
841 QString Application::HostName() {
850 QString Application::Version() {
851 return isisVersion();
862 FileName temp = FileName::createTempFile(
"$temporary/UnameConsoleInfo.txt");
872 #if defined(__linux__) 874 ProgramLauncher::RunSystemCommand(
"uname -m > " + tempFile);
875 ProgramLauncher::RunSystemCommand(
"uname -p > " + tempFile);
876 ProgramLauncher::RunSystemCommand(
"uname -i > " + tempFile);
877 ProgramLauncher::RunSystemCommand(
"uname -o > " + tempFile);
878 ProgramLauncher::RunSystemCommand(
"uname -s > " + tempFile);
879 ProgramLauncher::RunSystemCommand(
"uname -v > " + tempFile);
880 ProgramLauncher::RunSystemCommand(
"uname -r > " + tempFile);
881 ProgramLauncher::RunSystemCommand(
"uname -a > " + tempFile);
884 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
885 readTemp.getline(value, 256);
887 readTemp.getline(value, 256);
889 readTemp.getline(value, 256);
891 readTemp.getline(value, 256);
893 readTemp.getline(value, 256);
895 readTemp.getline(value, 256);
897 readTemp.getline(value, 256);
899 readTemp.getline(value, 256);
902 #elif defined(__APPLE__) 904 ProgramLauncher::RunSystemCommand(
"uname -m > " + tempFile);
905 ProgramLauncher::RunSystemCommand(
"uname -p > " + tempFile);
906 ProgramLauncher::RunSystemCommand(
"uname -s > " + tempFile);
907 ProgramLauncher::RunSystemCommand(
"uname -v > " + tempFile);
908 ProgramLauncher::RunSystemCommand(
"uname -r > " + tempFile);
909 ProgramLauncher::RunSystemCommand(
"uname -a > " + tempFile);
913 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
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);
924 readTemp.getline(value, 256);
929 remove(tempFile.toLatin1().data());
942 FileName temp = FileName::createTempFile(
"$temporary/EnviromentInfo.txt");
944 PvlGroup envGroup(
"EnviromentVariables");
947 QString env1 =
"printenv SHELL >| " + tempFile;
948 QString env2 =
"printenv HOME >> " + tempFile;
949 QString env3 =
"printenv PWD >> " + tempFile;
950 QString env5 =
"printenv ISISROOT >> " + tempFile;
951 QString env6 =
"printenv ISIS3DATA >> " + tempFile;
952 ProgramLauncher::RunSystemCommand(env1);
953 ProgramLauncher::RunSystemCommand(env2);
954 ProgramLauncher::RunSystemCommand(env3);
955 ProgramLauncher::RunSystemCommand(env5);
956 ProgramLauncher::RunSystemCommand(env6);
959 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
960 readTemp.getline(value, 255);
962 readTemp.getline(value, 255);
964 readTemp.getline(value, 255);
966 readTemp.getline(value, 255);
968 readTemp.getline(value, 255);
972 QString cleanup =
"rm -f " + tempFile;
973 ProgramLauncher::RunSystemCommand(cleanup);
982 QString Application::GetSystemDiskSpace() {
983 FileName temp = FileName::createTempFile(
"$temporary/SystemDiskSpace.txt");
986 QString diskspace =
"df >| " + tempFile;
987 ProgramLauncher::RunSystemCommand(diskspace);
988 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
990 QString results =
"";
992 while (!readTemp.eof()) {
993 readTemp.getline(tmp, 512);
999 QString cleanup =
"rm -f " + tempFile;
1000 ProgramLauncher::RunSystemCommand(cleanup);
1009 QString Application::GetLibraryDependencies(QString file) {
1010 FileName temp = FileName::createTempFile(
"$temporary/LibraryDependencies.txt");
1011 QString tempFile = temp.
expanded();
1013 QString dependencies =
"";
1014 #if defined(__linux__) 1015 dependencies =
"ldd -v " + file +
" >| " + tempFile;
1016 #elif defined(__APPLE__) 1017 dependencies =
"otool -L " + file +
" >| " + tempFile;
1018 #elif defined (__sun__) 1019 dependencies =
"ldd -v " + file +
" >| " + tempFile;
1021 ProgramLauncher::RunSystemCommand(dependencies);
1022 readTemp.open(tempFile.toLatin1().data(), ifstream::in);
1024 QString results =
"";
1026 while (!readTemp.eof()) {
1027 readTemp.getline(tmp, 512);
1033 QString cleanup =
"rm -f " + tempFile;
1034 ProgramLauncher::RunSystemCommand(cleanup);
static UserInterface & GetUserInterface()
Returns the UserInterface object.
File name manipulation and expansion.
bool IsInteractive()
Indicates if the Isis Graphical User Interface is operating.
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Namespace for the standard library.
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
UserInterface * p_ui
Pointer to a User Interface object.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
void SendParentData(QString, const QString &)
void Log(const QString &text)
Write text to the gui log.
Contains multiple PvlContainers.
#define _FILEINFO_
Macro for the filename and line number.
int ParentId()
Returns the parent id.
A single keyword-value pair.
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Pvl toPvl() const
Returns a PVL object representing the contents of this exception.
Container for cube-like labels.
void print() const
Prints a string representation of this exception to stderr.
PvlGroup & group(const int index)
Return the group at the specified index.
QString toString() const
Returns a string representation of this exception.
ErrorType errorType() const
Returns the source of the error for this exception.
Namespace for ISIS/Bullet specific routines.
QString FileOpen(const QString &filename)
This error should be used when a file could not be opened.
Command Line and Xml loader, validation, and access.
int groups() const
Returns the number of groups contained.
Contains Pvl Groups and Pvl Objects.
bool fileExists() const
Returns true if the file exists; false otherwise.