Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Printf/Fprintf instead of std::cout, std::cerr #3073

Merged
merged 16 commits into from
Sep 11, 2024
6 changes: 3 additions & 3 deletions src/ivoc/ivoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,12 @@ int Oc::run(const char* buf, bool show_err_mes) {
err = hoc_oc(buf);
} catch (std::exception const& e) {
if (show_err_mes) {
std::cerr << "Oc::run: caught exception";
Fprintf(stderr, "Oc::run: caught exception");
std::string_view what{e.what()};
if (!what.empty()) {
std::cerr << ": " << what;
Fprintf(stderr, fmt::format(": {}", what).c_str());
}
std::cerr << std::endl;
Fprintf(stderr, "\n");
}
err = 1;
}
Expand Down
4 changes: 3 additions & 1 deletion src/ivoc/ivocmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ void iv_display_scale(float);
#include <IV-X11/ivx11_dynam.h>
#endif

#include <fmt/format.h>

#if 1
void pr_profile();
#define PR_PROFILE pr_profile();
Expand Down Expand Up @@ -312,7 +314,7 @@ void hoc_nrnmpi_init() {
nrnmpi_stubs();
auto const pmes = nrnmpi_load();
if (!pmes.empty()) {
std::cout << pmes << std::endl;
Printf(fmt::format("{}\n", pmes).c_str());
}
#endif

Expand Down
7 changes: 5 additions & 2 deletions src/ivoc/strfun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#if HAVE_IV
#include <ocbox.h>
#endif

#include <fmt/format.h>

extern Objectdata* hoc_top_level_data;
extern Symlist* hoc_built_in_symlist;
extern int nrn_is_artificial(int);
Expand Down Expand Up @@ -55,7 +58,7 @@
result = sm.prefix().str();
}
} catch (const std::regex_error& e) {
std::cerr << e.what() << std::endl;
Fprintf(stderr, fmt::format("{}\n", e.what()).c_str());

Check warning on line 61 in src/ivoc/strfun.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/strfun.cpp#L61

Added line #L61 was not covered by tests
}
char** head = hoc_pgargstr(3);
hoc_assign_str(head, result.c_str());
Expand All @@ -81,7 +84,7 @@
result = sm.suffix().str();
}
} catch (const std::regex_error& e) {
std::cerr << e.what() << std::endl;
Fprintf(stderr, fmt::format("{}\n", e.what()).c_str());

Check warning on line 87 in src/ivoc/strfun.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/strfun.cpp#L87

Added line #L87 was not covered by tests
}
char** tail = hoc_pgargstr(3);
hoc_assign_str(tail, result.c_str());
Expand Down
17 changes: 12 additions & 5 deletions src/neuron/container/soa_identifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <ostream>
#include <vector>

#include <fmt/format.h>

#include "hocdec.h"

