You are here

function jsonlog_test_filing in JSONlog 7

Same name and namespace in other branches
  1. 7.2 jsonlog.inc \jsonlog_test_filing()

Checks if log dir+file exists and is writable, and logs to watchdog.

Usable by drush script; prints messages via drush_log() instead drupal_set_message() if in CLI mode.

Parameters

string $file: Default: empty (~ use default file location algo)

Return value

boolean

See also

jsonlog_default_file()

1 call to jsonlog_test_filing()
jsonlog_form_system_logging_settings_submit in ./jsonlog.module
Custom submit handler for the system logging settings form.

File

./jsonlog.inc, line 90
JSONlog module helper functions.

Code

function jsonlog_test_filing($file = '') {
  $is_drush = drupal_is_cli();
  if ($file) {
    $restore_file_setting = TRUE;
  }
  else {
    $restore_file_setting = FALSE;
    if (!($file = getenv('drupal_jsonlog_file'))) {
      if (!($file = variable_get('jsonlog_file'))) {
        if (!($_file = jsonlog_default_file())) {
          if (!$is_drush) {
            drupal_set_message(t('jsonlog: Failed to establish the server\'s default logging directory.'), 'error');
          }
          else {
            drush_log(dt('jsonlog: Failed to establish the server\'s default logging directory.'), 'error');
          }
          return FALSE;
        }
      }
    }
  }
  $dir = dirname($file);
  if ($dir === '') {
    if (!$is_drush) {
      drupal_set_message(t('jsonlog: Log directory path name is empty.'), 'error');
    }
    else {
      drush_log(dt('jsonlog: Log directory path name is empty.'), 'error');
    }
    return FALSE;
  }
  if (!file_exists($dir)) {
    if (!$is_drush) {
      drupal_set_message(t('jsonlog: The log directory doesn\'t exist.!breakPlease create the directory \'!dir\'!breakor change the \'Log file\' setting.', array(
        '!dir' => $dir,
        '!break' => '<br/>',
      )), 'error');
    }
    else {
      drush_log(dt('jsonlog: The log directory doesn\'t exist.!breakPlease create the directory \'!dir\'!breakor change the \'Log file\' setting.', array(
        '!dir' => $dir,
        '!break' => ', ',
      )), 'error');
    }
    return FALSE;
  }
  if (!file_exists($file)) {
    if (!touch($file)) {
      if (!$is_drush) {
        drupal_set_message(t('jsonlog: The log file doesn\'t exist.!breakPlease create the file \'!file\'!breakor change the \'Log file\' setting.', array(
          '!file' => $file,
          '!break' => '<br/>',
        )), 'error');
      }
      else {
        drush_log(dt('jsonlog: The log file doesn\'t exist.!breakPlease create the file \'!file\'!breakor change the \'Log file\' setting.', array(
          '!file' => $file,
          '!break' => ', ',
        )), 'error');
      }
      return FALSE;
    }
  }
  elseif (!is_writable($file)) {
    if (!$is_drush) {
      drupal_set_message(t('jsonlog: The log file is not writable for the webserver user.!breakPlease check permissions of the file \'!file\'!breakand make sure that it\'s parent directory is executable for the webserver user.', array(
        '!file' => $file,
        '!break' => '<br/>',
      )), 'error');
    }
    else {
      drush_log(dt('jsonlog: The log file is not writable for the webserver user.!breakPlease check permissions of the file \'!file\'!breakand make sure that it\'s parent directory is executable for the webserver user.', array(
        '!file' => $file,
        '!break' => ', ',
      )), 'error');
    }
    return FALSE;
  }

  // Temporarily set the conf var to test value
  // - otherwise jsonlog_watchdog() would still use the old value for this write.
  $original_file = NULL;
  if ($restore_file_setting) {
    $original_file = variable_get('jsonlog_file', NULL);
    variable_set('jsonlog_file', $file);
  }
  try {
    watchdog('jsonlog', 'Testing watchdog logging - please check if this entry was written to file \'!file\'.', array(
      '!file' => $file,
    ), ($threshold = getenv('drupal_jsonlog_severity_threshold')) ? $threshold : variable_get('jsonlog_severity_threshold', WATCHDOG_WARNING));
  } catch (Exception $xc) {

    // Ignore; some watchdog implementation failed, and (probably) threw a (database) PDOException.
  }

  // Restore original value.
  if ($restore_file_setting) {
    if ($original_file === NULL) {
      variable_del('jsonlog_file');
    }
    else {
      variable_set('jsonlog_file', $original_file);
    }
  }
  if (!$is_drush) {
    drupal_set_message(t('jsonlog: Logging to \'!file\' seems to work,!breakbut please check if that file now contains an entry whose message starts with \'Testing watchdog logging\'.', array(
      '!file' => $file,
      '!break' => '<br/>',
    )), 'warning');
  }
  else {
    drush_log(dt('jsonlog: Logging to \'!file\' seems to work,!breakbut please check if that file now contains an entry whose message starts with \'Testing watchdog logging\'.', array(
      '!file' => $file,
      '!break' => ' ',
    )), 'warning');
  }
  return TRUE;
}