Skip to content

Commit 8282a29

Browse files
authored
Getting rid of spurious logging messages from HTTP (#170)
1 parent 09a29e7 commit 8282a29

File tree

3 files changed

+76
-63
lines changed

3 files changed

+76
-63
lines changed

Project.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
name = "LiveServer"
22
uuid = "16fef848-5104-11e9-1b77-fb7a48bbb589"
3-
authors = [
4-
"Jonas Asprion <jonas.asprion@gmx.ch",
5-
"Thibaut Lienart <tlienart@me.com>"
6-
]
7-
version = "1.2.2"
3+
authors = ["Jonas Asprion <jonas.asprion@gmx.ch", "Thibaut Lienart <tlienart@me.com>"]
4+
version = "1.2.3"
85

96
[deps]
107
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
118
MIMEs = "6c6e2e6c-3030-632d-7369-2d6c69616d65"
129
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1310
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
11+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1412

1513
[compat]
1614
HTTP = "1"

src/LiveServer.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module LiveServer
22

33
import Sockets, Pkg, MIMEs
44
using Base.Filesystem
5+
using Test: TestLogger
56

67
using HTTP
78

src/server.jl

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -606,72 +606,86 @@ current directory. (See also [`example`](@ref) for an example folder).
606606
# starts the file watcher
607607
start(fw)
608608

609-
# make request handler
610-
req_handler = HTTP.Handlers.streamhandler() do req
611-
req = preprocess_request(req)
612-
serve_file(
613-
fw, req;
614-
inject_browser_reload_script = inject_browser_reload_script,
615-
allow_cors = allow_cors
609+
# HTTP uses LoggingExtras and, in particular, a @logmsgv which is very
610+
# annoying for LiveServer, see https://github.com/JuliaWeb/HTTP.jl/issues/938
611+
# as a result we just capture all the logging and discard everything
612+
Base.CoreLogging.with_logger(TestLogger()) do
613+
614+
# make request handler
615+
req_handler = HTTP.Handlers.streamhandler() do req
616+
req = preprocess_request(req)
617+
serve_file(
618+
fw, req;
619+
inject_browser_reload_script = inject_browser_reload_script,
620+
allow_cors = allow_cors
621+
)
622+
end
623+
624+
server, port = get_server(host, port, req_handler)
625+
host_str = ifelse(host == string(Sockets.localhost), "localhost", host)
626+
url = "http://$host_str:$port"
627+
println(
628+
"✓ LiveServer listening on $url/ ...\n (use CTRL+C to shut down)"
616629
)
617-
end
618630

619-
server, port = get_server(host, port, req_handler)
620-
host_str = ifelse(host == string(Sockets.localhost), "localhost", host)
621-
url = "http://$host_str:$port"
622-
println(
623-
"✓ LiveServer listening on $url/ ...\n (use CTRL+C to shut down)"
624-
)
625-
626-
launch_browser && open_in_default_browser(url)
627-
# wait until user interrupts the LiveServer (using CTRL+C).
628-
try
629-
counter = 1
630-
while true
631-
if WS_INTERRUPT[] || fw.status == :interrupted
632-
# rethrow the interruption (which may have happened during
633-
# the websocket handling or during the file watching)
634-
throw(InterruptException())
631+
launch_browser && open_in_default_browser(url)
632+
# wait until user interrupts the LiveServer (using CTRL+C).
633+
try
634+
counter = 1
635+
while true
636+
if WS_INTERRUPT[] || fw.status == :interrupted
637+
# rethrow the interruption (which may have happened during
638+
# the websocket handling or during the file watching)
639+
throw(InterruptException())
640+
end
641+
642+
sleep(2)
643+
try
644+
sqrt(-1)
645+
catch e
646+
HTTP.LoggingExtras.@logmsgv 1 HTTP.Logging.Error "I don't want to see this" exception=(e, stacktrace(catch_backtrace()))
647+
end
648+
649+
# run the auxiliary function if there is one (by default this does
650+
# nothing)
651+
coreloopfun(counter, fw)
652+
# update the cycle counter and sleep (yields to other threads)
653+
counter += 1
654+
sleep(0.1)
635655
end
636-
# run the auxiliary function if there is one (by default this does
637-
# nothing)
638-
coreloopfun(counter, fw)
639-
# update the cycle counter and sleep (yields to other threads)
640-
counter += 1
641-
sleep(0.1)
642-
end
643-
catch err
644-
if !isa(err, InterruptException)
645-
if VERBOSE[]
646-
@error "serve error" exception=(err, catch_backtrace())
656+
catch err
657+
if !isa(err, InterruptException)
658+
if VERBOSE[]
659+
@error "serve error" exception=(err, catch_backtrace())
660+
end
661+
throw(err)
647662
end
648-
throw(err)
649-
end
650-
finally
651-
# cleanup: close everything that might still be alive
652-
print("\n⋮ shutting down LiveServer… ")
653-
# stop the filewatcher
654-
stop(fw)
655-
# close any remaining websockets
656-
for wss values(WS_VIEWERS)
657-
@sync for wsi in wss
658-
isopen(wsi.io) && @async begin
659-
try
660-
wsi.writeclosed = wsi.readclosed = true
661-
close(wsi.io)
662-
catch
663+
finally
664+
# cleanup: close everything that might still be alive
665+
print("\n⋮ shutting down LiveServer… ")
666+
# stop the filewatcher
667+
stop(fw)
668+
# close any remaining websockets
669+
for wss values(WS_VIEWERS)
670+
@sync for wsi in wss
671+
isopen(wsi.io) && @async begin
672+
try
673+
wsi.writeclosed = wsi.readclosed = true
674+
close(wsi.io)
675+
catch
676+
end
663677
end
664678
end
665679
end
680+
# empty the dictionary of viewers
681+
empty!(WS_VIEWERS)
682+
# shut down the server
683+
HTTP.Servers.forceclose(server)
684+
# reset other environment variables
685+
reset_content_dir()
686+
reset_ws_interrupt()
687+
println("")
666688
end
667-
# empty the dictionary of viewers
668-
empty!(WS_VIEWERS)
669-
# shut down the server
670-
HTTP.Servers.forceclose(server)
671-
# reset other environment variables
672-
reset_content_dir()
673-
reset_ws_interrupt()
674-
println("")
675689
end
676690
return nothing
677691
end

0 commit comments

Comments
 (0)