1 /* Copyright StrongLoop, Inc. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22 #include "defs.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 static void pr_do(FILE *stream,
28 const char *label,
29 const char *fmt,
30 va_list ap);
31
xmalloc(size_t size)32 void *xmalloc(size_t size) {
33 void *ptr;
34
35 ptr = malloc(size);
36 if (ptr == NULL) {
37 pr_err("out of memory, need %lu bytes", (unsigned long) size);
38 exit(1);
39 }
40
41 return ptr;
42 }
43
pr_info(const char * fmt,...)44 void pr_info(const char *fmt, ...) {
45 va_list ap;
46 va_start(ap, fmt);
47 pr_do(stdout, "info", fmt, ap);
48 va_end(ap);
49 }
50
pr_warn(const char * fmt,...)51 void pr_warn(const char *fmt, ...) {
52 va_list ap;
53 va_start(ap, fmt);
54 pr_do(stderr, "warn", fmt, ap);
55 va_end(ap);
56 }
57
pr_err(const char * fmt,...)58 void pr_err(const char *fmt, ...) {
59 va_list ap;
60 va_start(ap, fmt);
61 pr_do(stderr, "error", fmt, ap);
62 va_end(ap);
63 }
64
pr_do(FILE * stream,const char * label,const char * fmt,va_list ap)65 static void pr_do(FILE *stream,
66 const char *label,
67 const char *fmt,
68 va_list ap) {
69 char fmtbuf[1024];
70 vsnprintf(fmtbuf, sizeof(fmtbuf), fmt, ap);
71 fprintf(stream, "%s:%s: %s\n", _getprogname(), label, fmtbuf);
72 }
73