Skip to content

Commit b173d84

Browse files
amabluea-maurice
authored andcommitted
Added a logger class that supports logging to the system logger, or a filtered logger that can be chained off other loggers (including the system logger).
PiperOrigin-RevId: 266273967
1 parent 9436947 commit b173d84

File tree

7 files changed

+261
-4
lines changed

7 files changed

+261
-4
lines changed

app/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ build_flatbuffers("${CMAKE_CURRENT_LIST_DIR}/google_services.fbs"
8484

8585
set(log_common_SRCS
8686
src/log.cc
87-
set/log.h)
87+
set/log.h
88+
src/logger.cc
89+
src/logger.h)
8890
set(log_common_HDRS)
8991
set(log_android_SRCS
9092
src/jobject_reference.cc

app/src/app_common.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,18 @@ const char* kCpuArchitecture = "x86";
147147

148148
const char* kApiClientHeader = "x-goog-api-client";
149149

150+
SystemLogger g_system_logger; // NOLINT
151+
150152
// Private cross platform data associated with an app.
151153
struct AppData {
154+
AppData() : logger(&g_system_logger) {}
155+
152156
// App associated with this data.
153157
App* app;
154158
// Notifies subscribers when the app is about to be destroyed.
155159
CleanupNotifier cleanup_notifier;
160+
// A per-app logger.
161+
Logger logger;
156162
};
157163

158164
// Tracks library registrations.
@@ -244,7 +250,7 @@ App* AddApp(App* app, std::map<std::string, InitResult>* results) {
244250
g_default_app = app;
245251
created_first_app = true;
246252
}
247-
UniquePtr<AppData> app_data(new AppData);
253+
UniquePtr<AppData> app_data = MakeUnique<AppData>();
248254
app_data->app = app;
249255
app_data->cleanup_notifier.RegisterOwner(app);
250256
if (!g_apps) g_apps = new std::map<std::string, UniquePtr<AppData>>();
@@ -431,6 +437,18 @@ void GetOuterMostSdkAndVersion(std::string* sdk, std::string* version) {
431437
}
432438
}
433439

440+
// Find a logger associated with an app by app name.
441+
Logger* FindAppLoggerByName(const char* name) {
442+
assert(name);
443+
MutexLock lock(g_app_mutex);
444+
if (g_apps) {
445+
auto it = g_apps->find(std::string(name));
446+
if (it == g_apps->end()) return nullptr;
447+
return &it->second->logger;
448+
}
449+
return nullptr;
450+
}
451+
434452
} // namespace app_common
435453
// NOLINTNEXTLINE - allow namespace overridden
436454
} // namespace FIREBASE_NAMESPACE

app/src/app_common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <string>
2424

2525
#include "app/src/include/firebase/app.h"
26+
#include "app/src/logger.h"
2627

2728
#if !defined(FIREBASE_NAMESPACE)
2829
#define FIREBASE_NAMESPACE firebase
@@ -95,6 +96,9 @@ std::string GetLibraryVersion(const char* library);
9596
// version is the version of the SDK respective SDK.
9697
void GetOuterMostSdkAndVersion(std::string* sdk, std::string* version);
9798

99+
// Find a logger associated with an app by app name.
100+
Logger* FindAppLoggerByName(const char* name);
101+
98102
} // namespace app_common
99103
// NOLINTNEXTLINE - allow namespace overridden
100104
} // namespace FIREBASE_NAMESPACE

app/src/log.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <stdarg.h>
2020
#include <stdio.h>
21+
2122
#include <cstdlib>
2223

2324
#include "app/src/assert.h"
@@ -45,9 +46,9 @@ static void DefaultLogCallback(LogLevel log_level, const char* message,
4546
void* /*callback_data*/);
4647

