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

Remove global C++ objects #1054

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions lib/api/LogManagerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,16 @@

namespace MAT_NS_BEGIN
{
// This mutex has to be recursive because we allow both
// Destroy and destrutor to lock it. destructor could be
// called directly, and Destroy calls destructor.
std::recursive_mutex ILogManagerInternal::managers_lock;
std::set<ILogManager*> ILogManagerInternal::managers;

/// <summary>
/// Creates an instance of ILogManager using specified configuration.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <returns>ILogManager instance</returns>
ILogManager* LogManagerFactory::Create(ILogConfiguration& configuration)
{
LOCKGUARD(ILogManagerInternal::managers_lock);
LOCKGUARD(ILogManagerInternal::GetManagersLock());
auto logManager = new LogManagerImpl(configuration);
ILogManagerInternal::managers.emplace(logManager);
ILogManagerInternal::GetManagers().emplace(logManager);
return logManager;
}

Expand All @@ -50,11 +44,12 @@ namespace MAT_NS_BEGIN
return STATUS_EFAIL;
}

LOCKGUARD(ILogManagerInternal::managers_lock);
auto it = ILogManagerInternal::managers.find(instance);
if (it != std::end(ILogManagerInternal::managers))
LOCKGUARD(ILogManagerInternal::GetManagersLock());
std::set<ILogManager*>& managers = ILogManagerInternal::GetManagers();
auto it = managers.find(instance);
if (it != std::end(managers))
{
ILogManagerInternal::managers.erase(it);
managers.erase(it);
delete instance;
return STATUS_SUCCESS;
}
Expand Down
26 changes: 22 additions & 4 deletions lib/api/LogManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@

