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

Fix windows - debug logging #229

Open
wants to merge 7 commits into
base: Omega
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
2 changes: 1 addition & 1 deletion pvr.stalker/addon.xml.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<addon
id="pvr.stalker"
version="21.1.0"
version="21.1.1"
name="Stalker Client"
provider-name="Jamal Edey">
<requires>@ADDON_DEPENDS@</requires>
Expand Down
13 changes: 13 additions & 0 deletions src/StalkerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ bool StalkerInstance::ConfigureStalkerAPISettings()

SError ret;

kodi::Log(ADDON_LOG_DEBUG, "ZZ ConfigureStalkerAPISettings 1");


sc_identity_defaults(&m_identity);
SC_STR_SET(m_identity.mac, settings->mac.c_str());
SC_STR_SET(m_identity.time_zone, settings->timeZone.c_str());
Expand Down Expand Up @@ -295,10 +298,15 @@ bool StalkerInstance::ConfigureStalkerAPISettings()
m_guideManager->SetGuidePreference(settings->guidePreference);
m_guideManager->SetCacheOptions(settings->guideCache, settings->guideCacheHours * 3600);

kodi::Log(ADDON_LOG_DEBUG, "ZZ ConfigureStalkerAPISettings 2");

ret = Authenticate();
kodi::Log(ADDON_LOG_DEBUG, "ZZ ConfigureStalkerAPISettings 3");
if (ret != SERROR_OK)
QueueErrorNotification(ret);

kodi::Log(ADDON_LOG_DEBUG, "ZZ ConfigureStalkerAPISettings 4");

return ret == SERROR_OK;
}

Expand All @@ -309,16 +317,21 @@ bool StalkerInstance::IsAuthenticated() const

SError StalkerInstance::Authenticate()
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ Authenticate 1");
kodi::Log(ADDON_LOG_DEBUG, "%s", __func__);

SError ret;

if (!m_sessionManager->IsAuthenticated() && SERROR_OK != (ret = m_sessionManager->Authenticate()))
return ret;

kodi::Log(ADDON_LOG_DEBUG, "ZZ Authenticate 2");

if (m_profile.store_auth_data_on_stb && !SaveCache())
return SERROR_UNKNOWN;

kodi::Log(ADDON_LOG_DEBUG, "ZZ Authenticate 3");

return SERROR_OK;
}

Expand Down
77 changes: 66 additions & 11 deletions src/stalker/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,58 +127,104 @@ SError SessionManager::GetProfile(bool authSecondStep)

SError SessionManager::Authenticate()
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 1");
bool wasAuthenticated(m_authenticated);
int maxRetires(5);
int numRetries(0);

if (m_isAuthenticating)
return SERROR_OK;

kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 2");

StopWatchdog();

m_authMutex.lock();
m_isAuthenticating = true;
m_authenticated = false;
m_lastUnknownError.clear();
m_authMutex.unlock();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3");

{
std::lock_guard<std::mutex> guard(m_authMutex);
//m_authMutex.lock();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3a");
m_isAuthenticating = true;
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3b");
m_authenticated = false;
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3c");
m_lastUnknownError.clear();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3d");
//m_authMutex.unlock();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 3e");
}

kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 4");

if (wasAuthenticated && m_statusCallback != nullptr)
m_statusCallback(SERROR_AUTHORIZATION);

kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 5");

while (!m_authenticated && ++numRetries <= maxRetires)
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 6");

// notify once after the first try failed
if (numRetries == 2)
{
if (m_statusCallback != nullptr)
m_statusCallback(SERROR_AUTHENTICATION);
}

kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 7");

// don't sleep on first try
if (numRetries > 1)
std::this_thread::sleep_for(std::chrono::milliseconds(5000));

kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 8");

if (!m_hasUserDefinedToken && SERROR_OK != DoHandshake())
continue;

kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 9");

if (SERROR_OK != GetProfile())
continue;

m_authMutex.lock();
m_authenticated = true;
m_isAuthenticating = false;
m_authMutex.unlock();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 10");

{
std::lock_guard<std::mutex> guard(m_authMutex);
//m_authMutex.lock();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 10a");
m_authenticated = true;
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 10b");
m_isAuthenticating = false;
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 10c");
//m_authMutex.unlock();
}

kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 11");


if (wasAuthenticated && m_statusCallback != nullptr)
m_statusCallback(SERROR_OK);

kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 12");

}

kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 13");

if (m_authenticated)
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 14");
StartAuthInvoker();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 15");
StartWatchdog();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 16");
}

kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::Authenticate 17");

return SERROR_OK;
}

Expand Down Expand Up @@ -218,20 +264,29 @@ void SessionManager::StopAuthInvoker()

void SessionManager::StartWatchdog()
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 1");
if (!m_watchdog)
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 2");
m_watchdog = new CWatchdog((unsigned int)m_profile->timeslot, m_api, [this](SError err) {
if (err == SERROR_AUTHORIZATION)
{
m_authMutex.lock();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 3");
std::lock_guard<std::mutex> guard(m_authMutex);
//m_authMutex.lock();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 4");
m_authenticated = false;
m_authMutex.unlock();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 5");
// m_authMutex.unlock();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 6");
}
});
}
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 7");

