@@ -32,14 +32,17 @@ namespace
3232
3333thread_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
4447sinsp_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+
104112void 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 " ,
@@ -193,6 +193,14 @@ void sinsp_logger::log(std::string msg, const severity sev)
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, ENCODE_LEN);
202+ }
203+
196204 if (is_callback ())
197205 {
198206 cb = m_callback;
@@ -250,3 +258,58 @@ 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+ uint64_t msg_level = *((uint64_t *)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+ uint64_t level = *((uint64_t *) sev_levels[i]);
307+ if (msg_level == level)
308+ {
309+ sev = static_cast <severity>(i);
310+ return ENCODE_LEN;
311+ }
312+ }
313+
314+ return 0 ;
315+ }
0 commit comments