Files
IfcOpenShell/src/ifcparse/IfcLogger.cpp
T

122 lines
4.6 KiB
C++
Raw Normal View History

2016-05-11 09:56:40 +01:00
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#include "IfcLogger.h"
2017-12-22 13:51:11 +01:00
2016-05-11 09:56:40 +01:00
#include "../ifcparse/IfcException.h"
2017-12-22 13:51:11 +01:00
#include "../ifcparse/Argument.h"
2016-05-11 09:56:40 +01:00
#include <boost/algorithm/string/replace.hpp>
#include <boost/optional.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/version.hpp>
2016-05-11 09:56:40 +01:00
#include <iostream>
#include <algorithm>
using boost::property_tree::ptree;
namespace {
static const char* severity_strings[] = {"Notice", "Warning", "Error"};
2018-07-07 17:15:54 +02:00
void plain_text_message(std::ostream& os, const boost::optional<IfcUtil::IfcBaseClass*>& current_product, Logger::Severity type, const std::string& message, const IfcUtil::IfcBaseClass* instance) {
os << "[" << severity_strings[type] << "] ";
if (current_product) {
2018-07-07 17:15:54 +02:00
std::string global_id = *(**current_product).data().getArgument((**current_product).declaration().as_entity()->attribute_index("GlobalId"));
os << "{" << global_id << "} ";
}
os << message << std::endl;
2018-07-07 17:15:54 +02:00
if (instance) {
2018-09-17 17:46:13 +02:00
std::string instance_string = instance->data().toString();
2018-09-12 13:26:11 +02:00
if (instance_string.size() > 259) {
instance_string = instance_string.substr(0, 256) + "...";
}
os << instance_string << std::endl;
}
}
2018-07-07 17:15:54 +02:00
void json_message(std::ostream& os, const boost::optional<IfcUtil::IfcBaseClass*>& current_product, Logger::Severity type, const std::string& message, const IfcUtil::IfcBaseClass* instance) {
ptree pt;
pt.put("level", severity_strings[type]);
if (current_product) {
2018-07-07 17:15:54 +02:00
pt.put("product", (**current_product).data().toString());
}
pt.put("message", message);
2018-07-07 17:15:54 +02:00
if (instance) {
pt.put("instance", instance->data().toString());
}
boost::property_tree::write_json(os, pt, false);
}
}
2016-05-11 09:56:40 +01:00
void Logger::SetOutput(std::ostream* l1, std::ostream* l2) {
log1 = l1;
log2 = l2;
if ( ! log2 ) {
log2 = &log_stream;
}
}
2017-08-01 16:45:11 +02:00
2017-12-11 15:07:40 +01:00
void Logger::Message(Logger::Severity type, const std::string& message, const IfcUtil::IfcBaseClass* instance) {
2018-07-07 17:15:54 +02:00
if (log2 && type >= verbosity) {
if (format == FMT_PLAIN) {
2018-07-07 17:15:54 +02:00
plain_text_message(*log2, current_product, type, message, instance);
} else if (format == FMT_JSON) {
2018-07-07 17:15:54 +02:00
json_message(*log2, current_product, type, message, instance);
2017-12-11 14:47:30 +01:00
}
2016-05-11 09:56:40 +01:00
}
}
2017-08-01 16:45:11 +02:00
2017-12-11 15:07:40 +01:00
void Logger::Message(Logger::Severity type, const std::exception& exception, const IfcUtil::IfcBaseClass* instance) {
2018-01-08 12:33:27 +01:00
Message(type, std::string(exception.what()), instance);
2017-08-01 16:45:11 +02:00
}
2016-05-11 09:56:40 +01:00
void Logger::Status(const std::string& message, bool new_line) {
if (log1) {
2016-05-11 09:56:40 +01:00
(*log1) << message;
if ( new_line ) (*log1) << std::endl;
else (*log1) << std::flush;
}
}
2016-05-11 09:56:40 +01:00
void Logger::ProgressBar(int progress) {
if (log1) {
2016-05-11 09:56:40 +01:00
Status("\r[" + std::string(progress,'#') + std::string(50 - progress,' ') + "]", false);
}
}
2016-05-11 09:56:40 +01:00
std::string Logger::GetLog() {
return log_stream.str();
}
2016-05-11 09:56:40 +01:00
void Logger::Verbosity(Logger::Severity v) { verbosity = v; }
Logger::Severity Logger::Verbosity() { return verbosity; }
void Logger::OutputFormat(Format f) { format = f; }
Logger::Format Logger::OutputFormat() { return format; }
2016-05-11 09:56:40 +01:00
std::ostream* Logger::log1 = 0;
std::ostream* Logger::log2 = 0;
std::stringstream Logger::log_stream;
Logger::Severity Logger::verbosity = Logger::LOG_NOTICE;
Logger::Format Logger::format = Logger::FMT_PLAIN;
2018-01-08 12:33:27 +01:00
boost::optional<IfcUtil::IfcBaseClass*> Logger::current_product;