| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Copyright (c) 2011-2017 FastMail Pty Ltd. All rights reserved. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions |
| 7 |
# are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer in |
| 14 |
# the documentation and/or other materials provided with the |
| 15 |
# distribution. |
| 16 |
# |
| 17 |
# 3. The name "Fastmail Pty Ltd" must not be used to |
| 18 |
# endorse or promote products derived from this software without |
| 19 |
# prior written permission. For permission or any legal |
| 20 |
# details, please contact |
| 21 |
# FastMail Pty Ltd |
| 22 |
# PO Box 234 |
| 23 |
# Collins St West 8007 |
| 24 |
# Victoria |
| 25 |
# Australia |
| 26 |
# |
| 27 |
# 4. Redistributions of any form whatsoever must retain the following |
| 28 |
# acknowledgment: |
| 29 |
# "This product includes software developed by Fastmail Pty. Ltd." |
| 30 |
# |
| 31 |
# FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 32 |
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 33 |
# EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT |
| 34 |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF |
| 35 |
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 36 |
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 37 |
# OF THIS SOFTWARE. |
| 38 |
# |
| 39 |
|
| 40 |
package Cassandane::Util::Log; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use File::Basename; |
| 44 |
use Scalar::Util qw(blessed); |
| 45 |
use Sys::Syslog qw(:standard :macros); |
| 46 |
|
| 47 |
use Exporter (); |
| 48 |
our @ISA = qw(Exporter); |
| 49 |
our @EXPORT = qw( |
| 50 |
&xlog &set_verbose &get_verbose |
| 51 |
); |
| 52 |
|
| 53 |
my $verbose = 0; |
| 54 |
|
| 55 |
openlog('cassandane', '', LOG_LOCAL6) |
| 56 |
or die "Cannot openlog"; |
| 57 |
|
| 58 |
sub xlog |
| 59 |
{ |
| 60 |
my $id; |
| 61 |
my $highlight = 0; |
| 62 |
|
| 63 |
# if the first argument is an object with an id() method, |
| 64 |
# include the id it returns in the log message |
| 65 |
if (ref $_[0] && blessed $_[0] && $_[0]->can('id')) { |
| 66 |
my $obj = shift @_; |
| 67 |
$id = $obj->id(); |
| 68 |
} |
| 69 |
|
| 70 |
# if the first output argument starts with XXX, highlight the |
| 71 |
# whole line when printing to stderr |
| 72 |
if ($_[0] =~ m/^XXX/) { |
| 73 |
$highlight = 1; |
| 74 |
} |
| 75 |
|
| 76 |
# the current line number is in this frame |
| 77 |
my (undef, undef, $line) = caller(); |
| 78 |
# but the current subroutine name is in the parent frame, |
| 79 |
# as the function-the-caller-called |
| 80 |
my (undef, undef, undef, $sub) = caller(1); |
| 81 |
$sub //= basename($0); |
| 82 |
$sub =~ s/^Cassandane:://; |
| 83 |
my $msg = "[$$] =====> $sub\[$line] "; |
| 84 |
$msg .= "($id) " if $id; |
| 85 |
$msg .= join(' ', @_); |
| 86 |
if ($highlight) { |
| 87 |
print STDERR "\033[33m" . $msg . "\033[0m\n"; |
| 88 |
} |
| 89 |
else { |
| 90 |
print STDERR "$msg\n"; |
| 91 |
} |
| 92 |
syslog(LOG_ERR, "$msg"); |
| 93 |
} |
| 94 |
|
| 95 |
sub set_verbose |
| 96 |
{ |
| 97 |
my ($v) = @_; |
| 98 |
$verbose = 0 + $v; |
| 99 |
} |
| 100 |
|
| 101 |
sub get_verbose |
| 102 |
{ |
| 103 |
return $verbose; |
| 104 |
} |
| 105 |
|
| 106 |
1; |