xref: /openbsd/usr.sbin/pkg_add/OpenBSD/Log.pm (revision 8529ddd3)
1# ex:ts=8 sw=4:
2# $OpenBSD: Log.pm,v 1.9 2014/07/27 22:17:33 espie Exp $
3#
4# Copyright (c) 2007-2010 Marc Espie <espie@openbsd.org>
5#
6# Permission to use, copy, modify, and distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17#
18
19use strict;
20use warnings;
21
22package OpenBSD::Log;
23
24sub new
25{
26	my ($class, $printer) = @_;
27	bless { p => $printer }, $class;
28}
29
30sub set_context
31{
32	my ($self, $context) = @_;
33	$self->{context} = $context;
34}
35
36sub messages
37{
38	my $self = shift;
39	$self->{context} //= "???";
40	return $self->{messages}{$self->{context}} //= [];
41}
42
43sub errmessages
44{
45	my $self = shift;
46	$self->{context} //= "???";
47	return $self->{errmessages}{$self->{context}} //= [];
48}
49
50sub f
51{
52	my $self = shift;
53	$self->{p}->f(@_);
54}
55
56sub print
57{
58	my $self = shift;
59	push(@{$self->messages}, $self->f(@_));
60}
61
62sub say
63{
64	my $self = shift;
65	push(@{$self->messages}, $self->f(@_)."\n");
66}
67
68sub errprint
69{
70	my $self = shift;
71	push(@{$self->errmessages}, $self->f(@_));
72}
73
74sub errsay
75{
76	my $self = shift;
77	push(@{$self->errmessages}, $self->f(@_)."\n");
78}
79
80sub specialsort
81{
82	return ((sort grep { /^\-/ } @_), (sort grep { /^\+/} @_),
83	    (sort grep { !/^[\-+]/ } @_));
84}
85
86sub dump
87{
88	my $self = shift;
89	for my $ctxt (specialsort keys %{$self->{errmessages}}) {
90		my $msgs = $self->{errmessages}{$ctxt};
91		if (@$msgs > 0) {
92			$self->{p}->errsay("--- #1 -------------------", $ctxt);
93			$self->{p}->_errprint(@$msgs);
94		}
95	}
96	$self->{errmessages} = {};
97	for my $ctxt (specialsort keys %{$self->{messages}}) {
98		my $msgs = $self->{messages}{$ctxt};
99		if (@$msgs > 0) {
100			$self->{p}->say("--- #1 -------------------", $ctxt);
101			$self->{p}->_print(@$msgs);
102		}
103	}
104	$self->{messages} = {};
105}
106
107sub fatal
108{
109	my $self = shift;
110	if (defined $self->{context}) {
111		$self->{p}->_fatal($self->{context}, ":", $self->f(@_));
112	}
113
114	$self->{p}->_fatal($self->f(@_));
115}
116
117sub system
118{
119	my $self = shift;
120	if (open(my $grab, "-|", @_)) {
121		while (<$grab>) {
122			$self->{p}->_print($_);
123		}
124		if (!close $grab) {
125			$self->{p}->say("system(#1) failed: #2 #3",
126			    join(", ", @_), $!,
127			    $self->{p}->child_error);
128		}
129		return $?;
130	} else {
131		$self->{p}->say("system(#1) was not run: #2 #3",
132		    join(", ", @_), $!, $self->{p}->child_error);
133	}
134}
135
1361;
137