Class: ConfCtl::Logger
- Inherits:
-
Object
- Object
- ConfCtl::Logger
- Includes:
- Singleton
- Defined in:
- lib/confctl/logger.rb
Instance Method Summary collapse
- #<<(str) ⇒ Object
- #add_reader(obj) ⇒ Object
- #cli(cmd, gopts, opts, args) ⇒ Object
- #close ⇒ Object
- #close_and_unlink ⇒ Object
-
#initialize ⇒ Logger
constructor
A new instance of Logger.
- #io ⇒ Object
- #keep ⇒ Object
- #keep? ⇒ Boolean
- #open(name, output: nil) ⇒ Object
- #open? ⇒ Boolean
- #path ⇒ Object
- #relative_path ⇒ Object
- #remove_reader(obj) ⇒ Object
- #unlink ⇒ Object
- #write(str) ⇒ Object
Constructor Details
#initialize ⇒ Logger
Returns a new instance of Logger.
26 27 28 29 30 |
# File 'lib/confctl/logger.rb', line 26 def initialize @mutex = Mutex.new @readers = [] @keep = false end |
Instance Method Details
#<<(str) ⇒ Object
101 102 103 |
# File 'lib/confctl/logger.rb', line 101 def <<(str) write(str) end |
#add_reader(obj) ⇒ Object
117 118 119 |
# File 'lib/confctl/logger.rb', line 117 def add_reader(obj) @readers << obj end |
#cli(cmd, gopts, opts, args) ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/confctl/logger.rb', line 105 def cli(cmd, gopts, opts, args) sync do PP.pp({ command: cmd, global_options: prune_opts(gopts.clone), command_options: prune_opts(opts.clone), arguments: args }, @io) end end |
#close ⇒ Object
47 48 49 50 51 |
# File 'lib/confctl/logger.rb', line 47 def close raise 'log file not open' if @io.nil? @io.close end |
#close_and_unlink ⇒ Object
87 88 89 90 |
# File 'lib/confctl/logger.rb', line 87 def close_and_unlink close unlink end |
#io ⇒ Object
53 54 55 56 57 |
# File 'lib/confctl/logger.rb', line 53 def io raise 'log file not open' if @io.nil? @io end |
#keep ⇒ Object
77 78 79 |
# File 'lib/confctl/logger.rb', line 77 def keep @keep = true end |
#keep? ⇒ Boolean
73 74 75 |
# File 'lib/confctl/logger.rb', line 73 def keep? @keep end |
#open(name, output: nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/confctl/logger.rb', line 32 def open(name, output: nil) if output @io = output else dir = ConfDir.log_dir FileUtils.mkdir_p(dir) @io = File.new(File.join(dir, file_name(name)), 'w') end end |
#open? ⇒ Boolean
43 44 45 |
# File 'lib/confctl/logger.rb', line 43 def open? !@io.nil? end |
#path ⇒ Object
59 60 61 62 63 |
# File 'lib/confctl/logger.rb', line 59 def path raise 'log file not open' if @io.nil? @io.path end |
#relative_path ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/confctl/logger.rb', line 65 def relative_path return @relative_path if @relative_path abs = Pathname.new(path) dir = Pathname.new(ConfDir.path) abs.relative_path_from(dir).to_s end |
#remove_reader(obj) ⇒ Object
122 123 124 |
# File 'lib/confctl/logger.rb', line 122 def remove_reader(obj) @readers.delete(obj) end |
#unlink ⇒ Object
81 82 83 84 85 |
# File 'lib/confctl/logger.rb', line 81 def unlink raise 'log file not open' if @io.nil? File.unlink(@io.path) end |
#write(str) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/confctl/logger.rb', line 92 def write(str) sync do @io << str @io.flush end readers.each { |r| r << str } end |