forked from sonic-net/sonic-pins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Comb] Add syslog sink to be used with GLOG. (sonic-net#482)
Co-authored-by: rhalstea <[email protected]> Co-authored-by: kishanps <[email protected]>
- Loading branch information
1 parent
b0b2a3a
commit 4354967
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "gutil/syslog_sink.h" | ||
|
||
#include <sys/syscall.h> | ||
#include <sys/syslog.h> | ||
#include <sys/time.h> | ||
|
||
#include <array> | ||
|
||
#include "glog/logging.h" | ||
|
||
namespace gutil { | ||
namespace { | ||
|
||
// GLOG severity uses integers in the range [0-3]: | ||
// https://github.com/google/glog/blob/master/src/glog/log_severity.h | ||
constexpr std::array<int, 4> kGlogSeverityToSyslog = { | ||
LOG_INFO, | ||
LOG_WARNING, | ||
LOG_ERR, | ||
LOG_EMERG, | ||
}; | ||
constexpr std::array<char, 4> kGlogSeverityLetter = { | ||
'I', | ||
'W', | ||
'E', | ||
'F', | ||
}; | ||
|
||
} // namespace | ||
|
||
SyslogSink::SyslogSink(const char* process_name) { | ||
openlog(process_name, LOG_NDELAY, LOG_USER); | ||
google::AddLogSink(this); | ||
} | ||
|
||
SyslogSink::~SyslogSink() { | ||
google::RemoveLogSink(this); | ||
closelog(); | ||
} | ||
|
||
void SyslogSink::send(google::LogSeverity severity, const char* full_filename, | ||
const char* base_filename, int line, | ||
const google::LogMessageTime& logmsgtime, | ||
const char* message, size_t message_len) { | ||
// Create a timestamp with micosecond resolution. | ||
struct timeval tv; | ||
struct timezone tz; | ||
struct tm time; | ||
gettimeofday(&tv, &tz); | ||
localtime_r(&tv.tv_sec, &time); | ||
|
||
// Output format: | ||
// I0104 23:00:59.123456 71 filename.cc:100] Your Message Here! | ||
syslog(kGlogSeverityToSyslog[severity], | ||
"%c%02d%02d %02d:%02d:%02d.%06ld %5ld %s:%d] %.*s", | ||
kGlogSeverityLetter[severity], 1 + time.tm_mon, time.tm_mday, | ||
time.tm_hour, time.tm_min, time.tm_sec, static_cast<long>(tv.tv_usec), | ||
syscall(SYS_gettid), base_filename, line, | ||
static_cast<int>(message_len), message); | ||
} | ||
|
||
} // namespace gutil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef PINS_GUTIL_SYSLOG_SINK_H_ | ||
#define PINS_GUTIL_SYSLOG_SINK_H_ | ||
|
||
#include <cstddef> | ||
|
||
#include "glog/logging.h" | ||
|
||
namespace gutil { | ||
|
||
// When constructed SyslogSink will automatically register with GLOG and begin | ||
// forwarding all LOG() messages to syslog. In the dtor we unregister with GLOG | ||
// so the SyslogSink should outlive the program. These messages are treated as | ||
// user-level (i.e. a LOG_USER facility), and serverities are mapped as follows: | ||
// LOG(INFO) -> LOG_INFO | ||
// LOG(WARNING) -> LOG_WARNING | ||
// LOG(ERROR) -> LOG_ERR | ||
// LOG(FATAL) -> LOG_EMERG | ||
// | ||
// The syslog message will be formatted similar to a GLOG message with a | ||
// microsecond timestamp. | ||
class SyslogSink : google::LogSink { | ||
public: | ||
SyslogSink(const char* process_name); | ||
~SyslogSink(); | ||
|
||
void send(google::LogSeverity severity, const char* full_filename, | ||
const char* base_filename, int line, | ||
const google::LogMessageTime& logmsgtime, const char* message, | ||
size_t message_len) override; | ||
}; | ||
|
||
} // namespace gutil | ||
|
||
#endif // PINS_GUTIL_SYSLOG_SINK_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters