use System::Introspector::State;
use File::Tree::Snapshot;
use System::Introspector::Config;
-use System::Introspector::Logger qw(:log);
+use System::Introspector::Logger qw(:log);
+use System::Introspector::Logger::VerboseOutput;
+use System::Introspector::Logger::Output;
+use System::Introspector::Logger::WarnOutput;
GetOptions(
'c|config=s' => \my $config_file,
's|storage=s' => \my $storage_dir,
'a|all' => \my $update_all,
'g|group=s' => \my @update_groups,
+ 'v|verbose' => \my $verbose,
'd|debug' => \my $debug,
'l|log-level=s' => \my $log_level,
'h|help' => sub { pod2usage(0) },
$log_level = 'debug';
}
-my $log_destination;
+my ($stderr_destination, $stdout_destination, $warn_destination);
+
+$warn_destination = Object::Remote::LogDestination->new({
+ logger => System::Introspector::Logger::WarnOutput->new({
+ levels_upto => 'warn',
+ }),
+});
+
+$warn_destination->connect(System::Introspector::Logger->arg_router);
+
if (defined($log_level)) {
- $log_destination = Object::Remote::LogDestination->new(
- logger => Log::Contextual::SimpleLogger->new({ levels_upto => $log_level }),
+ $stderr_destination = Object::Remote::LogDestination->new(
+ logger => System::Introspector::Logger::Output->new({
+ levels_upto => $log_level,
+ }),
);
- $log_destination->connect(System::Introspector::Logging->arg_router);
+ $stderr_destination->connect(System::Introspector::Logger->arg_router);
+}
+
+if (defined($verbose)) {
+ $stdout_destination = Object::Remote::LogDestination->new(
+ logger => System::Introspector::Logger::VerboseOutput->new({
+ levels_upto => 'info',
+ }),
+ );
+
+ $stdout_destination->connect(System::Introspector::Logger->arg_router);
}
$config_file = "$storage_dir/main.conf" unless defined $config_file;
Fetch the specified group. Can be used multiple times.
+=head2 -v, --verbose
+
+Output progress information to STDOUT
+
=head2 -d, --debug
-Force the log level to DEBUG unless -l or --log-level is specified
+Force the log level to debug unless -l or --log-level is specified
=head2 -l <level>, --log-level <level>
-Set the log level to the value specified; this will override -d. Valid levels are
-TRACE DEBUG INFO WARN ERROR FATAL
+Set the log level to the value specified and output logs to STDERR; this
+will override -d. Valid levels are trace debug info warn error fatal
=head2 -h, --help
use strictures 1;
-#using simple logger because the configuration
-#interface is slightly nicer
-use Log::Contextual::SimpleLogger;
-use base qw ( Log::Contextual::SimpleLogger );
+use base qw( Log::Contextual::SimpleLogger );
-sub set_execution_context {
- my ($self, $context_name) = @_;
+sub _output {
+ my ($self, @msg) = @_;
- die "must specify an execution context name"
- unless defined $context_name;
+ print STDERR @msg or die "Could not print to STDERR: $!";
+}
+
+sub _format {
+ my ($self, $level, @msg) = @_;
+ my $logbuf = join('', @msg);
+ my @timedata = localtime;
+ my $message = sprintf("[%0.2i:%0.2i:%0.2i %s] %s", $timedata[2], $timedata[1], $timedata[0], $level, $logbuf);
- $self->{_introspector}->{context_name} = $context_name;
+ return $message;
}
+#TODO this should be called _format
sub _log {
my $self = shift;
- my $level = shift;
- my $message = join( "\n", @_ );
- my @timedata = localtime;
- my $time = sprintf("%0.2i:%0.2i:%0.2i", $timedata[2], $timedata[1], $timedata[0]);
- $message .= "\n" unless $message =~ /\n$/;
- my $context = $self->{_introspector}->{context_name};
- $context = 'undefined' unless defined $context;
- warn "[$level $time] [$context] $message";
+ my $formatted = $self->_format(@_);
+
+ $formatted .= "\n" unless $formatted =~ /\n$/;
+
+ $self->_output($formatted);
}
1;
--- /dev/null
+package System::Introspector::Logger::VerboseOutput;
+
+use strictures 1;
+
+use base qw(System::Introspector::Logger::Output);
+
+sub _output {
+ my ($self, $msg) = @_;
+
+ print $msg or die "Could not print: $!";
+}
+
+sub _format {
+ my ($self, $level, $msg) = @_;
+
+ return join('', $msg);
+}
+
+1;
+
--- /dev/null
+package System::Introspector::Logger::WarnOutput;
+
+use strictures 1;
+
+use base qw(System::Introspector::Logger::VerboseOutput );
+
+sub output {
+ my ($self, $msg) = @_;
+
+ warn $msg;
+}
+
+1;
+
for my $group (@groups) {
my @hosts = $self->config->hosts;
- log_info { my $c = scalar(@hosts); "This gather run is for $c hosts" };
+ log_debug { my $c = scalar(@hosts); "This gather run is for $c hosts" };
while(scalar(@hosts) > 0) {
my (@scan, @waiting);
eval { $to_fetch = [$host, $self->fetch($host, $group)] };
if ($@) {
- log_trace { "Could not start fetching for $host: '$@'" };
+ log_error { "Could not start fetching for $host: $@" };
next;
}
eval { ($report) = await_all @futures };
if ($@) {
- log_error { "Failure when probing '$host': $@" };
+ log_error { "Failure when probing $host: $@" };
next;
}
sub _store {
my ($self, $host, $group, $gathered) = @_;
- log_info { "Storing data for group '$group' on '$host'" };
+ log_info { "Storing data for group $group on $host" };
my $storage = $self->storage($host, $group);
my $ok = eval {
my @files;
for my $class ($gathered->probe_names) {
- log_trace { "Storing data for probe name '$class'" };
+ log_info { "Storing data for probe name $class" };
my $file = sprintf '%s.json', join '/',
map lc, map {
s{([a-z0-9])([A-Z])}{${1}_${2}}g;
log_trace { "Collecting probe data for '$class' from '$host'" };
my $data = $gathered->get_probe_data($class);
log_debug { "Generated file name for storage: '$file'; Writing state to '$full_path'" };
- Dlog_trace { "Input to storage engine: $_" } $data;
print $fh encode_json($gathered->get_probe_data($class));
push @files, $full_path;
log_trace { "Finished storing data for '$class'" };
}
$self->_cleanup($storage, [@files]);
- log_trace { "Comitting stored data" };
+ log_info { "Comitting stored data" };
$storage->commit;
};