Module: Ki::HashLog
- Defined in:
- lib/util/hash_log.rb
Overview
Generate hash object based log entries.
Constant Summary collapse
- HashLogThreadCurrentKey =
:hash_log
- HashLogMutex =
Mutex.new
Instance Method Summary collapse
- #hash_log_current ⇒ Object
-
#log(*args, &block) ⇒ Object
Create a hash log entry or return current hash log entry * Supports hierarchic logs, where log entry can contain any number of sub entries * Supports parallel execution, threads can log in to one log structure * Logs start and stop time of execution.
- #set_hash_log_root_for_thread(root) ⇒ Object
Instance Method Details
#hash_log_current ⇒ Object
87 88 89 90 91 |
# File 'lib/util/hash_log.rb', line 87 def hash_log_current HashLogMutex.synchronize do (Thread.current[HashLogThreadCurrentKey] ||= []).last end end |
#log(*args, &block) ⇒ Object
Create a hash log entry or return current hash log entry
-
Supports hierarchic logs, where log entry can contain any number of sub entries
-
Supports parallel execution, threads can log in to one log structure
-
Logs start and stop time of execution
Hierarchic logging works by keeping a stack of log entries in Thread local store
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 |
# File 'lib/util/hash_log.rb', line 33 def log(*args, &block) current_log_entry = hash_log_current # return if args.empty? && block.nil? current_log_entry else new_entry = {"start" => Time.now} if args.first.kind_of?(String) new_entry["name"] = args.delete_at(0) end if args.first.kind_of?(Hash) new_entry.merge!(args.first) end log_list = nil if current_log_entry # there is current log entry, create a new sub log entry HashLogMutex.synchronize do log_list = current_log_entry["logs"] ||= [] end else # append new_entry to end of log list log_list = Thread.current[HashLogThreadCurrentKey] end HashLogMutex.synchronize do log_list << new_entry end if block HashLogMutex.synchronize do Thread.current[HashLogThreadCurrentKey] << new_entry end begin block.call new_entry rescue Exception => e new_entry["exception"] = e. new_entry["backtrace"] = e.backtrace.join("\n") raise ensure HashLogMutex.synchronize do Thread.current[HashLogThreadCurrentKey].delete(new_entry) end new_entry["end"] = Time.now end else new_entry end end end |
#set_hash_log_root_for_thread(root) ⇒ Object
81 82 83 84 85 |
# File 'lib/util/hash_log.rb', line 81 def set_hash_log_root_for_thread(root) HashLogMutex.synchronize do (Thread.current[HashLogThreadCurrentKey] ||= []) << root end end |