mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 09:48:32 +00:00
IfcConvert Windows unicode support (#258)
This commit is contained in:
+87
-26
@@ -30,35 +30,56 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
using boost::property_tree::ptree;
|
||||
|
||||
namespace {
|
||||
static const char* severity_strings[] = {"Notice", "Warning", "Error"};
|
||||
|
||||
template <typename T>
|
||||
struct severity_strings {
|
||||
static const std::array<std::basic_string<T>, 3> value;
|
||||
};
|
||||
|
||||
void plain_text_message(std::ostream& os, const boost::optional<IfcSchema::IfcProduct*>& current_product, Logger::Severity type, const std::string& message, IfcEntityInstanceData* entity) {
|
||||
os << "[" << severity_strings[type] << "] ";
|
||||
const std::array<std::basic_string<char>, 3> severity_strings<char>::value = { "Notice", "Warning", "Error" };
|
||||
const std::array<std::basic_string<wchar_t>, 3> severity_strings<wchar_t>::value = { L"Notice", L"Warning", L"Error" };
|
||||
|
||||
template <typename T>
|
||||
void plain_text_message(T& os, const boost::optional<IfcSchema::IfcProduct*>& current_product, Logger::Severity type, const std::string& message, IfcEntityInstanceData* entity) {
|
||||
os << "[" << severity_strings<T::char_type>::value[type] << "] ";
|
||||
if (current_product) {
|
||||
os << "{" << (*current_product)->GlobalId() << "} ";
|
||||
os << "{" << (*current_product)->GlobalId().c_str() << "} ";
|
||||
}
|
||||
os << message << std::endl;
|
||||
os << message.c_str() << std::endl;
|
||||
if (entity) {
|
||||
std::string instance_string = entity->toString();
|
||||
if (instance_string.size() > 259) {
|
||||
instance_string = instance_string.substr(0, 256) + "...";
|
||||
}
|
||||
os << instance_string << std::endl;
|
||||
os << instance_string.c_str() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void json_message(std::ostream& os, const boost::optional<IfcSchema::IfcProduct*>& current_product, Logger::Severity type, const std::string& message, IfcEntityInstanceData* entity) {
|
||||
ptree pt;
|
||||
pt.put("level", severity_strings[type]);
|
||||
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>
|
||||
void json_message(T& os, const boost::optional<IfcSchema::IfcProduct*>& current_product, Logger::Severity type, const std::string& message, IfcEntityInstanceData* entity) {
|
||||
boost::property_tree::basic_ptree<std::basic_string<T::char_type>, std::basic_string<T::char_type> > pt;
|
||||
|
||||
// @todo this is crazy
|
||||
static const T::char_type level_string[] = { 'l', 'e', 'v', 'e', 'l', 0 };
|
||||
static const T::char_type product_string[] = { 'p', 'r', 'o', 'd', 'u', 'c', 't', 0 };
|
||||
static const T::char_type message_string[] = { 'm', 'e', 's', 's', 'a', 'g', 'e', 0 };
|
||||
static const T::char_type instance_string[] = { 'i', 'n', 's', 't', 'a', 'n', 'c', 'e', 0 };
|
||||
|
||||
pt.put(level_string, severity_strings<T::char_type>::value[type]);
|
||||
if (current_product) {
|
||||
pt.put("product", (**current_product).entity->toString());
|
||||
pt.put(product_string, string_as<T::char_type>((**current_product).entity->toString()));
|
||||
}
|
||||
pt.put("message", message);
|
||||
pt.put(message_string, string_as<T::char_type>(message));
|
||||
if (entity) {
|
||||
pt.put("instance", entity);
|
||||
pt.put(instance_string, string_as<T::char_type>(entity->toString()));
|
||||
}
|
||||
boost::property_tree::write_json(os, pt, false);
|
||||
}
|
||||
@@ -68,20 +89,50 @@ void Logger::SetProduct(boost::optional<IfcSchema::IfcProduct*> product) {
|
||||
current_product = product;
|
||||
}
|
||||
|
||||
void Logger::SetOutput(std::ostream* l1, std::ostream* l2) {
|
||||
void Logger::SetOutput(std::ostream* l1, std::ostream* l2) {
|
||||
wlog1 = wlog2 = 0;
|
||||
log1 = l1;
|
||||
log2 = l2;
|
||||
if ( ! log2 ) {
|
||||
if (!log2) {
|
||||
log2 = &log_stream;
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::SetOutput(std::wostream* l1, std::wostream* l2) {
|
||||
log1 = log2 = 0;
|
||||
wlog1 = l1;
|
||||
wlog2 = l2;
|
||||
if (!wlog2) {
|
||||
log2 = &log_stream;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Logger::log(T& log2, Logger::Severity type, const std::string& message, IfcEntityInstanceData* entity) {
|
||||
log2 << "[" << severity_strings[type] << "] ";
|
||||
if (current_product) {
|
||||
log2 << "{" << (*current_product)->GlobalId().c_str() << "} ";
|
||||
}
|
||||
log2 << message.c_str() << std::endl;
|
||||
if (entity) {
|
||||
log2 << entity->toString().c_str() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::Message(Logger::Severity type, const std::string& message, IfcEntityInstanceData* entity) {
|
||||
if (log2 && type >= verbosity) {
|
||||
if ((log2 || wlog2) && type >= verbosity) {
|
||||
if (format == FMT_PLAIN) {
|
||||
plain_text_message(*log2, current_product, type, message, entity);
|
||||
if (log2) {
|
||||
plain_text_message(*log2, current_product, type, message, entity);
|
||||
} else if (wlog2) {
|
||||
plain_text_message(*wlog2, current_product, type, message, entity);
|
||||
}
|
||||
} else if (format == FMT_JSON) {
|
||||
json_message(*log2, current_product, type, message, entity);
|
||||
if (log2) {
|
||||
json_message(*log2, current_product, type, message, entity);
|
||||
} else if (wlog2) {
|
||||
json_message(*wlog2, current_product, type, message, entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,18 +141,26 @@ void Logger::Message(Logger::Severity type, const std::exception& exception, Ifc
|
||||
Message(type, exception.what(), entity);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::Status(const std::string& message, bool new_line) {
|
||||
if (log1) {
|
||||
(*log1) << message;
|
||||
if ( new_line ) (*log1) << std::endl;
|
||||
else (*log1) << std::flush;
|
||||
status(*log1, message, new_line);
|
||||
} else if (wlog1) {
|
||||
status(*wlog1, message, new_line);
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::ProgressBar(int progress) {
|
||||
if (log1) {
|
||||
Status("\r[" + std::string(progress,'#') + std::string(50 - progress,' ') + "]", false);
|
||||
}
|
||||
Status("\r[" + std::string(progress,'#') + std::string(50 - progress,' ') + "]", false);
|
||||
}
|
||||
|
||||
std::string Logger::GetLog() {
|
||||
@@ -116,7 +175,9 @@ Logger::Format Logger::OutputFormat() { return format; }
|
||||
|
||||
std::ostream* Logger::log1 = 0;
|
||||
std::ostream* Logger::log2 = 0;
|
||||
std::wostream* Logger::wlog1 = 0;
|
||||
std::wostream* Logger::wlog2 = 0;
|
||||
std::stringstream Logger::log_stream;
|
||||
Logger::Severity Logger::verbosity = Logger::LOG_NOTICE;
|
||||
Logger::Format Logger::format = Logger::FMT_PLAIN;
|
||||
boost::optional<IfcSchema::IfcProduct*> Logger::current_product;
|
||||
boost::optional<IfcSchema::IfcProduct*> Logger::current_product;
|
||||
|
||||
Reference in New Issue
Block a user