Pass around non-static logger instances and programmatic access to messages in-memory

This commit is contained in:
Thomas Krijnen
2026-06-10 18:30:54 +02:00
parent a751fb956d
commit a7738eeb64
132 changed files with 1029 additions and 884 deletions
+15 -15
View File
@@ -882,10 +882,10 @@ struct ShapeRTTI : public boost::static_visitor<PyObject*>
}
template <typename Schema>
static boost::variant<IfcGeom::Element*, IfcGeom::Representation::Representation*, IfcGeom::Transformation*> helper_fn_create_shape(const std::string& geometry_library, ifcopenshell::geometry::Settings& st, IfcUtil::IfcBaseClass* instance, IfcUtil::IfcBaseClass* representation = 0) {
static boost::variant<IfcGeom::Element*, IfcGeom::Representation::Representation*, IfcGeom::Transformation*> helper_fn_create_shape(Logger& logger, const std::string& geometry_library, ifcopenshell::geometry::Settings& st, IfcUtil::IfcBaseClass* instance, IfcUtil::IfcBaseClass* representation = 0) {
IfcParse::IfcFile* file = instance->file_;
ifcopenshell::geometry::Converter kernel(ifcopenshell::geometry::kernels::construct(file, geometry_library, st), file, st);
ifcopenshell::geometry::Converter kernel(ifcopenshell::geometry::kernels::construct(file, geometry_library, st, logger), file, st, logger);
if (typename Schema::IfcProduct* product = instance->as<typename Schema::IfcProduct>()) {
if (representation) {
@@ -1064,67 +1064,67 @@ ifcopenshell::geometry::taxonomy::item::ptr try_upcast(PyObject* obj0, swig_type
%}
%inline %{
static boost::variant<IfcGeom::Element*, IfcGeom::Representation::Representation*, IfcGeom::Transformation*> create_shape(ifcopenshell::geometry::Settings& settings, IfcUtil::IfcBaseClass* instance, IfcUtil::IfcBaseClass* representation = 0, const char* const geometry_library="opencascade") {
static boost::variant<IfcGeom::Element*, IfcGeom::Representation::Representation*, IfcGeom::Transformation*> create_shape(ifcopenshell::geometry::Settings& settings, IfcUtil::IfcBaseClass* instance, IfcUtil::IfcBaseClass* representation = 0, const char* const geometry_library="opencascade", Logger& logger = Logger::Root()) {
const std::string& schema_name = instance->declaration().schema()->name();
#ifdef HAS_SCHEMA_2x3
if (schema_name == "IFC2X3") {
return helper_fn_create_shape<Ifc2x3>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc2x3>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4
if (schema_name == "IFC4") {
return helper_fn_create_shape<Ifc4>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4x1
if (schema_name == "IFC4X1") {
return helper_fn_create_shape<Ifc4x1>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4x1>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4x2
if (schema_name == "IFC4X2") {
return helper_fn_create_shape<Ifc4x2>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4x2>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4x3_rc1
if (schema_name == "IFC4X3_RC1") {
return helper_fn_create_shape<Ifc4x3_rc1>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4x3_rc1>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4x3_rc2
if (schema_name == "IFC4X3_RC2") {
return helper_fn_create_shape<Ifc4x3_rc2>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4x3_rc2>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4x3_rc3
if (schema_name == "IFC4X3_RC3") {
return helper_fn_create_shape<Ifc4x3_rc3>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4x3_rc3>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4x3_rc4
if (schema_name == "IFC4X3_RC4") {
return helper_fn_create_shape<Ifc4x3_rc4>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4x3_rc4>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4x3
if (schema_name == "IFC4X3") {
return helper_fn_create_shape<Ifc4x3>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4x3>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4x3_tc1
if (schema_name == "IFC4X3_TC1") {
return helper_fn_create_shape<Ifc4x3_tc1>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4x3_tc1>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4x3_add1
if (schema_name == "IFC4X3_ADD1") {
return helper_fn_create_shape<Ifc4x3_add1>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4x3_add1>(logger, geometry_library, settings, instance, representation);
}
#endif
#ifdef HAS_SCHEMA_4x3_add2
if (schema_name == "IFC4X3_ADD2") {
return helper_fn_create_shape<Ifc4x3_add2>(geometry_library, settings, instance, representation);
return helper_fn_create_shape<Ifc4x3_add2>(logger, geometry_library, settings, instance, representation);
}
#endif
+34 -8
View File
@@ -80,6 +80,7 @@ private:
%rename("file") IfcFile;
%rename("add") addEntity;
%rename("remove") removeEntity;
%rename("logger") Logger;
class attribute_value_derived {};
%{
@@ -702,6 +703,7 @@ private:
%include "../ifcparse/IfcBaseClass.h"
%include "../ifcparse/IfcSchema.h"
%include "../serializers/RocksDbSerializer.h"
%include "../ifcparse/IfcLogger.h"
// The IfcFile* returned by open() is to be freed by SWIG/Python
%newobject open;
@@ -878,7 +880,7 @@ private:
static std::stringstream ifcopenshell_log_stream;
%}
%init %{
Logger::SetOutput(0, &ifcopenshell_log_stream);
Logger::Root().SetOutput(0, &ifcopenshell_log_stream);
%}
%inline %{
std::string get_log() {
@@ -887,20 +889,20 @@ private:
return log;
}
void turn_on_detailed_logging() {
Logger::SetOutput(&std::cout, &std::cout);
Logger::Verbosity(Logger::LOG_DEBUG);
Logger::Root().SetOutput(&std::cout, &std::cout);
Logger::Root().Verbosity(Logger::LOG_DEBUG);
}
void turn_off_detailed_logging() {
Logger::SetOutput(0, &ifcopenshell_log_stream);
Logger::Verbosity(Logger::LOG_WARNING);
Logger::Root().SetOutput(0, &ifcopenshell_log_stream);
Logger::Root().Verbosity(Logger::LOG_WARNING);
}
void set_log_format_json() {
ifcopenshell_log_stream.str("");
Logger::OutputFormat(Logger::FMT_JSON);
Logger::Root().OutputFormat(Logger::FMT_JSON);
}
void set_log_format_text() {
ifcopenshell_log_stream.str("");
Logger::OutputFormat(Logger::FMT_PLAIN);
Logger::Root().OutputFormat(Logger::FMT_PLAIN);
}
%}
@@ -1221,4 +1223,28 @@ private:
return d;
}
}
%extend Logger {
%pythoncode %{
def __iter__(self):
return iter(self.log_messages())
%}
}
%extend log_message {
std::string severity_string() const {
static const char* const severity_strings[] = {"PERF", "DEBUG", "NOTICE", "WARNING", "ERROR"};
return severity_strings[(int)$self->severity];
}
%pythoncode %{
severity_string = property(severity_string)
def to_tuple(self):
return self.severity_string, self.code, self.message, self.instance
def __eq__(self, other):
return type(self) == type(other) and self.to_tuple() == other.to_tuple()
def __hash__(self):
return hash(self.to_tuple())
def __repr__(self):
return "<log_message '[%s] [%s] %s'>" % (self.severity_string, self.code, self.message)
%}
}
+1
View File
@@ -169,6 +169,7 @@
PyObject* pythonize(const IfcParse::inverse_attribute* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_IfcParse__inverse_attribute, 0); }
PyObject* pythonize(const IfcParse::entity* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_IfcParse__entity, 0); }
PyObject* pythonize(const IfcParse::declaration* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), declaration_type_to_swig(t), 0); }
PyObject* pythonize(const log_message& t) { return SWIG_NewPointerObj(SWIG_as_voidptr(&t), SWIGTYPE_p_log_message, 0); }
// @nb ownership
PyObject* pythonize(const IfcGeom::ConversionResultShape* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_IfcGeom__ConversionResultShape, SWIG_POINTER_OWN); }
// PyObject* pythonize(const IfcGeom::ConversionResultShape* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_IfcGeom__ConversionResultShape, 0); }
+18
View File
@@ -406,3 +406,21 @@ CREATE_OPTIONAL_TYPEMAP_IN(std::string, string, str)
CREATE_SET_TYPEMAP_IN(int)
CREATE_SET_TYPEMAP_IN(std::string)
// for the logger codes
%typemap(typecheck, precedence=SWIG_TYPECHECK_STRING) SWIGTYPE &code_prefix {
$1 = PyUnicode_Check($input);
}
%typemap(in) SWIGTYPE &code_prefix (char tmp[4]) {
Py_ssize_t len = 0;
const char *s = PyUnicode_AsUTF8AndSize($input, &len);
if (!s || len != 3) {
SWIG_exception_fail(SWIG_ValueError, "Expected Python str of length 3");
}
memcpy(tmp, s, 3);
tmp[3] = '\0';
$1 = &tmp;
}
+1
View File
@@ -108,6 +108,7 @@ CREATE_VECTOR_TYPEMAP_OUT(IfcParse::inverse_attribute const *)
CREATE_VECTOR_TYPEMAP_OUT(IfcParse::entity const *)
CREATE_VECTOR_TYPEMAP_OUT(IfcParse::declaration const *)
CREATE_VECTOR_TYPEMAP_OUT(IfcGeom::ConversionResultShape *)
CREATE_VECTOR_TYPEMAP_OUT(log_message)
%typemap(out) ifcopenshell::geometry::Settings::value_variant_t {
pythonizing_visitor vis;