pygments / pygments.rb

💎 Ruby wrapper for Pygments syntax highlighter
MIT License
572 stars 141 forks source link

Instrument Pygments.start #109

Closed asenchi closed 10 years ago

asenchi commented 10 years ago

Add an options hash to start and allow a logger to be specified by passing :logger through the options hash.

For example:

#!/usr/bin/env ruby

require 'pygments'
require 'scrolls'

code = "#!/usr/bin/ruby\nputs 'foo'"

path = "/Users/asenchi/github/pygments.rb/vendor/pygments-main"
opts = {:logger => Scrolls}
Pygments.start(path, opts)
Pygments.highlight(code)
asenchi commented 10 years ago

I found a bug in Scrolls, so this won't exactly work as advertised in the example code. But everything falls back to Logger, so this PR should have no effects on existing behavior.

asenchi commented 10 years ago

Scrolls shipped v0.3.8 that fixes the Logger compatibility bug, so the above would output this now:

github/pygments.rb instrument-logger! : ruby ./test.rb
log_message="[2014-02-11T11:34:42-05:00] Starting pid 95864 with fd 7."
log_message='[2014-02-11T11:34:42-05:00] Out header: {"method":"highlight","args":null,"kwargs":{"options":{"outencoding":"utf-8"},"fd":7,"id":"XUMSFDPI","bytes":46}}'
log_message="[2014-02-11T11:34:42-05:00] Size in: 00000000000000000000000000100101 (38)"
log_message='[2014-02-11T11:34:42-05:00] In header: {"bytes": 169, "method": "highlight"}
 '
log_message="[2014-02-11T11:34:42-05:00] Highlight in process."
log_message="[2014-02-11T11:34:42-05:00] Highlighting complete."
log_message="[2014-02-11T11:34:42-05:00] Killing pid: 95864. Reason: Exiting"
asenchi commented 10 years ago

Just as an added documentation piece, here is the standard behavior:

require 'pygments'

ENV['MENTOS_LOG'] = "test.log"

code = "#!/usr/bin/ruby\nputs 'foo'"
output = Pygments.highlight(code)
puts output
# Logfile created on 2014-02-11 12:30:35 -0500 by logger.rb/44203
I, [2014-02-11 12:30 #11147]  INFO -- : [2014-02-11T12:30:35-05:00] Starting pid 11193 with fd 10.
I, [2014-02-11 12:30 #11147]  INFO -- : [2014-02-11T12:30:35-05:00] Out header: {"method":"highlight","args":null,"kwargs":{"options":{"outencoding":"utf-8"},"fd":10,"id":"MMHIKUET","bytes":46}}
I, [2014-02-11 12:30 #11147]  INFO -- : [2014-02-11T12:30:35-05:00] Size in: 00000000000000000000000000100101 (38)
I, [2014-02-11 12:30 #11147]  INFO -- : [2014-02-11T12:30:35-05:00] In header: {"bytes": 169, "method": "highlight"}

I, [2014-02-11 12:30 #11147]  INFO -- : [2014-02-11T12:30:35-05:00] Highlight in process.
I, [2014-02-11 12:30 #11147]  INFO -- : [2014-02-11T12:30:35-05:00] Highlighting complete.
I, [2014-02-11 12:30 #11147]  INFO -- : [2014-02-11T12:30:35-05:00] Killing pid: 11193. Reason: Exiting
asenchi commented 10 years ago

Closing this as "not needed"