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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
// Has to be defined before including due to legacy Unices
#define SYSLOG_NAMES 1
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
// This implementation doesn't talk to a syslog server. Any options related to
// that are no-ops.
extern "C" {
// For implementation simplicity, we actually only use the re-entrant version
// of each function, and the version that isn't just redirects with a static
// struct to share.
static struct syslog_data global_log_data = {
.ident = nullptr,
.logopt = 0,
.facility = LOG_USER,
.maskpri = LOG_UPTO(LOG_DEBUG)
};
// Used when ident is null, since syslog traditionally prints the program's
// own name; the process name will always be the same unless we exec.
static char program_name_buffer[256];
static bool program_name_set = false;
// Convenience function for initialization and checking what string to use
// for the program name.
static const char* get_syslog_ident(struct syslog_data* data)
{
if (!program_name_set && data->ident == nullptr)
program_name_set = get_process_name(program_name_buffer, sizeof(program_name_buffer)) >= 0;
if (data->ident != nullptr)
return data->ident;
else if (program_name_set)
return program_name_buffer;
VERIFY_NOT_REACHED();
}
void openlog_r(const char* ident, int logopt, int facility, struct syslog_data* data)
{
data->ident = ident;
data->logopt = logopt;
data->facility = facility;
// default value
data->maskpri = LOG_UPTO(LOG_DEBUG);
// would be where we connect to a daemon
}
void openlog(const char* ident, int logopt, int facility)
{
openlog_r(ident, logopt, facility, &global_log_data);
}
void closelog_r(struct syslog_data* data)
{
// would be where we disconnect from a daemon
// restore defaults
data->ident = nullptr;
data->logopt = 0;
data->facility = LOG_USER;
data->maskpri = LOG_UPTO(LOG_DEBUG);
}
void closelog(void)
{
closelog_r(&global_log_data);
}
int setlogmask_r(int maskpri, struct syslog_data* data)
{
// Remember, this takes the input of LOG_MASK/LOG_UPTO
int old_maskpri = data->maskpri;
data->maskpri = maskpri;
return old_maskpri;
}
int setlogmask(int maskpri)
{
return setlogmask_r(maskpri, &global_log_data);
}
void syslog_r(int priority, struct syslog_data* data, const char* message, ...)
{
va_list ap;
va_start(ap, message);
vsyslog_r(priority, data, message, ap);
va_end(ap);
}
void syslog(int priority, const char* message, ...)
{
va_list ap;
va_start(ap, message);
vsyslog_r(priority, &global_log_data, message, ap);
va_end(ap);
}
void vsyslog_r(int priority, struct syslog_data* data, const char* message, va_list args)
{
StringBuilder combined;
int real_priority = LOG_PRI(priority);
// Lots of parens, but it just extracts the priority from combo and masks.
if (!(data->maskpri & LOG_MASK(real_priority)))
return;
// Some metadata would be consumed by a syslog daemon, if we had one.
if (data->logopt & LOG_PID)
combined.appendff("{}[{}]: ", get_syslog_ident(data), getpid());
else
combined.appendff("{}: ", get_syslog_ident(data));
combined.appendvf(message, args);
String combined_string = combined.build();
if (data->logopt & LOG_CONS)
dbgputstr(combined_string.characters(), combined_string.length());
if (data->logopt & LOG_PERROR)
fputs(combined_string.characters(), stderr);
}
void vsyslog(int priority, const char* message, va_list args)
{
vsyslog_r(priority, &global_log_data, message, args);
}
}
|