namespace MAT_NS_BEGIN
{
// This mutex has to be recursive because we allow both
// Destroy and destrutor to lock it. destructor could be
// called directly, and Destroy calls destructor.

// static
std::recursive_mutex& ILogManagerInternal::GetManagersLock()
{
static std::recursive_mutex managers_lock;
return managers_lock;
};

// static
std::set<ILogManager*>& ILogManagerInternal::GetManagers() noexcept
{
static std::set<ILogManager*> managers;
return managers;
}

void DeadLoggers::AddMap(LoggerMap&& source)
{
std::lock_guard<std::mutex> lock(m_deadLoggersMutex);
Expand All @@ -94,8 +112,8 @@ namespace MAT_NS_BEGIN

bool ILogManager::DispatchEventBroadcast(DebugEvent evt)
{
// LOCKGUARD(ILogManagerInternal::managers_lock);
for (auto instance : ILogManagerInternal::managers)
// LOCKGUARD(ILogManagerInternal::GetManagersLock());
for (auto instance : ILogManagerInternal::GetManagers())
{
instance->DispatchEvent(evt);
}
Expand Down Expand Up @@ -353,8 +371,8 @@ namespace MAT_NS_BEGIN
LogManagerImpl::~LogManagerImpl() noexcept
{
FlushAndTeardown();
LOCKGUARD(ILogManagerInternal::managers_lock);
ILogManagerInternal::managers.erase(this);
LOCKGUARD(ILogManagerInternal::GetManagersLock());
ILogManagerInternal::GetManagers().erase(this);
}

size_t LogManagerImpl::GetDeadLoggerCount()
Expand Down
4 changes: 2 additions & 2 deletions lib/api/LogManagerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ namespace MAT_NS_BEGIN
class ILogManagerInternal : public ILogManager
{
public:
static std::recursive_mutex managers_lock;
static std::set<ILogManager*> managers;
static std::recursive_mutex& GetManagersLock();
static std::set<ILogManager*>& GetManagers() noexcept;

/// <summary>
/// Optional decorator runs on event before passing it to sendEvent
Expand Down
133 changes: 69 additions & 64 deletions lib/config/RuntimeConfig_Default.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,6 @@

namespace MAT_NS_BEGIN
{
static ILogConfiguration defaultRuntimeConfig{
{CFG_INT_TRACE_LEVEL_MIN, ACTTraceLevel::ACTTraceLevel_Error},
{CFG_INT_SDK_MODE, SdkModeTypes::SdkModeTypes_CS},
{CFG_BOOL_ENABLE_ANALYTICS, false},
{CFG_INT_CACHE_FILE_SIZE, 3145728},
{CFG_INT_RAM_QUEUE_SIZE, 524288},
{CFG_BOOL_ENABLE_MULTITENANT, true},
{CFG_BOOL_ENABLE_DB_DROP_IF_FULL, false},
{CFG_INT_MAX_TEARDOWN_TIME, 1},
{CFG_INT_MAX_PENDING_REQ, 4},
{CFG_INT_RAM_QUEUE_BUFFERS, 3},
{CFG_INT_TRACE_LEVEL_MASK, 0},
{CFG_BOOL_ENABLE_TRACE, true},
{CFG_STR_COLLECTOR_URL, COLLECTOR_URL_PROD},
{CFG_INT_STORAGE_FULL_PCT, 75},
{CFG_INT_STORAGE_FULL_CHECK_TIME, 5000},
{CFG_INT_RAMCACHE_FULL_PCT, 75},
{CFG_BOOL_ENABLE_NET_DETECT, true},
{CFG_BOOL_SESSION_RESET_ENABLED, false},
{CFG_MAP_METASTATS_CONFIG,
{/* Parameter that allows to split stats events by tenant */
{"split", false},
{"interval", 1800},
{"tokenProd", STATS_TOKEN_PROD},
{"tokenInt", STATS_TOKEN_INT}}},
{"utc",
{
#ifdef HAVE_MAT_UTC
{"providerGroupId", "780dddc8-18a1-5781-895a-a690464fa89c"},
{CFG_BOOL_UTC_ENABLED, true},
{CFG_BOOL_UTC_ACTIVE, false},
{CFG_BOOL_UTC_LARGE_PAYLOADS, false}
#else
{CFG_BOOL_UTC_ENABLED, false}
#endif
}},
{CFG_MAP_HTTP,
{
#ifdef HAVE_MAT_ZLIB
{CFG_BOOL_HTTP_COMPRESSION, true}
#else
{CFG_BOOL_HTTP_COMPRESSION, false}
#endif
,
{"contentEncoding", "deflate"},
/* Optional parameter to require Microsoft Root CA */
{CFG_BOOL_HTTP_MS_ROOT_CHECK, false}}},
{CFG_MAP_TPM,
{
{CFG_INT_TPM_MAX_BLOB_BYTES, 2097152},
{CFG_INT_TPM_MAX_RETRY, 5},
{CFG_BOOL_TPM_CLOCK_SKEW_ENABLED, true},
{CFG_STR_TPM_BACKOFF, "E,3000,300000,2,1"},
}},
{CFG_MAP_COMPAT,
{
{CFG_BOOL_COMPAT_DOTS, true}, // false: v1 backwards-compat: event.SetType("My.Custom.Type") => custom.my_custom_type
{CFG_STR_COMPAT_PREFIX, EVENTRECORD_TYPE_CUSTOM_EVENT} // custom type prefix for Interchange / Geneva / Cosmos flow
}},
{"sample",
{{"rate", 0}}}};

/// <summary>
/// This class overlays a custom configuration provided by the customer
/// on top of default configuration above (defaultRuntimeConfig)
Expand All @@ -81,12 +19,79 @@ namespace MAT_NS_BEGIN
{
protected:
ILogConfiguration& config;

public:
static ILogConfiguration& GetDefaultRuntimeConfig()
{
static ILogConfiguration defaultRuntimeConfig {
{CFG_INT_TRACE_LEVEL_MIN, ACTTraceLevel::ACTTraceLevel_Error},
{CFG_INT_SDK_MODE, SdkModeTypes::SdkModeTypes_CS},
{CFG_BOOL_ENABLE_ANALYTICS, false},
{CFG_INT_CACHE_FILE_SIZE, 3145728},
{CFG_INT_RAM_QUEUE_SIZE, 524288},
{CFG_BOOL_ENABLE_MULTITENANT, true},
{CFG_BOOL_ENABLE_DB_DROP_IF_FULL, false},
{CFG_INT_MAX_TEARDOWN_TIME, 1},
{CFG_INT_MAX_PENDING_REQ, 4},
{CFG_INT_RAM_QUEUE_BUFFERS, 3},
{CFG_INT_TRACE_LEVEL_MASK, 0},
{CFG_BOOL_ENABLE_TRACE, true},
{CFG_STR_COLLECTOR_URL, COLLECTOR_URL_PROD},
{CFG_INT_STORAGE_FULL_PCT, 75},
{CFG_INT_STORAGE_FULL_CHECK_TIME, 5000},
{CFG_INT_RAMCACHE_FULL_PCT, 75},
{CFG_BOOL_ENABLE_NET_DETECT, true},
{CFG_BOOL_SESSION_RESET_ENABLED, false},
{CFG_MAP_METASTATS_CONFIG,
{/* Parameter that allows to split stats events by tenant */
{"split", false},
{"interval", 1800},
{"tokenProd", STATS_TOKEN_PROD},
{"tokenInt", STATS_TOKEN_INT}}},
{"utc",
{
#ifdef HAVE_MAT_UTC
{"providerGroupId", "780dddc8-18a1-5781-895a-a690464fa89c"},
{CFG_BOOL_UTC_ENABLED, true},
{CFG_BOOL_UTC_ACTIVE, false},
{CFG_BOOL_UTC_LARGE_PAYLOADS, false}
#else
{CFG_BOOL_UTC_ENABLED, false}
#endif
}},
{CFG_MAP_HTTP,
{
#ifdef HAVE_MAT_ZLIB
{CFG_BOOL_HTTP_COMPRESSION, true}
#else
{CFG_BOOL_HTTP_COMPRESSION, false}
#endif
,
{"contentEncoding", "deflate"},
/* Optional parameter to require Microsoft Root CA */
{CFG_BOOL_HTTP_MS_ROOT_CHECK, false}}},
{CFG_MAP_TPM,
{
{CFG_INT_TPM_MAX_BLOB_BYTES, 2097152},
{CFG_INT_TPM_MAX_RETRY, 5},
{CFG_BOOL_TPM_CLOCK_SKEW_ENABLED, true},
{CFG_STR_TPM_BACKOFF, "E,3000,300000,2,1"},
}},
{CFG_MAP_COMPAT,
{
{CFG_BOOL_COMPAT_DOTS, true}, // false: v1 backwards-compat: event.SetType("My.Custom.Type") => custom.my_custom_type
{CFG_STR_COMPAT_PREFIX, EVENTRECORD_TYPE_CUSTOM_EVENT} // custom type prefix for Interchange / Geneva / Cosmos flow
}},
{"sample",
{{"rate", 0}}}};

return defaultRuntimeConfig;
}

RuntimeConfig_Default(ILogConfiguration& customConfig) :
config(customConfig)
{
Variant::merge_map(*customConfig, *defaultRuntimeConfig);
Variant::merge_map(*customConfig, *GetDefaultRuntimeConfig());
};

virtual ~RuntimeConfig_Default()
Expand Down
20 changes: 10 additions & 10 deletions lib/include/public/TransmitProfiles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,6 @@ namespace MAT_NS_BEGIN
class TransmitProfiles {

protected:
/// <summary>
/// A map that contains all transmit profiles.
/// </summary>
static std::map<std::string, TransmitProfileRules> profiles;

/// <summary>
/// A string that contains the name of the currently active transmit profile.
/// </summary>
static std::string currProfileName;

/// <summary>
/// The size of the currently active transmit profile rule.
Expand All @@ -161,13 +152,22 @@ namespace MAT_NS_BEGIN
/// </summary>
static bool isTimerUpdated;

/// <summary>
/// A map that contains all transmit profiles.
/// </summary>
static std::map<std::string, TransmitProfileRules>& GetProfiles() noexcept;

/// <summary>
/// A string that contains the name of the currently active transmit profile.
/// </summary>
static std::string& GetCurrProfileName() noexcept;

static void UpdateProfiles(const std::vector<TransmitProfileRules>& newProfiles) noexcept;

static void EnsureDefaultProfiles() noexcept;

public:


/// <summary>
/// The TransmitProfiles default constructor.
/// </summary>
Expand Down
35 changes: 22 additions & 13 deletions lib/pal/PAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,18 @@ namespace PAL_NS_BEGIN {
bool isLoggingInited = false;

#ifdef HAVE_MAT_LOGGING
std::recursive_mutex debugLogMutex;
std::string debugLogPath;
std::unique_ptr<std::fstream> debugLogStream;

std::recursive_mutex& GetDebugLogMutex()
{
static std::recursive_mutex debugLogMutex;
return debugLogMutex;
}

std::shared_ptr<std::fstream>& GetDebugLogStream() noexcept
{
static std::shared_ptr<std::fstream> debugLogStream;
return debugLogStream;
}

bool log_init(bool isTraceEnabled, const std::string& traceFolderPath)
{
Expand All @@ -108,38 +117,38 @@ namespace PAL_NS_BEGIN {
}

bool result = true;
if (debugLogStream != nullptr)
if (GetDebugLogStream() != nullptr)
{
return result;
}

debugLogMutex.lock();
debugLogPath = traceFolderPath;
std::lock_guard<std::recursive_mutex> lockguard(GetDebugLogMutex());
std::string debugLogPath = traceFolderPath;
debugLogPath += "mat-debug-";
debugLogPath += std::to_string(MAT::GetCurrentProcessId());
debugLogPath += ".log";

debugLogStream = std::unique_ptr<std::fstream>(new std::fstream());
std::shared_ptr<std::fstream>& debugLogStream = GetDebugLogStream();
debugLogStream = std::make_shared<std::fstream>();
debugLogStream->open(debugLogPath, std::fstream::out);
if (!debugLogStream->is_open())
{
// If file cannot be created, log to /dev/null
debugLogStream->open(DEBUG_LOG_NULL);
result = false;
}
debugLogMutex.unlock();
return result;
}

void log_done()
{
debugLogMutex.lock();
if (debugLogStream)
std::lock_guard<std::recursive_mutex> lockguard(GetDebugLogMutex());
std::shared_ptr<std::fstream>& debugLogStream = GetDebugLogStream();
if (debugLogStream != nullptr)
{
debugLogStream = nullptr;
isLoggingInited = false;
}
debugLogMutex.unlock();
}
#else
bool log_init(bool /*isTraceEnabled*/, const std::string& /*traceFolderPath*/)
Expand Down Expand Up @@ -258,14 +267,14 @@ namespace PAL_NS_BEGIN {
// Make sure all of our debug strings contain EOL
buffer[len] = '\n';
// Log to debug log file if enabled
debugLogMutex.lock();
std::lock_guard<std::recursive_mutex> lockguard(GetDebugLogMutex());
std::shared_ptr<std::fstream> debugLogStream = GetDebugLogStream();
if (debugLogStream->good())
{
(*debugLogStream) << buffer;
// flush is not very efficient, but needed to get realtime file updates
debugLogStream->flush();
}
debugLogMutex.unlock();
}
va_end(ap);
#endif
Expand Down
Loading