blob: ac7a75c802fc6574ae147460f2ad910d0d0d7e7b [file] [log] [blame]
Patrick Williamsb2a3aa22021-07-27 13:30:52 -05001#define SD_JOURNAL_SUPPRESS_LOCATION
2
3#include <systemd/sd-journal.h>
4#include <unistd.h>
5
6#include <algorithm>
7#include <bitset>
8#include <cstdarg>
9#include <cstdio>
10#include <iostream>
11#include <phosphor-logging/lg2.hpp>
12#include <vector>
13
14namespace lg2::details
15{
16/** Convert unsigned to string using format flags. */
17static std::string value_to_string(uint64_t f, uint64_t v)
18{
19 switch (f & (hex | bin | dec).value)
20 {
21 // For binary, use bitset<>::to_string.
22 // Treat values without a field-length format flag as 64 bit.
23 case bin.value:
24 {
25 switch (f & (field8 | field16 | field32 | field64).value)
26 {
27 case field8.value:
28 {
29 return "0b" + std::bitset<8>(v).to_string();
30 }
31 case field16.value:
32 {
33 return "0b" + std::bitset<16>(v).to_string();
34 }
35 case field32.value:
36 {
37 return "0b" + std::bitset<32>(v).to_string();
38 }
39 case field64.value:
40 default:
41 {
42 return "0b" + std::bitset<64>(v).to_string();
43 }
44 }
45 }
46
47 // For hex, use the appropriate sprintf.
48 case hex.value:
49 {
50 char value[19];
51 const char* format = nullptr;
52
53 switch (f & (field8 | field16 | field32 | field64).value)
54 {
55 case field8.value:
56 {
57 format = "0x%02" PRIx64;
58 break;
59 }
60
61 case field16.value:
62 {
63 format = "0x%04" PRIx64;
64 break;
65 }
66
67 case field32.value:
68 {
69 format = "0x%08" PRIx64;
70 break;
71 }
72
73 case field64.value:
74 {
75 format = "0x%016" PRIx64;
76 break;
77 }
78
79 default:
80 {
81 format = "0x%" PRIx64;
82 break;
83 }
84 }
85
86 snprintf(value, sizeof(value), format, v);
87 return value;
88 }
89
90 // For dec, use the simple to_string.
91 case dec.value:
92 default:
93 {
94 return std::to_string(v);
95 }
96 }
97}
98
99/** Convert signed to string using format flags. */
100static std::string value_to_string(uint64_t f, int64_t v)
101{
102 // If hex or bin was requested just use the unsigned formatting
103 // rules. (What should a negative binary number look like otherwise?)
104 if (f & (hex | bin).value)
105 {
106 return value_to_string(f, static_cast<uint64_t>(v));
107 }
108 return std::to_string(v);
109}
110
111/** Convert float to string using format flags. */
112static std::string value_to_string(uint64_t, double v)
113{
114 // No format flags supported for floats.
115 return std::to_string(v);
116}
117
118// Positions of various strings in an iovec.
119static constexpr size_t pos_msg = 0;
120static constexpr size_t pos_fmtmsg = 1;
121static constexpr size_t pos_prio = 2;
122static constexpr size_t pos_file = 3;
123static constexpr size_t pos_line = 4;
124static constexpr size_t pos_func = 5;
125static constexpr size_t static_locs = pos_func + 1;
126
127/** No-op output of a message. */
128static void noop_extra_output(level, const std::source_location&,
129 const std::string&)
130{
131}
132
133/** std::cerr output of a message. */
134static void cerr_extra_output(level l, const std::source_location& s,
135 const std::string& m)
136{
137 std::cerr << s.file_name() << ":" << s.line() << ":" << s.function_name()
138 << "|<" << static_cast<uint64_t>(l) << "> " << m << std::endl;
139}
140
141// Use the cerr output method if we are on a TTY.
142static auto extra_output_method =
143 isatty(fileno(stderr)) ? cerr_extra_output : noop_extra_output;
144
145// Do_log implementation.
Patrick Williamsb5988ff2021-08-26 11:58:36 -0500146void do_log(level l, const std::source_location& s, const char* m, size_t count,
147 ...)
Patrick Williamsb2a3aa22021-07-27 13:30:52 -0500148{
149 using namespace std::string_literals;
150
151 const size_t entries = count + static_locs;
152 std::vector<std::string> strings{entries};
153
154 std::string message{m};
155
156 // Assign all the static fields.
Patrick Williamsb5988ff2021-08-26 11:58:36 -0500157 strings[pos_fmtmsg] = "LOG2_FMTMSG="s + m;
Patrick Williamsb2a3aa22021-07-27 13:30:52 -0500158 strings[pos_prio] = "PRIORITY="s + std::to_string(static_cast<uint64_t>(l));
159 strings[pos_file] = "CODE_FILE="s + s.file_name();
160 strings[pos_line] = "CODE_LINE="s + std::to_string(s.line());
161 strings[pos_func] = "CODE_FUNC="s + s.function_name();
162
163 // Handle all the va_list args.
164 std::va_list args;
165 va_start(args, count);
166 for (size_t i = static_locs; i < entries; ++i)
167 {
168 // Get the header out.
169 std::string h = va_arg(args, const char*);
170
171 // Get the format flag.
172 auto f = va_arg(args, uint64_t);
173
174 // Handle the value depending on which type format flag it has.
175 std::string value = {};
176 switch (f & (signed_val | unsigned_val | str | floating).value)
177 {
178 case signed_val.value:
179 {
180 auto v = va_arg(args, int64_t);
181 value = value_to_string(f, v);
182 break;
183 }
184
185 case unsigned_val.value:
186 {
187 auto v = va_arg(args, uint64_t);
188 value = value_to_string(f, v);
189 break;
190 }
191
192 case str.value:
193 {
194 value = va_arg(args, const char*);
195 break;
196 }
197
198 case floating.value:
199 {
200 auto v = va_arg(args, double);
201 value = value_to_string(f, v);
202 break;
203 }
204 }
205
206 // Create the field for this value.
207 strings[i] = h + '=' + value;
208
209 // Check for {HEADER} in the message and replace with value.
210 auto h_brace = '{' + h + '}';
211 if (auto start = message.find(h_brace); start != std::string::npos)
212 {
213 message.replace(start, h_brace.size(), value);
214 }
215 }
216 va_end(args);
217
218 // Add the final message into the strings array.
219 strings[pos_msg] = "MESSAGE="s + message.data();
220
221 // Trasform strings -> iovec.
222 std::vector<iovec> iov{};
223 std::ranges::transform(strings, std::back_inserter(iov), [](auto& s) {
224 return iovec{s.data(), s.length()};
225 });
226
227 // Output the iovec.
228 sd_journal_sendv(iov.data(), entries);
229 extra_output_method(l, s, message);
230}
231
232} // namespace lg2::details