diff options
author | Chunseok Lee <chunseok.lee@samsung.com> | 2020-07-30 11:32:26 +0900 |
---|---|---|
committer | Chunseok Lee <chunseok.lee@samsung.com> | 2020-07-30 11:32:26 +0900 |
commit | 05e0ec30a632339a8533082476f27bda31ccde16 (patch) | |
tree | 5f220ac83084fe133ffb08a6a17e99f9bb36ec1c /compiler/tflchef/log | |
parent | e2ef8438a24f7c56a0744eb579a6e293ee2fbf8e (diff) | |
download | nnfw-05e0ec30a632339a8533082476f27bda31ccde16.tar.gz nnfw-05e0ec30a632339a8533082476f27bda31ccde16.tar.bz2 nnfw-05e0ec30a632339a8533082476f27bda31ccde16.zip |
Imported Upstream version 1.7.0upstream/1.7.0
Diffstat (limited to 'compiler/tflchef/log')
-rw-r--r-- | compiler/tflchef/log/CMakeLists.txt | 7 | ||||
-rw-r--r-- | compiler/tflchef/log/include/Log.h | 75 | ||||
-rw-r--r-- | compiler/tflchef/log/include/LoggingContext.h | 35 | ||||
-rw-r--r-- | compiler/tflchef/log/src/Log.cpp | 87 | ||||
-rw-r--r-- | compiler/tflchef/log/src/LoggingContext.cpp | 41 |
5 files changed, 245 insertions, 0 deletions
diff --git a/compiler/tflchef/log/CMakeLists.txt b/compiler/tflchef/log/CMakeLists.txt new file mode 100644 index 000000000..330459ec1 --- /dev/null +++ b/compiler/tflchef/log/CMakeLists.txt @@ -0,0 +1,7 @@ +# TODO Find how to test logging framework +file(GLOB_RECURSE SOURCES "src/*.cpp") + +add_library(tflchef_log STATIC ${SOURCES}) +target_include_directories(tflchef_log PUBLIC include) +target_link_libraries(tflchef_log PUBLIC hermes) +target_link_libraries(tflchef_log PRIVATE hermes_std) diff --git a/compiler/tflchef/log/include/Log.h b/compiler/tflchef/log/include/Log.h new file mode 100644 index 000000000..178fe31c4 --- /dev/null +++ b/compiler/tflchef/log/include/Log.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved + * + * 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. + */ + +#ifndef __TFLCHEF_LOG_H__ +#define __TFLCHEF_LOG_H__ + +#include <hermes.h> + +namespace tflchef +{ + +/** + * @brief Logger Implementation + */ +class Logger final : public hermes::Source +{ +public: + Logger(hermes::Context *ctx); + ~Logger(); +}; + +/** + * @brief Logger Configuration + * + * Users are able to turn logging on/off via TFLCHEF_LOG environment variable. + */ +class LoggerConfig final : public hermes::Config +{ +public: + LoggerConfig(); + +public: + void configure(const hermes::Source *, hermes::Source::Setting &) const final; + void configure(const Logger *, hermes::Source::Setting &) const; + +private: + bool _enabled; +}; + +} // namespace tflchef + +#include "LoggingContext.h" + +/** + * HOW TO USE: + * + * LOGGER(l); + * + * INFO(l) << "Hello, World" << std::endl; + * + */ +#define LOGGER(name) ::tflchef::Logger name{::tflchef::LoggingContext::get()}; + +// TODO Support FATAL, ERROR, WARN, and VERBOSE +#define INFO(name) HERMES_INFO(name) + +// WARNING! +// +// THE CURRENT IMPLEMENTATION IS NOT THREAD SAFE. +// + +#endif // __TFLCHEF_LOG_H__ diff --git a/compiler/tflchef/log/include/LoggingContext.h b/compiler/tflchef/log/include/LoggingContext.h new file mode 100644 index 000000000..860099482 --- /dev/null +++ b/compiler/tflchef/log/include/LoggingContext.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved + * + * 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. + */ + +#ifndef __TFLCHEF_LOGGING_CONTEXT_H__ +#define __TFLCHEF_LOGGING_CONTEXT_H__ + +#include <hermes.h> + +namespace tflchef +{ + +/** + * @brief Global logging context + */ +struct LoggingContext +{ + static hermes::Context *get(void); +}; + +} // namespace tflchef + +#endif // __TFLCHEF_LOGGING_CONTEXT_H__ diff --git a/compiler/tflchef/log/src/Log.cpp b/compiler/tflchef/log/src/Log.cpp new file mode 100644 index 000000000..62c377745 --- /dev/null +++ b/compiler/tflchef/log/src/Log.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved + * + * 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. + */ + +#include "Log.h" + +#include <cassert> +#include <cstdlib> +#include <iostream> + +// TODO Extract these lexical conversion routines as a library +namespace +{ + +/** + * @brief Convert C-string as a value of type T + * + * safecast(s, v) returns v if s is nullptr. + */ +template <typename T> T safecast(const char *, const T &); + +template <> bool safecast<bool>(const char *s, const bool &value) +{ + return (s == nullptr) ? value : (std::stoi(s) != 0); +} + +} // namespace + +// +// Logger +// +namespace tflchef +{ + +Logger::Logger(hermes::Context *ctx) { activate(ctx->sources(), ctx->bus()); } +Logger::~Logger() { deactivate(); } + +} // namespace tflchef + +// +// LoggerConfig +// +namespace tflchef +{ + +LoggerConfig::LoggerConfig() +{ + // Turn on logging if TFLCHEF_LOG is set as non-zero value + _enabled = safecast<bool>(std::getenv("TFLCHEF_LOG"), false); +} + +void LoggerConfig::configure(const hermes::Source *source, hermes::Source::Setting &setting) const +{ + // Let's ignore hermes::Sources if that is not a moco logger + if (auto logger = dynamic_cast<const Logger *>(source)) + { + configure(logger, setting); + } +} + +void LoggerConfig::configure(const Logger *, hermes::Source::Setting &setting) const +{ + if (_enabled) + { + // Enable all catagories + setting.accept_all(); + } + else + { + // Disable all catagories + setting.reject_all(); + } +} + +} // namespace tflchef diff --git a/compiler/tflchef/log/src/LoggingContext.cpp b/compiler/tflchef/log/src/LoggingContext.cpp new file mode 100644 index 000000000..0514dc38c --- /dev/null +++ b/compiler/tflchef/log/src/LoggingContext.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved + * + * 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. + */ + +#include "LoggingContext.h" +#include "Log.h" + +#include <hermes/ConsoleReporter.h> + +#include <memory> + +namespace tflchef +{ + +hermes::Context *LoggingContext::get(void) +{ + static hermes::Context *ctx = nullptr; + + if (ctx == nullptr) + { + ctx = new hermes::Context; + ctx->sinks()->append(std::make_unique<hermes::ConsoleReporter>()); + ctx->config(std::make_unique<LoggerConfig>()); + } + + return ctx; +} + +} // namespace tflchef |