-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlogging.h
67 lines (49 loc) · 1.22 KB
/
logging.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdbool.h>
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define RESET "\x1b[0m"
// this flag determines whether these functions actually print anything
bool verbose = true;
int trace_fd = 2;
bool colored = false;
// pass through to printf that can be disabled by the verbose flag [white]
void trace_print(char *color, char *msg, ...) {
va_list args;
if (!verbose) return;
va_start(args, msg);
if (colored) dprintf(trace_fd, "%s", color);
vdprintf(trace_fd, msg, args);
if (colored) dprintf(trace_fd, "%s", RESET);
}
// logs an error to the console and quits - only for extreme errors [red]
void error(char *msg, ...) {
va_list args;
va_start(args, msg);
printf("%s[!] Error: ", RED);
vprintf(msg, args);
printf("%s\n", RESET);
exit(1);
}
// logs an info [green]
void info(char *msg, ...) {
va_list args;
if (!verbose) return;
va_start(args, msg);
printf("%s[+] ", GREEN);
vprintf(msg, args);
printf("%s\n", RESET);
}
// logs a warning [yellow]
void warn(char *msg, ...) {
va_list args;
if (!verbose) return;
va_start(args, msg);
printf("%s[-] ", YELLOW);
vprintf(msg, args);
printf("%s\n", RESET);
}