1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
23 # Initialize a clock for the resquest.
25 # Can be used to compute the time passed to respond that request.
29 redef fun all
(req
, res
) do req
.clock
= new Clock
32 # Display log info about request processing.
44 # Request status are always logged, whatever the logger level is.
45 var level
= 4 is writable
47 # Do we want colors in the console output?
50 redef fun all
(req
, res
) do
53 log
"{req.method} {req.url} {status(res)} ({clock.total}s)"
55 log
"{req.method} {req.url} {status(res)}"
59 # Colorize the request status.
60 private fun status
(res
: HttpResponse): String do
61 if no_colors
then return res
.status_code
.to_s
62 return res
.color_status
65 # Display a `message` with `level`.
67 # Message will only be displayed if `level <= self.level`.
68 # Colors will be used depending on `colors`.
70 # Use `0` for no coloration.
71 private fun display
(level
: Int, message
: String) do
72 if level
> self.level
then return
77 if level
== 0 then print message
78 if level
== 1 then print message
.red
79 if level
== 2 then print message
.yellow
80 if level
== 3 then print message
.blue
81 if level
== 4 then print message
.gray
84 # Display a message wathever the `level`
85 fun log
(message
: String) do display
(0, message
)
87 # Display a red error `message`.
88 fun error
(message
: String) do display
(1, "[ERROR] {message}")
90 # Display a yellow warning `message`.
91 fun warning
(message
: String) do display
(2, "[WARN] {message}")
93 # Display a blue info `message`.
94 fun info
(message
: String) do display
(3, "[INFO] {message}")
96 # Display a gray debug `message`.
97 fun debug
(message
: String) do display
(4, "[DEBUG] {message}")
100 redef class HttpRequest
101 # Time that request was received by the Popcorn app.
102 var clock
: nullable Clock = null
105 redef class HttpResponse
106 # Return `self` status colored for console.
107 fun color_status
: String do
108 if status_code
>= 100 and status_code
< 200 then return status_code
.to_s
.gray
109 if status_code
>= 200 and status_code
< 300 then return status_code
.to_s
.green
110 if status_code
>= 300 and status_code
< 400 then return status_code
.to_s
.blue
111 if status_code
>= 400 and status_code
< 500 then return status_code
.to_s
.yellow
112 if status_code
>= 500 and status_code
< 600 then return status_code
.to_s
.red
113 return status_code
.to_s