<'
#
# Copyright (c) 2012, 2026 LAAS/CNRS
# All rights reserved.
#
# Redistribution  and  use  in  source  and binary  forms,  with  or  without
# modification, are permitted provided that the following conditions are met:
#
#   1. Redistributions of  source  code must retain the  above copyright
#      notice and this list of conditions.
#   2. Redistributions in binary form must reproduce the above copyright
#      notice and  this list of  conditions in the  documentation and/or
#      other materials provided with the distribution.
#
# 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.
#
#                                           Anthony Mallet on Wed Feb 29 2012
#
'>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>

static int enable_info = 1;
static int enable_warn = 1;
static int enable_debug = 0;

void
genom_log_enable(int info, int warn, int debug)
{
  enable_info = info;
  enable_warn = warn;
  enable_debug = debug;
}

void
genom_log_info(const char *prefix, const char *format, ...)
{
  va_list va;
  if (!enable_info) return;

  printf("%s: ", prefix);
  va_start(va, format);
  vprintf(format, va);
  va_end(va);
  printf("\n");
}

void
genom_log_warnx(const char *prefix, const char *format, ...)
{
  va_list va;
  if (!enable_warn) return;

  fprintf(stderr, "%s: ", prefix);
  va_start(va, format);
  vfprintf(stderr, format, va);
  va_end(va);
  fprintf(stderr, "\n");
}

void
genom_log_warn(const char *prefix, const char *format, ...)
{
  va_list va;
  if (!enable_warn) return;

  fprintf(stderr, "%s: ", prefix);
  va_start(va, format);
  vfprintf(stderr, format, va);
  va_end(va);
  fprintf(stderr, ": ");
  perror(NULL);
}

void
genom_log_debug(const char *prefix, const char *format, ...)
{
  struct timespec ts;
  va_list va;
  if (!enable_debug) return;

  clock_gettime(CLOCK_REALTIME, &ts);
  fprintf(
    stderr, "[%lld.%.9ld] %s: ", (long long)ts.tv_sec, ts.tv_nsec, prefix);
  va_start(va, format);
  vfprintf(stderr, format, va);
  va_end(va);
  fprintf(stderr, "\n");
}
