Isis 3 Programmer Reference
SessionLog.cpp
Go to the documentation of this file.
1 
23 #include "SessionLog.h"
24 #include "Preference.h"
25 #include "IString.h"
26 #include "Application.h"
27 
28 using namespace std;
29 namespace Isis {
30  // Constructor
31  SessionLog::SessionLog() {
32  // Grab the user preferences for logging
33  Isis::PvlGroup &slog = Isis::Preference::Preferences().findGroup("SessionLog");
34  p_termOutput = ((QString)slog["TerminalOutput"]).toUpper() == "ON";
35  p_fileOutput = ((QString)slog["FileOutput"]).toUpper() == "ON";
36  p_outputFile = (QString) slog["FileName"];
37  p_access = ((QString) slog["FileAccess"]).toUpper();
38 
39  // Add root
40  this->addObject(Isis::iApp->History());
41  p_errorAdded = false;
42  p_acctAdded = false;
43  p_root = &this->object(0);
44 
45  atexit(Shutdown);
46  }
47 
48  // Destructor
49  SessionLog::~SessionLog() {
50  }
51 
52  // Singleton creator
53  SessionLog *SessionLog::p_log = NULL;
54 
55  SessionLog &SessionLog::TheLog(bool restart) {
56  if(restart && (p_log != NULL)) {
57  delete p_log;
58  p_log = NULL;
59  }
60 
61  if(p_log == NULL) {
62  // Create the singleton
63  p_log = new SessionLog();
64  }
65  return *p_log;
66  }
67 
68  // Write info to the session log
69  void SessionLog::Write() {
70  AddAccounting();
71 
72  // See if we should write to the print file
73  if(p_fileOutput) {
74  setTerminator("\n");
75  try {
76  if(p_access == "OVERWRITE") {
77  this->Isis::Pvl::write(p_outputFile);
78  }
79  else {
80  this->append(p_outputFile);
81  }
82  }
83  catch(...) {
84  std::cerr << "**WARNING** Unable to write session log [" <<
85  p_outputFile << "] Disk may be full or directory permissions not writeable"
86  << std::endl;
87  exit(1);
88  }
89  setTerminator("End");
90  }
91  }
92 
93  void SessionLog::AddAccounting() {
94  // Update accounting if no errors
95  if(p_acctAdded) return;
96  if(!p_errorAdded) {
97  p_root->addGroup(Isis::iApp->Accounting());
98  }
99  p_acctAdded = true;
100  }
101 
102  // Add an error message
103  void SessionLog::AddError(Isis::Pvl &e) {
104  for(int i = 0; i < e.groups(); i++) {
105  if(e.group(i).isNamed("Error")) {
106  p_root->addGroup(e.group(i));
107  p_errorAdded = true;
108  }
109  }
110  }
111 
112  // Add results from an application
113  void SessionLog::AddResults(Isis::PvlGroup &results) {
114  p_root->addGroup(results);
115  }
116 
117  std::ostream &operator<<(std::ostream &os, Isis::SessionLog &log) {
118  log.AddAccounting();
119  return operator<<(os, (Isis::Pvl &) log);
120  }
121 
122  void SessionLog::Shutdown() {
123  if(p_log) {
124  delete p_log;
125  p_log = NULL;
126  }
127  }
128 } // end namespace isis
129 
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
Namespace for the standard library.
QDebug operator<<(QDebug dbg, const Isis::Angle &angleToPrint)
Display an Angle for a debugging statement.
Definition: Angle.cpp:383
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
Container for cube-like labels.
Definition: Pvl.h:135
bool isNamed(const QString &match) const
Returns whether the given string is equal to the container name or not.
Definition: PvlContainer.h:86
PvlGroup & group(const int index)
Return the group at the specified index.
Definition: PvlObject.cpp:423
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
int groups() const
Returns the number of groups contained.
Definition: PvlObject.h:87
void write(const QString &file)
Opens and writes PVL information to a file and handles the end of line sequence.
Definition: Pvl.cpp:116