From f1f4110493cfd99ce0ebbb4d54bb824b65948fd3 Mon Sep 17 00:00:00 2001 From: Armando Montanez Date: Mon, 2 Nov 2020 15:58:39 -0800 Subject: [PATCH] pw_log_basic: Make module configurable Updates pw_log_basic to use the new configuration aproach so projects can configure log contents. Change-Id: If7149b5e60d33bed6977b06765f6e1e12222d460 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/23200 Reviewed-by: Wyatt Hepler Reviewed-by: Keir Mierle Commit-Queue: Armando Montanez --- pw_log_basic/BUILD | 1 + pw_log_basic/BUILD.gn | 14 +++++- pw_log_basic/log_basic.cc | 11 +---- pw_log_basic/pw_log_basic_private/config.h | 53 ++++++++++++++++++++++ 4 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 pw_log_basic/pw_log_basic_private/config.h diff --git a/pw_log_basic/BUILD b/pw_log_basic/BUILD index 4b877e7e9..2c4598d6d 100644 --- a/pw_log_basic/BUILD +++ b/pw_log_basic/BUILD @@ -40,6 +40,7 @@ pw_cc_library( name = "pw_log_basic", srcs = [ "log_basic.cc", + "pw_log_basic_private/config.h", ], deps = [ ":headers", diff --git a/pw_log_basic/BUILD.gn b/pw_log_basic/BUILD.gn index 438eac98a..5269f09b7 100644 --- a/pw_log_basic/BUILD.gn +++ b/pw_log_basic/BUILD.gn @@ -14,9 +14,17 @@ import("//build_overrides/pigweed.gni") +import("$dir_pw_build/module_config.gni") import("$dir_pw_build/target_types.gni") import("$dir_pw_docgen/docs.gni") +declare_args() { + # The build target that overrides the default configuration options for this + # module. This should point to a source set that provides defines through a + # public config (which may -include a file or add defines directly). + pw_log_basic_CONFIG = pw_build_DEFAULT_MODULE_CONFIG +} + config("default_config") { include_dirs = [ "public" ] } @@ -41,6 +49,7 @@ pw_source_set("core") { "$dir_pw_log:facade", dir_pw_string, dir_pw_sys_io, + pw_log_basic_CONFIG, ] public = [ "public/pw_log_basic/log_basic.h" ] @@ -51,7 +60,10 @@ pw_source_set("core") { defines += [ "PW_EMOJI=1" ] } - sources = [ "log_basic.cc" ] + sources = [ + "log_basic.cc", + "pw_log_basic_private/config.h", + ] } pw_doc_group("docs") { diff --git a/pw_log_basic/log_basic.cc b/pw_log_basic/log_basic.cc index 18423a37f..e48061469 100644 --- a/pw_log_basic/log_basic.cc +++ b/pw_log_basic/log_basic.cc @@ -19,6 +19,7 @@ #include #include "pw_log/levels.h" +#include "pw_log_basic_private/config.h" #include "pw_string/string_builder.h" #include "pw_sys_io/sys_io.h" @@ -37,16 +38,6 @@ #define RESET "\033[0m" // clang-format on -#ifndef PW_EMOJI -#define PW_EMOJI 0 -#endif // PW_EMOJI - -// TODO(pwbug/17): Expose these through the config system. -#define PW_LOG_SHOW_FILENAME 0 -#define PW_LOG_SHOW_FUNCTION 0 -#define PW_LOG_SHOW_FLAG 0 -#define PW_LOG_SHOW_MODULE 0 - namespace pw::log_basic { namespace { diff --git a/pw_log_basic/pw_log_basic_private/config.h b/pw_log_basic/pw_log_basic_private/config.h new file mode 100644 index 000000000..6bb943861 --- /dev/null +++ b/pw_log_basic/pw_log_basic_private/config.h @@ -0,0 +1,53 @@ +// Copyright 2020 The Pigweed 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 +// +// https://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. +#pragma once + +// Replaces log levels and flag presence indicator with emoji. +#ifndef PW_EMOJI +#define PW_EMOJI 0 +#endif // PW_EMOJI + +// With all the following flags enabled, log messages look like this: +// +// clang-format off +// my_file.cc : 42 | Foo | TST | INF Hello, world! +// buggy.cc :2145 | ReadBuggyBuffer | * ERR No, BAD! +// +// With emoji: +// my_file.cc : 42 | Foo | TST ℹ️ Hello, world! +// buggy.cc :2145 | ReadBuggyBuffer | 🚩 ❌ No, BAD! +// clang-format on + +// Prints the name of the file that emitted the log message. +#ifndef PW_LOG_SHOW_FILENAME +#define PW_LOG_SHOW_FILENAME 0 +#endif // PW_LOG_SHOW_FILENAME + +// Prints the name of the function that emitted the log message. +#ifndef PW_LOG_SHOW_FUNCTION +#define PW_LOG_SHOW_FUNCTION 0 +#endif // PW_LOG_SHOW_FUNCTION + +// Prints an indicator for whether or not there are any active flags for a given +// log statement. +#ifndef PW_LOG_SHOW_FLAG +#define PW_LOG_SHOW_FLAG 0 +#endif // PW_LOG_SHOW_FLAG + +// Prints the module name associated with a log statement. This is provided by +// defining PW_LOG_MODULE_NAME inside module source files, it is not implied by +// module structure or file path magic. +#ifndef PW_LOG_SHOW_MODULE +#define PW_LOG_SHOW_MODULE 0 +#endif // PW_LOG_SHOW_MODULE