Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ config_setting(
values = {"define": "manual_stamp=manual_stamp"},
)

config_setting(
name = "android_logger",
values = {"define": "logger=android"},
)

alias(
name = "apple",
actual = select(
Expand Down
1 change: 1 addition & 0 deletions bazel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ The following optional features can be enabled on the Bazel build command-line:
This is needed if the `version_info_lib` is compiled via a non-binary bazel rules, e.g `envoy_cc_library`.
Otherwise, the linker will fail to resolve symbols that are included via the `linktamp` rule, which is only available to binary targets.
This is being tracked as a feature in: https://github.com/envoyproxy/envoy/issues/6859.
* Process logging for Android applications can be enabled with `--define logger=android`.

## Disabling extensions

Expand Down
33 changes: 32 additions & 1 deletion source/common/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,41 @@ envoy_cc_library(
hdrs = ["logger.h"],
external_deps = ["abseil_synchronization"],
deps = [
":base_logger_lib",
":lock_guard_lib",
":macros",
":non_copyable",
],
] + select({
"//bazel:android_logger": ["logger_impl_lib_android"],
"//conditions:default": ["logger_impl_lib_standard"],
}),
)

envoy_cc_library(
name = "base_logger_lib",
srcs = ["base_logger.cc"],
hdrs = ["base_logger.h"],
)

envoy_cc_library(
name = "logger_impl_lib_standard",
hdrs = ["standard/logger_impl.h"],
strip_include_prefix = "standard",
deps = [":base_logger_lib"],
)

envoy_cc_library(
name = "logger_impl_lib_android",
srcs = select({
"//bazel:android_logger": ["android/logger_impl.cc"],
"//conditions:default": [],
}),
hdrs = select({
"//bazel:android_logger": ["android/logger_impl.h"],
"//conditions:default": [],
}),
strip_include_prefix = "android",
deps = [":base_logger_lib"],
Copy link
Member

@zuercher zuercher Jan 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the andoid sink in spdlog is #ifdef __ANDROID__, does it make sense to gate this logger on the android equivalent of //bazel::windows_x86_64 and //bazel:apple (instead of a flag that won't work in any other env) and following the pattern used for envoy_cc_win32_library?

(The file watcher lib should get the same treatment for apple, but that's another PR.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first idea, but @goaway pointed out that there might be cases where we might want the StandardLogger when __ANDROID__ is defined, e.g running envoy as a process in an android device, rather than in a sandboxed application.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Do we want to pull the selection logic out into helpers?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also debated about this. Given that it was only used once now and for the foreseeable future I opted for not introducing more helpers into the build system until they are needed. But if you think it is worth it, I am happy to add!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the thing I'm getting it is whether having an empty envoy_cc_library (logger_impl_lib_android) when android logging is disabled causes some kind of problem for bazel (particular the query subcommand). It seemed like there was something related to that with win32 libs, but I'm a bit out of my depth when it comes to bazel. I suppose it can always be fixed later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I was copying the pattern from win32

def envoy_cc_win32_library(name, srcs = [], hdrs = [], **kargs):
. Yeah, I agree, if there is a reported problem we can fix later.

)

envoy_cc_library(
Expand Down
13 changes: 13 additions & 0 deletions source/common/common/android/logger_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "common/common/logger_impl.h"

#include "spdlog/sinks/android_sink.h"

namespace Envoy {
namespace Logger {

AndroidLogger::AndroidLogger(const std::string& name)
: Logger(std::make_shared<spdlog::logger>(
name, std::make_shared<spdlog::sinks::android_sink<std::mutex>>())) {}

} // namespace Logger
} // namespace Envoy
21 changes: 21 additions & 0 deletions source/common/common/android/logger_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "common/common/base_logger.h"

namespace Envoy {
namespace Logger {

#define GENERATE_LOGGER(X) AndroidLogger(#X),

/**
* Logger that uses spdlog::sinks::android_sink.
*/
class AndroidLogger : public Logger {
private:
AndroidLogger(const std::string& name);

friend class Registry;
};

} // namespace Logger
} // namespace Envoy
17 changes: 17 additions & 0 deletions source/common/common/base_logger.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "common/common/base_logger.h"

namespace Envoy {
namespace Logger {

const char* Logger::DEFAULT_LOG_FORMAT = "[%Y-%m-%d %T.%e][%t][%l][%n] %v";

Logger::Logger(std::shared_ptr<spdlog::logger> logger) : logger_(logger) {
logger_->set_pattern(DEFAULT_LOG_FORMAT);
logger_->set_level(spdlog::level::trace);

// Ensure that critical errors, especially ASSERT/PANIC, get flushed
logger_->flush_on(spdlog::level::critical);
}

} // namespace Logger
} // namespace Envoy
51 changes: 51 additions & 0 deletions source/common/common/base_logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <memory>
#include <string>

#include "spdlog/spdlog.h"

namespace Envoy {
namespace Logger {

/**
* Logger wrapper for a spdlog logger.
*/
class Logger {
public:
/* This is simple mapping between Logger severity levels and spdlog severity levels.
* The only reason for this mapping is to go around the fact that spdlog defines level as err
* but the method to log at err level is called LOGGER.error not LOGGER.err. All other level are
* fine spdlog::info corresponds to LOGGER.info method.
*/
using Levels = enum {
trace = spdlog::level::trace, // NOLINT(readability-identifier-naming)
debug = spdlog::level::debug, // NOLINT(readability-identifier-naming)
info = spdlog::level::info, // NOLINT(readability-identifier-naming)
warn = spdlog::level::warn, // NOLINT(readability-identifier-naming)
error = spdlog::level::err, // NOLINT(readability-identifier-naming)
critical = spdlog::level::critical, // NOLINT(readability-identifier-naming)
off = spdlog::level::off // NOLINT(readability-identifier-naming)
};

spdlog::string_view_t levelString() const {
return spdlog::level::level_string_views[logger_->level()];
}
std::string name() const { return logger_->name(); }
void setLevel(spdlog::level::level_enum level) { logger_->set_level(level); }
spdlog::level::level_enum level() const { return logger_->level(); }

static const char* DEFAULT_LOG_FORMAT;

protected:
Logger(std::shared_ptr<spdlog::logger> logger);

private:
std::shared_ptr<spdlog::logger> logger_; // Use shared_ptr here to allow static construction
// of vector in Registry::allLoggers().
// TODO(junr03): expand Logger's public API to delete this friendship.
friend class Registry;
};

} // namespace Logger
} // namespace Envoy
14 changes: 2 additions & 12 deletions source/common/common/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@
namespace Envoy {
namespace Logger {

#define GENERATE_LOGGER(X) Logger(#X),

const char* Logger::DEFAULT_LOG_FORMAT = "[%Y-%m-%d %T.%e][%t][%l][%n] %v";

Logger::Logger(const std::string& name) {
logger_ = std::make_shared<spdlog::logger>(name, Registry::getSink());
logger_->set_pattern(DEFAULT_LOG_FORMAT);
logger_->set_level(spdlog::level::trace);

// Ensure that critical errors, especially ASSERT/PANIC, get flushed
logger_->flush_on(spdlog::level::critical);
}
StandardLogger::StandardLogger(const std::string& name)
: Logger(std::make_shared<spdlog::logger>(name, Registry::getSink())) {}

SinkDelegate::SinkDelegate(DelegatingLogSinkSharedPtr log_sink)
: previous_delegate_(log_sink->delegate()), log_sink_(log_sink) {
Expand Down
35 changes: 5 additions & 30 deletions source/common/common/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include "envoy/thread/thread.h"

#include "common/common/base_logger.h"
#include "common/common/fmt.h"
#include "common/common/logger_impl.h"
#include "common/common/macros.h"
#include "common/common/non_copyable.h"

Expand Down Expand Up @@ -72,39 +74,12 @@ enum class Id {
// clang-format on

/**
* Logger wrapper for a spdlog logger.
* Logger that uses the DelegatingLogSink.
*/
class Logger {
public:
/* This is simple mapping between Logger severity levels and spdlog severity levels.
* The only reason for this mapping is to go around the fact that spdlog defines level as err
* but the method to log at err level is called LOGGER.error not LOGGER.err. All other level are
* fine spdlog::info corresponds to LOGGER.info method.
*/
using Levels = enum {
trace = spdlog::level::trace, // NOLINT(readability-identifier-naming)
debug = spdlog::level::debug, // NOLINT(readability-identifier-naming)
info = spdlog::level::info, // NOLINT(readability-identifier-naming)
warn = spdlog::level::warn, // NOLINT(readability-identifier-naming)
error = spdlog::level::err, // NOLINT(readability-identifier-naming)
critical = spdlog::level::critical, // NOLINT(readability-identifier-naming)
off = spdlog::level::off // NOLINT(readability-identifier-naming)
};

spdlog::string_view_t levelString() const {
return spdlog::level::level_string_views[logger_->level()];
}
std::string name() const { return logger_->name(); }
void setLevel(spdlog::level::level_enum level) { logger_->set_level(level); }
spdlog::level::level_enum level() const { return logger_->level(); }

static const char* DEFAULT_LOG_FORMAT;

class StandardLogger : public Logger {
private:
Logger(const std::string& name);
StandardLogger(const std::string& name);

std::shared_ptr<spdlog::logger> logger_; // Use shared_ptr here to allow static construction
// of constant vector below.
friend class Registry;
};

Expand Down
11 changes: 11 additions & 0 deletions source/common/common/standard/logger_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "common/common/base_logger.h"

namespace Envoy {
namespace Logger {

#define GENERATE_LOGGER(X) StandardLogger(#X),

} // namespace Logger
} // namespace Envoy