Skip to content

Commit

Permalink
(maint) Stop invalid debug logs from crashing services
Browse files Browse the repository at this point in the history
Previously if an invalid logfile location was specified e.g. C:\ or a missing
parent directory, the service (Language or Debug Server) would error.  This is
undesired as a bad log file should not cause a catastrophic failure.  This
commit modifies the logging so that an error occurs when the log file is
created it, the error is sent to STDERR, and service continues but with logging
disabled.

This commit also flushes the log file on each write if needed.  This is required
as OS buffering makes real time debugging very difficult.
  • Loading branch information
glennsarti committed Apr 19, 2018
1 parent f0f33c1 commit a601208
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

- ([GH-11](https://github.com/lingua-pupuli/puppet-editor-services/issues/11)) Refactor the transport layers to loosen object coupling
- ([GH-11](https://github.com/lingua-pupuli/puppet-editor-services/issues/11)) Fix STDIO server
- Stop bad logfile destinations from crashing the language and debug servers

## 0.10.0 - 2018-03-29

Expand Down
14 changes: 13 additions & 1 deletion lib/puppet-editor-services/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@ def self.log_message(severity, message)
else
@logger.unknown(message)
end
@log_file.fsync unless @log_file.nil?
end

def self.init_logging(options)
@log_file = nil
if options[:debug].nil?
@logger = nil
elsif (options[:debug].casecmp 'stdout').zero?
@logger = Logger.new($stdout)
elsif !options[:debug].to_s.empty?
# Log to file
@logger = Logger.new(options[:debug])
begin
@log_file = File.open(options[:debug], 'w')
rescue Errno::ENOENT => e
# We can't open the log file and we can't log to STDOUT if we're in STDIO mode
# So log the error to STDERR and disable logging
$stderr.puts "Error opening log file #{options[:debug]} : #{e}" # rubocop:disable Style/StderrPuts
@log_file = nil
return
end
@log_file.sync = true
@logger = Logger.new(@log_file)
end
end
end

0 comments on commit a601208

Please sign in to comment.