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

[debug] [logging] Automatically turn off fs_debug_modeafter 24 hours #691

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

DanieleAlessandra
Copy link
Collaborator

When the user activates debugging a single event cron starts:
wp_schedule_single_event(time() + 24 * HOUR_IN_SECONDS, 'fs_debug_turn_off_logging_hook');

When the user deactivates debuggong this cron is unscheduled:
wp_unschedule_event($timestamp, 'fs_debug_turn_off_logging_hook');

If the event happens, the option is set to off:
add_action('fs_debug_turn_off_logging_hook', array( self::class, '_turn_off_debug_mode' ) );

Also, in the debug page I added a countdown timer, so the user is informed about the fact that debugging will be disabled automatically.
image

Copy link
Contributor

@swashata swashata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanieleAlessandra greately done 👏 ... I think your logic is sound. Please see my suggestion for a little refactoring to make the main class a little bit slim and also code convention fixes.

includes/class-freemius.php Outdated Show resolved Hide resolved
includes/class-freemius.php Outdated Show resolved Hide resolved
includes/class-freemius.php Show resolved Hide resolved
templates/debug.php Outdated Show resolved Hide resolved
templates/debug.php Outdated Show resolved Hide resolved
templates/debug.php Outdated Show resolved Hide resolved
templates/debug.php Show resolved Hide resolved
templates/debug.php Show resolved Hide resolved
templates/debug.php Show resolved Hide resolved
Copy link
Contributor

@swashata swashata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @DanieleAlessandra nicely done 👏 ... Please see my few comments. Also kindly do a self review and fix the indentation glitches. We are getting close to merging this.

includes/class-freemius.php Outdated Show resolved Hide resolved
includes/class-freemius.php Outdated Show resolved Hide resolved
includes/class-fs-storage.php Show resolved Hide resolved
includes/managers/class-fs-debug-manager.php Show resolved Hide resolved
templates/debug.php Show resolved Hide resolved
@DanieleAlessandra DanieleAlessandra force-pushed the feature/auto-off-debugging branch 8 times, most recently from 2977f06 to f34b38f Compare March 28, 2024 08:53
Copy link
Contributor

@swashata swashata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greatly done @DanieleAlessandra 👍 Please see my two comments and we can merge this.

Also don't forget to rebase on develop and bump pre-release version of start.php

/**
* @var bool|int
*/
public $install_timestamp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanieleAlessandra would it also work if you just add a @property bool|int $install_timestamp in the PHP doc block, instead of this change?

The reason I am asking is, let's not make changes to the existing system, unless needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it will work, I try...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 👏

require.php Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert indentation changes please.

