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 (59ac9353c32934ff54d1f1607c89a4a48dffda70) (3,100B) (mode 100644) [raw]
<?php
require_once($INC . "/util.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";

$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 = "";

/*
 * 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_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;
		// 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);
		}
	}

	$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 . " " . $str . "\n";

	$rg_log_buf .= $buf;

	if ($rg_log_fd !== NULL)
		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);
}

function rg_fatal($msg)
{
	global $rg_admin_email;
	global $rg_log_buf;

	rg_log("FATAL: $msg");

	$bt = debug_backtrace();
	rg_log_ml("Backtrace: " . print_r($bt, TRUE));
	mail("rg_fatal@embedromix.ro", "FATAL ERROR",
		$msg
		. "\n" . rg_log_buffer()
		. "\n" . print_r($bt, TRUE), "", "-f $rg_admin_email");
	exit(1);
}

function rg_internal_error($msg)
{
	global $rg_admin_email;

	rg_log("Internal error: $msg");

	$bt = debug_backtrace();
	rg_log_ml("Backtrace: " . print_r($bt, TRUE));
	mail("rg_ie@embedromix.ro", "INTERNAL ERROR",
		$msg
		. "\n" . rg_log_buffer()
		. "\n" . print_r($bt, TRUE), "", "-f $rg_admin_email");
}

// TODO: send mail or store all data in database
function rg_security_violation($msg)
{
	global $rg_admin_email;

	rg_log("Security violation: $msg");

	$bt = debug_backtrace();
	rg_log_ml("Backtrace: " . print_r($bt, TRUE));
	mail("rg_sec@embedromix.ro", "SECURITY VIOLATION",
		$msg
		. "\n" . rg_log_buffer()
		. "\n" . print_r($bt, TRUE), "", "-f $rg_admin_email");
	exit(1);
}

?>
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