Skip to content

Commit

Permalink
set log level in context ctor
Browse files Browse the repository at this point in the history
add set_log_level method
add python bindings for set_log_level and log levels enum
use set_log_level in mamba
  • Loading branch information
adriendelsalle committed Nov 19, 2021
1 parent 3e310f0 commit 4e32959
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions libmamba/include/mamba/core/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ namespace mamba
long max_parallel_downloads = 5;
int verbosity = 0;
void set_verbosity(int lvl);
void set_log_level(const spdlog::level::level_enum& level);

spdlog::level::level_enum log_level = spdlog::level::level_enum::warn;
std::string log_pattern = "%^%-8!l%$ %v";
Expand Down
4 changes: 2 additions & 2 deletions libmamba/src/api/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,14 @@ namespace mamba
}
else
{
ctx.set_verbosity(lvl);
ctx.verbosity = lvl;
// Make it appears like set with the CLI
// TODO: find a better way than passing by YAML to convert to string?
log_level.set_cli_config(YAML::Node(ctx.verbosity).as<std::string>());
}
}
*/
ctx.set_verbosity(lvl);
ctx.verbosity = lvl;
}

void target_prefix_checks_hook(int& options)
Expand Down
8 changes: 7 additions & 1 deletion libmamba/src/core/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace mamba
{
Context::Context()
{
set_verbosity(0);
on_ci = (std::getenv("CI") != nullptr);
if (on_ci || !termcolor::_internal::is_atty(std::cout))
{
Expand All @@ -29,6 +28,7 @@ namespace mamba
std::shared_ptr<spdlog::logger> l = std::make_shared<Logger>(log_pattern);
spdlog::set_default_logger(l);
logger = std::dynamic_pointer_cast<Logger>(l);
spdlog::set_level(log_level);
}

Context& Context::instance()
Expand All @@ -42,6 +42,12 @@ namespace mamba
this->verbosity = lvl;
}

void Context::set_log_level(const spdlog::level::level_enum& level)
{
log_level = level;
spdlog::set_level(level);
}

std::vector<std::string> Context::platforms()
{
return { platform, "noarch" };
Expand Down
2 changes: 1 addition & 1 deletion libmamba/tests/test_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace mamba
{
// TEST(cpp_install, install)
// {
// Context::instance().set_verbosity(3);
// Context::instance().verbosity = 3;
// PackageInfo pkg("wheel", "0.34.2", "py_1", 1);
// fs::path prefix = "C:\\Users\\wolfv\\miniconda3\\";
// TransactionContext tc(prefix, "3.8.x");
Expand Down
14 changes: 12 additions & 2 deletions libmambapy/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ PYBIND11_MODULE(bindings, m)
.value("kStrict", ChannelPriority::kStrict)
.value("kDisabled", ChannelPriority::kDisabled);

py::enum_<spdlog::level::level_enum>(m, "LogLevel")
.value("TRACE", spdlog::level::trace)
.value("DEBUG", spdlog::level::debug)
.value("INFO", spdlog::level::info)
.value("WARNING", spdlog::level::warn)
.value("ERROR", spdlog::level::err)
.value("CRITICAL", spdlog::level::critical)
.value("OFF", spdlog::level::off);

py::class_<Context, std::unique_ptr<Context, py::nodelete>>(m, "Context")
.def(
py::init([]() { return std::unique_ptr<Context, py::nodelete>(&Context::instance()); }))
Expand All @@ -242,14 +251,15 @@ PYBIND11_MODULE(bindings, m)
.def_readwrite("envs_dirs", &Context::envs_dirs)
.def_readwrite("pkgs_dirs", &Context::pkgs_dirs)
.def_readwrite("platform", &Context::platform)
.def("set_verbosity", &Context::set_verbosity)
.def_readwrite("channels", &Context::channels)
.def_readwrite("custom_channels", &Context::custom_channels)
.def_readwrite("custom_multichannels", &Context::custom_multichannels)
.def_readwrite("default_channels", &Context::default_channels)
.def_readwrite("channel_alias", &Context::channel_alias)
.def_readwrite("use_only_tar_bz2", &Context::use_only_tar_bz2)
.def_readwrite("channel_priority", &Context::channel_priority);
.def_readwrite("channel_priority", &Context::channel_priority)
.def("set_verbosity", &Context::set_verbosity)
.def("set_log_level", &Context::set_log_level);

py::class_<PrefixData>(m, "PrefixData")
.def(py::init<const std::string&>())
Expand Down
14 changes: 13 additions & 1 deletion mamba/mamba/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ def load_channels(
return index


def log_level_from_verbosity(verbosity: int):
if verbosity == 0:
return api.LogLevel.WARNING
elif verbosity == 1:
return api.LogLevel.INFO
elif verbosity == 2:
return api.LogLevel.DEBUG
else:
return api.LogLevel.TRACE


def init_api_context(use_mamba_experimental=False):
api_ctx = api.Context()

Expand All @@ -202,7 +213,8 @@ def init_api_context(use_mamba_experimental=False):
if use_mamba_experimental:
context.json = False

api_ctx.set_verbosity(context.verbosity)
api_ctx.verbosity = context.verbosity
api_ctx.set_log_level(log_level_from_verbosity(context.verbosity))
api_ctx.quiet = context.quiet
api_ctx.offline = context.offline
api_ctx.local_repodata_ttl = context.local_repodata_ttl
Expand Down

0 comments on commit 4e32959

Please sign in to comment.