Blame samples/socks5-proxy/util.c

Packit b5b901
/* Copyright StrongLoop, Inc. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#include "defs.h"
Packit b5b901
#include <stdarg.h>
Packit b5b901
#include <stdio.h>
Packit b5b901
#include <stdlib.h>
Packit b5b901
Packit b5b901
static void pr_do(FILE *stream,
Packit b5b901
                  const char *label,
Packit b5b901
                  const char *fmt,
Packit b5b901
                  va_list ap);
Packit b5b901
Packit b5b901
void *xmalloc(size_t size) {
Packit b5b901
  void *ptr;
Packit b5b901
Packit b5b901
  ptr = malloc(size);
Packit b5b901
  if (ptr == NULL) {
Packit b5b901
    pr_err("out of memory, need %lu bytes", (unsigned long) size);
Packit b5b901
    exit(1);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  return ptr;
Packit b5b901
}
Packit b5b901
Packit b5b901
void pr_info(const char *fmt, ...) {
Packit b5b901
  va_list ap;
Packit b5b901
  va_start(ap, fmt);
Packit b5b901
  pr_do(stdout, "info", fmt, ap);
Packit b5b901
  va_end(ap);
Packit b5b901
}
Packit b5b901
Packit b5b901
void pr_warn(const char *fmt, ...) {
Packit b5b901
  va_list ap;
Packit b5b901
  va_start(ap, fmt);
Packit b5b901
  pr_do(stderr, "warn", fmt, ap);
Packit b5b901
  va_end(ap);
Packit b5b901
}
Packit b5b901
Packit b5b901
void pr_err(const char *fmt, ...) {
Packit b5b901
  va_list ap;
Packit b5b901
  va_start(ap, fmt);
Packit b5b901
  pr_do(stderr, "error", fmt, ap);
Packit b5b901
  va_end(ap);
Packit b5b901
}
Packit b5b901
Packit b5b901
static void pr_do(FILE *stream,
Packit b5b901
                  const char *label,
Packit b5b901
                  const char *fmt,
Packit b5b901
                  va_list ap) {
Packit b5b901
  char fmtbuf[1024];
Packit b5b901
  vsnprintf(fmtbuf, sizeof(fmtbuf), fmt, ap);
Packit b5b901
  fprintf(stream, "%s:%s: %s\n", _getprogname(), label, fmtbuf);
Packit b5b901
}