Source
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* Zabbix log file helper.
*/
class CLogHelper {
/**
* Log file offsets (for incremental log read).
*
* @var array
*/
private static $log_offsets = [];
/**
* Clear contents of log.
*
* @param string $path log file path
*/
public static function clearLog($path) {
file_put_contents($path, '');
self::resetLogOffset($path);
}
/**
* Reset log offset.
*
* @param string $path log file path
*/
public static function resetLogOffset($path) {
self::$log_offsets[$path] = 0;
}
/**
* Read content of the log.
*
* @param string $path log file path
* @param boolean $incremental flag to be used to enable incremental read
*
* @return string
*
* @throws Exception on cases when log is not available
*/
public static function readLog($path, $incremental = false) {
$offset = ($incremental && array_key_exists($path, self::$log_offsets))
? self::$log_offsets[$path] : 0;
if (($content = file_get_contents($path, false, null, $offset)) === false) {
throw new Exception('Failed to read log "'.$path.'".');
}