@@ -20815,7 +20448,7 @@ private function _fetch_newer_version( $plugin_id = false, $flush = true, $expir
*
* @return bool|FS_Plugin_Tag
*/
function get_update( $plugin_id = false, $flush = true, $expiration = FS_Plugin_Updater::UPDATES_CHECK_CACHE_EXPIRATION, $newer_than = false ) {
function get_update( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC, $newer_than = false ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanieleAlessandra rebase glitch.. Please use $expiration = FS_Plugin_Updater::UPDATES_CHECK_CACHE_EXPIRATION

*/
$was_license_expired_before_sync = $this->_license->is_expired();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanieleAlessandra rebase glitch, please revert

@@ -22484,7 +22115,7 @@ private function check_updates(
$background = false,
$plugin_id = false,
$flush = true,
$expiration = FS_Plugin_Updater::UPDATES_CHECK_CACHE_EXPIRATION,
$expiration = WP_FS__TIME_24_HOURS_IN_SEC,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanieleAlessandra rebase glitch

Copy link
Contributor

@swashata swashata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @DanieleAlessandra, nicely done. But it looks like I (accidentally) stumbled upon a race-condition or bad-state like issue in the class-fs-logger that can bite us.

Please see this

image

To reproduce, I have

  • Changed wp_schedule_single_event( time() + 24 * HOUR_IN_SECONDS, 'fs_debug_turn_off_logging_hook' ); to wp_schedule_single_event( time() + 5, 'fs_debug_turn_off_logging_hook' );
  • Turned on WP_DEBUG
  • Enabled the Freemius debugging.
  • Waited 5 sec and refreshed
  • The error pops up sometimes and sometimes it doesn't.

I think the issue is in class-fs-logger and the static variable $_isStorageLoggingOn which is not cleared/updated after calling _set_storage_logging. Kindly take a look. Thanks.

…, moved debug logic from Freemius to new FS_DebugManager class, fix a bug about logging attempt to DB when table does not exist.
/**
* @var bool
*/
public static $ON = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanieleAlessandra I think we are mixing two things here :)

We have the function _set_storage_logging which only deals with DB related storage. So making it off shouldn't really turn off regular logging.

For calling $wpdb->insert when we have just turned off the logging

We need to update the already existing $_isStorageLoggingOn static variable inside the _set_storage_logging. Something like

public static function _set_storage_logging( $is_on = true ) {
    // .. existing code

    self::$_isStorageLoggingOn = $is_on;
}

and that's it. I think this will work just fine.

Out of sync DB creation issue

I see you've made adjustment in the creation statement with CREATE TABLE IF NOT EXISTS. I propose we do it like this:

  1. Drop table if exists.
  2. Then create the table.

I think this way when the logging is turned on we are 100% sure to get the table schema we are after. Since we are dropping table anyway when turning off, I think this approach is fine.


So kindly see if the above suggestion would work.

Copy link
Contributor

@swashata swashata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the indentation glitch and one conceptual thing @DanieleAlessandra

@@ -125,6 +128,14 @@ function on() {
self::hook_footer();
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation glitch

@@ -240,6 +251,9 @@ function api_error( $api_result, $wrapper = false ) {
}

function entrance( $message = '', $wrapper = false ) {
if ( ! $this->is_on() ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation glitch

@@ -240,6 +251,9 @@ function api_error( $api_result, $wrapper = false ) {
}

function entrance( $message = '', $wrapper = false ) {
if ( ! $this->is_on() ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be needed since the _log is already bailing. If you want to be more memory/compute optimistic, then we need to have this check for every public log related function, but that isn't needed IMO.

@@ -320,6 +334,11 @@ public static function _set_storage_logging( $is_on = true ) {

$table = "{$wpdb->prefix}fs_logger";

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation glitch, also please fix for the rest of the file.

@@ -329,7 +348,7 @@ public static function _set_storage_logging( $is_on = true ) {
*
* @link https://core.trac.wordpress.org/ticket/2695
*/
$result = $wpdb->query( "CREATE TABLE {$table} (
$result = $wpdb->query( "CREATE TABLE IF NOT EXISTS {$table} (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this IF NOT EXISTS needed now that we are dropping the table anyway?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Table could exist if it was created with a previous version and never deleted

/**
* Since logging table does not exist anymore, we need to turn off all instances
*/
foreach ( self::$LOGGERS as $logger ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think this isn't needed. Because we have just turned off "DB" storage of the log. The actual log happens with the constant WP_FS__DEBUG_SDK. So if DB storage is turned off, let there still be memory logging within the _log and it will show up in the JavaScript console. The only fix needed is turning off the $_isStorageLoggingOn in line 385 like you did :)

*
* @return void
*/
public function off() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method won't be needed anymore (please see my suggestion below). Also just a code convention thingy, we add a blank line after a function/method declaration.

}

// Turn on/off storage logging.
FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanieleAlessandra please see that you are already setting storage login from the _turn_off_debug_mode or the _turn_on_debug_mode above (line 284). So I feel this call is redundant.

* develop:
  [opt-in] Fixed an issue with the opt-in moderation and API connectivity test logic.
  [compile] Version 2.7.3
  [cleanup] [version] Delete old commented out code and prepare v2.7.3.
  [deactivation] [ui] [fix] Fix UI glitch in the deactivation feedback form.
  [version] Version 2.7.2
  Hotfix
  [version] [compile] [i18n] Version 2.7.1
  [version] [pre-release] 2.7.0.2
  [subscription-cancellation] [button-text] [l10n] Use proper localization function to populate the "Cancel" button of the subscription cancellation modal.
  [i18n] Update the strings for case sensitivity. Remove lines spacings that can be removed while translating.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants