let format_log_event ev = 
  let rlst = ref [] in
  let timestamp_str = OUnitUtils.date_iso8601 ev.timestamp in
  let spf pre fmt = 
    Printf.ksprintf
      (multiline 
         (fun l ->
            rlst := (timestamp_str^" "^ev.shard^" "^pre^": "^l) :: !rlst))
      fmt
  in
  let ispf fmt = spf "I" fmt in
  let wspf fmt = spf "W" fmt in
  let espf fmt = spf "E" fmt in
  let format_result path result =
    let path_str = string_of_path path in
    match result with 
    | RTimeout test_length ->
        espf "Test %s timed out after %.1fs"
          path_str (delay_of_length test_length)
    | RError (msg, backtrace_opt) ->
        espf "Test %s exited with an error." path_str;
        espf "%s in test %s." msg path_str;
        OUnitUtils.opt (espf "%s") backtrace_opt
    | RFailure (msg, _, backtrace_opt) ->
        espf "Test %s has failed." path_str;
        espf "%s in test %s." msg path_str;
        OUnitUtils.opt (espf "%s") backtrace_opt
    | RTodo msg -> wspf "TODO test %s: %s." path_str msg
    | RSkip msg -> wspf "Skip test %s: %s." path_str msg
    | RSuccess -> ispf "Test %s is successful." path_str
  in

  begin
    match ev.event with
      | GlobalEvent e ->
          begin
            match e with
            | GConf (k, v) -> ispf "Configuration %s = %S" k v
            | GLog (`Error, str) -> espf "%s" str
            | GLog (`Warning, str) -> wspf "%s" str
            | GLog (`Info, str) -> ispf "%s" str
            | GStart -> ispf "Start testing."
            | GEnd -> ispf "End testing."
            | GResults (running_time, results, test_case_count) ->
                let countr = count results in
                ispf "==============";
                ispf "Summary:";
                List.iter
                  (fun (path, test_result, _) ->
                     format_result path test_result)
                  results;
                (* Print final verdict *)
                ispf "Ran: %d tests in: %.2f seconds."
                  (List.length results) running_time;
                ispf "Cases: %d." test_case_count;
                ispf "Tried: %d." (List.length results);
                ispf "Errors: %d." (countr is_error);
                ispf "Failures: %d." (countr is_failure);
                ispf "Skip: %d." (countr is_skip);
                ispf "Todo: %d." (countr is_todo);
                ispf "Timeout: %d." (countr is_timeout)
          end

      | TestEvent (path, e) ->
          begin
            let path_str = string_of_path path in
            match e with
            | EStart -> ispf "Start test %s." path_str
            | EEnd -> ispf "End test %s." path_str
            | EResult result -> format_result path result
            | ELog (`Error, str) -> espf "%s" str
            | ELog (`Warning, str) -> wspf "%s" str
            | ELog (`Info, str) -> ispf "%s" str
            | ELogRaw str -> ispf "%s" str
          end
  end;
  List.rev !rlst