Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | magnus | 1 | package DebPool::Logging; |
2 | |||
3 | ### |
||
4 | # |
||
5 | # DebPool::Logging - Module to handle logging messages |
||
6 | # |
||
7 | # Copyright 2003-2004 Joel Aelwyn. All rights reserved. |
||
8 | # |
||
9 | # Redistribution and use in source and binary forms, with or without |
||
10 | # modification, are permitted provided that the following conditions |
||
11 | # are met: |
||
12 | # 1. Redistributions of source code must retain the above copyright |
||
13 | # notice, this list of conditions and the following disclaimer. |
||
14 | # 2. Redistributions in binary form must reproduce the above copyright |
||
15 | # notice, this list of conditions and the following disclaimer in the |
||
16 | # documentation and/or other materials provided with the distribution. |
||
17 | # 3. Neither the name of the Author nor the names of any contributors |
||
18 | # may be used to endorse or promote products derived from this software |
||
19 | # without specific prior written permission. |
||
20 | # |
||
21 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
||
22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
||
25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
||
27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
||
30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
||
31 | # SUCH DAMAGE. |
||
32 | # |
||
33 | # $Id: Logging.pm 31 2005-01-19 17:32:38Z joel $ |
||
34 | # |
||
35 | ### |
||
36 | |||
37 | # We use 'our', so we must have at least Perl 5.6 |
||
38 | |||
39 | require 5.006_000; |
||
40 | |||
41 | # Always good ideas. |
||
42 | |||
43 | use strict; |
||
44 | use warnings; |
||
45 | |||
46 | # For strftime() |
||
47 | |||
48 | use POSIX; |
||
49 | |||
50 | # We need to pull config option information |
||
51 | |||
52 | use DebPool::Config qw(:vars); |
||
53 | use DebPool::DB qw(:functions); # DB::Close_Databases |
||
54 | |||
55 | ### Module setup |
||
56 | |||
57 | BEGIN { |
||
58 | use Exporter (); |
||
59 | our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); |
||
60 | |||
61 | # Version checking |
||
62 | $VERSION = '0.1.5'; |
||
63 | |||
64 | @ISA = qw(Exporter); |
||
65 | |||
66 | @EXPORT = qw( |
||
67 | ); |
||
68 | |||
69 | @EXPORT_OK = qw( |
||
70 | &Log_Message |
||
71 | &LOG_AUDIT |
||
72 | &LOG_CONFIG |
||
73 | &LOG_DEBUG |
||
74 | &LOG_ERROR |
||
75 | &LOG_FATAL |
||
76 | &LOG_GENERAL |
||
77 | &LOG_GPG |
||
78 | &LOG_INFO |
||
79 | &LOG_INSTALL |
||
80 | &LOG_PARSE |
||
81 | &LOG_REJECT |
||
82 | &LOG_WARNING |
||
83 | ); |
||
84 | |||
85 | %EXPORT_TAGS = ( |
||
86 | 'functions' => [qw(&Log_Message)], |
||
87 | 'vars' => [qw()], |
||
88 | 'facility' => [qw(&LOG_AUDIT &LOG_CONFIG &LOG_GENERAL &LOG_GPG |
||
89 | &LOG_INSTALL &LOG_PARSE &LOG_REJECT)], |
||
90 | 'level' => [qw(&LOG_DEBUG &LOG_INFO &LOG_WARNING &LOG_ERROR |
||
91 | &LOG_FATAL)], |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | ### Exported package globals |
||
96 | |||
97 | # None |
||
98 | |||
99 | ### Non-exported package globals |
||
100 | |||
101 | # Thread-safe? What's that? Package global error value. We don't export |
||
102 | # this directly, because it would conflict with other modules. |
||
103 | |||
104 | our($Error); |
||
105 | |||
106 | ### File lexicals |
||
107 | |||
108 | # None |
||
109 | |||
110 | ### Constant functions - facility |
||
111 | |||
112 | sub LOG_AUDIT() { 'AUDIT' } |
||
113 | sub LOG_CONFIG() { 'CONFIG' } |
||
114 | sub LOG_GENERAL() { 'GENERAL' } |
||
115 | sub LOG_GPG() { 'GPG' } |
||
116 | sub LOG_INSTALL() { 'INSTALL' } |
||
117 | sub LOG_REJECT() { 'REJECT' } |
||
118 | sub LOG_PARSE() { 'PARSE' } |
||
119 | |||
120 | ### Constant functions - level |
||
121 | |||
122 | sub LOG_DEBUG() { 'DEBUG' } |
||
123 | sub LOG_INFO() { 'INFO' } |
||
124 | sub LOG_WARNING() { 'WARNING' } |
||
125 | sub LOG_ERROR() { 'ERROR' } |
||
126 | sub LOG_FATAL() { 'FATAL' } |
||
127 | |||
128 | ### Meaningful functions |
||
129 | |||
130 | # Log_Message($message, FACILITY, LEVEL) |
||
131 | # |
||
132 | # Log a message with text $message using FACILITY and LEVEL, via the current |
||
133 | # configured log method. |
||
134 | |||
135 | # FIXME - this is a really crude logging setup. We should probably support |
||
136 | # a variety of things, like logging to processes, syslogging, not doing an |
||
137 | # open/close for each message, maybe email logging with batched messages. |
||
138 | # |
||
139 | # However, this is an early version, so it will suffice for now. |
||
140 | |||
141 | sub Log_Message { |
||
142 | my($msg, $facility, $level) = @_; |
||
143 | |||
144 | # First, do we have anywhere to log? We assume that 'undef' is an |
||
145 | # explicit request to not log, since it isn't a default value. |
||
146 | |||
147 | if (!defined($Options{'log_file'})) { |
||
148 | return; |
||
149 | } |
||
150 | |||
151 | # If we can't log to it, die with a message (on the off chance that we're |
||
152 | # not in daemon mode, and the user will see it). |
||
153 | |||
154 | if (!open(LOG, ">>$Options{'log_file'}")) { |
||
155 | Close_Databases(); # If they were open |
||
156 | unlink($Options{'lock_file'}); # In case we had one |
||
157 | |||
158 | die "Couldn't write to log file '$Options{'log_file'}'."; |
||
159 | } |
||
160 | |||
161 | print LOG strftime("%Y-%m-%d %H:%M:%S", localtime()); |
||
162 | print LOG " [$facility/$level] $msg\n"; |
||
163 | close(LOG); |
||
164 | } |
||
165 | |||
166 | END {} |
||
167 | |||
168 | 1; |
||
169 | |||
170 | __END__ |
||
171 | |||
172 | # vim:set tabstop=4 expandtab: |