Skip to content

Commit

Permalink
Replace LogEntry struct with LogMessage protobuf (project-oak#568)
Browse files Browse the repository at this point in the history
* LogEntry in sdk/rust/oak/src/logger/mod.rs is removed
* oak/proto/logger.proto containing LogMessage is added
* OakChannelLogger is updated to send LogMessage instead of string
* LoggingNode is updated to handle LogMessage instances
  • Loading branch information
rbehjati committed Mar 11, 2020
1 parent a39e166 commit 126d8d4
Show file tree
Hide file tree
Showing 9 changed files with 478 additions and 30 deletions.
11 changes: 11 additions & 0 deletions oak/proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ cc_proto_library(
deps = [":policy_proto"],
)

proto_library(
name = "logger_proto",
srcs = ["logger.proto"],
deps = [],
)

cc_proto_library(
name = "logger_cc_proto",
deps = [":logger_proto"],
)

proto_library(
name = "storage_channel_proto",
srcs = ["storage_channel.proto"],
Expand Down
42 changes: 42 additions & 0 deletions oak/proto/logger.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Copyright 2019 The Project Oak Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

syntax = "proto3";

package oak.logging;

message LogMessage {
// The source file containing the message.
string file = 1;

// The line containing the message.
uint32 line = 2;

// The verbosity level of the message.
Level level = 3;

// The message body.
string message = 4;
}

// Logging levels as defined in https://docs.rs/log/0.4.10/log/enum.Level.html.
enum Level {
ERROR = 0;
WARN = 1;
INFO = 2;
DEBUG = 3;
TRACE = 4;
}
1 change: 1 addition & 0 deletions oak/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ cc_library(
":channel",
":node_thread",
"//oak/common:handles",
"//oak/proto:logger_cc_proto",
"@com_google_absl//absl/memory",
"@com_google_asylo//asylo/util:logging",
],
Expand Down
9 changes: 8 additions & 1 deletion oak/server/logging_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

#include "absl/memory/memory.h"
#include "asylo/util/logging.h"
#include "oak/proto/logger.pb.h"

namespace oak {

const std::string LoggingNode::log_levels[5] = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"};

void LoggingNode::Run(Handle handle) {
// Borrow pointer to the channel half.
MessageChannelReadHalf* channel = BorrowReadChannel(handle);
Expand All @@ -47,8 +50,12 @@ void LoggingNode::Run(Handle handle) {
if (result.msg == nullptr) {
break;
}
oak::logging::LogMessage log_msg;
std::string msg_str(result.msg->data.begin(), result.msg->data.end());
log_msg.ParseFromString(msg_str);
LOG(INFO) << "{" << name_ << "} "
<< "LOG: " << std::string(result.msg->data.data(), result.msg->data.size());
<< "LOG: " << log_levels[log_msg.level()] << " " << log_msg.file() << ":"
<< log_msg.line() << ": " << log_msg.message();
// Any channel references included with the message will be dropped.
}
}
Expand Down
1 change: 1 addition & 0 deletions oak/server/logging_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class LoggingNode final : public NodeThread {
explicit LoggingNode(BaseRuntime* runtime, const std::string& name) : NodeThread(runtime, name) {}

private:
static const std::string log_levels[5];
void Run(Handle handle) override;
};

Expand Down
1 change: 1 addition & 0 deletions sdk/rust/oak/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn main() {
"../../../oak/proto/policy.proto",
"../../../oak/proto/storage.proto",
"../../../oak/proto/storage_channel.proto",
"../../../oak/proto/logger.proto",
],
includes: &["../../.."],
customize: protoc_rust::Customize::default(),
Expand Down
48 changes: 19 additions & 29 deletions sdk/rust/oak/src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,7 @@
use log::{Level, Log, Metadata, Record, SetLoggerError};

struct OakChannelLogger {
channel: crate::io::Sender<LogEntry>,
}

/// An object representing a log entry. Currently just a wrapper around a string, but it may be
/// extended in the future with additional fields, e.g. level or file / line information, though
/// that would probably require defining a cross-language schema such as protobuf or FIDL for it,
/// rather than just a Rust struct.
struct LogEntry {
message: String,
}

/// Trivial implementation of [`oak::io::Encodable`], just converting the log entry message to bytes
/// and no handles.
impl crate::io::Encodable for LogEntry {
fn encode(&self) -> Result<crate::io::Message, crate::OakError> {
let bytes = self.message.as_bytes().into();
let handles = vec![];
Ok(crate::io::Message { bytes, handles })
}
channel: crate::io::Sender<crate::proto::logger::LogMessage>,
}

impl Log for OakChannelLogger {
Expand All @@ -53,17 +35,15 @@ impl Log for OakChannelLogger {
if !self.enabled(record.metadata()) {
return;
}
let log_entry = LogEntry {
// We add a newline to the message to force flushing when printed by the host.
message: format!(
"{} {} : {} : {}\n",
record.level(),
record.file().unwrap_or_default(),
record.line().unwrap_or_default(),
record.args()
),
let log_msg = crate::proto::logger::LogMessage {
file: record.file().unwrap_or_default().to_string(),
line: record.line().unwrap_or_default(),
level: map_level(record.level()),
message: format!("{}", record.args()),
cached_size: ::std::default::Default::default(),
unknown_fields: ::std::default::Default::default(),
};
match self.channel.send(&log_entry) {
match self.channel.send(&log_msg) {
Ok(()) => (),
Err(crate::OakError::OakStatus(crate::OakStatus::ErrTerminated)) => (),
Err(e) => panic!("could not send log message over log channel: {}", e),
Expand All @@ -72,6 +52,16 @@ impl Log for OakChannelLogger {
fn flush(&self) {}
}

fn map_level(level: Level) -> crate::proto::logger::Level {
match level {
Level::Error => crate::proto::logger::Level::ERROR,
Level::Warn => crate::proto::logger::Level::WARN,
Level::Info => crate::proto::logger::Level::INFO,
Level::Debug => crate::proto::logger::Level::DEBUG,
Level::Trace => crate::proto::logger::Level::TRACE,
}
}

/// Default name for predefined node configuration that corresponds to a logging
/// pseudo-Node.
pub const DEFAULT_CONFIG_NAME: &str = "log";
Expand Down
Loading

0 comments on commit 126d8d4

Please sign in to comment.