Class: Logging::Layouts::EvenOddPattern::SprintfBag

Inherits:
Object
  • Object
show all
Defined in:
lib/logging/layouts/odd_even_pattern.rb

Instance Method Summary collapse

Constructor Details

#initialize(color_scheme, color_alias_table, directive_table) ⇒ SprintfBag

Returns a new instance of SprintfBag.



49
50
51
52
53
54
55
56
57
58
# File 'lib/logging/layouts/odd_even_pattern.rb', line 49

def initialize(color_scheme,color_alias_table, directive_table)
  @format_string='"'
  @format_string_bright='"'
  @args=[]
  @name_map={}
  @color_scheme=color_scheme
  @color_alias_table=color_alias_table
  @directive_table = directive_table

end

Instance Method Details

#add(str) ⇒ Object Also known as: <<



79
80
81
82
83
# File 'lib/logging/layouts/odd_even_pattern.rb', line 79

def add(str)
  @format_string << str
  @format_string_bright << str
  self
end

#add_arg(arg, modify = false) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/logging/layouts/odd_even_pattern.rb', line 86

def add_arg(arg, modify=false)
  if modify
    @args.last << arg
  else
    @args << arg
  end
  self
end

#add_colored(string, pattern_name, arg = nil) ⇒ Object

Parameters:

  • pattern_name (String)

    like ‘message’ or ‘time’

  • string (String)

    the string that needs coloring



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/logging/layouts/odd_even_pattern.rb', line 62

def add_colored(string,pattern_name,arg=nil)
  if @color_scheme and !@color_scheme.lines?
    @format_string << @color_scheme.color(string,@color_alias_table[pattern_name])
    @format_string_bright << @color_scheme.color(string,"#{@color_alias_table[pattern_name]}_bright")
  else
    self << string
  end

  directive=@directive_table[pattern_name]
  if @directive_table[pattern_name].respond_to?(:call)
    @args << directive.call(arg)
  else
    @args << directive.dup
  end
  self
end

#add_variable(value, arg = "@var") ⇒ Object



95
96
97
98
99
100
# File 'lib/logging/layouts/odd_even_pattern.rb', line 95

def add_variable(value,arg="@var")
  map_name="@map_name_#{@name_map.size}"
  @name_map[map_name]=value
  @args << arg.gsub("@var",map_name)
  self
end

#set_variables(obj) ⇒ Object



102
103
104
105
106
# File 'lib/logging/layouts/odd_even_pattern.rb', line 102

def set_variables(obj)
  @name_map.each_pair do |name, value|
    obj.instance_variable_set(name.to_sym, value)
  end
end

#to_sprintf(bright = false) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/logging/layouts/odd_even_pattern.rb', line 108

def to_sprintf(bright=false)
  format = bright ? @format_string_bright : @format_string
  sprintf = "sprintf("
  sprintf <<  format + '"'
  sprintf << ', ' + @args.join(', ') unless @args.empty?
  sprintf << ")"

  if @color_scheme and @color_scheme.lines? #generate colors at runtime
    sprintf = "color_scheme.color(#{sprintf}, event.even? ? ::Logging::LNAMES[event.level] : ::Logging::LNAMES[event.level].to_s + '_bright' )"

  end

  sprintf
end