From c0e9c79a518fbb3249e4c6f76607a8c361a8a2e7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 5 Nov 2010 01:54:04 +0000 Subject: [PATCH] Function appendLineToFile() introduced --- inc/classes/cachesystem.class.php | 40 ++++++++++++++++++++++--------- inc/db/lib-mysql3.php | 12 ++++------ inc/functions.php | 4 +--- inc/wrapper-functions.php | 8 +++++++ 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/inc/classes/cachesystem.class.php b/inc/classes/cachesystem.class.php index b422b47ef0..ed67f4a327 100644 --- a/inc/classes/cachesystem.class.php +++ b/inc/classes/cachesystem.class.php @@ -109,13 +109,29 @@ class CacheSystem { $this->pointer = fopen($this->fqfn, 'w') or debug_report_bug(__METHOD__, __LINE__, 'Cannot write to cache ' . $this->fqfn . ' !'); // Add open PHP tag - fwrite($this->pointer, "writeLine('' . __LINE__ . '): {--CACHE_PROBLEMS_DETECTED'); } } + /** + * Writes a line to the pointer and adds a \n (new-line) to the end + * + * @access private + */ + function writeLine ($line) { + // Is the pointer a valid resource? + if (is_resource($this->pointer)) { + // Write the line + fwrite($this->pointer, $line . "\n"); + } else { + // Something bad happened + debug_report_bug(__METHOD__, __LINE__, 'Pointer type is ' . gettype($this->pointer) . ', expected is resource.'); + } + } + // Reset the read status function resetCacheReadStatus () { unset($this->readable[$this->name]); @@ -190,7 +206,7 @@ class CacheSystem { } // Write cache line to file - fwrite($this->pointer, $this->rewriteEntry($k, $v)); + $this->writeLine($this->rewriteEntry($k, $v)); } // END - foreach } else { // Cannot create file @@ -205,7 +221,7 @@ class CacheSystem { $this->storeExtensionVersion('cache'); // Write footer - fwrite($this->pointer, "?>\n"); + $this->writeLine('?>'); // Close file add destroy handler fclose($this->pointer); @@ -365,7 +381,7 @@ class CacheSystem { } // Write line(s) - fwrite($this->pointer, $LINE); + $this->writeLine($LINE); } // END - foreach } else { // Cannot write array! @@ -423,7 +439,7 @@ class CacheSystem { $this->version[$this->name][$ext_name] = $ext_ver; // Write cache line to file - fwrite($this->pointer, $this->rewriteEntry($ext_name, $ext_ver, 'version', true)); + $this->writeLine($this->rewriteEntry($ext_name, $ext_ver, 'version', true)); } // END - if //* DEBUG: */ debugOutput(__METHOD__ . '(' . __LINE__ . '): '.$this->name.' - '.$ext_name.'='.$ext_ver); } else { @@ -475,7 +491,9 @@ class CacheSystem { $extender = '[]'; // Add only for single array entry? - if ($single === true) $extender = ''; + if ($single === true) { + $extender = ''; + } // END - if // Init line $line = ''; @@ -483,25 +501,25 @@ class CacheSystem { // String or non-string? ;-) if (is_string($value)) { // String... - $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = '" . escapeQuotes($value) . "';\n"; + $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = '" . escapeQuotes($value) . "';"; } elseif (is_null($value)) { // Null - $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = null;\n"; + $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = null;'; } elseif (is_bool($value)) { // Boolean value if ($value === true) { // True - $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = true;\n"; + $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = true;'; } else { // False - $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = false;\n"; + $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = false;'; } } elseif (isset($value[0])) { // These lines needs fixing debug_report_bug(__METHOD__, __LINE__, 'Invalid entry with [0] found. key=' . $key); } else { // Non-string - $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = ' . $value . ";\n"; + $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = ' . $value . ';'; } // Return line diff --git a/inc/db/lib-mysql3.php b/inc/db/lib-mysql3.php index 1b00955cee..2e95ceb6ac 100644 --- a/inc/db/lib-mysql3.php +++ b/inc/db/lib-mysql3.php @@ -100,17 +100,15 @@ function SQL_QUERY ($sqlString, $F, $L, $enableCodes = true) { // Debug output if ((!isCssOutputMode()) && (isDebugModeEnabled()) && (isSqlDebuggingEnabled())) { - // - // Debugging stuff... - // - $fp = fopen(getCachePath() . 'mysql.log', 'a') or debug_report_bug(__FUNCTION__, __LINE__, 'Cannot write mysql.log!'); + // Is this the first call? if (!isset($GLOBALS['sql_first_entry'])) { // Write first entry - fwrite($fp, 'Module=' . getModule() . "\n"); + appendLineToFile(getCachePath() . 'mysql.log', 'Module=' . getModule()); $GLOBALS['sql_first_entry'] = true; } // END - if - fwrite($fp, $F . '(LINE=' . $L . '|NUM=' . SQL_NUMROWS($result) . '|AFFECTED=' . SQL_AFFECTEDROWS() . '|QUERYTIME:' . $queryTime . '): ' . str_replace("\r", '', str_replace("\n", ' ', $GLOBALS['last_sql'])) . "\n"); - fclose($fp); + + // Append debug line + appendLineToFile(getCachePath() . 'mysql.log', $F . '(LINE=' . $L . '|NUM=' . SQL_NUMROWS($result) . '|AFFECTED=' . SQL_AFFECTEDROWS() . '|QUERYTIME:' . $queryTime . '): ' . str_replace("\r", '', str_replace("\n", ' ', $GLOBALS['last_sql']))); } // END - if // Count DB hits diff --git a/inc/functions.php b/inc/functions.php index 4cd8715360..4a1d8929b7 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -1705,9 +1705,7 @@ function logDebugMessage ($funcFile, $line, $message, $force=true) { $message = str_replace("\r", '', str_replace("\n", '', $message)); // Log this message away - $fp = fopen(getCachePath() . 'debug.log', 'a') or debug_report_bug(__FUNCTION__, __LINE__, 'Cannot write logfile debug.log!'); - fwrite($fp, generateDateTime(time(), '4') . '|' . getModule(false) . '|' . basename($funcFile) . '|' . $line . '|' . $message . "\n"); - fclose($fp); + appendLineToFile(getCachePath() . 'debug.log', generateDateTime(time(), '4') . '|' . getModule(false) . '|' . basename($funcFile) . '|' . $line . '|' . $message); } // END - if } diff --git a/inc/wrapper-functions.php b/inc/wrapper-functions.php index d910f8da2d..fe3c593b43 100644 --- a/inc/wrapper-functions.php +++ b/inc/wrapper-functions.php @@ -2018,5 +2018,13 @@ function ifUserPointsLocked ($userid) { return $GLOBALS[__FUNCTION__][$userid]; } +// Appends a line to an existing file or creates it instantly with given content. +// This function does always add a new-line character to every line. +function appendLineToFile ($file, $line) { + $fp = fopen($file, 'a') or debug_report_bug(__FUNCTION__, __LINE__, 'Cannot write to file ' . basename($file) . '!'); + fwrite($fp, $line . "\n"); + fclose($fp); +} + // [EOF] ?> -- 2.39.5