Isis 3 Programmer Reference
SessionLog.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "SessionLog.h"
8#include "Preference.h"
9#include "IString.h"
10#include "Application.h"
11
12using namespace std;
13namespace Isis {
14 // Constructor
15 SessionLog::SessionLog() {
16 // Grab the user preferences for logging
17 Isis::PvlGroup &slog = Isis::Preference::Preferences().findGroup("SessionLog");
18 p_termOutput = ((QString)slog["TerminalOutput"]).toUpper() == "ON";
19 p_fileOutput = ((QString)slog["FileOutput"]).toUpper() == "ON";
20 p_outputFile = (QString) slog["FileName"];
21 p_access = ((QString) slog["FileAccess"]).toUpper();
22
23 // Add root
24 this->addObject(Isis::iApp->History());
25 p_errorAdded = false;
26 p_acctAdded = false;
27 p_root = &this->object(0);
28
29 atexit(Shutdown);
30 }
31
32 // Destructor
33 SessionLog::~SessionLog() {
34 }
35
36 // Singleton creator
37 SessionLog *SessionLog::p_log = NULL;
38
39 SessionLog &SessionLog::TheLog(bool restart) {
40 if(restart && (p_log != NULL)) {
41 delete p_log;
42 p_log = NULL;
43 }
44
45 if(p_log == NULL) {
46 // Create the singleton
47 p_log = new SessionLog();
48 }
49 return *p_log;
50 }
51
52 // Write info to the session log
53 void SessionLog::Write() {
54 AddAccounting();
55
56 // See if we should write to the print file
57 if(p_fileOutput) {
58 setTerminator("\n");
59 try {
60 if(p_access == "OVERWRITE") {
61 this->Isis::Pvl::write(p_outputFile);
62 }
63 else {
64 this->append(p_outputFile);
65 }
66 }
67 catch(...) {
68 std::cerr << "**WARNING** Unable to write session log [" <<
69 p_outputFile << "] Disk may be full or directory permissions not writeable"
70 << std::endl;
71 exit(1);
72 }
73 setTerminator("End");
74 }
75 }
76
77 void SessionLog::AddAccounting() {
78 // Update accounting if no errors
79 if(p_acctAdded) return;
80 if(!p_errorAdded) {
81 p_root->addGroup(Isis::iApp->Accounting());
82 }
83 p_acctAdded = true;
84 }
85
86 // Add an error message
87 void SessionLog::AddError(Isis::Pvl &e) {
88 for(int i = 0; i < e.groups(); i++) {
89 if(e.group(i).isNamed("Error")) {
90 p_root->addGroup(e.group(i));
91 p_errorAdded = true;
92 }
93 }
94 }
95
96 // Add results from an application
97 void SessionLog::AddResults(Isis::PvlGroup &results) {
98 p_root->addGroup(results);
99 }
100
101 std::ostream &operator<<(std::ostream &os, Isis::SessionLog &log) {
102 log.AddAccounting();
103 return operator<<(os, (Isis::Pvl &) log);
104 }
105
106 void SessionLog::Shutdown() {
107 if(p_log) {
108 delete p_log;
109 p_log = NULL;
110 }
111 }
112} // end namespace isis
113
Contains multiple PvlContainers.
Definition PvlGroup.h:41
Container for cube-like labels.
Definition Pvl.h:119
void write(const QString &file)
Opens and writes PVL information to a file and handles the end of line sequence.
Definition Pvl.cpp:130
void setTerminator(const QString &term)
Sets the terminator used to signify the end of the PVL informationDefaults to "END".
Definition Pvl.h:144
void append(const QString &file)
Appends PVL information to a file and handles the end of line sequence.
Definition Pvl.cpp:184
PvlObject & object(const int index)
Return the object at the specified index.
void addObject(const PvlObject &object)
Add a PvlObject.
Definition PvlObject.h:309
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition PvlObject.h:186
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QDebug operator<<(QDebug debug, const Hillshade &hillshade)
Print this class out to a QDebug object.
Namespace for the standard library.