-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog.c
90 lines (84 loc) · 3.46 KB
/
log.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2017 J Cody Baker. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
#include "log.h"
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
/**
* Convert a snip_log_level_t to a string describing the severity level.
* @param level
* @return - String describing the severity level.
*/
const char *
snip_log_level_to_string(snip_log_level_t level) {
switch(level) {
case SNIP_LOG_LEVEL_FATAL:
return SNIP_LOG_LEVEL_FATAL_STRING;
case SNIP_LOG_LEVEL_ALERT:
return SNIP_LOG_LEVEL_ALERT_STRING;
case SNIP_LOG_LEVEL_CRITICAL:
return SNIP_LOG_LEVEL_CRITICAL_STRING;
case SNIP_LOG_LEVEL_ERROR:
return SNIP_LOG_LEVEL_ERROR_STRING;
case SNIP_LOG_LEVEL_WARNING:
return SNIP_LOG_LEVEL_WARNING_STRING;
case SNIP_LOG_LEVEL_NOTICE:
return SNIP_LOG_LEVEL_NOTICE_STRING;
case SNIP_LOG_LEVEL_INFO:
return SNIP_LOG_LEVEL_INFO_STRING;
case SNIP_LOG_LEVEL_DEBUG:
return SNIP_LOG_LEVEL_DEBUG_STRING;
default:
return "UNKNOWN";
}
}
/**
* Log a message.
* @param level - Severity level for the log message. See https://en.wikipedia.org/wiki/Syslog#Severity_level
* @param msg_format - Single-line description of the condition we want to log. printf style format string populated
* with variadic arguments provided in args.
* @param args - List of variadic arguments for populating in the format string.
*/
void snip_vlog(snip_log_level_t level, const char *msg_format, va_list args) {
fprintf(stderr, "%-8s: ", snip_log_level_to_string(level));
vfprintf(stderr, msg_format, args);
fputs("\n", stderr);
}
/**
* Log a message.
* @param level - Severity level for the log message. See https://en.wikipedia.org/wiki/Syslog#Severity_level
* @param msg_format - Single-line description of the condition we want to log. printf style format string populated
* with remaining variadic arguments.
* @param ... - List of arguments for populating the format string msg_format.
*/
void snip_log(snip_log_level_t level, const char *msg_format, ...) {
va_list args;
va_start(args, msg_format);
snip_vlog(level, msg_format, args);
va_end(args);
}
/**
* Log a message with the SNIP_LOG_LEVEL_FATAL severity and then exit.
* @param exit_code Process exit code. Use SNIP_EXIT_ERROR_GENERAL for undefined errors.
* @param msg_format - Single-line description of the condition we want to log. printf style format string populated
* with variadic arguments provided in args.
* @param ... - List of arguments for populating the format string msg_format.
*/
void snip_log_fatal(int code, const char *msg_format, ...) {
va_list args;
va_start(args, msg_format);
snip_vlog_fatal(SNIP_LOG_LEVEL_FATAL, msg_format, args);
va_end(args);
}
/**
* Log a message with the SNIP_LOG_LEVEL_FATAL severity and then exit.
* @param exit_code Process exit code. Use SNIP_EXIT_ERROR_GENERAL for undefined errors.
* @param msg_format - Single-line string description which should be logged on error.
* @param args - List of variadic arguments for populating in the format string msg_format.
*/
void snip_vlog_fatal(int code, const char *msg_format, va_list args) {
snip_vlog(SNIP_LOG_LEVEL_FATAL, msg_format, args);
// TODO - Clean-shutdown or gentle failure if this happens on a SIGHUP reload.
exit(code);
}