forked from aws/aws-fpga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger-kmsg.c
101 lines (89 loc) · 2.19 KB
/
logger-kmsg.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
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
/**
* @file
* kmsg logger.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <utils/io.h>
#include <utils/log.h>
/** kmsg logger private data. */
static struct {
int fd;
} priv;
/**
* Initialize the kmsg logger.
*
* @param[in] opaque Unused.
* @param[in] flags Unused.
*
* @returns 0 on success, a negative error number on failure.
*/
static int kmsg_init(void *opaque, unsigned int flags)
{
int fd;
(void)opaque;
(void)flags;
fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC);
if (fd == -1)
return -errno;
priv.fd = fd;
return 0;
}
/** Stringify (kmsg prefix) a log level. */
static const char *str_log_level(enum log_level level)
{
static const char * const str[] = {
[LOG_LEVEL_ERROR] = "<3>",
[LOG_LEVEL_WARNING] = "<4>",
[LOG_LEVEL_INFO] = "<6>",
[LOG_LEVEL_DEBUG] = "<7>",
[LOG_LEVEL_END] = "",
};
if (level >= LOG_LEVEL_END)
level = LOG_LEVEL_END;
return str[level];
}
/**
* Log a message via the kmsg logger.
*
* @see logger#log .
*/
static int kmsg_log(enum log_level level, const char *message)
{
int rc;
char prefixed_message[LOG_MESSAGE_LENGTH_MAX + 8];
size_t prefixed_message_len;
rc = snprintf(prefixed_message, sizeof(prefixed_message), "%s%s",
str_log_level(level), message);
if ((size_t)rc >= sizeof(prefixed_message))
return -ENAMETOOLONG;
prefixed_message_len = rc;
rc = write_loop(priv.fd, prefixed_message, prefixed_message_len);
if (rc)
return -errno;
return 0;
}
/** kmsg logger. */
const struct logger logger_kmsg = {
.name = "kmsg",
.init = kmsg_init,
.log = kmsg_log,
};