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>
|
|
|
|
|
|
2018-05-12 20:12:25 +02:00
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
|
#include <boost/property_tree/json_parser.hpp>
|
|
|
|
|
#include <boost/version.hpp>
|
|
|
|
|
|
2019-07-03 12:10:27 +02:00
|
|
|
#include <mutex>
|
2016-05-11 09:56:40 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2018-05-12 20:12:25 +02:00
|
|
|
namespace {
|
2019-02-08 15:18:55 +01:00
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
struct severity_strings {
|
2020-08-16 13:37:58 +02:00
|
|
|
static const std::array<std::basic_string<T>, 4> value;
|
2019-02-08 15:18:55 +01:00
|
|
|
};
|
|
|
|
|
|
2019-02-12 14:55:03 +01:00
|
|
|
template <>
|
2020-08-16 13:37:58 +02:00
|
|
|
const std::array<std::basic_string<char>, 4> severity_strings<char>::value = { "Debug", "Notice", "Warning", "Error" };
|
2019-02-12 14:55:03 +01:00
|
|
|
|
|
|
|
|
template <>
|
2020-08-16 13:37:58 +02:00
|
|
|
const std::array<std::basic_string<wchar_t>, 4> severity_strings<wchar_t>::value = { L"Debug", L"Notice", L"Warning", L"Error" };
|
2019-02-08 15:18:55 +01:00
|
|
|
|
|
|
|
|
template <typename T>
|
2021-09-06 18:52:37 +02:00
|
|
|
void plain_text_message(T& os, const boost::optional<IfcUtil::IfcBaseClass*>& current_product, Logger::Severity type, const std::string& message, const IfcUtil::IfcBaseInterface* instance) {
|
2019-02-12 14:55:03 +01:00
|
|
|
os << "[" << severity_strings<typename T::char_type>::value[type] << "] ";
|
2018-05-12 20:12:25 +02:00
|
|
|
if (current_product) {
|
2019-03-08 17:22:05 +01:00
|
|
|
std::string global_id = *((IfcUtil::IfcBaseEntity*)*current_product)->get("GlobalId");
|
2019-03-08 11:40:16 +01:00
|
|
|
os << "{" << global_id.c_str() << "} ";
|
2018-05-12 20:12:25 +02:00
|
|
|
}
|
2019-02-08 15:18:55 +01:00
|
|
|
os << message.c_str() << std::endl;
|
2018-07-07 17:15:54 +02:00
|
|
|
if (instance) {
|
2019-03-08 17:22:05 +01: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) + "...";
|
|
|
|
|
}
|
2019-02-08 15:18:55 +01:00
|
|
|
os << instance_string.c_str() << std::endl;
|
2018-05-12 20:12:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 15:18:55 +01:00
|
|
|
template <typename T>
|
|
|
|
|
std::basic_string<T> string_as(const std::string& s) {
|
|
|
|
|
std::basic_string<T> v;
|
|
|
|
|
v.assign(s.begin(), s.end());
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2021-09-06 18:52:37 +02:00
|
|
|
void json_message(T& os, const boost::optional<IfcUtil::IfcBaseClass*>& current_product, Logger::Severity type, const std::string& message, const IfcUtil::IfcBaseInterface* instance) {
|
2019-02-12 14:55:03 +01:00
|
|
|
boost::property_tree::basic_ptree<std::basic_string<typename T::char_type>, std::basic_string<typename T::char_type> > pt;
|
2019-02-08 15:18:55 +01:00
|
|
|
|
|
|
|
|
// @todo this is crazy
|
2019-02-12 14:55:03 +01:00
|
|
|
static const typename T::char_type level_string[] = { 'l', 'e', 'v', 'e', 'l', 0 };
|
|
|
|
|
static const typename T::char_type product_string[] = { 'p', 'r', 'o', 'd', 'u', 'c', 't', 0 };
|
|
|
|
|
static const typename T::char_type message_string[] = { 'm', 'e', 's', 's', 'a', 'g', 'e', 0 };
|
|
|
|
|
static const typename T::char_type instance_string[] = { 'i', 'n', 's', 't', 'a', 'n', 'c', 'e', 0 };
|
2019-02-08 15:18:55 +01:00
|
|
|
|
2019-02-12 14:55:03 +01:00
|
|
|
pt.put(level_string, severity_strings<typename T::char_type>::value[type]);
|
2018-05-12 20:12:25 +02:00
|
|
|
if (current_product) {
|
2019-03-08 11:40:16 +01:00
|
|
|
pt.put(product_string, string_as<typename T::char_type>((**current_product).data().toString()));
|
2018-05-12 20:12:25 +02:00
|
|
|
}
|
2019-02-12 14:55:03 +01:00
|
|
|
pt.put(message_string, string_as<typename T::char_type>(message));
|
2018-07-07 17:15:54 +02:00
|
|
|
if (instance) {
|
2019-03-08 11:40:16 +01:00
|
|
|
pt.put(instance_string, string_as<typename T::char_type>(instance->data().toString()));
|
2018-05-12 20:12:25 +02:00
|
|
|
}
|
|
|
|
|
boost::property_tree::write_json(os, pt, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-11 09:56:40 +01:00
|
|
|
|
2019-03-08 11:40:16 +01:00
|
|
|
void Logger::SetProduct(boost::optional<IfcUtil::IfcBaseClass*> product) {
|
2020-08-16 13:37:58 +02:00
|
|
|
if (verbosity == LOG_DEBUG && product) {
|
|
|
|
|
Message(LOG_DEBUG, "Begin processing", *product);
|
|
|
|
|
}
|
2016-05-11 09:56:40 +01:00
|
|
|
current_product = product;
|
|
|
|
|
}
|
2018-05-12 20:12:25 +02:00
|
|
|
|
2019-02-08 15:18:55 +01:00
|
|
|
void Logger::SetOutput(std::ostream* l1, std::ostream* l2) {
|
|
|
|
|
wlog1 = wlog2 = 0;
|
2016-05-11 09:56:40 +01:00
|
|
|
log1 = l1;
|
|
|
|
|
log2 = l2;
|
2019-02-08 15:18:55 +01:00
|
|
|
if (!log2) {
|
2016-05-11 09:56:40 +01:00
|
|
|
log2 = &log_stream;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-01 16:45:11 +02:00
|
|
|
|
2019-02-08 15:18:55 +01:00
|
|
|
void Logger::SetOutput(std::wostream* l1, std::wostream* l2) {
|
|
|
|
|
log1 = log2 = 0;
|
|
|
|
|
wlog1 = l1;
|
|
|
|
|
wlog2 = l2;
|
|
|
|
|
if (!wlog2) {
|
|
|
|
|
log2 = &log_stream;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 18:52:37 +02:00
|
|
|
void Logger::Message(Logger::Severity type, const std::string& message, const IfcUtil::IfcBaseInterface* instance) {
|
2019-07-03 12:10:27 +02:00
|
|
|
static std::mutex m;
|
|
|
|
|
std::lock_guard<std::mutex> lk(m);
|
|
|
|
|
|
2019-06-04 17:24:59 +02:00
|
|
|
if (type > max_severity) {
|
|
|
|
|
max_severity = type;
|
|
|
|
|
}
|
2019-02-08 15:18:55 +01:00
|
|
|
if ((log2 || wlog2) && type >= verbosity) {
|
2018-05-12 20:12:25 +02:00
|
|
|
if (format == FMT_PLAIN) {
|
2019-02-08 15:18:55 +01:00
|
|
|
if (log2) {
|
2019-03-08 11:40:16 +01:00
|
|
|
plain_text_message(*log2, current_product, type, message, instance);
|
2019-02-08 15:18:55 +01:00
|
|
|
} else if (wlog2) {
|
2019-03-08 11:40:16 +01:00
|
|
|
plain_text_message(*wlog2, current_product, type, message, instance);
|
2019-02-08 15:18:55 +01:00
|
|
|
}
|
2018-05-12 20:12:25 +02:00
|
|
|
} else if (format == FMT_JSON) {
|
2019-02-08 15:18:55 +01:00
|
|
|
if (log2) {
|
2019-03-08 11:40:16 +01:00
|
|
|
json_message(*log2, current_product, type, message, instance);
|
2019-02-08 15:18:55 +01:00
|
|
|
} else if (wlog2) {
|
2019-03-08 11:40:16 +01:00
|
|
|
json_message(*wlog2, current_product, type, message, instance);
|
2019-02-08 15:18:55 +01:00
|
|
|
}
|
2017-12-11 14:47:30 +01:00
|
|
|
}
|
2016-05-11 09:56:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
2017-08-01 16:45:11 +02:00
|
|
|
|
2021-09-06 18:52:37 +02:00
|
|
|
void Logger::Message(Logger::Severity type, const std::exception& exception, const IfcUtil::IfcBaseInterface* instance) {
|
2018-01-08 12:33:27 +01:00
|
|
|
Message(type, std::string(exception.what()), instance);
|
2017-08-01 16:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-08 15:18:55 +01:00
|
|
|
template <typename T>
|
|
|
|
|
void status(T& log1, const std::string& message, bool new_line) {
|
|
|
|
|
log1 << message.c_str();
|
|
|
|
|
if (new_line) {
|
|
|
|
|
log1 << std::endl;
|
|
|
|
|
} else {
|
|
|
|
|
log1 << std::flush;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-11 09:56:40 +01:00
|
|
|
void Logger::Status(const std::string& message, bool new_line) {
|
2018-05-12 20:12:25 +02:00
|
|
|
if (log1) {
|
2019-02-08 15:18:55 +01:00
|
|
|
status(*log1, message, new_line);
|
|
|
|
|
} else if (wlog1) {
|
|
|
|
|
status(*wlog1, message, new_line);
|
2016-05-11 09:56:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-05-12 20:12:25 +02:00
|
|
|
|
2016-05-11 09:56:40 +01:00
|
|
|
void Logger::ProgressBar(int progress) {
|
2019-02-08 15:18:55 +01:00
|
|
|
Status("\r[" + std::string(progress,'#') + std::string(50 - progress,' ') + "]", false);
|
2016-05-11 09:56:40 +01:00
|
|
|
}
|
2018-05-12 20:12:25 +02:00
|
|
|
|
2016-05-11 09:56:40 +01:00
|
|
|
std::string Logger::GetLog() {
|
|
|
|
|
return log_stream.str();
|
|
|
|
|
}
|
2018-05-12 20:12:25 +02:00
|
|
|
|
2016-05-11 09:56:40 +01:00
|
|
|
void Logger::Verbosity(Logger::Severity v) { verbosity = v; }
|
|
|
|
|
Logger::Severity Logger::Verbosity() { return verbosity; }
|
|
|
|
|
|
2018-12-12 12:33:09 +01:00
|
|
|
Logger::Severity Logger::MaxSeverity() { return max_severity; }
|
|
|
|
|
|
2018-05-12 20:12:25 +02:00
|
|
|
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;
|
2019-02-08 15:18:55 +01:00
|
|
|
std::wostream* Logger::wlog1 = 0;
|
|
|
|
|
std::wostream* Logger::wlog2 = 0;
|
2016-05-11 09:56:40 +01:00
|
|
|
std::stringstream Logger::log_stream;
|
|
|
|
|
Logger::Severity Logger::verbosity = Logger::LOG_NOTICE;
|
2018-12-12 12:33:09 +01:00
|
|
|
Logger::Severity Logger::max_severity = Logger::LOG_NOTICE;
|
2018-05-12 20:12:25 +02:00
|
|
|
Logger::Format Logger::format = Logger::FMT_PLAIN;
|
2018-01-08 12:33:27 +01:00
|
|
|
boost::optional<IfcUtil::IfcBaseClass*> Logger::current_product;
|