Hello,
so, I did something very simple and silly, but I’m happy about it since it’s been useful when debugging tests: it’s a module that allows to print colored logging on the terminal (warning, it uses RGB ANSI escapes).
What is nice about it is simply that it is basically Debug.log
, but also takes a “function name” as first argument, computes the hash of the function and prints it before the message… And the name is uniquely colored, so in the logs you’ll be able to quickly discern which rows belong to the same function.
You’d use it like Debug.log
, with an additional function name as a first argument:
myFunction =
clog "myFunction" "some message" returnValue
Here’s a screenshot to give you the idea:
Here’s the code, free to use as you like
module Logging exposing (..)
-- Debug log, in a separate function so it's easy to turn this off
color : Int -> Int -> Int -> String
color r g b =
String.join "" [ "\u{001B}[38;2;", String.fromInt r, ";", String.fromInt g, ";", String.fromInt b, "m" ]
-- Stupid 24-bit hash of a string
hash24 : String -> Int
hash24 s =
let
-- Seed
seed =
7927
-- Modulo, 24 bit
mod =
256 * 256 * 256
hashFunc : Char -> Int -> Int
hashFunc ch hashAcc =
modBy mod <| Char.toCode ch + (hashAcc * seed)
in
s
|> String.toList
|> List.foldl hashFunc 131
u24tot3 : Int -> ( Int, Int, Int )
u24tot3 n =
( modBy 256 (n // 65536), modBy 256 (n // 256), modBy 256 n )
-- Colored logging: the function is hashed and colored uniquely
clog : String -> String -> x -> x
clog func msg val =
let
( r, g, b ) =
u24tot3 <| hash24 func
in
Debug.log
(String.join "" [ color r g b, func, "\u{001B}[39m: ", msg ])
val
Hope it’s useful!
This could be improved to give colors in a specific range instead of the full 24bit, that might help avoiding dark colors on dark terminals and similar issues.