Skip to content

Commit

Permalink
logging elapsed time and which file changed
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuellr committed Jan 17, 2012
1 parent 4443442 commit 5212d5c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
30 changes: 24 additions & 6 deletions lib/Executor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ module.exports = class Executor
resetAfterCommand: () ->
@fileSet.resetAfterCommand()

#---------------------------------------------------------------------------
timerStart: () ->
@startTime = (new Date()).valueOf()

#---------------------------------------------------------------------------
timerElapsed: () ->
ms = (new Date()).valueOf() - @startTime
ms = Math.round(ms / 100)
ms / 10

#---------------------------------------------------------------------------
logSuccess: (message) ->
@opts.logSuccess message
Expand All @@ -60,6 +70,8 @@ class ExecutorExec extends Executor

#---------------------------------------------------------------------------
run: (cmd) ->
@timerStart()

callback = (error, stdout, stderr) => @done(error, stdout, stderr)

childProcess.exec(cmd, callback)
Expand All @@ -85,10 +97,12 @@ class ExecutorExec extends Executor
.write(stderr)
.pop(true)

secs = @timerElapsed()

if error
@logError "command failed with rc:#{error.code}"
@logError "#{secs}s - command failed with rc:#{error.code}"
else
@logSuccess "command succeeded"
@logSuccess "#{secs}s - command succeeded"

@resetAfterCommand()

Expand All @@ -101,6 +115,8 @@ class ExecutorSpawn extends Executor

#---------------------------------------------------------------------------
run: (cmd) ->
@timerStart()

[ cmd, args ] = @splitCmd(cmd)

proc = childProcess.spawn(cmd, args)
Expand Down Expand Up @@ -137,14 +153,16 @@ class ExecutorSpawn extends Executor

#---------------------------------------------------------------------------
exit: (code, sig) ->
secs = @timerElapsed()

if code == 0
@logSuccess "command succeeded"
@logSuccess "#{secs}s - command succeeded"
else if code
@logError "command failed with rc:#{code}"
@logError "#{secs}s - command failed with rc:#{code}"
else if sig
@logError "command failed with signal:#{sig}"
@logError "#{secs}s - command failed with signal:#{sig}"
else
@logError "command failed for some unknown reason"
@logError "#{secs}s - command failed for some unknown reason"

@resetAfterCommand()

Expand Down
16 changes: 9 additions & 7 deletions lib/FileWatcher.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ module.exports = class FileWatcher
constructor: (@fileSet, @opts) ->

#---------------------------------------------------------------------------
fileChanged: () ->
fileChanged: (fileName) ->
@fileSet.logInfo "file changed: #{fileName}" if fileName
@stopWatching()
@fileSet.fileChanged()

#---------------------------------------------------------------------------
getCB: (fileName) ->
watcher = @
-> watcher.fileChanged(fileName)

#-------------------------------------------------------------------------------
class FileWatcherNoPoll extends FileWatcher

Expand All @@ -43,12 +49,10 @@ class FileWatcherNoPoll extends FileWatcher

#---------------------------------------------------------------------------
watch: (files) ->
fileChanged = => @fileChanged()

@watchers = []
for file in files
try
watcher = fs.watch(file, {persist: true}, fileChanged)
watcher = fs.watch(file, {persist: true}, @getCB(file))
@watchers.push(watcher)
catch e
@fileSet.logError "exception watching '#{file}': #{e}"
Expand All @@ -69,15 +73,13 @@ class FileWatcherPoll extends FileWatcher

#---------------------------------------------------------------------------
watch: (@files) ->
fileChanged = => @fileChanged()

options =
interval: 1000 * @opts.poll
persistent: true

for file in @files
try
fs.watchFile(file, options, fileChanged)
fs.watchFile(file, options, @getCB(file))
catch e
@fileSet.logError "exception watching '#{file}': #{e}"

Expand Down

1 comment on commit 5212d5c

@pmuellr
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are fixes for #8, BTW

Please sign in to comment.