Skip to content

Commit

Permalink
chore(debug): Make the debug logging slightly less annoying by using …
Browse files Browse the repository at this point in the history
…a boilerplate fn
  • Loading branch information
Brandon Fryslie committed Jun 24, 2016
1 parent 5593886 commit 5899183
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ You can define CLI options in each individual task file as well.

### Debugging

Stacker allows you to enable debugging on a per-file basis. Passing `--debug` on the command line will enable debuggin in all files.
Stacker allows you to enable debugging on a per-file basis. Passing `--debug` on the command line will enable debugging in all files.
Otherwise, pass the filename (without extension) to the debug command line argument.

e.g. `--debug task_config`
Expand Down
7 changes: 4 additions & 3 deletions lib/config_lib.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
_ = require 'lodash'
util = require './util'
exported_util = require './exported_util'
util = require './util'
_log = (args...) -> util.debug_log.apply null, [__filename].concat args

config_dir = process.env.STACKER_CONFIG_DIR ? "#{process.env.HOME}/.stacker"
config_file = "#{config_dir}/config.coffee"
Expand All @@ -13,10 +14,10 @@ exports =
get_config: _.memoize ->
config = {}
try
util._log __filename, "requiring config file #{config_file}"
_log "requiring config file #{config_file}"
config = require config_file
catch e
util._log __filename, e
_log e
config?(exported_util) ? config

module.exports[k] = v for k, v of exports
3 changes: 2 additions & 1 deletion lib/repl_commands.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ repl_lib = require './repl_lib'
state_lib = require './state_lib'
task_config_lib = require './task_config'
task_lib = require './task_lib'
_log = (args...) -> util.debug_log.apply null, [__filename].concat args

invalid_command_invocation = (cmd) ->
util.log_error "usage: #{cmd.usage}"
Expand Down Expand Up @@ -119,7 +120,7 @@ tell_target = (target, cmd) ->
"#{process.env.HOME}/projects/#{target}"
catch e
unless e.code is 'ENOENT' # handle missing directory below
util._log __filename, e.stack
_log e.stack
throw e

unless path?
Expand Down
5 changes: 3 additions & 2 deletions lib/run_cmd.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mexpect = require './mexpect'
util = require './util'
state_lib = require './state_lib'
proc_lib = require './proc_lib'
_log = (args...) -> util.debug_log.apply null, [__filename].concat args

module.exports.run_cmd =
# Run a command
Expand All @@ -18,7 +19,7 @@ module.exports.run_cmd =
unless fs.statSync(cwd).isDirectory()
throw new Error missing_dir_error
catch e
util._log __filename, e.stack
_log e.stack
throw new Error missing_dir_error

silent ?= false
Expand Down Expand Up @@ -52,7 +53,7 @@ module.exports.run_cmd =
util.kill_tree mproc.proc.pid
.catch (error) ->
console.log error
util._log __filename, error.stack
_log error.stack

proc_lib.add_proc child_id, mproc.proc

Expand Down
3 changes: 2 additions & 1 deletion lib/stacker_lib.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repl_commands = require './repl_commands'
state_lib = require './state_lib'
task_config_lib = require './task_config'
task_lib = require './task_lib'
_log = (args...) -> util.debug_log.apply null, [__filename].concat args

################################################################################
# boot stack
Expand All @@ -29,7 +30,7 @@ check_config = ->
catch

catch e
util._log __filename, e
_log e
util.print 'No config found. Using:'.yellow, config_dir.cyan

boot_stack = ->
Expand Down
3 changes: 2 additions & 1 deletion lib/task_config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ util = require './util'
config = require './config_lib'
state_lib = require './state_lib'
exported_util = require './exported_util'
_log = (args...) -> util.debug_log.apply null, [__filename].concat args

################################################################################
# Configuration of task configs and aliass
Expand All @@ -19,7 +20,7 @@ require_task_config = _.memoize ->

try
for file in fs.readdirSync(task_dir, ->) when fs.statSync("#{task_dir}/#{file}").isFile()
util._log __filename, "requiring task file #{task_dir}/#{file}"
_log "requiring task file #{task_dir}/#{file}"
task_config[file.replace(/\.coffee$/, '')] = require "#{task_dir}/#{file}"
catch e

Expand Down
7 changes: 4 additions & 3 deletions lib/task_lib.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ proc_lib = require './proc_lib'
{run_cmd} = require './run_cmd'
state_lib = require './state_lib'
task_config_lib = require './task_config'
_log = (args...) -> util.debug_log.apply null, [__filename].concat args

################################################################################
# Run Tasks
Expand Down Expand Up @@ -122,7 +123,7 @@ start_foreground_task = (task_name, task_config, callback) ->
return new_state
catch e
util.print "Failed to start #{task_config.name}!".bold
util._log __filename, e.stack
_log e.stack
return state_lib.get_stacker_state()


Expand Down Expand Up @@ -262,11 +263,11 @@ kill_foreground_task = (task_name, proc) ->
proc.on 'close', ->
resolve()

util.kill_tree proc.pid
util.print "Killing #{task_name.red}..."
util.kill_tree proc.pid
.catch (error) ->
util.print 'Error killing'.red, task_name.cyan
util._log __filename, error.stack
_log error.stack

# Str -> Promise
kill_task = (task_name) ->
Expand Down
8 changes: 6 additions & 2 deletions lib/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ get_debug = ->
set_debug = (areas) ->
DEBUG = areas

_log = (area, args...) ->
debug_log = (area, args...) ->
area = path.basename(area).replace(/\.\w+$/, '')
if DEBUG is true or _.includes DEBUG, area
process.stdout.write "DEBUG #{area}:".bgRed.black + ' '
console.log.apply console, args

# Boilerplate fn to prevent needing to always pass __filename
# I'll keep looking for a better solution
_log = (args...) -> debug_log.apply null, [__filename].concat args

log_proc_error = (err) ->
msg = switch err.code
when 'ENOENT' then 'File not found'
Expand Down Expand Up @@ -229,7 +233,7 @@ export TERM=xterm-256color'''
module.exports = {
get_debug: -> DEBUG
set_debug
_log
debug_log
error
log_error
log_proc_error
Expand Down

0 comments on commit 5899183

Please sign in to comment.