Permalink
Browse files

--verbose logs to file if --json is true

  • Loading branch information...
brendanjerwin committed Aug 31, 2010
1 parent 3705136 commit 1c6d3e0e98dd5c33e9b239eb2015affb6fd56862
Showing with 19 additions and 10 deletions.
  1. +2 −1 .gitignore
  2. +0 −3 lib/nu/api.rb
  3. +17 −6 lib/nu/app.rb
View
@@ -28,4 +28,5 @@ spec_lib/*
.nu/*
nu_spec.yaml
meta_data.yaml
-nuniverse.yaml
+nuniverse.yaml
+*.log
View
@@ -43,16 +43,13 @@ def self.load_project_settings(settings_file)
def self.store_setting(name, value)
log "Store Setting called: name=#{name} value=#{value}"
- log "Before:"
- load_project_settings(@settings_file) if @verbose
@project_settings.set_setting_by_path(name, value, @log)
assert_project_settings
File.open(@settings_file, 'w') do |out|
YAML.dump(@project_settings, out)
end
- log "After:"
load_project_settings(@settings_file)
end
View
@@ -3,6 +3,7 @@
require 'optparse'
require 'ostruct'
require 'date'
+require 'logger'
require File.expand_path(File.dirname(__FILE__) + "/api.rb")
require File.expand_path(File.dirname(__FILE__) + "/loader.rb")
require File.expand_path(File.dirname(__FILE__) + "/cli_shim.rb")
@@ -60,7 +61,10 @@ def initialize(arguments, stdin, stdout)
opts.on('-q', '--quiet') { @options.quiet = true }
opts.on('--json', 'Run in JSON mode. All outputs will be in JSON, status messages silenced.') do
@options.json = true
- @shim = JsonShim.new(lambda {|json| puts json}, lambda {|msg| log msg})
+ @shim = JsonShim.new(lambda do |json|
+ puts json
+ log_to_file(json)
+ end, lambda {|msg| log msg})
end
opts.on_tail( '-h', '--help', 'Display this screen' ) do
@@ -82,15 +86,15 @@ def run
if arguments_valid?
- disp "Start at #{DateTime.now}\n\n" if @options.verbose
+ log "Start at #{DateTime.now}\n\n"
output_version if @options.verbose
output_inputs if @options.verbose
Nu::Api.load_project_settings('nuniverse.yaml')
@commands.reverse.each {|command| command.call}
- disp "\nFinished at #{DateTime.now}" if @options.verbose
+ log "\nFinished at #{DateTime.now}"
else
@help_command.call
@@ -135,7 +139,6 @@ def assert_param_count(count)
end
def post_process_options
- @options.quiet = true if @options.json
@options.verbose = false if !@options.verbose
@options.verbose = false if @options.quiet
end
@@ -175,10 +178,18 @@ def output_description
end
def disp(msg)
- puts msg unless @options.quiet
+ puts msg unless @options.quiet || @options.json
end
def log(msg)
- puts msg if @options.verbose
+ log_to_file(msg) if @options.json
+ puts msg unless @options.json
+ end
+
+ def log_to_file(msg)
+ if @options.verbose
+ @file_logger ||= Logger.new('nu.log', 5, 1024000)
+ @file_logger.debug("#{DateTime.now} - #{msg.strip}")
+ end
end
end

0 comments on commit 1c6d3e0

Please sign in to comment.