namespace neuron::container {
/**
* @brief A non-owning permutation-stable identifier for a entry in a container.
Expand Down Expand Up @@ -164,11 +168,14 @@ struct owning_identifier {
// auto const read_only_token = node_data.issue_frozen_token();
// list_of_nodes.pop_back();
// which tries to delete a row from a container in read-only mode.
std::cerr << "neuron::container::owning_identifier<"
<< cxx_demangle(typeid(Storage).name())
<< "> destructor could not delete from the underlying storage: " << e.what()
<< " [" << cxx_demangle(typeid(e).name())
<< "]. This is not recoverable, aborting." << std::endl;
Fprintf(stderr,
fmt::format(
"neuron::container::owning_identifier<{}> destructor could not delete from "
"the underlying storage: {} [{}]. This is not recoverable, aborting.\n",
cxx_demangle(typeid(Storage).name()),
e.what(),
cxx_demangle(typeid(e).name()))
.c_str());
terminate = true;
}
if (terminate) {
Expand Down
5 changes: 4 additions & 1 deletion src/nrniv/memory_usage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include <neuron/cache/model_data.hpp>
#include <stdexcept>
#include "oc_ansi.h"
#include "hocdec.h"

#include <fmt/format.h>

namespace neuron::container {
ModelMemoryUsage memory_usage(const Model& model) {
Expand Down Expand Up @@ -134,7 +137,7 @@ std::string format_memory_usage(const MemoryUsage& usage) {


void print_memory_usage(MemoryUsage const& memory_usage) {
std::cout << format_memory_usage(memory_usage) << "\n";
Printf(fmt::format("{}\n", format_memory_usage(memory_usage)).c_str());
}

} // namespace neuron::container
Expand Down
2 changes: 1 addition & 1 deletion src/nrniv/nrncore_write/utils/nrncore_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
int nlfp = vector_capacity(lfp);

if (nsec != nseg) {
std::cout << "Error: Section and Segment mapping vectors should have same size!\n";
Printf("Error: Section and Segment mapping vectors should have same size!\n");

Check warning on line 108 in src/nrniv/nrncore_write/utils/nrncore_utils.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/nrncore_write/utils/nrncore_utils.cpp#L108

Added line #L108 was not covered by tests
abort();
}

Expand Down
46 changes: 29 additions & 17 deletions src/nrniv/nrnpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@
line.remove_prefix(prefix.size());
line.remove_suffix(suffix.size());
if (!glob_var.empty() && glob_var != line) {
std::cout << "WARNING: overriding " << env_var << '=' << glob_var << " with "
<< line << std::endl;
Printf(fmt::format(

Check warning on line 166 in src/nrniv/nrnpy.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/nrnpy.cpp#L166

Added line #L166 was not covered by tests
"WARNING: overriding {} = {} with {}\n", env_var, glob_var, line)
.c_str());
}
glob_var = line;
}
Expand Down Expand Up @@ -216,13 +217,15 @@
try {
set_nrnpylib();
} catch (std::exception const& e) {
std::cerr << "Could not determine Python library details: " << e.what()
<< std::endl;
Fprintf(stderr,
fmt::format("Could not determine Python library details: {}\n", e.what())
.c_str());
exit(1);
}
handle = dlopen(nrnpy_pylib.c_str(), RTLD_NOW | RTLD_GLOBAL);
if (!handle) {
std::cerr << "Could not dlopen NRN_PYLIB: " << nrnpy_pylib << std::endl;
Fprintf(stderr,
fmt::format("Could not dlopen NRN_PYLIB: {}\n", nrnpy_pylib).c_str());

Check warning on line 228 in src/nrniv/nrnpy.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/nrnpy.cpp#L227-L228

Added lines #L227 - L228 were not covered by tests
#if DARWIN
nrn_possible_mismatched_arch(nrnpy_pylib.c_str());
#endif
Expand Down Expand Up @@ -260,8 +263,12 @@
pyversion = std::to_string(pv10 / factor) + "." + std::to_string(pv10 % factor);
} else {
if (nrnpy_pylib.empty() || nrnpy_pyversion.empty()) {
std::cerr << "Do not know what Python to load [nrnpy_pylib=" << nrnpy_pylib
<< " nrnpy_pyversion=" << nrnpy_pyversion << ']' << std::endl;
Fprintf(

Check warning on line 266 in src/nrniv/nrnpy.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/nrnpy.cpp#L266

Added line #L266 was not covered by tests
stderr,
fmt::format("Do not know what Python to load [nrnpy_pylib={} nrnpy_pyversion={}]\n",

Check warning on line 268 in src/nrniv/nrnpy.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/nrnpy.cpp#L268

Added line #L268 was not covered by tests
nrnpy_pylib,
nrnpy_pyversion)
.c_str());
return nullptr;
}
pyversion = nrnpy_pyversion;
Expand All @@ -272,15 +279,18 @@
auto const iter =
std::find(supported_versions.begin(), supported_versions.end(), pyversion);
if (iter == supported_versions.end()) {
std::cerr << "Python " << pyversion
<< " is not supported by this NEURON installation (supported:";
Fprintf(
stderr,
fmt::format("Python {} is not supported by this NEURON installation (supported:",
pyversion)
.c_str());
for (auto const& good_ver: supported_versions) {
std::cerr << ' ' << good_ver;
Fprintf(stderr, fmt::format(" {}", good_ver).c_str());
1uc marked this conversation as resolved.
Show resolved Hide resolved
}
std::cerr << "). 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."
<< std::endl;
Fprintf(stderr,
"). 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");
return nullptr;
}
}
Expand All @@ -296,13 +306,15 @@
#endif
auto* const handle = dlopen(name.c_str(), RTLD_NOW);
if (!handle) {
std::cerr << "Could not load " << name << std::endl;
std::cerr << "nrn_is_python_extension=" << nrn_is_python_extension << std::endl;
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());

Check warning on line 311 in src/nrniv/nrnpy.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/nrnpy.cpp#L309-L311

Added lines #L309 - L311 were not covered by tests
return nullptr;
}
auto* const reg = reinterpret_cast<nrnpython_reg_real_t>(dlsym(handle, "nrnpython_reg_real"));
if (!reg) {
std::cerr << "Could not load registration function from " << name << std::endl;
Fprintf(stderr,
fmt::format("Could not load registration function from {}\n", name).c_str());

Check warning on line 317 in src/nrniv/nrnpy.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/nrnpy.cpp#L316-L317

Added lines #L316 - L317 were not covered by tests
}
return reg;
}
Expand Down
5 changes: 4 additions & 1 deletion src/nrnmpi/memory_usage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include <iostream>

#include "hocdec.h"
#include <fmt/format.h>

#include "neuron/container/memory_usage.hpp"

static void sum_reduce_memory_usage(void* invec, void* inoutvec, int* len_, MPI_Datatype*) {
Expand Down Expand Up @@ -44,7 +47,7 @@

void nrnmpi_print_memory_stats(neuron::container::MemoryStats const& memory_stats) {
if (nrnmpi_myid_world == 0) {
std::cout << format_memory_usage(memory_stats.total) << "\n";
Printf(fmt::format("{}\n", format_memory_usage(memory_stats.total)).c_str());

Check warning on line 50 in src/nrnmpi/memory_usage.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnmpi/memory_usage.cpp#L50

Added line #L50 was not covered by tests
}
}
#endif
7 changes: 5 additions & 2 deletions src/nrnmpi/nrnmpi_dynam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#include "nrnmpi.h"

#include "hocdec.h"
#include <fmt/format.h>

extern char* cxx_char_alloc(size_t);
extern std::string corenrn_mpi_library;

Expand Down Expand Up @@ -173,7 +176,7 @@ std::string nrnmpi_load() {
return true;
};
if (!promote_to_global("libnrniv.so") && !promote_to_global("libnrniv-without-nvidia.so")) {
std::cerr << error << " to RTLD_GLOBAL" << std::endl;
Fprintf(stderr, fmt::format("{} to RTLD_GLOBAL\n", error).c_str());
}
}
#endif
Expand Down Expand Up @@ -258,7 +261,7 @@ std::string nrnmpi_load() {
void nrnmpi_load_or_exit() {
auto const err = nrnmpi_load();
if (!err.empty()) {
std::cout << err << std::endl;
Printf(fmt::format("{}\n", err).c_str());
std::exit(1);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/nrnoc/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,12 @@
} catch (const std::filesystem::filesystem_error& e) {
handle = dlopen(relpath, flags);
if (!handle) {
std::cerr << "std::filesystem::absolute failed (" << e.what()
<< ") and dlopen failed with '" << relpath << "'" << std::endl;
Fprintf(

Check warning on line 242 in src/nrnoc/init.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/init.cpp#L242

Added line #L242 was not covered by tests
stderr,
fmt::format("std::filesystem::absolute failed ({}) and dlopen failed with '{}'\n",
e.what(),

Check warning on line 245 in src/nrnoc/init.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/init.cpp#L244-L245

Added lines #L244 - L245 were not covered by tests
relpath)
.c_str());
#if DARWIN
nrn_possible_mismatched_arch(relpath);
#endif
Expand Down
4 changes: 3 additions & 1 deletion src/nrnoc/treeset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <algorithm>
#include <string>

#include <fmt/format.h>

extern spREAL* spGetElement(char*, int, int);

int nrn_shape_changed_; /* for notifying Shape class in nrniv */
Expand Down Expand Up @@ -2168,7 +2170,7 @@
// objects. In this case we can figure out which the missing entries are and permute them to
// the end of the global vectors.
auto missing_elements = node_data_size - global_i;
std::cout << "permuting " << missing_elements << " 'lost' Nodes to the end\n";
Printf(fmt::format("permuting {} 'lost' Nodes to the end\n", missing_elements).c_str());

Check warning on line 2173 in src/nrnoc/treeset.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/treeset.cpp#L2173

Added line #L2173 was not covered by tests
// There are `missing_elements` integers from the range [0 .. node_data_size-1] whose values
// in `node_data_permutation` are still std::numeric_limits<std::size_t>::max().
for (auto global_row = 0ul; global_row < node_data_size; ++global_row) {
Expand Down
2 changes: 1 addition & 1 deletion src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@
if (!result) {
char* mes = nrnpyerr_str();
if (mes) {
std::cerr << mes << std::endl;
Fprintf(stderr, fmt::format("{}\n", mes).c_str());

Check warning on line 735 in src/nrnpython/nrnpy_p2h.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L735

Added line #L735 was not covered by tests
free(mes);
hoc_execerror("PyObject method call failed:", NULL);
}
Expand Down
2 changes: 1 addition & 1 deletion src/nrnpython/nrnpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
if (fp) {
nrnpython_set_path(fname);
} else {
std::cerr << "Could not open " << fname << std::endl;
Fprintf(stderr, fmt::format("Could not open {}\n", fname).c_str());

Check warning on line 127 in src/nrnpython/nrnpython.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpython.cpp#L127

Added line #L127 was not covered by tests
return 0;
}
fclose(fp);
Expand Down Expand Up @@ -193,7 +193,7 @@
if (b == 1 && !started) {
p_nrnpy_pyrun = nrnpy_pyrun;
if (nrnpy_nositeflag) {
Py_NoSiteFlag = 1;

Check warning on line 196 in src/nrnpython/nrnpython.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04 - cmake (-DNRN_ENABLE_CORENEURON=ON -DNRN_ENABLE_INTERVIEWS=OFF -DNMODL_SANITIZERS=undefinedundefined)

'Py_NoSiteFlag' is deprecated [-Wdeprecated-declarations]
}
// Create a Python configuration, see
// https://docs.python.org/3.8/c-api/init_config.html#python-configuration, so that
Expand Down
10 changes: 6 additions & 4 deletions src/oc/hoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <thread>
#include <utility>

#include <fmt/format.h>

/* for eliminating "ignoreing return value" warnings. */
int nrnignore;

Expand Down Expand Up @@ -928,7 +930,7 @@
}
return exit_status;
} catch (std::exception const& e) {
std::cerr << "hoc_main1 caught exception: " << e.what() << std::endl;
Fprintf(stderr, fmt::format("hoc_main1 caught exception: {}\n", e.what()).c_str());
nrn_exit(1);
}
}
Expand Down Expand Up @@ -1250,12 +1252,12 @@
}
} catch (std::exception const& e) {
hoc_fin = sav_fin;
std::cerr << "hoc_run1: caught exception";
Fprintf(stderr, "hoc_run1: caught exception");

Check warning on line 1255 in src/oc/hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/hoc.cpp#L1255

Added line #L1255 was not covered by tests
std::string_view what{e.what()};
if (!what.empty()) {
std::cerr << ": " << what;
Fprintf(stderr, fmt::format(": {}", what).c_str());

Check warning on line 1258 in src/oc/hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/hoc.cpp#L1258

Added line #L1258 was not covered by tests
}
std::cerr << std::endl;
Fprintf(stderr, "\n");

Check warning on line 1260 in src/oc/hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/hoc.cpp#L1260

Added line #L1260 was not covered by tests
// Exit if we're not in interactive mode
if (!nrn_fw_eq(hoc_fin, stdin)) {
return EXIT_FAILURE;
Expand Down
3 changes: 1 addition & 2 deletions src/oc/hoc_oop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "nrnfilewrap.h"
#include "ocfunc.h"


#define PDEBUG 0

Symbol* nrnpy_pyobj_sym_{};
Expand Down Expand Up @@ -1060,7 +1059,7 @@
auto err = fmt::format("'{}' not a public member of '{}'",
sym0->name,
obp->ctemplate->sym->name);
std::cerr << err << std::endl;
Fprintf(stderr, fmt::format("{}\n", err).c_str());

Check warning on line 1062 in src/oc/hoc_oop.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/hoc_oop.cpp#L1062

Added line #L1062 was not covered by tests
hoc_execerror(err.c_str(), nullptr);
}
*ptid = obp->ctemplate->id;
Expand Down
Loading