if (m_watchdog)
m_watchdog->Start();
kodi::Log(ADDON_LOG_DEBUG, "ZZ SessionManager::StartWatchdog 8");
}

void SessionManager::StopWatchdog()
Expand Down
38 changes: 38 additions & 0 deletions src/stalker/SettingsMigration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,48 +44,65 @@ const std::vector<std::pair<const char*, bool>> boolMap = {{"guide_cache", true}

bool SettingsMigration::MigrateSettings(kodi::addon::IAddonInstance& target)
{
kodi::Log(ADDON_LOG_DEBUG, "MigrateSettings 1");

std::string stringValue;
bool boolValue{false};
int intValue{0};

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 2");
if (target.CheckInstanceSettingString("kodi_addon_instance_name", stringValue) &&
!stringValue.empty())
{
// Instance already has valid instance settings
return false;
}

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 3");

// Read pre-multi-instance settings from settings.xml, transfer to instance settings
SettingsMigration mig(target);

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 4");
for (const auto& setting : stringMap)
mig.MigrateStringSetting(setting.first, setting.second);

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 5");
for (const auto& setting : intMap)
mig.MigrateIntSetting(setting.first, setting.second);

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 6");
for (const auto& setting : floatMap)
mig.MigrateFloatSetting(setting.first, setting.second);

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 7");
for (const auto& setting : boolMap)
mig.MigrateBoolSetting(setting.first, setting.second);

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 8");
if (mig.Changed())
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 9");
// Set a title for the new instance settings
std::string title = "Migrated Add-on Config";
target.SetInstanceSettingString("kodi_addon_instance_name", title);

return true;
}
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateSettings 10");

return false;
}

bool SettingsMigration::IsMigrationSetting(const std::string& key)
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ IsMigrationSetting 1");

std::string oldSettingsKey{key};
oldSettingsKey += "_0";

kodi::Log(ADDON_LOG_DEBUG, "ZZ IsMigrationSetting 2");

return std::any_of(stringMap.cbegin(), stringMap.cend(),
[&key](const auto& entry) { return entry.first == key; }) ||
std::any_of(intMap.cbegin(), intMap.cend(),
Expand All @@ -106,9 +123,13 @@ bool SettingsMigration::IsMigrationSetting(const std::string& key)

void SettingsMigration::MigrateStringSetting(const char* key, const std::string& defaultValue)
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 1");

std::string oldSettingsKey{key};
oldSettingsKey += "_0";

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 2");

std::string value;
if (kodi::addon::CheckSettingString(oldSettingsKey, value) && value != defaultValue)
{
Expand All @@ -120,65 +141,82 @@ void SettingsMigration::MigrateStringSetting(const char* key, const std::string&
m_target.SetInstanceSettingString(key, value);
m_changed = true;
}
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 3");
}

void SettingsMigration::MigrateIntSetting(const char* key, int defaultValue)
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 1");
std::string oldSettingsKey{key};
oldSettingsKey += "_0";

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 2");
// Stalker uses the old settings format prior to migration and reading int and boolean reliably requires reading into
// a string and then into a int. Luckily after this messy migration, the settings will be in the new format going forward.
int value;
std::string stringValue;
if (kodi::addon::CheckSettingString(oldSettingsKey, stringValue) && stringValue != std::to_string(defaultValue))
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 3");
value = std::atoi(stringValue.c_str());
m_target.SetInstanceSettingInt(key, value);
m_changed = true;
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 4");
}
else if (kodi::addon::CheckSettingString(key, stringValue) && stringValue != std::to_string(defaultValue))
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 5");
value = std::atoi(stringValue.c_str());
if (oldSettingsKey == "connection_timeout_0")
value *= 5;

m_target.SetInstanceSettingInt(key, value);
m_changed = true;
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 6");
}
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateStringSetting 7");
}

void SettingsMigration::MigrateFloatSetting(const char* key, float defaultValue)
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateFloatSetting 1");
std::string oldSettingsKey{key};
oldSettingsKey += "_0";

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateFloatSetting 2");
float value;
if (kodi::addon::CheckSettingFloat(oldSettingsKey, value) && value != defaultValue)
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateFloatSetting 3");
m_target.SetInstanceSettingFloat(key, value);
m_changed = true;
}
else if (kodi::addon::CheckSettingFloat(key, value) && value != defaultValue)
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateFloatSetting 4");
m_target.SetInstanceSettingFloat(key, value);
m_changed = true;
}
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateFloatSetting 5");
}

void SettingsMigration::MigrateBoolSetting(const char* key, bool defaultValue)
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateBoolSetting 1");
std::string oldSettingsKey{key};
oldSettingsKey += "_0";

kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateBoolSetting 2");
// Stalker uses the old settings format prior to migration and reading int and boolean reliably requires reading into
// a string and then into a int. Luckily after this messy migration, the settings will be in the new format going forward.
bool value;
std::string stringValue;
if (kodi::addon::CheckSettingString(oldSettingsKey, stringValue) && stringValue != (defaultValue ? "true" : "false"))
{
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateBoolSetting 3");
value = stringValue == "true";
m_target.SetInstanceSettingBoolean(key, value);
m_changed = true;
}
kodi::Log(ADDON_LOG_DEBUG, "ZZ MigrateBoolSetting 4");
}