|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef FIREBASE_APP_CLIENT_CPP_SRC_LOGGER_H_ |
| 16 | +#define FIREBASE_APP_CLIENT_CPP_SRC_LOGGER_H_ |
| 17 | + |
| 18 | +#include <stdarg.h> |
| 19 | + |
| 20 | +#include "app/src/include/firebase/log.h" |
| 21 | +#include "app/src/log.h" |
| 22 | + |
| 23 | +#if !defined(FIREBASE_NAMESPACE) |
| 24 | +#define FIREBASE_NAMESPACE firebase |
| 25 | +#endif |
| 26 | + |
| 27 | +namespace FIREBASE_NAMESPACE { |
| 28 | + |
| 29 | +// This is a base class for logger implementations. |
| 30 | +class LoggerBase { |
| 31 | + public: |
| 32 | + virtual ~LoggerBase(); |
| 33 | + |
| 34 | + // Set the log level. |
| 35 | + // |
| 36 | + // Implementations of LoggerBase are responsible for tracking the log level. |
| 37 | + virtual void SetLogLevel(LogLevel log_level) = 0; |
| 38 | + |
| 39 | + // Get the currently set log level. |
| 40 | + // |
| 41 | + // Implementations of LoggerBase are responsible for tracking the log level. |
| 42 | + virtual LogLevel GetLogLevel() const = 0; |
| 43 | + |
| 44 | + // Log a debug message to the system log. |
| 45 | + void LogDebug(const char* format, ...) const; |
| 46 | + |
| 47 | + // Log an info message to the system log. |
| 48 | + void LogInfo(const char* format, ...) const; |
| 49 | + |
| 50 | + // Log a warning to the system log. |
| 51 | + void LogWarning(const char* format, ...) const; |
| 52 | + |
| 53 | + // Log an error to the system log. |
| 54 | + void LogError(const char* format, ...) const; |
| 55 | + |
| 56 | + // Log an assert and stop the application. |
| 57 | + void LogAssert(const char* format, ...) const; |
| 58 | + |
| 59 | + // Log a firebase message via LogMessageV(). |
| 60 | + void LogMessage(LogLevel log_level, const char* format, ...) const; |
| 61 | + |
| 62 | + // Log a firebase message. |
| 63 | + void LogMessageV(LogLevel log_level, const char* format, va_list args) const; |
| 64 | + |
| 65 | + private: |
| 66 | + // Handles the filtering for any message passed to it. If the log level of the |
| 67 | + // message is greater than or equal to the log level of the logger, the |
| 68 | + // message will be passed along to LogMessageImplV. |
| 69 | + void FilterLogMessageV(LogLevel log_level, const char* format, |
| 70 | + va_list args) const; |
| 71 | + |
| 72 | + // LogMessageImplV is responsible for doing the actual logging. It should pass |
| 73 | + // along the format string and arguments to whatever library calls handle |
| 74 | + // displaying logs for the given platform. |
| 75 | + virtual void LogMessageImplV(LogLevel log_level, const char* format, |
| 76 | + va_list args) const = 0; |
| 77 | +}; |
| 78 | + |
| 79 | +// A logger that calls through to the system logger. |
| 80 | +class SystemLogger : public LoggerBase { |
| 81 | + public: |
| 82 | + ~SystemLogger() override; |
| 83 | + |
| 84 | + void SetLogLevel(LogLevel log_level) override; |
| 85 | + |
| 86 | + LogLevel GetLogLevel() const override; |
| 87 | + |
| 88 | + private: |
| 89 | + // Logs a message to the system logger. |
| 90 | + // |
| 91 | + // In Firebase we already have a whole set of wrappers around system level |
| 92 | + // logging, so this merely calls through to ::firebase::LogMessageV. |
| 93 | + void LogMessageImplV(LogLevel log_level, const char* format, |
| 94 | + va_list args) const override; |
| 95 | +}; |
| 96 | + |
| 97 | +// Logger is a general logger class that can be chained off of other loggers. It |
| 98 | +// does not actually handle displaying the logs themselves, but rather passes |
| 99 | +// the message and arguments up to its parent if the message is not filtered. |
| 100 | +// Filtering follows the standard rules: the log level of the message must be at |
| 101 | +// least as high as that of the current logger being used. This is useful for |
| 102 | +// if you want to have finer grained control over subsystems. |
| 103 | +class Logger : public LoggerBase { |
| 104 | + public: |
| 105 | + explicit Logger(const LoggerBase* parent_logger) |
| 106 | + : parent_logger_(parent_logger), log_level_(kDefaultLogLevel) {} |
| 107 | + Logger(const LoggerBase* parent_logger, LogLevel log_level) |
| 108 | + : parent_logger_(parent_logger), log_level_(log_level) {} |
| 109 | + |
| 110 | + ~Logger() override; |
| 111 | + |
| 112 | + void SetLogLevel(LogLevel log_level) override; |
| 113 | + |
| 114 | + LogLevel GetLogLevel() const override; |
| 115 | + |
| 116 | + private: |
| 117 | + // Passes messages to the parent logger to be displayed. |
| 118 | + void LogMessageImplV(LogLevel log_level, const char* format, |
| 119 | + va_list args) const override; |
| 120 | + |
| 121 | + const LoggerBase* parent_logger_; |
| 122 | + LogLevel log_level_; |
| 123 | +}; |
| 124 | + |
| 125 | +} // namespace FIREBASE_NAMESPACE |
| 126 | + |
| 127 | +#endif // FIREBASE_APP_CLIENT_CPP_SRC_LOGGER_H_ |
0 commit comments