nmslib / hnswlib

Header-only C++/python library for fast approximate nearest neighbors
https://github.com/nmslib/hnswlib
Apache License 2.0
4.12k stars 609 forks source link

Provide a macro to override the use of std::cerr #508

Closed jlmelville closed 9 months ago

jlmelville commented 9 months ago

484 introduced a call to std::cerr when M is excessively large. This makes life hard for the R bindings because CRAN checks for and does not like to find anything writing to stdout/stderr (these streams should be sent to the console instead). Fortunately, the Rcpp library provides alternative streams (e.g. Rcpp::Rcout and Rcpp::Rcerr).

I propose using a macro to define the stream. By default this will be std::cerr, so users of this library need do nothing and will notice no changes. In RcppHNSW, I will create a header file which will declare the override, e.g.:

#include <Rcpp.h>

#define HNSWLIB_ERR_OVERRIDE Rcpp::Rcerr

#include "hnswlib.h"

A similar arrangement works for Annoy and RcppAnnoy.

yurymalkov commented 9 months ago

Hi @jlmelville,

Thank you for the PR and sorry for the late response!

Looks good to me, but can you please add a link (it can be the link to this PR)?

Thanks!

jlmelville commented 9 months ago

@yurymalkov do you mean add a link to this PR in the comment to explain why the macro has been created? No problem to do so, just want to make sure I understand the request. (edit: I have updated the PR to include a link to this PR in the comment anyway as it seems like a good idea).

yurymalkov commented 9 months ago

Thank you!