Skip to content

Commit a5bd48e

Browse files
authoredFeb 22, 2023
Fix static initialization fiasco problem with the logger and code calling into it from other TUs (#358)
* Addresses static initialization fiasco problem with the logger and code calling into it from other translation units * fix namespace * make logger init multithreading safe * actually we can use magic statics to simplify * Fix #356 * gate GMH/GPA on Windows only
1 parent 9c777df commit a5bd48e

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed
 

‎AnnService/inc/Core/Common.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ extern std::shared_ptr<Helper::DiskIO>(*f_createIO)();
123123
#define IOBINARY(ptr, func, bytes, ...) if (ptr->func(bytes, __VA_ARGS__) != bytes) return ErrorCode::DiskIOFail
124124
#define IOSTRING(ptr, func, ...) if (ptr->func(__VA_ARGS__) == 0) return ErrorCode::DiskIOFail
125125

126-
extern std::shared_ptr<Helper::Logger> g_pLogger;
126+
extern std::shared_ptr<Helper::Logger> GetLogger();
127127

128-
#define LOG(l, ...) g_pLogger->Logging("SPTAG", l, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
128+
#define LOG(l, ...) GetLogger()->Logging("SPTAG", l, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
129129

130130
class MyException : public std::exception
131131
{

‎AnnService/inc/SSDServing/SSDIndex.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ namespace SPTAG {
174174

175175
if (!p_opts.m_logFile.empty())
176176
{
177-
g_pLogger.reset(new Helper::FileLogger(Helper::LogLevel::LL_Info, p_opts.m_logFile.c_str()));
177+
GetLogger().reset(new Helper::FileLogger(Helper::LogLevel::LL_Info, p_opts.m_logFile.c_str()));
178178
}
179179
int numThreads = p_opts.m_iSSDNumberOfThreads;
180180
int internalResultNum = p_opts.m_searchInternalResultNum;

‎AnnService/src/Core/VectorIndex.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,23 @@ typedef typename SPTAG::Helper::Concurrent::ConcurrentMap<std::string, SPTAG::Si
1515

1616
using namespace SPTAG;
1717

18+
std::shared_ptr<Helper::Logger> SPTAG::GetLogger() {
1819
#ifdef DEBUG
19-
std::shared_ptr<Helper::Logger> SPTAG::g_pLogger(new Helper::SimpleLogger(Helper::LogLevel::LL_Debug));
20+
auto logLevel = Helper::LogLevel::LL_Debug;
2021
#else
21-
std::shared_ptr<Helper::Logger> SPTAG::g_pLogger(new Helper::SimpleLogger(Helper::LogLevel::LL_Info));
22+
auto logLevel = Helper::LogLevel::LL_Info;
2223
#endif
24+
#ifdef _WINDOWS_
25+
if (auto exeHandle = GetModuleHandleW(nullptr)) {
26+
if (auto SPTAG_GetLoggerLevel = reinterpret_cast<SPTAG::Helper::LogLevel(*)()>(GetProcAddress(exeHandle, "SPTAG_GetLoggerLevel"))) {
27+
logLevel = SPTAG_GetLoggerLevel();
28+
}
29+
}
30+
#endif // _WINDOWS_
31+
32+
static std::shared_ptr<Helper::Logger> s_pLogger = std::make_shared<Helper::SimpleLogger>(logLevel);
33+
return s_pLogger;
34+
}
2335

2436
std::mt19937 SPTAG::rg;
2537

‎AnnService/src/Quantizer/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void QuantizeAndSave(std::shared_ptr<SPTAG::Helper::VectorSetReader>& vectorRead
6868

6969
int main(int argc, char* argv[])
7070
{
71-
std::shared_ptr<QuantizerOptions> options = std::make_shared<QuantizerOptions>(10000, true, 0.0, SPTAG::QuantizerType::None, std::string(), -1, std::string(), std::string());
71+
std::shared_ptr<QuantizerOptions> options = std::make_shared<QuantizerOptions>(10000, true, 0.0f, SPTAG::QuantizerType::None, std::string(), -1, std::string(), std::string());
7272

7373
if (!options->Parse(argc - 1, argv + 1))
7474
{

‎AnnService/src/Server/SearchService.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ SearchService::Initialize(int p_argNum, char* p_args[])
8484
}
8585

8686
if (!cmdOptions.m_logFile.empty()) {
87-
g_pLogger.reset(new Helper::FileLogger(Helper::LogLevel::LL_Debug, cmdOptions.m_logFile.c_str()));
87+
GetLogger().reset(new Helper::FileLogger(Helper::LogLevel::LL_Debug, cmdOptions.m_logFile.c_str()));
8888
}
8989

9090
m_serviceContext.reset(new ServiceContext(cmdOptions.m_configFile));

0 commit comments

Comments
 (0)
Please sign in to comment.