Skip to content

Commit 9104e0f

Browse files
bwolmaransgnosek
authored andcommitted
Add the ability to prepend encoded log severity in the log message
1 parent 9ef2d64 commit 9104e0f

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

userspace/libsinsp/logger.cpp

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ namespace
3232

3333
thread_local char s_tbuf[16384];
3434

35+
const size_t ENCODE_LEN = sizeof(uint64_t);
36+
3537
} // end namespace
3638

37-
const uint32_t sinsp_logger::OT_NONE = 0;
38-
const uint32_t sinsp_logger::OT_STDOUT = 1;
39-
const uint32_t sinsp_logger::OT_STDERR = (OT_STDOUT << 1);
40-
const uint32_t sinsp_logger::OT_FILE = (OT_STDERR << 1);
41-
const uint32_t sinsp_logger::OT_CALLBACK = (OT_FILE << 1);
42-
const uint32_t sinsp_logger::OT_NOTS = (OT_CALLBACK << 1);
39+
const uint32_t sinsp_logger::OT_NONE = 0;
40+
const uint32_t sinsp_logger::OT_STDOUT = 1;
41+
const uint32_t sinsp_logger::OT_STDERR = (OT_STDOUT << 1);
42+
const uint32_t sinsp_logger::OT_FILE = (OT_STDERR << 1);
43+
const uint32_t sinsp_logger::OT_CALLBACK = (OT_FILE << 1);
44+
const uint32_t sinsp_logger::OT_NOTS = (OT_CALLBACK << 1);
45+
const uint32_t sinsp_logger::OT_ENCODE_SEV = (OT_NOTS << 1);
4346

4447
sinsp_logger::sinsp_logger():
4548
m_file(nullptr),
@@ -101,6 +104,11 @@ void sinsp_logger::disable_timestamps()
101104
m_flags |= sinsp_logger::OT_NOTS;
102105
}
103106

107+
void sinsp_logger::add_encoded_severity()
108+
{
109+
m_flags |= sinsp_logger::OT_ENCODE_SEV;
110+
}
111+
104112
void sinsp_logger::add_callback_log(const sinsp_logger_callback callback)
105113
{
106114
const sinsp_logger_callback old_cb = m_callback.exchange(callback);
@@ -165,19 +173,11 @@ void sinsp_logger::log(std::string msg, const severity sev)
165173

166174
if(gettimeofday(&ts, nullptr) == 0)
167175
{
168-
const std::string::size_type ts_length = 22;
169-
char ts_buf[ts_length + 1];
176+
const std::string::size_type ts_length = sizeof("31-12 23:59:59.999999 ");
177+
char ts_buf[ts_length];
170178
struct tm time_info = {};
171179

172180
gmtime_r(&ts.tv_sec, &time_info);
173-
174-
//
175-
// This formatted string is ts_length bytes long:
176-
//
177-
// 1 2
178-
// "1234567890123456789012
179-
// "xx-xx xx:xx:xx.xxxxxx "
180-
//
181181
snprintf(ts_buf,
182182
sizeof(ts_buf),
183183
"%.2d-%.2d %.2d:%.2d:%.2d.%.6d ",
@@ -189,10 +189,18 @@ void sinsp_logger::log(std::string msg, const severity sev)
189189
(int)ts.tv_usec);
190190

191191
ts_buf[sizeof(ts_buf) - 1] = '\0';
192-
msg.insert(0, ts_buf, ts_length);
192+
msg.insert(0, ts_buf);
193193
}
194194
}
195195

196+
if(m_flags & sinsp_logger::OT_ENCODE_SEV)
197+
{
198+
char sev_buf[ENCODE_LEN + 1];
199+
strncpy(sev_buf, encode_severity(sev), sizeof(sev_buf));
200+
sev_buf[sizeof(sev_buf) - 1] = 0;
201+
msg.insert(0, sev_buf);
202+
}
203+
196204
if(is_callback())
197205
{
198206
cb = m_callback;
@@ -250,3 +258,57 @@ const char* sinsp_logger::format(const char* const fmt, ...)
250258

251259
return s_tbuf;
252260
}
261+
262+
namespace {
263+
// All severity strings should be ENCODE_LEN chars long
264+
const char* SEV_LEVELS[] = {
265+
"SEV_DEF ",
266+
"SEV_FAT ",
267+
"SEV_CRI ",
268+
"SEV_ERR ",
269+
"SEV_WAR ",
270+
"SEV_NOT ",
271+
"SEV_INF ",
272+
"SEV_DEB ",
273+
"SEV_TRA "
274+
};
275+
276+
static_assert(sizeof(SEV_LEVELS) == sizeof(*SEV_LEVELS) * ((size_t)(sinsp_logger::SEV_MAX) + 1),
277+
"severity array must have SEV_MAX+1 elements");
278+
}
279+
280+
const char* sinsp_logger::encode_severity(const sinsp_logger::severity sev)
281+
{
282+
const char* ret;
283+
auto sev_int = (size_t)sev;
284+
if (sev_int > SEV_MAX)
285+
{
286+
sev_int = 0;
287+
}
288+
289+
ret = SEV_LEVELS[sev_int];
290+
assert(strlen(ret) == ENCODE_LEN);
291+
return ret;
292+
}
293+
294+
size_t sinsp_logger::decode_severity(const std::string &str, severity& sev)
295+
{
296+
if(str.length() < ENCODE_LEN)
297+
{
298+
return 0;
299+
}
300+
301+
const char* msg = str.c_str();
302+
303+
// we don't really expect "SEV_DEF " messages so skip severity 0
304+
for(size_t i = SEV_MIN; i <= SEV_MAX; ++i)
305+
{
306+
if(!strncmp(msg, SEV_LEVELS[i], ENCODE_LEN))
307+
{
308+
sev = static_cast<severity>(i);
309+
return ENCODE_LEN;
310+
}
311+
}
312+
313+
return 0;
314+
}

userspace/libsinsp/logger.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class SINSP_PUBLIC sinsp_logger
7676
const static uint32_t OT_FILE;
7777
const static uint32_t OT_CALLBACK;
7878
const static uint32_t OT_NOTS;
79+
const static uint32_t OT_ENCODE_SEV;
7980

8081
/**
8182
* Initialize this sinsp_logger with no output sinks enabled.
@@ -107,6 +108,9 @@ class SINSP_PUBLIC sinsp_logger
107108
/** Disables tagging logs with the current timestamp. */
108109
void disable_timestamps();
109110

111+
/** Adds encoded severity to log messages */
112+
void add_encoded_severity();
113+
110114
/**
111115
* Registered the given callback as the logging callback.
112116
*
@@ -154,6 +158,12 @@ class SINSP_PUBLIC sinsp_logger
154158
*/
155159
const char* format(const char* fmt, ...);
156160

161+
/** Sets `sev` to the decoded severity or SEV_MAX+1 for errors.
162+
* Returns the length of the severity string on success
163+
* and 0 in case of errors
164+
*/
165+
static size_t decode_severity(const std::string &s, severity& sev);
166+
157167
private:
158168
/** Returns true if the callback log sync is enabled, false otherwise. */
159169
bool is_callback() const;
@@ -167,6 +177,9 @@ class SINSP_PUBLIC sinsp_logger
167177
*/
168178
static bool is_event_severity(severity sev);
169179

180+
/** Returns a string containing encoded severity, for OT_ENCODE_SEV. */
181+
static const char* encode_severity(severity sev);
182+
170183
std::atomic<FILE*> m_file;
171184
std::atomic<sinsp_logger_callback> m_callback;
172185
std::atomic<uint32_t> m_flags;

0 commit comments

Comments
 (0)