Skip to content

Commit

Permalink
expose log levels in Python bindings (f3d-app#1417)
Browse files Browse the repository at this point in the history
* expose log levels in Python bindings

* CI formatting

* make tests cross-platform

* disable `F3D_WINDOWS_GUI`
  • Loading branch information
snoyer authored May 1, 2024
1 parent f0a3730 commit e748359
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/actions/python-ci/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ runs:
run: >
echo "SKBUILD_CMAKE_ARGS=-DF3D_DEPENDENCIES_DIR=${{ env.WORKSPACE }}/dependencies/install/bin;
-DCMAKE_MODULE_PATH=${{ env.WORKSPACE }}/dependencies/install/lib/cmake/OpenVDB/;
-DF3D_WINDOWS_GUI=OFF;
-DF3D_PLUGIN_BUILD_ALEMBIC=ON;
-DF3D_PLUGIN_BUILD_ASSIMP=ON;
-DF3D_PLUGIN_BUILD_DRACO=ON;
Expand Down
20 changes: 20 additions & 0 deletions python/F3DPythonBindings.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "image.h"
#include "interactor.h"
#include "loader.h"
#include "log.h"
#include "options.h"
#include "types.h"
#include "utils.h"
Expand Down Expand Up @@ -339,6 +340,25 @@ PYBIND11_MODULE(pyf3d, module)
"autoload_plugins", &f3d::engine::autoloadPlugins, "Automatically load internal plugins")
.def_static("get_plugins_list", &f3d::engine::getPluginsList);

// f3d::log
py::class_<f3d::log> log(module, "Log");

log //
.def_static("set_verbose_level", &f3d::log::setVerboseLevel, py::arg("level"),
py::arg("force_std_err") = false)
.def_static("set_use_coloring", &f3d::log::setUseColoring)
.def_static("print",
[](f3d::log::VerboseLevel& level, const std::string& message)
{ f3d::log::print(level, message); });

py::enum_<f3d::log::VerboseLevel>(log, "VerboseLevel")
.value("DEBUG", f3d::log::VerboseLevel::DEBUG)
.value("INFO", f3d::log::VerboseLevel::INFO)
.value("WARN", f3d::log::VerboseLevel::WARN)
.value("ERROR", f3d::log::VerboseLevel::ERROR)
.value("QUIET", f3d::log::VerboseLevel::QUIET)
.export_values();

// deprecated functions, will be removed in the next major release, F3D v3.0.0
#ifndef F3D_NO_DEPRECATED
#pragma GCC diagnostic push
Expand Down
44 changes: 44 additions & 0 deletions python/testing/test_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import subprocess
import sys


def test_default_level():
assert (
run_python(
"from f3d import Log",
"Log.set_use_coloring(False)",
"Log.print(Log.DEBUG, 'debug')",
"Log.print(Log.INFO, 'info')",
)
== "info\n"
)


def test_debug():
assert (
run_python(
"from f3d import Log",
"Log.set_use_coloring(False)",
"Log.set_verbose_level(Log.DEBUG)",
"Log.print(Log.DEBUG, 'debug')",
)
== "debug\n"
)


def test_coloring():
assert (
run_python(
"from f3d import Log",
"Log.set_use_coloring(True)",
"Log.print(Log.INFO, 'info')",
)
== "info\x1b[0m\n"
)


def run_python(*statements: str):
return subprocess.check_output(
[sys.executable, "-c", "; ".join(statements)],
text=True,
)

0 comments on commit e748359

Please sign in to comment.