summaryrefslogtreecommitdiff
path: root/LedgerSMB/Log.pm
blob: 558578ec6b8ecbc349569baaf2bcd64fefadf1b2 (plain)
  1. =head1 NAME
  2. LedgerSMB::Log - LedgerSMB logging and debugging framework
  3. =head1 SYOPSIS
  4. This module maintains a connection to the LedgerSMB log file
  5. (Seperate from the apche error log, for now)
  6. =head1 METHODS
  7. This module is loosly based on Apache::Log.
  8. Available methods: (in order, most to least severe)
  9. =over 4
  10. =item emerg
  11. =item alert
  12. =item crit
  13. =item error
  14. =item warn
  15. =item notice
  16. =item info
  17. =item debug
  18. =item longmess
  19. This uses Carp to make a debug message with the full stack backtrace, including function arguments, where Carp can infer them.
  20. =item dump
  21. This uses Data::Dumper to dump the contents of a data structure as a debug message.
  22. =back
  23. =cut
  24. package LedgerSMB::Log;
  25. use strict;
  26. use warnings;
  27. use IO::File;
  28. use Data::Dumper;
  29. use LedgerSMB::Sysconfig;
  30. use Carp ();
  31. our $fh;
  32. sub print {
  33. if (!$LedgerSMB::Sysconfig::logging){
  34. return 0;
  35. }
  36. shift;
  37. unless($fh) {
  38. # TODO: this is grosly wrong, but so is this module in the first place.
  39. # the log messages *should* end up in the apache log, but that will
  40. # hopefully be corrected in the future.
  41. $fh=IO::File->new('>>users/ledger-smb.log');
  42. $fh->autoflush(1);
  43. __PACKAGE__->print('general',"Log file opened");
  44. }
  45. $fh->print(sprintf('[%s] [%s] %i %s',
  46. scalar(localtime),
  47. +shift,
  48. $$,
  49. join(' ',@_))."\n");
  50. }
  51. sub emerg { shift->print('emerg',@_) }
  52. sub alert { shift->print('alert',@_) }
  53. sub crit { shift->print('crit',@_) }
  54. sub error { shift->print('error',@_) }
  55. sub warn { shift->print('warn',@_) }
  56. sub notice { shift->print('notice',@_) }
  57. sub info { shift->print('info',@_) }
  58. sub debug { shift->print('debug',@_) }
  59. sub longmess { shift->print('debug',Carp::longmess(@_)) }
  60. sub dump {
  61. my $self = shift;
  62. my $d = Data::Dumper->new([@_]);
  63. $d->Sortkeys(1);
  64. $self->print('debug',$d->Dump());
  65. }
  66. 1;