File: NDC.rb

package info (click to toggle)
ruby-log4r 1.1.10-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 648 kB
  • sloc: ruby: 2,744; xml: 96; makefile: 5
file content (86 lines) | stat: -rw-r--r-- 1,953 bytes parent folder | download | duplicates (4)
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
# :include: rdoc/NDC
#
# == Other Info
# 
# Version:: $Id$
# Author:: Colby Gutierrez-Kraybill <colby(at)astro.berkeley.edu>

module Log4r
  NDCNAME = "log4rNDC"
  NDCNAMEMAXDEPTH = "log4rNDCMAXDEPTH"
  NDCDEFAULTMAXDEPTH = 256

  # See log4r/NDC.rb
  class NDC
    private_class_method :new

    def self.check_thread_instance()
      if ( Thread.current[NDCNAME] == nil ) then
	Thread.current[NDCNAME] = Array.new
	Thread.current[NDCNAMEMAXDEPTH] = NDCDEFAULTMAXDEPTH
      end
    end

    def self.clear()
      self.check_thread_instance()
      Thread.current[NDCNAME].clear
    end

    def self.clone_stack()
      self.check_thread_instance()
      return Thread.current[NDCNAME].clone
    end

    def self.get_depth()
      self.check_thread_instance()
      return Thread.current[NDCNAME].length
    end

    def self.inherit( a_stack )
      if ( a_stack.class == Array ) then
	if ( Thread.current[NDCNAME] != nil ) then
	  Thread.current[NDCNAME].clear
	  Thread.current[NDCNAME] = nil
	end
	Thread.current[NDCNAME] = a_stack
      else
	raise "Expecting Array in NDC.inherit"
      end
    end

    def self.get()
      self.check_thread_instance
      return Thread.current[NDCNAME] * " "
    end

    def self.peek()
      self.check_thread_instance()
      return Thread.current[NDCNAME].last
    end

    def self.pop()
      self.check_thread_instance()
      return Thread.current[NDCNAME].pop
    end

    def self.push( value )
      self.check_thread_instance()
      if ( Thread.current[NDCNAME].length < Thread.current[NDCNAMEMAXDEPTH] ) then
	Thread.current[NDCNAME].push( value )
      end
    end

    def self.remove()
      self.check_thread_instance()
      Thread.current[NDCNAME].clear
      Thread.current[NDCNAMEMAXDEPTH] = nil
      Thread.current[NDCNAME] = nil
    end

    def self.set_max_depth( max_depth )
      self.check_thread_instance()
      Thread.current[NDCNAMEMAXDEPTH] = max_depth
    end
  end
end