Add -o option for redirecting output

pull/314/head^2
Omar Roth 6 years ago
parent 827e68acf5
commit af449161ff

@ -16,6 +16,7 @@
require "detect_language" require "detect_language"
require "digest/md5" require "digest/md5"
require "file_utils"
require "kemal" require "kemal"
require "openssl/hmac" require "openssl/hmac"
require "option_parser" require "option_parser"
@ -35,6 +36,8 @@ channel_threads = CONFIG.channel_threads
feed_threads = CONFIG.feed_threads feed_threads = CONFIG.feed_threads
video_threads = CONFIG.video_threads video_threads = CONFIG.video_threads
logger = Invidious::LogHandler.new
Kemal.config.extra_options do |parser| Kemal.config.extra_options do |parser|
parser.banner = "Usage: invidious [arguments]" parser.banner = "Usage: invidious [arguments]"
parser.on("-t THREADS", "--crawl-threads=THREADS", "Number of threads for crawling YouTube (default: #{crawl_threads})") do |number| parser.on("-t THREADS", "--crawl-threads=THREADS", "Number of threads for crawling YouTube (default: #{crawl_threads})") do |number|
@ -69,6 +72,10 @@ Kemal.config.extra_options do |parser|
exit exit
end end
end end
parser.on("-o OUTPUT", "--output=OUTPUT", "Redirect output (default: STDOUT)") do |output|
FileUtils.mkdir_p(File.dirname(output))
logger = Invidious::LogHandler.new(File.open(output, mode: "a"))
end
end end
Kemal::CLI.new Kemal::CLI.new
@ -295,7 +302,7 @@ get "/watch" do |env|
next env.redirect "/watch?v=#{ex.message}" next env.redirect "/watch?v=#{ex.message}"
rescue ex rescue ex
error_message = ex.message error_message = ex.message
STDOUT << id << " : " << ex.message << "\n" logger.write("#{id} : #{ex.message}\n")
next templated "error" next templated "error"
end end
@ -3849,4 +3856,5 @@ add_handler FilteredCompressHandler.new
add_handler DenyFrame.new add_handler DenyFrame.new
add_context_storage_type(User) add_context_storage_type(User)
Kemal.config.logger = logger
Kemal.run Kemal.run

@ -0,0 +1,35 @@
require "logger"
class Invidious::LogHandler < Kemal::BaseLogHandler
def initialize(@io : IO = STDOUT)
end
def call(context : HTTP::Server::Context)
time = Time.now
call_next(context)
elapsed_text = elapsed_text(Time.now - time)
@io << time << ' ' << context.response.status_code << ' ' << context.request.method << ' ' << context.request.resource << ' ' << elapsed_text << '\n'
if @io.is_a? File
@io.flush
end
context
end
def write(message : String)
@io << message
if @io.is_a? File
@io.flush
end
end
private def elapsed_text(elapsed)
millis = elapsed.total_milliseconds
return "#{millis.round(2)}ms" if millis >= 1
"#{(millis * 1000).round(2)}µs"
end
end
Loading…
Cancel
Save