1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
# -----------------------------------------------------------------------------
# $Id: Writer.pm 11365 2008-05-10 14:58:28Z topia $
# -----------------------------------------------------------------------------
# copyright (C) 2004 Topia <topia@clovery.jp>. all rights reserved.
package Log::Writer;
use strict;
use warnings;
use RunLoop;
use Carp;
use File::Spec;
use DirHandle;
use Tiarra::SharedMixin qw(shared shared_writer);
use Tiarra::WrapMainLoop;
use Tiarra::Utils;
our $_shared_instance;
Tiarra::Utils->define_attr_getter(0, qw(mainloop));
Tiarra::Utils->define_proxy('mainloop', 0,
map { ["_mainloop_$_", "lazy_$_"] }
qw(install uninstall));
# todo:
# - accept uri(maybe: ssh, syslog, ...)
sub _new {
my $class = shift;
my ($this) = {
objects => {},
schemes => {},
protocols => {},
fallbacks => [],
};
bless $this, $class;
$this->{mainloop} = Tiarra::WrapMainLoop->new(
type => 'timer',
interval => 120,
closure => sub { $this->run; });
return $this;
}
sub _initialize {
my $this = shift;
$this->load_all_protocols;
}
sub find_object {
my ($this, $path, %options) = @_;
my $object = $this->{objects}->{$path};
if (defined($object)) {
# ファイルが存在したので返す。
return $object;
} else {
# ファイルは存在しないので、登録して返す。
return $this->_register_inner($path, %options);
}
}
sub register {
my ($this, $path, %options) = @_;
my $object = $this->find_object($path, %options);
if (defined $object) {
# ファイルを得られた。
# 参照回数を増やして返す。
$object->register;
return $object;
} else {
return undef;
}
}
sub unregister {
my ($this, $path) = @_;
my $object = $this->{objects}->{$path};
if (defined $object) {
return $object->unregister;
} else {
croak('object "' . $path . '" has not registered yet!');
}
}
sub _register_inner {
my ($this, $path, %options) = @_;
my $object;
my @classes;
if ($path =~ m|^([^:]+):|) {
if (defined $this->{schemes}->{$1}) {
push(@classes, @{$this->{schemes}->{$1}});
}
}
push(@classes, @{$this->{fallbacks}});
foreach my $class (@classes) {
$object = $class->new($this, $path, %options);
last if defined $object;
}
if (defined $object) {
$this->{objects}->{$path} = $object;
$this->_mainloop_install;
return $object;
} else {
return undef;
}
}
sub run {
my ($this, $destruct) = @_;
# do object
foreach my $key (keys %{$this->{objects}}) {
my $object = $this->{objects}->{$key};
$object->flush;
$object->destruct(1) if $destruct;
}
}
sub destruct {
shared_writer->run(1);
shared_writer->{mainloop} = undef;
}
sub object_release {
my ($this, $path) = @_;
delete $this->{objects}->{$path};
if (!(%{$this->{objects}})) {
$this->_mainloop_uninstall;
}
}
# protocol
sub load_all_protocols {
my $class_or_this = shift;
my $this = $class_or_this->_this;
my $pkg_dir = File::Spec->catdir(split(/::/, ref($this)));
foreach (@INC) {
my $dir = File::Spec->catdir($_, $pkg_dir);
my $dh = DirHandle->new($dir);
if (defined $dh) {
my $path;
foreach my $file ($dh->read) {
$path = File::Spec->catdir($dir, $file);
next if !-r $path || -d $path;
next if $file !~ /^(.+)\.pm$/;
$this->load_protocol(ref($this).'::'.$1);
}
}
}
}
sub load_protocol {
my ($class_or_this, $pkg) = @_;
my $this = $class_or_this->_this;
return 1 if $this->{protocols}->{$pkg};
eval 'use ' . $pkg;
if ($@) {
$this->notify_error("load protocol($pkg) failed: $@");
return undef;
}
eval 'use Module::Use ($pkg);';
if ($@) {
$this->notify_error("register protocol($pkg) to module manager failed: $@");
return undef;
}
foreach my $scheme ($pkg->supported_schemes) {
push(@{$this->{schemes}->{$scheme}}, $pkg);
}
if ($pkg->capability('fallback')) {
push(@{$this->{fallbacks}}, $pkg);
}
$this->{protocols}->{$pkg} = 1;
return 1;
}
sub unload_protocol {
my ($class_or_this, $pkg) = @_;
my $this = $class_or_this->_this;
return 0 if !$this->{protocols}->{$pkg};
if ($pkg->capability('fallback')) {
@{$this->{fallbacks}} = grep $_ ne $pkg, @{$this->{fallbacks}};
}
foreach my $scheme ($pkg->supported_schemes) {
@{$this->{schemes}->{$scheme}} = grep $_ ne $pkg,
@{$this->{schemes}->{$scheme}};
}
delete $this->{protocols}->{$pkg};
return 1;
}
# util
sub notify_warn {
my ($this, $str) = @_;
RunLoop->shared_loop->notify_warn($str);
}
sub notify_error {
my ($this, $str) = @_;
RunLoop->shared_loop->notify_error($str);
}
sub notify_msg {
my ($this, $str) = @_;
RunLoop->shared_loop->notify_msg($str);
}
1;
|