xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / inc / log.inc.php (0d41733a9e1d74d8cd22c55a158f1c9a8cdb78ec) (5,067B) (mode 100644) [raw]
<?php
require_once($INC . "/util.inc.php");
require_once($INC . "/cache.inc.php");

if (!isset($rg_log_dir))
	$rg_log_dir = "/var/log/rocketgit";

if (!isset($rg_web_log_dir))
	$rg_web_log_dir = "/var/log/rocketgit-web";

if (!isset($rg_log_file)) {
	if (isset($_SERVER['REMOTE_ADDR']))
		$rg_log_file = $rg_web_log_dir . "/fallback.log";
	else
		$rg_log_file = $rg_log_dir . "/fallback.log";
}
$rg_log_fd = FALSE;
$rg_log_sid = rg_id(6);
$rg_log_buf = "";
$rg_log_last_date = "";
$rg_log_level = 0;

/*
 * Clears 'rg_log_buf' to not send a big log in case of errors
 */
function rg_log_buffer_clear()
{
	global $rg_log_buf;

	$rg_log_buf = "";
}

/*
 * Allow forcing sid, so we can spread the logs to multiple file
 * or to supress the output of it.
 */
function rg_log_set_sid($v)
{
	global $rg_log_sid;
	$rg_log_sid = $v;
}

/*
 * When we generate fatal/warn e-mails, we need to insert also the logs.
 */
function rg_log_buffer()
{
	global $rg_log_buf;
	return $rg_log_buf;
}

function rg_log_set_file($file)
{
	global $rg_log_file;

	$rg_log_file = $file;
}

function rg_log($str)
{
	global $rg_log_level;
	global $rg_log_file;
	global $rg_log_fd;
	global $rg_log_sid;
	global $rg_log_buf;
	global $rg_log_last_date;

	$date = gmdate("Ymd");
	if ($rg_log_fd === FALSE) {
		$_pi = pathinfo($rg_log_file);
		$lf = $_pi['dirname'] . "/" . $_pi['filename']
			. "-" . $date . "." . $_pi['extension'];
		$rg_log_fd = fopen($lf, "a");
		if ($rg_log_fd === FALSE)
			return;

		// Set correct rights
		chmod($lf, 0600);

		// write an empty line
		fwrite($rg_log_fd, "\n");
		$rg_log_last_date = $date;
		$rg_log_last_sid = $rg_log_sid;
	} else if (strcmp($rg_log_sid, "000000") == 0) { // we reopen log only if sid=000000 (long running tasks)
		if ((strcmp($date, $rg_log_last_date) != 0)) {
			// date changed, we need to reopen the log file
			fclose($rg_log_fd);
			$rg_log_fd = FALSE;
			return rg_log($str);
		}
	}

	// We insert some spaces based on backtrace len
	$spaces = "";
	for ($i = 0; $i < $rg_log_level; $i++)
		$spaces .= '  ';

	$t = gettimeofday();

	$buf0 = gmdate('Y-m-d H:i:s', $t['sec']) . "." . sprintf('%03u', $t['usec'] / 1000);
	if (strcmp($rg_log_sid, '000000') != 0)
		$buf0 .= " " . $rg_log_sid;
	$buf = "";
	$str = preg_replace_callback('/[^\pL\pN\pP\pS \t]/uU', 'rg_callback_hexa', $str);
	$buf .= $buf0 . ' ' . $spaces . $str . "\n";

	$rg_log_buf .= $buf;

	fwrite($rg_log_fd, $buf);
}

/*
 * Log a multiline string
 */
function rg_log_ml($str)
{
	$lines = explode("\n", $str);
	if (empty($lines))
		return;

	foreach ($lines as $line)
		rg_log($line);
}

/*
 * This function will log fatal errors.
 */
$rg_error_core_seen = array();
function rg_error_core($msg)
{
	global $rg_web_log_dir;
	global $rg_log_dir;
	global $rg_admin_email;
	global $rg_error_core_seen;

	rg_log($msg);

	$me = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : "?";

	$bt = print_r(debug_backtrace(), TRUE);
	$bt = substr($bt, 0, 1024 * 1024);

	if (isset($_SERVER['REMOTE_ADDR']))
		$dir = $rg_web_log_dir;
	else
		$dir = $rg_log_dir;

	$key = md5($msg);
	if (isset($rg_error_core_seen[$key]))
		return;

	$r = file_put_contents($dir . "/err-" . $key,
		"Script: " . $me . "\n" . rg_log_buffer() . "\n\n" . $bt,
		FILE_APPEND);
	if ($r === FALSE)
		return;
	chmod($dir . "/err-" . $key, 0600);

	$rg_error_core_seen[$key] = 1;
}

/*
 * This function is run from cron and will signal pending fatal errors.
 */
function rg_log_cron()
{
	global $rg_web_log_dir;
	global $rg_log_dir;
	global $rg_admin_email;
	global $rg_state_dir;

	$wd = rg_dir_load_pattern($rg_web_log_dir, "err-.*");
	if ($wd === FALSE)
		$wcount = "?";
	else
		$wcount = count($wd);

	$nd = rg_dir_load_pattern($rg_log_dir, "err-.*");
	if ($nd === FALSE)
		$ncount = "?";
	else
		$ncount = count($nd);

	if ($wcount + $ncount == 0)
		return;

	// Do not send less than 1 per 4 hours
	$last_ts = 0;
	if (file_exists($rg_state_dir . "/log_cron_last"))
		$last_ts = @filemtime($rg_state_dir . "/log_cron_last");

	if ($last_ts + 4 * 3600 > time())
		return 0;

	mail($rg_admin_email,
		$wcount . ' + ' . $ncount . ' errors are waiting...',
		'Please forward the content of /var/log/rocketgit*/err-*'
		. ' to RocketGit developers at rg-bugs@embedromix.ro.\n\n'
		. 'Thank you!\n\n',
		'Content-Type: text/plain;charset=UTF-8',
		"-f $rg_admin_email");

	file_put_contents($rg_state_dir . "/log_cron_last", time());
}

function rg_fatal($msg)
{
	rg_error_core("FATAL: $msg");
	exit(1);
}

function rg_internal_error($msg)
{
	rg_error_core("Internal error: $msg");
}

function rg_security_violation_no_exit($msg)
{
	rg_error_core("Security violation: $msg");
}

function rg_security_violation($msg)
{
	rg_error_core("Security violation: $msg");
	exit(1);
}

function rg_log_enter($a)
{
	global $rg_log_level;

	rg_log($a);
	$rg_log_level++;
}

function rg_log_ml_enter($a)
{
	global $rg_log_level;

	$rg_log_level++;
	rg_log_ml($a);
}

function rg_log_exit()
{
	global $rg_log_level;

	if ($rg_log_level == 0)
		rg_internal_error('rg_log_exit called with level 0');

	$rg_log_level--;
}

?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master