lib/log4r/outputter/evernoteoutputter.rb in log4ever-0.1.2 vs lib/log4r/outputter/evernoteoutputter.rb in log4ever-0.1.3

- old
+ new

@@ -3,19 +3,20 @@ require 'log4r/outputter/outputter' require "log4r/staticlogger" require 'active_support' require 'active_support/time' require 'active_support/core_ext' +require 'nkf' module Log4r class EvernoteOutputter < Outputter def initialize(_name, hash = {}) super(_name, hash) validate(hash) end - + # synchronize note def sync @note = @evernote.note(@notebook) @tag = @evernote.tag(@note) @tag.names = @tags @@ -27,15 +28,15 @@ def validate(hash) is_sandbox = hash[:sandbox] || hash['sandbox'] || false raise ArgumentError, "Sandbox must be type of boolean" unless is_sandbox == false || is_sandbox == true @auth_token = hash[:auth_token] || hash['auth_token'] || "" raise ArgumentError, "Must specify from auth token" if @auth_token.empty? - notebook_name = hash[:notebook] || hash['notebook'] || "" + notebook_name = to_utf8(hash[:notebook] || hash['notebook'] || "") raise ArgumentError, "Must specify from notebook" if notebook_name.empty? - stack_name = hash[:stack] || hash['stack'] + stack_name = to_utf8(hash[:stack] || hash['stack']) @evernote = Log4ever::Evernote.new(@auth_token, is_sandbox) - @tags = hash[:tags] || hash['tags'] || [] + @tags = to_utf8(hash[:tags] || hash['tags'] || []) notebook = @evernote.notebook @notebook = notebook.get(notebook_name, stack_name) @hash = hash sync end @@ -54,42 +55,42 @@ private # write log to note def create_log(content) @note.clear - @note.title = @name + " - " + Time.now.strftime("%Y-%m-%d %H:%M:%S") + @note.title = to_utf8(@name) + " - " + Time.now.strftime("%Y-%m-%d %H:%M:%S") @note.tags = @tag.get - @note.content = content + @note.content = to_utf8(content) @note.create Logger.log_internal { "Create note: #{@note.guid}" } end - + # update log in note def update_log(content) - @note.addContent(content) + @note.addContent(to_utf8(content)) @note.tags = @tag.get @note.update Logger.log_internal { "Update note: #{@note.guid}" } end - + # more expensive, only for startup def note_size_requires_roll? @note.size == 0 || (@maxsize > 0 && @note.size >= @maxsize) end # whether or not to rotate def time_requires_roll? !@endTime.nil? && Time.now.to_i >= @endTime end - # diff note's tag and register tag + # diff note's tag and register tag def different_tag? note_tags = @note.tags || [] tag = @tag.get || [] (note_tags - tag).size != 0 || (tag - note_tags).size != 0 end - + # max amount of log in note def set_maxsize(options) if options.has_key?(:maxsize) || options.has_key?('maxsize') maxsize = options[:maxsize] || options['maxsize'] @@ -113,11 +114,11 @@ @maxsize = _maxsize else @maxsize = 0 end end - + # rolling interval def set_shift_age(options) if options.has_key?(:shift_age) || options.has_key?('shift_age') _shift_age = (options[:shift_age] or options['shift_age']).to_i if _shift_age.class != Fixnum @@ -142,8 +143,20 @@ else raise TypeError, "Argument 'shift_age' must be '1' or '2' or '3'", caller end end end + end + + # encode for evernote internal charset + # convert character encoding to UTF-8 from Shift_JIS or EUC-JP + def to_utf8(mixed) + if mixed.kind_of? Array + mixed.each {|elem| to_utf8(elem)} + else mixed.kind_of? String + charset = NKF.guess(mixed).name + charset == "UTF-8" ? mixed : mixed.encode!("UTF-8", charset) + end + mixed end end end \ No newline at end of file