## Fluentd## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#require'json'require'fluent/config'require'fluent/config/element'moduleFluentmoduleConfigmoduleDSLRESERVED_PARAMETERS=[:type,:id,:log_level,:label]# Need '@' prefix for reserved parametersmoduleParserdefself.read(path)path=File.expand_path(path)data=File.read(path)parse(data,path)enddefself.parse(source,source_path="config.rb")Proxy.new('ROOT',nil).eval(source,source_path).to_config_elementendendclassProxydefinitialize(name,arg,include_basepath=Dir.pwd)@element=Element.new(name,arg,self)@include_basepath=include_basepathenddefelement@elementenddefinclude_basepath@include_basepathenddefeval(source,source_path)@element.instance_eval(source,source_path)selfenddefto_config_element@element.instance_evaldoConfig::Element.new(@name,@arg,@attrs,@elements)endenddefadd_element(name,arg,block)::Kernel.raise::ArgumentError,"#{name} block must be specified"ifblock.nil?proxy=self.class.new(name.to_s,arg)proxy.element.instance_exec(&block)@element.instance_evaldo@elements.push(proxy.to_config_element)endselfendendclassElement<BasicObjectdefinitialize(name,arg,proxy)@name=name@arg=arg||''@attrs={}@elements=[]@proxy=proxyenddefto_int__id__enddefmethod_missing(name,*args,&block)::Kernel.raise::ArgumentError,"Configuration DSL Syntax Error: only one argument allowed"ifargs.size>1value=args.firstifblockproxy=Proxy.new(name.to_s,value)proxy.element.instance_exec(&block)@elements.push(proxy.to_config_element)elseparam_name=RESERVED_PARAMETERS.include?(name)?"@#{name}":name.to_s@attrs[param_name]=ifvalue.is_a?(Array)||value.is_a?(Hash)JSON.dump(value)elsevalue.to_sendendselfenddefinclude(*args)::Kernel.raise::ArgumentError,"#{name} block requires arguments for include path"ifargs.nil?||args.size!=1ifargs.first=~/\.rb$/path=File.expand_path(args.first)data=File.read(path)self.instance_eval(data,path)elsess=StringScanner.new('')Config::V1Parser.new(ss,@proxy.include_basepath,'',nil).eval_include(@attrs,@elements,args.first)endenddefsource(&block)@proxy.add_element('source',nil,block)enddefmatch(*args,&block)::Kernel.raise::ArgumentError,"#{name} block requires arguments for match pattern"ifargs.nil?||args.size!=1@proxy.add_element('match',args.first,block)enddefself.const_missing(name)return::Kernel.const_get(name)if::Kernel.const_defined?(name)ifname.to_s=~/^Fluent::Config::DSL::Element::(.*)$/name="#{$1}".to_symreturn::Kernel.const_get(name)if::Kernel.const_defined?(name)end::Kernel.eval("#{name}")enddefruby(&block)ifblock@proxy.instance_exec(&block)else::Kernelendendendendendend