ONO::ToolBox::Logfile

package ONO::ToolBox::Logfile;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2000-2025 Jos KIRPS [ www.kirps.com | jos_AT_kirps_DOT_com ]
# and The Joopita Project [ www.joopita.org | contact_AT_joopita_DOT_com ]
#
# This file, as well as other parts of the ONO Software Project or related
# elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# 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.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################


use strict;

use ONO::IO;

use ONO::FW::User::Init;

use ONO::Core::HostOS;

use ONO::ToolBox::Logfile;

use ONO::Lib::Web::Client;
use ONO::Lib::DateTime::ToolBox;

###############################################################################
# LOG
###############################################################################

sub log {

my (
$self,
$logfile,
$user,
$text,
$id,
) = @_;

#: This is ONO's default logging mechanism, it will write a logfile entry
#: that may then be read using the ONO_ToolBox_LogfileReader.

my (
$sec,$min,$hour,
$mday,$mon,$year,
$wday,$yday,
$timestamp,$isdst,
) = ONO::Lib::DateTime::ToolBox->get;

my $IP = ONO::Lib::Web::Client->ip();

my @files = ($logfile);
if ($logfile =~ /:/) {
@files = split(/:/,$logfile);
}

foreach my $file (@files) {

$file =~ m~(.*)/~;
my $dir = $1;

# generate logfile /var/log/name/yyyymmdd.log structure if only name has been provided:

if (!$dir) {
$dir = "var/log/$file";
$file = "$dir/$year$mon$mday.log";
}

$text =~ s~(\n|\r|\t|\;)~ ~g;
if (length $text > 512) {
$text = substr($text,0,512)." [too-much-data-for-log]";
}

ONO::IO->mkpath($dir);
ONO::IO->append($file,"$timestamp;$year$mon$mday;$hour$min$sec;$IP;$user;$text;$id;\n");

}

}

###############################################################################
# DEBUG
###############################################################################

sub debug {

my (
$self,
$vars_ref,
$enable,
) = @_;

#: Write to ONO's default debugging logfile, located in ONO's
#: /var/log/debug/ directory.

if ($enable) {

my %vars = %$vars_ref;

my (
$vpath,
$lang,$domain,$community,
$sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst
) = ONO::FW::User::Init->getvar(\%vars);

my ($load1,$load2,$load3) = ONO::Core::HostOS->cpu_load;

ONO::IO->mkdir("var/log/debug");
ONO::ToolBox::Logfile->log("var/log/debug/$year$mon$mday.log",$vars{'username'},"$ENV{'REQUEST_URI'} ($ENV{'SCRIPT_NAME'})","$load1,$load2,$load3");

}

}

###############################################################################
# end of script
###############################################################################

1;

__END__