Skip to content

Commit

Permalink
[Comb] Add syslog sink to be used with GLOG. (sonic-net#482)
Browse files Browse the repository at this point in the history
Co-authored-by: rhalstea <[email protected]>
Co-authored-by: kishanps <[email protected]>
  • Loading branch information
3 people authored Aug 22, 2024
1 parent b0b2a3a commit 4354967
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
7 changes: 7 additions & 0 deletions gutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,10 @@ cc_test(
"@com_google_absl//absl/strings",
],
)

cc_library(
name = "syslog_sink",
srcs = ["syslog_sink.cc"],
hdrs = ["syslog_sink.h"],
deps = ["@com_github_google_glog//:glog"],
)
62 changes: 62 additions & 0 deletions gutil/syslog_sink.cc
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
34 changes: 34 additions & 0 deletions gutil/syslog_sink.h
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_
1 change: 1 addition & 0 deletions p4rt_app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ cc_binary(
"@com_google_absl//absl/time",
"@com_github_grpc_grpc//:grpc++_public_hdrs",
"//gutil:status",
"//gutil:syslog_sink",
"//p4rt_app/event_monitoring:app_state_db_port_table_event",
"//p4rt_app/event_monitoring:app_state_db_send_to_ingress_port_table_event",
"//p4rt_app/event_monitoring:config_db_cpu_queue_table_event",
Expand Down
2 changes: 2 additions & 0 deletions p4rt_app/p4rt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "grpcpp/server.h"
#include "grpcpp/server_builder.h"
#include "gutil/status.h"
#include "gutil/syslog_sink.h"
#include "p4rt_app/event_monitoring/app_state_db_port_table_event.h"
#include "p4rt_app/event_monitoring/app_state_db_send_to_ingress_port_table_event.h"
#include "p4rt_app/event_monitoring/config_db_cpu_queue_table_event.h"
Expand Down Expand Up @@ -364,6 +365,7 @@ void ConfigDbEventLoop(P4RuntimeImpl* p4runtime_server,
} // namespace p4rt_app

int main(int argc, char** argv) {
gutil::SyslogSink syslog_sink("p4rt");
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);

Expand Down

0 comments on commit 4354967

Please sign in to comment.