Reporter.pm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: Reporter.pm,v 1.29 2016/06/28 15:28:20 espie Exp $
  3. #
  4. # Copyright (c) 2010-2013 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. use strict;
  18. use warnings;
  19. use OpenBSD::Error;
  20. use DPB::Util;
  21. use DPB::Clock;
  22. package DPB::Reporter;
  23. my $singleton;
  24. sub ttyclass()
  25. {
  26. require DPB::Reporter::Tty;
  27. return "DPB::Reporter::Tty";
  28. }
  29. sub term_send
  30. {
  31. }
  32. sub reset_cursor
  33. {
  34. my $self = shift;
  35. print $self->{visible} if defined $self->{visible};
  36. }
  37. sub set_cursor
  38. {
  39. my $self = shift;
  40. print $self->{invisible} if defined $self->{invisible};
  41. }
  42. sub reset
  43. {
  44. my $self = shift;
  45. $self->reset_cursor;
  46. print $self->{clear} if defined $self->{clear};
  47. }
  48. sub set_sigtstp
  49. {
  50. my $self =shift;
  51. $SIG{TSTP} = sub {
  52. $self->reset_cursor;
  53. DPB::Clock->stop;
  54. $SIG{TSTP} = 'DEFAULT';
  55. local $> = 0;
  56. kill TSTP => $$;
  57. };
  58. }
  59. sub set_sig_handlers
  60. {
  61. my $self = shift;
  62. $self->set_sigtstp;
  63. }
  64. sub sig_received
  65. {
  66. my ($self, $iscont) = @_;
  67. if ($iscont) {
  68. $self->set_sigtstp;
  69. $self->{continued} = 1;
  70. DPB::Clock->restart;
  71. }
  72. $self->handle_window;
  73. }
  74. sub refresh
  75. {
  76. }
  77. sub handle_window
  78. {
  79. }
  80. sub filter_can
  81. {
  82. my ($self, $r, $method) = @_;
  83. my @kept = ();
  84. for my $prod (@$r) {
  85. push @kept, $prod if $prod->can($method);
  86. }
  87. return \@kept;
  88. }
  89. sub new
  90. {
  91. my $class = shift;
  92. my $state = shift;
  93. my $dotty;
  94. if ($state->opt('x')) {
  95. $dotty = 0;
  96. } elsif ($state->opt('m')) {
  97. $dotty = 1;
  98. } else {
  99. $dotty = -t STDOUT;
  100. }
  101. if ($dotty) {
  102. $class->ttyclass->new($state, @_);
  103. } else {
  104. $class->make_singleton($state, @_);
  105. }
  106. }
  107. sub make_singleton
  108. {
  109. my $class = shift;
  110. my $state = shift;
  111. return if defined $singleton;
  112. $singleton = bless {msg => '',
  113. producers => $class->filter_can(\@_, $class->filter),
  114. timeout => $state->{display_timeout} // 10,
  115. state => $state,
  116. continued => 0}, $class;
  117. $state->{reporter} = $singleton;
  118. if ($state->{record}) {
  119. $singleton->{record} =
  120. $state->{log_user}->open('>>', $state->{record});
  121. }
  122. return $singleton;
  123. }
  124. sub filter
  125. {
  126. 'important';
  127. }
  128. sub timeout
  129. {
  130. my $self = shift;
  131. return $self->{timeout};
  132. }
  133. sub report
  134. {
  135. my $self = shift;
  136. for my $prod (@{$self->{producers}}) {
  137. my $important = $prod->important;
  138. if ($important) {
  139. print $important;
  140. }
  141. }
  142. }
  143. sub myprint
  144. {
  145. my $self = shift;
  146. if (!ref $self) {
  147. $singleton->myprint(@_);
  148. } else {
  149. print @_;
  150. }
  151. }
  152. 1;