home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / NDC.pm < prev    next >
Encoding:
Perl POD Document  |  2003-01-14  |  3.1 KB  |  123 lines

  1. ##################################################
  2. package Log::Log4perl::NDC;
  3. ##################################################
  4.  
  5. use 5.006;
  6. use strict;
  7. use warnings;
  8.  
  9. our @NDC_STACK = ();
  10. our $MAX_SIZE  = 5;
  11.  
  12. ###########################################
  13. sub get {
  14. ###########################################
  15.     if(@NDC_STACK) {
  16.         # Return elements blank separated
  17.         return join " ", @NDC_STACK;
  18.     } else {
  19.         return "[undef]";
  20.     }
  21. }
  22.  
  23. ###########################################
  24. sub pop {
  25. ###########################################
  26.     if(@NDC_STACK) {
  27.         return pop @NDC_STACK;
  28.     } else {
  29.         return undef;
  30.     }
  31. }
  32.  
  33. ###########################################
  34. sub push {
  35. ###########################################
  36.     my($self, $text) = @_;
  37.  
  38.     unless(defined $text) {
  39.         # Somebody called us via Log::Log4perl::NDC::push("blah") ?
  40.         $text = $self;
  41.     }
  42.  
  43.     if(@NDC_STACK >= $MAX_SIZE) {
  44.         CORE::pop(@NDC_STACK);
  45.     }
  46.  
  47.     return push @NDC_STACK, $text;
  48. }
  49.  
  50. ###########################################
  51. sub remove {
  52. ###########################################
  53.     @NDC_STACK = ();
  54. }
  55.  
  56. __END__
  57.  
  58. =head1 NAME
  59.  
  60. Log::Log4perl::NDC - Nested Diagnostic Context
  61.  
  62. =head1 DESCRIPTION
  63.  
  64. Log::Log4perl allows loggers to maintain global thread-specific data, 
  65. called the Nested Diagnostic Context (NDC).
  66.  
  67. At some point, the application might decide to push a piece of
  68. data onto the NDC stack, which other parts of the application might 
  69. want to reuse. For example, at the beginning of a web request in a server,
  70. the application might decide to push the IP address of the client
  71. onto the stack to provide it for other loggers down the road without
  72. having to pass the data from function to function.
  73.  
  74. The Log::Log4perl::Layout::PatternLayout class even provides the handy
  75. C<%x> placeholder which is replaced by the blank-separated list
  76. of elements currently on the stack.
  77.  
  78. This module maintains a simple stack which you can push data on to, query
  79. what's on top, pop it off again or delete the entire stack.
  80.  
  81. Its purpose is to provide a thread-specific context which all 
  82. Log::Log4perl loggers can refer to without the application having to
  83. pass around the context data between its functions.
  84.  
  85. Since in 5.8.0 perl's threads don't share data only upon request,
  86. global data is by definition thread-specific.
  87.  
  88. =over 4
  89.  
  90. =item Log::Log4perl::NDC->push($text);
  91.  
  92. Push an item onto the stack. If the stack grows beyond the defined
  93. limit (C<$Log::Log4perl::NDC::MAX_SIZE>), just the topmost element
  94. will be replated.
  95.  
  96. This is typically done when a context is entered.
  97.  
  98. =item Log::Log4perl::NDC->pop();
  99.  
  100. Discard the upmost element of the stack. This is typically done when
  101. a context is left.
  102.  
  103. =item my $text = Log::Log4perl::NDC->get();
  104.  
  105. Retrieve the content of the stack as a string of blank-separated values
  106. without disrupting the stack structure. Typically done by C<%x>.
  107. If the stack is empty the value C<"[undef]"> is being returned.
  108.  
  109. =item Log::Log4perl::NDC->remove();
  110.  
  111. Reset the stack, remove all items.
  112.  
  113. =back
  114.  
  115. Please note that all of the methods above are class methods, there's no
  116. instances of this class.
  117.  
  118. =head1 AUTHOR
  119.  
  120. Mike Schilli, E<lt>log4perl@perlmeister.comE<gt>
  121.  
  122. =cut
  123.