Skip to content

Commit

Permalink
Use fmt as backend for Printf/Fprintf (#3074)
Browse files Browse the repository at this point in the history
* Move Fprintf and Printf in utils/logger.hpp
* Make sections.c a c++ file
* Remove nrnpy_pr for only Printf (same function)
* Use fmt::ptr everywhere this is needed
  • Loading branch information
alkino authored Sep 12, 2024
1 parent b8299b9 commit ecbcd45
Show file tree
Hide file tree
Showing 28 changed files with 89 additions and 112 deletions.
2 changes: 1 addition & 1 deletion src/ivoc/ivocmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void iv_display_scale(float);
#include <IV-X11/ivx11_dynam.h>
#endif

#include <fmt/format.h>
#include "utils/logger.hpp"

#if 1
void pr_profile();
Expand Down
2 changes: 2 additions & 0 deletions src/ivoc/ivocvect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#include "gui-redirect.h"

#include "utils/logger.hpp"

#ifndef PI
#ifndef M_PI
#define M_PI 3.14159265358979323846
Expand Down
2 changes: 2 additions & 0 deletions src/ivoc/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "parse.hpp"
#include "ivocvect.h"

#include "utils/logger.hpp"

#define EPS hoc_epsilon
Symbol* nrn_matrix_sym; // also used in oc/hoc_oop.cpp

Expand Down
2 changes: 1 addition & 1 deletion src/ivoc/strfun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <ocbox.h>
#endif

#include <fmt/format.h>
#include "utils/logger.hpp"

extern Objectdata* hoc_top_level_data;
extern Symlist* hoc_built_in_symlist;
Expand Down
2 changes: 1 addition & 1 deletion src/neuron/container/soa_identifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <ostream>
#include <vector>

#include <fmt/format.h>
#include "utils/logger.hpp"

#include "hocdec.h"

Expand Down
14 changes: 7 additions & 7 deletions src/nrncvode/cvodeobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ int Cvode::cvode_init(double) {
// printf("CVodeReInit\n");
if (err != SUCCESS) {
Printf("Cvode %p %s CVReInit error %d\n",
this,
fmt::ptr(this),
secname(ctd_[0].v_node_[ctd_[0].rootnodecount_]->sec),
err);
return err;
Expand All @@ -1082,7 +1082,7 @@ int Cvode::cvode_init(double) {
CVodeMalloc(mem_, pf_, t0_, y_, CV_SV, &ncv_->rtol_, atolnvec_);
if (err != SUCCESS) {
Printf("Cvode %p %s CVodeMalloc error %d\n",
this,
fmt::ptr(this),
secname(ctd_[0].v_node_[ctd_[0].rootnodecount_]->sec),
err);
return err;
Expand Down Expand Up @@ -1302,7 +1302,7 @@ int Cvode::cvode_advance_tn(neuron::model_sorted_token const& sorted_token) {
#if PRINT_EVENT
if (net_cvode_instance->print_event_ > 1) {
Printf("Cvode::cvode_advance_tn %p %d initialize_=%d tstop=%.20g t_=%.20g to ",
this,
fmt::ptr(this),
nth_ ? nth_->id : 0,
initialize_,
tstop_,
Expand All @@ -1322,7 +1322,7 @@ int Cvode::cvode_advance_tn(neuron::model_sorted_token const& sorted_token) {
#endif
if (err < 0) {
Printf("CVode %p %s advance_tn failed, err=%d.\n",
this,
fmt::ptr(this),
secname(ctd_[0].v_node_[ctd_[0].rootnodecount_]->sec),
err);
pf_(t_, y_, nullptr, &opaque);
Expand All @@ -1344,7 +1344,7 @@ int Cvode::cvode_interpolate(double tout) {
#if PRINT_EVENT
if (net_cvode_instance->print_event_ > 1) {
Printf("Cvode::cvode_interpolate %p %d initialize_%d t=%.20g to ",
this,
fmt::ptr(this),
nth_ ? nth_->id : 0,
initialize_,
t_);
Expand All @@ -1365,7 +1365,7 @@ int Cvode::cvode_interpolate(double tout) {
#endif
if (err < 0) {
Printf("CVode %p %s interpolate failed, err=%d.\n",
this,
fmt::ptr(this),
secname(ctd_[0].v_node_[ctd_[0].rootnodecount_]->sec),
err);
return err;
Expand Down Expand Up @@ -1409,7 +1409,7 @@ N_Vector Cvode::acorvec() {
void Cvode::statistics() {
#if 1
Printf("\nCvode instance %p %s statistics : %d %s states\n",
this,
fmt::ptr(this),
secname(ctd_[0].v_node_[ctd_[0].rootnodecount_]->sec),
neq_,
(use_daspk_ ? "IDA" : "CVode"));
Expand Down
13 changes: 8 additions & 5 deletions src/nrncvode/netcvode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3493,7 +3493,7 @@ void NetCvode::local_retreat(double t, Cvode* cv) {
if (print_event_) {
Printf("microstep local retreat from %g (cvode_%p is at %g) for event onset=%g\n",
cv->tqitem_->t_,
cv,
fmt::ptr(cv),
cv->t_,
t);
}
Expand All @@ -3502,7 +3502,10 @@ void NetCvode::local_retreat(double t, Cvode* cv) {
tq->move(cv->tqitem_, t);
#if PRINT_EVENT
if (print_event_ > 1) {
Printf("after target solve time for %p is %g , dt=%g\n", cv, cv->time(), nt_dt);
Printf("after target solve time for %p is %g , dt=%g\n",
fmt::ptr(cv),
cv->time(),
nt_dt);
}
#endif
} else {
Expand All @@ -3519,7 +3522,7 @@ void NetCvode::retreat(double t, Cvode* cv) {
if (print_event_) {
Printf("microstep retreat from %g (cvode_%p is at %g) for event onset=%g\n",
tq ? cv->tqitem_->t_ : cv->t_,
cv,
fmt::ptr(cv),
cv->t_,
t);
}
Expand All @@ -3530,7 +3533,7 @@ void NetCvode::retreat(double t, Cvode* cv) {
}
#if PRINT_EVENT
if (print_event_ > 1) {
Printf("after target solve time for %p is %g , dt=%g\n", cv, cv->time(), dt);
Printf("after target solve time for %p is %g , dt=%g\n", fmt::ptr(cv), cv->time(), dt);
}
#endif
}
Expand Down Expand Up @@ -5577,7 +5580,7 @@ static int trajec_buffered(NrnThread& nt,
if (err) {
Fprintf(stderr,
"Pointer %p of PlayRecord type %d ignored because not a Range Variable",
static_cast<double*>(pd),
fmt::ptr(static_cast<double*>(pd)),
pr->type());
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/nrncvode/tqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ static void prnt(const TQItem* b, int level) {
for (i = 0; i < level; ++i) {
Printf(" ");
}
Printf("%g %c %d Q=%p D=%p\n", b->t_, b->data_ ? 'x' : 'o', b->cnt_, b, b->data_);
Printf("%g %c %d Q=%p D=%p\n",
b->t_,
b->data_ ? 'x' : 'o',
b->cnt_,
fmt::ptr(b),
fmt::ptr(b->data_));
}

TQueue::TQueue(TQItemPool* tp, int mkmut) {
Expand Down
13 changes: 8 additions & 5 deletions src/nrniv/multisplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1872,20 +1872,23 @@ void MultiSplitControl::prstruct() {
m.tag_);
if (m.nnode_) {
Printf(" nodeindex=%p nodeindex_buffer = %p\n",
m.nodeindex_,
nodeindex_buffer_);
fmt::ptr(m.nodeindex_),
fmt::ptr(nodeindex_buffer_));
}
}
Printf(" ndbsize=%d i nodeindex_buffer_=%p nodeindex_rthost_=%p\n",
ndbsize,
nodeindex_buffer_,
nodeindex_rthost_);
fmt::ptr(nodeindex_buffer_),
fmt::ptr(nodeindex_rthost_));
if (ndbsize) {
for (int i = 0; i < ndbsize; ++i) {
Printf(" %d %d %d\n", i, nodeindex_buffer_[i], nodeindex_rthost_[i]);
}
}
Printf(" tbsize=%d trecvbuf_=%p tsendbuf_=%p\n", tbsize, trecvbuf_, tsendbuf_);
Printf(" tbsize=%d trecvbuf_=%p tsendbuf_=%p\n",
tbsize,
fmt::ptr(trecvbuf_),
fmt::ptr(tsendbuf_));
Printf("\n");
}
}
Expand Down
1 change: 0 additions & 1 deletion src/nrniv/nrncore_write/callbacks/nrncore_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,6 @@ static void set_info(TQItem* tqi,
Fprintf(stderr,
"WARNING: CVode.event(...) for delivery at time step nearest %g discarded. "
"CoreNEURON cannot presently handle interpreter events (rank %d, thread %d).\n",
nrnmpi_myid,
tdeliver,
nrnmpi_myid,
tid);
Expand Down
3 changes: 1 addition & 2 deletions src/nrnmpi/memory_usage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

#include <iostream>

#include "hocdec.h"
#include <fmt/format.h>
#include "utils/logger.hpp"

#include "neuron/container/memory_usage.hpp"

Expand Down
3 changes: 1 addition & 2 deletions src/nrnmpi/nrnmpi_dynam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

#include "nrnmpi.h"

#include "hocdec.h"
#include <fmt/format.h>
#include "utils/logger.hpp"

extern char* cxx_char_alloc(size_t);
extern std::string corenrn_mpi_library;
Expand Down
4 changes: 2 additions & 2 deletions src/nrnpython/nrnpy_hoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ int hocobj_pushargs(PyObject* args, std::vector<char*>& s2free) {
Py2NRNString str(po, /* disable_release */ true);
if (str.err()) {
// Since Python error has been set, need to clear, or hoc_execerror
// printing with nrnpy_pr will generate a
// printing with Printf will generate a
// Exception ignored on calling ctypes callback function.
// So get the message, clear, and make the message
// part of the execerror.
Expand All @@ -399,7 +399,7 @@ int hocobj_pushargs(PyObject* args, std::vector<char*>& s2free) {
// the object which can raise an error for nrn.Section, nrn.Segment,
// etc. if the internal Section is invalid (Section.prop == NULL).
// That, in consequence, will generate an
// Exception ignored on calling ctypes callback function: <function nrnpy_pr
// Exception ignored on calling ctypes callback function: <function Printf
// thus obscuring the actual error, such as
// nrn.Segment associated with deleted internal Section.
PyHocObject* pho = (PyHocObject*) po;
Expand Down
2 changes: 2 additions & 0 deletions src/oc/axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ axis.cpp,v
#include <math.h>
#include "gui-redirect.h"

#include "utils/logger.hpp"

#define CLIP 1e9
#define XS 500.
#define YS 400.
Expand Down
4 changes: 2 additions & 2 deletions src/oc/code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2371,9 +2371,9 @@ int hoc_araypt(Symbol* sp, int type) {

// pop top value from stack, print it
void hoc_print() {
nrnpy_pr("\t");
Printf("\t");
hoc_prexpr();
nrnpy_pr("\n");
Printf("\n");
}

// print numeric value
Expand Down
2 changes: 2 additions & 0 deletions src/oc/code2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "nrnfilewrap.h"
#include <cstring>

#include "utils/logger.hpp"


int units_on_flag_;

Expand Down
4 changes: 3 additions & 1 deletion src/oc/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include "equation.h"
#include <stdio.h>

#include "utils/logger.hpp"

int hoc_zzdebug;

#define prcod(c1, c2) else if (p->pf == c1) Printf("%p %p %s", p, p->pf, c2)
#define prcod(c1, c2) else if (p->pf == c1) Printf("%p %p %s", fmt::ptr(p), fmt::ptr(p->pf), c2)

void hoc_debug(void) /* print the machine */
{
Expand Down
74 changes: 2 additions & 72 deletions src/oc/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <errno.h>
#include "nrnfilewrap.h"


extern char* neuron_home;

NrnFILEWrap* hoc_frin;
Expand Down Expand Up @@ -751,7 +750,7 @@ void hoc_Chdir(void) {
}

int nrn_is_python_extension;
static int (*nrnpy_pr_stdoe_callback)(int, char*);
int (*nrnpy_pr_stdoe_callback)(int, char*);
static int (*nrnpy_pass_callback)();

extern "C" void nrnpy_set_pr_etal(int (*cbpr_stdoe)(int, char*), int (*cbpass)()) {
Expand All @@ -762,78 +761,9 @@ extern "C" void nrnpy_set_pr_etal(int (*cbpr_stdoe)(int, char*), int (*cbpass)()
void nrnpy_pass() {
if (nrnpy_pass_callback) {
if ((*nrnpy_pass_callback)() != 1) {
hoc_execerror("nrnpy_pass", 0);
}
}
}

static int vnrnpy_pr_stdoe(FILE* stream, const char* fmt, va_list ap) {
int size = 0;
char* p = NULL;

if (!nrnpy_pr_stdoe_callback || (stream != stderr && stream != stdout)) {
size = vfprintf(stream, fmt, ap);
return size;
}

/* Determine required size */
va_list apc;
#ifndef va_copy
#if defined(__GNUC__) || defined(__clang__)
#define va_copy(dest, src) __builtin_va_copy(dest, src)
#else
#define va_copy(dest, src) (dest = src)
#endif
#endif
va_copy(apc, ap);
size = vsnprintf(p, size, fmt, apc);
va_end(apc);

if (size < 0)
return 0;

size++; /* For '\0' */
p = static_cast<char*>(malloc(size));
if (p == NULL)
return 0;

size = vsnprintf(p, size, fmt, ap);
if (size < 0) {
free(p);
return 0;
}

// if any non-ascii translate to '?' or nrnpy_pr will raise an exception.
if (stream == stderr) {
for (int i = 0; p[i] != '\0'; ++i) {
if (!isascii((unsigned char) p[i])) {
p[i] = '?';
}
hoc_execerror("nrnpy_pass", nullptr);
}
}

(*nrnpy_pr_stdoe_callback)((stream == stderr) ? 2 : 1, p);

free(p);
return size;
}

int nrnpy_pr(const char* fmt, ...) {
int n;
va_list ap;
va_start(ap, fmt);
n = vnrnpy_pr_stdoe(stdout, fmt, ap);
va_end(ap);
return n;
}

int Fprintf(FILE* stream, const char* fmt, ...) {
int n;
va_list ap;
va_start(ap, fmt);
n = vnrnpy_pr_stdoe(stream, fmt, ap);
va_end(ap);
return n;
}

/** printf style specification of hoc_execerror message. (512 char limit) **/
Expand Down
2 changes: 1 addition & 1 deletion src/oc/hoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <thread>
#include <utility>

#include <fmt/format.h>
#include "utils/logger.hpp"

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

0 comments on commit ecbcd45

Please sign in to comment.