/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2020 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/log/check.h"
#include "libc/log/color.internal.h"
#include "libc/log/internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/log/log.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"

STATIC_YOINK("strerror_wr");

/**
 * Handles failure of CHECK_xx() macros.
 */
relegated void __check_fail(const char *suffix,   //
                            const char *opstr,    //
                            uint64_t want,        //
                            const char *wantstr,  //
                            uint64_t got,         //
                            const char *gotstr,   //
                            const char *file,     //
                            int line,             //
                            const char *fmt,      //
                            ...) {
  int e;
  char *p;
  size_t i;
  va_list va;
  char hostname[32];
  strace_enabled(-1);
  ftrace_enabled(-1);
  e = errno;
  __start_fatal(file, line);
  __stpcpy(hostname, "unknown");
  gethostname(hostname, sizeof(hostname));
  kprintf("check failed on %s pid %d\n"
          "\tCHECK_%^s(%s, %s);\n"
          "\t\t â %p (%s)\n"
          "\t\t%s %p (%s)\n",       //
          hostname, getpid(),       //
          suffix, wantstr, gotstr,  //
          want, wantstr,            //
          opstr, got, gotstr);
  if (!isempty(fmt)) {
    kprintf("\t");
    va_start(va, fmt);
    kvprintf(fmt, va);
    va_end(va);
    kprintf("\n");
  }
  kprintf("\t%m\n\t%s%s", SUBTLE, program_invocation_name);
  for (i = 1; i < __argc; ++i) {
    kprintf(" %s", __argv[i]);
  }
  kprintf("%s\n", RESET);
  __die();
  unreachable;
}