4748
#if FIREBASE_LOG_DEBUG
48-
static const LogLevel kDefaultLogLevel = kLogLevelDebug;
49+
const LogLevel kDefaultLogLevel = kLogLevelDebug;
4950
#else
50-
static const LogLevel kDefaultLogLevel = kLogLevelInfo;
51+
const LogLevel kDefaultLogLevel = kLogLevelInfo;
5152
#endif // FIREBASE_LOG_DEBUG
5253

5354
LogLevel g_log_level = kDefaultLogLevel;

app/src/log.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
namespace FIREBASE_NAMESPACE {
3030

31+
extern const LogLevel kDefaultLogLevel;
32+
3133
// Common log methods.
3234

3335
// All messages at or above the specified log level value are displayed.

app/src/logger.cc

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
#include "app/src/logger.h"
16+
17+
#include "app/src/log.h"
18+
19+
namespace FIREBASE_NAMESPACE {
20+
21+
LoggerBase::~LoggerBase() {}
22+
23+
void LoggerBase::LogDebug(const char* format, ...) const {
24+
va_list list;
25+
va_start(list, format);
26+
FilterLogMessageV(kLogLevelDebug, format, list);
27+
va_end(list);
28+
}
29+
30+
void LoggerBase::LogInfo(const char* format, ...) const {
31+
va_list list;
32+
va_start(list, format);
33+
FilterLogMessageV(kLogLevelInfo, format, list);
34+
va_end(list);
35+
}
36+
37+
void LoggerBase::LogWarning(const char* format, ...) const {
38+
va_list list;
39+
va_start(list, format);
40+
FilterLogMessageV(kLogLevelWarning, format, list);
41+
va_end(list);
42+
}
43+
44+
void LoggerBase::LogError(const char* format, ...) const {
45+
va_list list;
46+
va_start(list, format);
47+
FilterLogMessageV(kLogLevelError, format, list);
48+
va_end(list);
49+
}
50+
51+
void LoggerBase::LogAssert(const char* format, ...) const {
52+
va_list list;
53+
va_start(list, format);
54+
FilterLogMessageV(kLogLevelAssert, format, list);
55+
va_end(list);
56+
}
57+
58+
void LoggerBase::LogMessage(LogLevel log_level, const char* format, ...) const {
59+
va_list list;
60+
va_start(list, format);
61+
FilterLogMessageV(log_level, format, list);
62+
va_end(list);
63+
}
64+
65+
void LoggerBase::LogMessageV(LogLevel log_level, const char* format,
66+
va_list args) const {
67+
FilterLogMessageV(log_level, format, args);
68+
}
69+
70+
void LoggerBase::FilterLogMessageV(LogLevel log_level, const char* format,
71+
va_list args) const {
72+
if (log_level >= this->GetLogLevel()) {
73+
LogMessageImplV(log_level, format, args);
74+
}
75+
}
76+
77+
SystemLogger::~SystemLogger() {}
78+
79+
void SystemLogger::SetLogLevel(LogLevel log_level) {
80+
::FIREBASE_NAMESPACE::SetLogLevel(log_level);
81+
}
82+
83+
LogLevel SystemLogger::GetLogLevel() const {
84+
return ::FIREBASE_NAMESPACE::GetLogLevel();
85+
}
86+
87+
void SystemLogger::LogMessageImplV(LogLevel log_level, const char* format,
88+
va_list args) const {
89+
::FIREBASE_NAMESPACE::LogMessageV(log_level, format, args);
90+
}
91+
92+
Logger::~Logger() {}
93+
94+
void Logger::SetLogLevel(LogLevel log_level) { log_level_ = log_level; }
95+
96+
LogLevel Logger::GetLogLevel() const { return log_level_; }
97+
98+
void Logger::LogMessageImplV(LogLevel log_level, const char* format,
99+
va_list args) const {
100+
parent_logger_->LogMessageV(log_level, format, args);
101+
}
102+
103+
} // namespace FIREBASE_NAMESPACE

app/src/logger.h

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)