| 1 |
#include "Ioss_Utils.h" |
| 2 |
#include "visualization/utils/Iovs_CatalystLogging.h" |
| 3 |
#include <fstream> |
| 4 |
#include <iostream> |
| 5 |
#include <time.h> |
| 6 |
|
| 7 |
namespace Iovs { |
| 8 |
|
| 9 |
CatalystLogging::CatalystLogging() { initializeDefaults(); } |
| 10 |
|
| 11 |
void CatalystLogging::initializeDefaults() |
| 12 |
{ |
| 13 |
catalystLoggingEnabled = false; |
| 14 |
logFileName = getDefaultLogFileName(); |
| 15 |
logOutputDirectoryPath = getDefaultLogOutputDirectoryPath(); |
| 16 |
properties = nullptr; |
| 17 |
} |
| 18 |
|
| 19 |
void CatalystLogging::setProperties(const Ioss::PropertyManager *my_properties) |
| 20 |
{ |
| 21 |
initializeDefaults(); |
| 22 |
this->properties = my_properties; |
| 23 |
if (this->properties) { |
| 24 |
if (this->properties->exists(enabledProp)) { |
| 25 |
catalystLoggingEnabled = this->properties->get(enabledProp).get_int(); |
| 26 |
} |
| 27 |
if (this->properties->exists(fileNameProp)) { |
| 28 |
logFileName = this->properties->get(fileNameProp).get_string(); |
| 29 |
} |
| 30 |
if (this->properties->exists(directoryPathProp)) { |
| 31 |
logOutputDirectoryPath = this->properties->get(directoryPathProp).get_string(); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
std::vector<std::string> CatalystLogging::writeToLogFile() |
| 37 |
{ |
| 38 |
std::vector<std::string> logLine; |
| 39 |
if (properties) { |
| 40 |
std::fstream logFile; |
| 41 |
logFile.open(getLogFilePath(), std::ios::out | std::ios::app); |
| 42 |
if (!logFile) { |
| 43 |
std::ostringstream errmsg; |
| 44 |
errmsg << "Unable to open Catalyst log file: " << getLogFilePath() << "\n"; |
| 45 |
IOSS_ERROR(errmsg); |
| 46 |
} |
| 47 |
else { |
| 48 |
std::vector<std::string> headers = getLogFileHeaders(); |
| 49 |
if (isLogFileEmpty()) { |
| 50 |
writeVectorWithDelimeter(logFile, headers, getDelimeter()); |
| 51 |
} |
| 52 |
logLine = getLogOutputFromProps(headers); |
| 53 |
writeVectorWithDelimeter(logFile, logLine, getDelimeter()); |
| 54 |
logFile.close(); |
| 55 |
} |
| 56 |
} |
| 57 |
return logLine; |
| 58 |
} |
| 59 |
|
| 60 |
std::vector<std::string> CatalystLogging::getLogFileHeaders() |
| 61 |
{ |
| 62 |
std::vector<std::string> headers; |
| 63 |
if (properties) { |
| 64 |
Ioss::NameList names = properties->describe(); |
| 65 |
for (auto &name : names) { |
| 66 |
if (isCatalystLoggingProp(name)) { |
| 67 |
if (isSupportedPropType(name) && !isReservedPropName(name)) { |
| 68 |
headers.push_back(getHeaderNameFromPropName(name)); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
std::sort(headers.begin(), headers.end()); |
| 74 |
return headers; |
| 75 |
} |
| 76 |
|
| 77 |
bool CatalystLogging::isLogFileEmpty() |
| 78 |
{ |
| 79 |
std::ifstream logFile; |
| 80 |
logFile.open(getLogFilePath()); |
| 81 |
bool result = logFile.peek() == std::ifstream::traits_type::eof(); |
| 82 |
logFile.close(); |
| 83 |
return result; |
| 84 |
} |
| 85 |
|
| 86 |
void CatalystLogging::writeVectorWithDelimeter(std::fstream &file, |
| 87 |
const std::vector<std::string> &string_vector, |
| 88 |
char delimeter) |
| 89 |
{ |
| 90 |
if (string_vector.empty()) { |
| 91 |
return; |
| 92 |
} |
| 93 |
for (size_t i = 0; i < string_vector.size(); i++) { |
| 94 |
file << string_vector[i]; |
| 95 |
if (i < string_vector.size() - 1) { |
| 96 |
file << delimeter; |
| 97 |
} |
| 98 |
} |
| 99 |
file << "\n"; |
| 100 |
} |
| 101 |
|
| 102 |
std::vector<std::vector<std::string>> CatalystLogging::readLogFile() |
| 103 |
{ |
| 104 |
return readLogFile(getLogFilePath()); |
| 105 |
} |
| 106 |
|
| 107 |
std::vector<std::vector<std::string>> CatalystLogging::readLogFile(const std::string &logFilePath) |
| 108 |
{ |
| 109 |
std::vector<std::vector<std::string>> result; |
| 110 |
std::fstream logFile; |
| 111 |
logFile.open(logFilePath); |
| 112 |
if (logFile) { |
| 113 |
std::string line; |
| 114 |
while (getline(logFile, line)) { |
| 115 |
result.push_back( |
| 116 |
CatalystLogging::splitStringWithDelimeter(line, CatalystLogging::getDelimeter())); |
| 117 |
} |
| 118 |
logFile.close(); |
| 119 |
} |
| 120 |
return result; |
| 121 |
} |
| 122 |
|
| 123 |
std::vector<std::string> CatalystLogging::splitStringWithDelimeter(const std::string &input, |
| 124 |
char delimeter) |
| 125 |
{ |
| 126 |
std::string buffer = ""; |
| 127 |
std::vector<std::string> result; |
| 128 |
enum ParseState { UNQUOTED, QUOTED, QUOTEDQUOTE }; |
| 129 |
ParseState state = UNQUOTED; |
| 130 |
for (size_t i = 0; i < input.size(); i++) { |
| 131 |
switch (state) { |
| 132 |
case UNQUOTED: |
| 133 |
if (input[i] == delimeter) { |
| 134 |
result.push_back(buffer); |
| 135 |
buffer.clear(); |
| 136 |
} |
| 137 |
else if (input[i] == '"') { |
| 138 |
state = QUOTED; |
| 139 |
buffer += "\""; |
| 140 |
} |
| 141 |
else { |
| 142 |
buffer += input[i]; |
| 143 |
} |
| 144 |
break; |
| 145 |
case QUOTED: |
| 146 |
if (input[i] == '"') { |
| 147 |
state = UNQUOTED; |
| 148 |
buffer += "\""; |
| 149 |
} |
| 150 |
else { |
| 151 |
buffer += input[i]; |
| 152 |
} |
| 153 |
break; |
| 154 |
case QUOTEDQUOTE: |
| 155 |
if (input[i] == delimeter) { |
| 156 |
state = UNQUOTED; |
| 157 |
result.push_back(buffer); |
| 158 |
buffer.clear(); |
| 159 |
} |
| 160 |
else if (input[i] == '"') { |
| 161 |
state = QUOTED; |
| 162 |
buffer += "\""; |
| 163 |
} |
| 164 |
else { |
| 165 |
state = UNQUOTED; |
| 166 |
buffer += input[i]; |
| 167 |
} |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
if (!buffer.empty()) { |
| 172 |
result.push_back(buffer); |
| 173 |
} |
| 174 |
return result; |
| 175 |
} |
| 176 |
|
| 177 |
std::vector<std::string> CatalystLogging::getLogOutputFromProps(std::vector<std::string> &headers) |
| 178 |
{ |
| 179 |
std::vector<std::string> logOutput; |
| 180 |
if (properties) { |
| 181 |
for (auto &header : headers) { |
| 182 |
std::string propName = getPropNameFromHeaderName(header); |
| 183 |
Ioss::Property prop = properties->get(propName); |
| 184 |
switch (prop.get_type()) { |
| 185 |
case Ioss::Property::REAL: logOutput.push_back(std::to_string(prop.get_real())); break; |
| 186 |
case Ioss::Property::INTEGER: logOutput.push_back(std::to_string(prop.get_int())); break; |
| 187 |
case Ioss::Property::STRING: logOutput.push_back(prop.get_string()); break; |
| 188 |
default: logOutput.push_back("Unsupported property type for " + propName); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
return logOutput; |
| 193 |
} |
| 194 |
|
| 195 |
} // namespace Iovs |