Skip to content

Commit f636da4

Browse files
authored
Bypass errors in logging for non-msft-prod environments (#392)
* Removed the logger and verified that the logging capability is the root cause of our consistent segfault errors in python. Perhaps it also will fix any issues in our label test too? I'd like to push it to GH and see. * Formatting fixes * Revert "Formatting fixes" This reverts commit 9042595. * Revert "Removed the logger and verified that the logging capability is the root cause of our consistent segfault errors in python. Perhaps it also will fix any issues in our label test too? I'd like to push it to GH and see." This reverts commit 7561009. * The custom logging implementation is causing segfaults in python. We're not sure exactly where, but this is the easiest and quickest way to getting a working python release. * All the integration tests are failing, and there's a chance the virtual dtor on AbstractDataStore might be the culprit, though I am not sure why. I'm hoping it is so it won't fall on the logging changes. * Formatting. Again.
1 parent 233c08c commit f636da4

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

include/abstract_data_store.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ template <typename data_t> class AbstractDataStore
1818
public:
1919
AbstractDataStore(const location_t capacity, const size_t dim);
2020

21+
// virtual ~AbstractDataStore() = default;
22+
2123
// Return number of points returned
2224
virtual location_t load(const std::string &filename) = 0;
2325

include/logger.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414

1515
namespace diskann
1616
{
17+
#ifdef ENABLE_CUSTOM_LOGGER
1718
DISKANN_DLLEXPORT extern std::basic_ostream<char> cout;
1819
DISKANN_DLLEXPORT extern std::basic_ostream<char> cerr;
20+
#else
21+
using std::cerr;
22+
using std::cout;
23+
#endif
1924

2025
enum class DISKANN_DLLEXPORT LogLevel
2126
{

include/logger_impl.h

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace diskann
1313
{
14+
#ifdef ENABLE_CUSTOM_LOGGER
1415
class ANNStreamBuf : public std::basic_streambuf<char>
1516
{
1617
public:
@@ -36,30 +37,25 @@ class ANNStreamBuf : public std::basic_streambuf<char>
3637
int flush();
3738
void logImpl(char *str, int numchars);
3839

39-
// Why the two buffer-sizes? If we are running normally, we are basically
40-
// interacting with a character output system, so we short-circuit the
41-
// output process by keeping an empty buffer and writing each character
42-
// to stdout/stderr. But if we are running in OLS, we have to take all
43-
// the text that is written to diskann::cout/diskann:cerr, consolidate it
44-
// and push it out in one-shot, because the OLS infra does not give us
45-
// character based output. Therefore, we use a larger buffer that is large
46-
// enough to store the longest message, and continuously add characters
47-
// to it. When the calling code outputs a std::endl or std::flush, sync()
48-
// will be called and will output a log level, component name, and the text
49-
// that has been collected. (sync() is also called if the buffer is full, so
50-
// overflows/missing text are not a concern).
51-
// This implies calling code _must_ either print std::endl or std::flush
52-
// to ensure that the message is written immediately.
53-
#ifdef ENABLE_CUSTOM_LOGGER
40+
// Why the two buffer-sizes? If we are running normally, we are basically
41+
// interacting with a character output system, so we short-circuit the
42+
// output process by keeping an empty buffer and writing each character
43+
// to stdout/stderr. But if we are running in OLS, we have to take all
44+
// the text that is written to diskann::cout/diskann:cerr, consolidate it
45+
// and push it out in one-shot, because the OLS infra does not give us
46+
// character based output. Therefore, we use a larger buffer that is large
47+
// enough to store the longest message, and continuously add characters
48+
// to it. When the calling code outputs a std::endl or std::flush, sync()
49+
// will be called and will output a log level, component name, and the text
50+
// that has been collected. (sync() is also called if the buffer is full, so
51+
// overflows/missing text are not a concern).
52+
// This implies calling code _must_ either print std::endl or std::flush
53+
// to ensure that the message is written immediately.
54+
5455
static const int BUFFER_SIZE = 1024;
55-
#else
56-
// Allocating an arbitrarily small buffer here because the overflow() and
57-
// other function implementations push the BUFFER_SIZE chars into the
58-
// buffer before flushing to fwrite.
59-
static const int BUFFER_SIZE = 4;
60-
#endif
6156

6257
ANNStreamBuf(const ANNStreamBuf &);
6358
ANNStreamBuf &operator=(const ANNStreamBuf &);
6459
};
60+
#endif
6561
} // namespace diskann

src/logger.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@
1010
namespace diskann
1111
{
1212

13+
#ifdef ENABLE_CUSTOM_LOGGER
1314
DISKANN_DLLEXPORT ANNStreamBuf coutBuff(stdout);
1415
DISKANN_DLLEXPORT ANNStreamBuf cerrBuff(stderr);
1516

1617
DISKANN_DLLEXPORT std::basic_ostream<char> cout(&coutBuff);
1718
DISKANN_DLLEXPORT std::basic_ostream<char> cerr(&cerrBuff);
18-
19-
#ifdef ENABLE_CUSTOM_LOGGER
2019
std::function<void(LogLevel, const char *)> g_logger;
2120

2221
void SetCustomLogger(std::function<void(LogLevel, const char *)> logger)
2322
{
2423
g_logger = logger;
2524
diskann::cout << "Set Custom Logger" << std::endl;
2625
}
27-
#endif
2826

2927
ANNStreamBuf::ANNStreamBuf(FILE *fp)
3028
{
@@ -38,11 +36,7 @@ ANNStreamBuf::ANNStreamBuf(FILE *fp)
3836
}
3937
_fp = fp;
4038
_logLevel = (_fp == stdout) ? LogLevel::LL_Info : LogLevel::LL_Error;
41-
#ifdef ENABLE_CUSTOM_LOGGER
4239
_buf = new char[BUFFER_SIZE + 1]; // See comment in the header
43-
#else
44-
_buf = new char[BUFFER_SIZE]; // See comment in the header
45-
#endif
4640

4741
std::memset(_buf, 0, (BUFFER_SIZE) * sizeof(char));
4842
setp(_buf, _buf + BUFFER_SIZE - 1);
@@ -88,17 +82,16 @@ int ANNStreamBuf::flush()
8882
}
8983
void ANNStreamBuf::logImpl(char *str, int num)
9084
{
91-
#ifdef ENABLE_CUSTOM_LOGGER
9285
str[num] = '\0'; // Safe. See the c'tor.
9386
// Invoke the OLS custom logging function.
9487
if (g_logger)
9588
{
9689
g_logger(_logLevel, str);
9790
}
91+
}
9892
#else
99-
fwrite(str, sizeof(char), num, _fp);
100-
fflush(_fp);
93+
using std::cerr;
94+
using std::cout;
10195
#endif
102-
}
10396

10497
} // namespace diskann

0 commit comments

Comments
 (0)