Skip to content

Commit

Permalink
use logger()
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino committed Sep 10, 2024
1 parent dbd898d commit d26272c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
36 changes: 9 additions & 27 deletions src/nrniv/nrnpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,12 @@ void nrnpython_reg() {
try {
set_nrnpylib();
} catch (std::exception const& e) {
Fprintf(stderr,
fmt::format("Could not determine Python library details: {}\n", e.what())
.c_str());
logger().error("Could not determine Python library details: {}\n", e.what());
exit(1);
}
handle = dlopen(nrnpy_pylib.c_str(), RTLD_NOW | RTLD_GLOBAL);
if (!handle) {
Fprintf(stderr,
fmt::format("Could not dlopen NRN_PYLIB: {}\n", nrnpy_pylib).c_str());
logger().error("Could not dlopen NRN_PYLIB: {}\n", nrnpy_pylib);
#if DARWIN
nrn_possible_mismatched_arch(nrnpy_pylib.c_str());
#endif
Expand Down Expand Up @@ -263,12 +260,7 @@ static nrnpython_reg_real_t load_nrnpython() {
pyversion = std::to_string(pv10 / factor) + "." + std::to_string(pv10 % factor);
} else {
if (nrnpy_pylib.empty() || nrnpy_pyversion.empty()) {
Fprintf(
stderr,
fmt::format("Do not know what Python to load [nrnpy_pylib={} nrnpy_pyversion={}]\n",
nrnpy_pylib,
nrnpy_pyversion)
.c_str());
logger().error("Do not know what Python to load [nrnpy_pylib={} nrnpy_pyversion={}]\n", nrnpy_pylib, nrnpy_pyversion);
return nullptr;
}
pyversion = nrnpy_pyversion;
Expand All @@ -279,18 +271,10 @@ static nrnpython_reg_real_t load_nrnpython() {
auto const iter =
std::find(supported_versions.begin(), supported_versions.end(), pyversion);
if (iter == supported_versions.end()) {
Fprintf(
stderr,
fmt::format("Python {} is not supported by this NEURON installation (supported:",
pyversion)
.c_str());
for (auto const& good_ver: supported_versions) {
Fprintf(stderr, fmt::format(" {}", good_ver).c_str());
}
Fprintf(stderr,
"). If you are seeing this message, your environment probably contains "
logger().error("Python {} is not supported by this NEURON installation (supported: {})."
"If you are seeing this message, your environment probably contains "
"NRN_PYLIB, NRN_PYTHONEXE and NRN_PYTHONVERSION settings that are "
"incompatible with this NEURON. Try unsetting them.\n");
"incompatible with this NEURON. Try unsetting them.\n", pyversion, fmt::join(supported_versions, " "));
return nullptr;
}
}
Expand All @@ -306,15 +290,13 @@ static nrnpython_reg_real_t load_nrnpython() {
#endif
auto* const handle = dlopen(name.c_str(), RTLD_NOW);
if (!handle) {
Fprintf(stderr, fmt::format("Could not load {}\n", name).c_str());
Fprintf(stderr,
fmt::format("nrn_is_python_extension={}\n", nrn_is_python_extension).c_str());
logger().error("Could not load {}\n", name);
logger().error("nrn_is_python_extension={}\n", nrn_is_python_extension);
return nullptr;
}
auto* const reg = reinterpret_cast<nrnpython_reg_real_t>(dlsym(handle, "nrnpython_reg_real"));
if (!reg) {
Fprintf(stderr,
fmt::format("Could not load registration function from {}\n", name).c_str());
logger().error("Could not load registration function from {}\n", name);
}
return reg;
}
Expand Down
2 changes: 1 addition & 1 deletion src/oc/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void nrnpy_pass() {
}

extern "C" void nrnpy_set_pr_etal(LoggerCallback* cb, int(*cbpass)(void)) {
getLogger().setCallback(cb);
logger().setCallback(cb);
nrnpy_pass_callback = cbpass;
}

Expand Down
14 changes: 7 additions & 7 deletions src/utils/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ class Logger {
LoggerCallback* callback = nullptr;
};

extern Logger nrnpy_logger;
extern Logger nrn_logger;

inline Logger& getLogger() {
return nrnpy_logger;
inline Logger& logger() {
return nrn_logger;
}

template <typename... T>
int Fprintf(FILE* stream, const char* fmt, T... args) {
if (getLogger().getCallback() && (stream == stdout || stream == stderr)) {
if (logger().getCallback() && (stream == stdout || stream == stderr)) {
std::string message = fmt::sprintf(fmt, args...);
getLogger().getCallback()(stream == stdout ? 1 : 2, message.c_str());
logger().getCallback()(stream == stdout ? 1 : 2, message.c_str());
return message.size();
}
return fmt::fprintf(stream, fmt, args...);
}

template <typename... T>
int Printf(const char* fmt, T... args) {
if (getLogger().getCallback()) {
if (logger().getCallback()) {
std::string message = fmt::sprintf(fmt, args...);
getLogger().getCallback()(1, message.c_str());
logger().getCallback()(1, message.c_str());
return message.size();
}
return fmt::printf(fmt, args...);
Expand Down

0 comments on commit d26272c

Please sign in to comment.