+ my %level_to_num = (
+ map( { $_ => } 0..7 ),
+ debug => 0,
+ info => 1,
+ notice => 2,
+ warning => 3,
+ error => 4, 'err' => 4,
+ critical => 5, crit => 5,
+ alert => 6,
+ emergency => 7, emerg => 7,
+ );
+
+ unless ( $RT::Logger ) {
+
+ $RT::Logger = Log::Dispatch->new;
+
+ my $stack_from_level;
+ if ( $stack_from_level = RT->Config->Get('LogStackTraces') ) {
+ # if option has old style '\d'(true) value
+ $stack_from_level = 0 if $stack_from_level =~ /^\d+$/;
+ $stack_from_level = $level_to_num{ $stack_from_level } || 0;
+ } else {
+ $stack_from_level = 99; # don't log
+ }
+
+ my $simple_cb = sub {
+ # if this code throw any warning we can get segfault
+ no warnings;
+ my %p = @_;
+
+ # skip Log::* stack frames
+ my $frame = 0;
+ $frame++ while caller($frame) && caller($frame) =~ /^Log::/;
+ my ($package, $filename, $line) = caller($frame);
+
+ # Encode to bytes, so we don't send wide characters
+ $p{message} = Encode::encode("UTF-8", $p{message});
+
+ $p{'message'} =~ s/(?:\r*\n)+$//;
+ return "[$$] [". gmtime(time) ."] [". $p{'level'} ."]: "
+ . $p{'message'} ." ($filename:$line)\n";
+ };
+
+ my $syslog_cb = sub {
+ # if this code throw any warning we can get segfault
+ no warnings;
+ my %p = @_;
+
+ my $frame = 0; # stack frame index
+ # skip Log::* stack frames
+ $frame++ while caller($frame) && caller($frame) =~ /^Log::/;
+ my ($package, $filename, $line) = caller($frame);
+
+ # Encode to bytes, so we don't send wide characters
+ $p{message} = Encode::encode("UTF-8", $p{message});
+
+ $p{message} =~ s/(?:\r*\n)+$//;
+ if ($p{level} eq 'debug') {
+ return "[$$] $p{message} ($filename:$line)\n";
+ } else {
+ return "[$$] $p{message}\n";
+ }
+ };
+
+ my $stack_cb = sub {
+ no warnings;
+ my %p = @_;
+ return $p{'message'} unless $level_to_num{ $p{'level'} } >= $stack_from_level;
+
+ require Devel::StackTrace;
+ my $trace = Devel::StackTrace->new( ignore_class => [ 'Log::Dispatch', 'Log::Dispatch::Base' ] );
+ return $p{'message'} . $trace->as_string;
+
+ # skip calling of the Log::* subroutins
+ my $frame = 0;
+ $frame++ while caller($frame) && caller($frame) =~ /^Log::/;
+ $frame++ while caller($frame) && (caller($frame))[3] =~ /^Log::/;
+
+ $p{'message'} .= "\nStack trace:\n";
+ while( my ($package, $filename, $line, $sub) = caller($frame++) ) {
+ $p{'message'} .= "\t$sub(...) called at $filename:$line\n";
+ }
+ return $p{'message'};
+ };
+
+ if ( $Config->Get('LogToFile') ) {
+ my ($filename, $logdir) = (
+ $Config->Get('LogToFileNamed') || 'rt.log',
+ $Config->Get('LogDir') || File::Spec->catdir( $VarPath, 'log' ),
+ );
+ if ( $filename =~ m![/\\]! ) { # looks like an absolute path.
+ ($logdir) = $filename =~ m{^(.*[/\\])};
+ }
+ else {
+ $filename = File::Spec->catfile( $logdir, $filename );
+ }
+
+ unless ( -d $logdir && ( ( -f $filename && -w $filename ) || -w $logdir ) ) {
+ # localizing here would be hard when we don't have a current user yet
+ die "Log file '$filename' couldn't be written or created.\n RT can't run.";
+ }
+
+ require Log::Dispatch::File;
+ $RT::Logger->add( Log::Dispatch::File->new
+ ( name=>'file',
+ min_level=> $Config->Get('LogToFile'),
+ filename=> $filename,
+ mode=>'append',
+ callbacks => [ $simple_cb, $stack_cb ],
+ ));
+ }
+ if ( $Config->Get('LogToSTDERR') ) {
+ require Log::Dispatch::Screen;
+ $RT::Logger->add( Log::Dispatch::Screen->new
+ ( name => 'screen',
+ min_level => $Config->Get('LogToSTDERR'),
+ callbacks => [ $simple_cb, $stack_cb ],
+ stderr => 1,
+ ));
+ }
+ if ( $Config->Get('LogToSyslog') ) {
+ require Log::Dispatch::Syslog;
+ $RT::Logger->add(Log::Dispatch::Syslog->new
+ ( name => 'syslog',
+ ident => 'RT',
+ min_level => $Config->Get('LogToSyslog'),
+ callbacks => [ $syslog_cb, $stack_cb ],
+ stderr => 1,
+ $Config->Get('LogToSyslogConf'),
+ ));
+ }
+ }
+ InitSignalHandlers(%arg);
+}
+
+sub InitSignalHandlers {
+
+ my %arg = @_;
+ return if $arg{'NoSignalHandlers'};
+
+# Signal handlers
+## This is the default handling of warnings and die'ings in the code
+## (including other used modules - maybe except for errors catched by
+## Mason). It will log all problems through the standard logging
+## mechanism (see above).
+
+ $SIG{__WARN__} = sub {
+ # use 'goto &foo' syntax to hide ANON sub from stack
+ unshift @_, $RT::Logger, qw(level warning message);
+ goto &Log::Dispatch::log;
+ };
+
+#When we call die, trap it and log->crit with the value of the die.
+
+ $SIG{__DIE__} = sub {
+ # if we are not in eval and perl is not parsing code
+ # then rollback transactions and log RT error
+ unless ($^S || !defined $^S ) {
+ $RT::Handle->Rollback(1) if $RT::Handle;
+ $RT::Logger->crit("$_[0]") if $RT::Logger;
+ }
+ die $_[0];
+ };
+}
+
+
+sub CheckPerlRequirements {
+ eval {require 5.010_001};
+ if ($@) {
+ die sprintf "RT requires Perl v5.10.1 or newer. Your current Perl is v%vd\n", $^V;
+ }
+
+ # use $error here so the following "die" can still affect the global $@
+ my $error;
+ {
+ local $@;
+ eval {
+ my $x = '';
+ my $y = \$x;
+ require Scalar::Util;
+ Scalar::Util::weaken($y);
+ };
+ $error = $@;
+ }
+
+ if ($error) {
+ die <<"EOF";
+
+RT requires the Scalar::Util module be built with support for the 'weaken'
+function.
+
+It is sometimes the case that operating system upgrades will replace
+a working Scalar::Util with a non-working one. If your system was working
+correctly up until now, this is likely the cause of the problem.
+
+Please reinstall Scalar::Util, being careful to let it build with your C
+compiler. Usually this is as simple as running the following command as
+root.
+
+ perl -MCPAN -e'install Scalar::Util'
+
+EOF
+
+ }
+}
+
+=head2 InitClasses
+
+Load all modules that define base classes.
+
+=cut
+
+sub InitClasses {
+ shift if @_%2; # so we can call it as a function or method
+ my %args = (@_);
+ require RT::Tickets;
+ require RT::Transactions;
+ require RT::Attachments;
+ require RT::Users;
+ require RT::Principals;
+ require RT::CurrentUser;
+ require RT::Templates;
+ require RT::Queues;
+ require RT::ScripActions;
+ require RT::ScripConditions;
+ require RT::Scrips;
+ require RT::Groups;
+ require RT::GroupMembers;
+ require RT::CustomFields;
+ require RT::CustomFieldValues;
+ require RT::ObjectCustomFields;
+ require RT::ObjectCustomFieldValues;
+ require RT::Attributes;
+ require RT::Dashboard;
+ require RT::Approval;
+ require RT::Lifecycle;
+ require RT::Link;
+ require RT::Links;
+ require RT::Article;
+ require RT::Articles;
+ require RT::Class;
+ require RT::Classes;
+ require RT::ObjectClass;
+ require RT::ObjectClasses;
+ require RT::ObjectTopic;
+ require RT::ObjectTopics;
+ require RT::Topic;
+ require RT::Topics;
+ require RT::Link;
+ require RT::Links;
+
+ _BuildTableAttributes();
+
+ if ( $args{'Heavy'} ) {
+ # load scrips' modules
+ my $scrips = RT::Scrips->new(RT->SystemUser);
+ while ( my $scrip = $scrips->Next ) {
+ local $@;
+ eval { $scrip->LoadModules } or
+ $RT::Logger->error("Invalid Scrip ".$scrip->Id.". Unable to load the Action or Condition. ".
+ "You should delete or repair this Scrip in the admin UI.\n$@\n");
+ }
+
+ foreach my $class ( grep $_, RT->Config->Get('CustomFieldValuesSources') ) {
+ $class->require or $RT::Logger->error(
+ "Class '$class' is listed in CustomFieldValuesSources option"
+ ." in the config, but we failed to load it:\n$@\n"
+ );
+ }
+
+ }
+}
+
+sub _BuildTableAttributes {
+ # on a cold server (just after restart) people could have an object
+ # in the session, as we deserialize it so we never call constructor
+ # of the class, so the list of accessible fields is empty and we die
+ # with "Method xxx is not implemented in RT::SomeClass"
+
+ # without this, we also can never call _ClassAccessible, because we
+ # won't have filled RT::Record::_TABLE_ATTR
+ $_->_BuildTableAttributes foreach qw(
+ RT::Ticket
+ RT::Transaction
+ RT::Attachment
+ RT::User
+ RT::Principal
+ RT::Template
+ RT::Queue
+ RT::ScripAction
+ RT::ScripCondition
+ RT::Scrip
+ RT::ObjectScrip
+ RT::Group
+ RT::GroupMember
+ RT::CustomField
+ RT::CustomFieldValue
+ RT::ObjectCustomField
+ RT::ObjectCustomFieldValue
+ RT::Attribute
+ RT::ACE
+ RT::Article
+ RT::Class
+ RT::Link
+ RT::ObjectClass
+ RT::ObjectTopic
+ RT::Topic
+ );
+}
+
+=head2 InitSystemObjects
+
+Initializes system objects: C<$RT::System>, C<< RT->SystemUser >>
+and C<< RT->Nobody >>.
+
+=cut
+
+sub InitSystemObjects {
+
+ #RT's system user is a genuine database user. its id lives here
+ require RT::CurrentUser;
+ $SystemUser = RT::CurrentUser->new;
+ $SystemUser->LoadByName('RT_System');
+
+ #RT's "nobody user" is a genuine database user. its ID lives here.
+ $Nobody = RT::CurrentUser->new;
+ $Nobody->LoadByName('Nobody');
+
+ require RT::System;
+ $System = RT::System->new( $SystemUser );
+}
+
+=head1 CLASS METHODS
+
+=head2 Config
+
+Returns the current L<config object|RT::Config>, but note that
+you must L<load config|/LoadConfig> first otherwise this method
+returns undef.
+
+Method can be called as class method.
+
+=cut
+
+sub Config { return $Config || shift->LoadConfig(); }
+
+=head2 DatabaseHandle
+
+Returns the current L<database handle object|RT::Handle>.
+
+See also L</ConnectToDatabase>.
+
+=cut
+
+sub DatabaseHandle { return $Handle }
+
+=head2 Logger
+
+Returns the logger. See also L</InitLogging>.