Skip to content

Commit f82aaad

Browse files
smilesa-maurice
authored andcommitted
Forward C++ API-wide log verbosity to platform specific SDK logger.
In addition, the logger now logs to the MSVC debug window. PiperOrigin-RevId: 252896509
1 parent bce0a4d commit f82aaad

File tree

5 files changed

+79
-20
lines changed

5 files changed

+79
-20
lines changed

app/src/log.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ static void LogMessageWithCallbackV(LogLevel log_level, const char* format,
150150

151151
void SetLogLevel(LogLevel level) {
152152
g_log_level = level;
153+
LogSetPlatformLevel(level);
153154
}
154155

155156
LogLevel GetLogLevel() { return g_log_level; }

app/src/log.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ LogLevel GetLogLevel();
3838
FIREBASE_DEPRECATED void LogSetLevel(LogLevel level);
3939
// Use firebase::GetLogLevel() instead.
4040
FIREBASE_DEPRECATED LogLevel LogGetLevel();
41+
// Set the platform specific SDK log level.
42+
// This is called internally by LogSetLevel().
43+
void LogSetPlatformLevel(LogLevel level);
4144
// Log a debug message to the system log.
4245
void LogDebug(const char* format, ...);
4346
// Log an info message to the system log.

app/src/log_android.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ void AndroidLogMessageV(int priority, const char* tag, const char* format,
4545
__android_log_vprint(priority, tag, format, args);
4646
}
4747

48+
// Set the platform specific SDK log level.
49+
void LogSetPlatformLevel(LogLevel level) {
50+
// This isn't available on Android, instead logs go through the framework's
51+
// android.util.Log. Some modules, like Analytics and Realtime Database,
52+
// have their own custom logging which are enabled via system configuration
53+
// variables or module-specific API calls.
54+
(void)level;
55+
}
56+
4857
// Log a firebase message.
4958
void LogMessageV(LogLevel log_level, const char* format, va_list args) {
5059
switch (log_level) {

app/src/log_ios.mm

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,28 @@
1818

1919
#import <Foundation/Foundation.h>
2020

21+
#import "FIRLoggerLevel.h"
22+
2123
#include <stdarg.h>
2224

25+
// Private API to set the logger level in Firebase/Core/Private/FIRLogger.h.
26+
extern "C" void FIRSetLoggerLevel(FIRLoggerLevel loggerLevel);
27+
2328
namespace firebase {
2429

2530
// Key used in Info.plist to configure the log level before a load of code is executed.
2631
static NSString* kLogLevelInfoPlistKey = @"FIRCPPLogLevel";
2732

33+
// Maps C++ to iOS SDK log levels.
34+
static const FIRLoggerLevel kCppToIOSLogLevel[] = {
35+
FIRLoggerLevelDebug, // kLogLevelVerbose = 0,
36+
FIRLoggerLevelDebug, // kLogLevelDebug,
37+
FIRLoggerLevelInfo, // kLogLevelInfo,
38+
FIRLoggerLevelWarning, // kLogLevelWarning,
39+
FIRLoggerLevelError, // kLogLevelError,
40+
FIRLoggerLevelError, // kLogLevelAssert,
41+
};
42+
2843
// Initialize the logging system.
2944
void LogInitialize() {
3045
static bool read_log_level_from_plist = false;
@@ -38,6 +53,14 @@ void LogInitialize() {
3853
LogSetLevel(static_cast<LogLevel>(log_level_value));
3954
}
4055
}
56+
// Synchronize the platform logger with the C++ log level.
57+
LogSetPlatformLevel(LogGetLevel());
58+
}
59+
60+
// Set the platform specific SDK log level.
61+
void LogSetPlatformLevel(LogLevel level) {
62+
assert(level < sizeof(kCppToIOSLogLevel) / sizeof(kCppToIOSLogLevel[0]));
63+
FIRSetLoggerLevel(kCppToIOSLogLevel[level]);
4164
}
4265

4366
// Log a firebase message.
@@ -64,5 +87,4 @@ void LogMessageV(LogLevel log_level, const char* format, va_list args) {
6487
}
6588
}
6689

67-
6890
} // namespace firebase

app/src/log_stdio.cc

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,65 @@
1616

1717
#include "app/src/log.h"
1818

19+
#include <assert.h>
1920
#include <stdarg.h>
2021
#include <stdio.h>
22+
#include <string.h>
23+
24+
#ifdef _WIN32
25+
#include <Windows.h>
26+
#endif // _WIN32
27+
28+
#include "app/src/mutex.h"
29+
#include "app/src/include/firebase/internal/platform.h"
2130

2231
#if !defined(FIREBASE_NAMESPACE)
2332
#define FIREBASE_NAMESPACE firebase
2433
#endif
2534

2635
namespace FIREBASE_NAMESPACE {
2736

37+
// Prefix for log messages at each level.
38+
static const char* kLogLevelPrefix[] = {
39+
"VERBOSE: ", // kLogLevelVerbose = 0,
40+
"DEBUG: ", // kLogLevelDebug,
41+
"INFO: ", // kLogLevelInfo,
42+
"WARNING: ", // kLogLevelWarning,
43+
"ERROR: ", // kLogLevelError,
44+
"ASSERT: ", // kLogLevelAssert,
45+
};
46+
47+
#ifdef _WIN32
48+
// Guards the log buffer on Windows.
49+
static Mutex g_log_mutex; // NOLINT
50+
#endif // _WIN32
51+
2852
// Initializes the logging module.
2953
void LogInitialize() {}
3054

55+
// Set the platform specific SDK log level.
56+
void LogSetPlatformLevel(LogLevel level) {}
57+
3158
// Log a firebase message.
3259
void LogMessageV(LogLevel log_level, const char* format, va_list args) {
33-
switch (log_level) {
34-
case kLogLevelVerbose:
35-
printf("VERBOSE: ");
36-
break;
37-
case kLogLevelDebug:
38-
printf("DEBUG: ");
39-
break;
40-
case kLogLevelInfo:
41-
break;
42-
case kLogLevelWarning:
43-
printf("WARNING: ");
44-
break;
45-
case kLogLevelError:
46-
printf("ERROR: ");
47-
break;
48-
case kLogLevelAssert:
49-
printf("ASSERT: ");
50-
break;
51-
}
60+
assert(log_level < (sizeof(kLogLevelPrefix) / sizeof(kLogLevelPrefix[0])));
61+
const char* prefix = kLogLevelPrefix[log_level];
62+
printf("%s", prefix);
5263
vprintf(format, args);
5364
printf("\n");
65+
// Platform specific logging.
66+
#if FIREBASE_PLATFORM_WINDOWS
67+
{
68+
MutexLock lock(g_log_mutex);
69+
static char log_buffer[1024];
70+
size_t prefix_length = strlen(prefix);
71+
strcpy(log_buffer, prefix); // // NOLINT
72+
vsnprintf(log_buffer + prefix_length,
73+
sizeof(log_buffer) - 1 - prefix_length, format, args); // NOLINT
74+
log_buffer[sizeof(log_buffer) - 1] = '\0';
75+
OutputDebugString(log_buffer);
76+
}
77+
#endif // FIREBASE_PLATFORM_WINDOWS
5478
}
5579

5680
// NOLINTNEXTLINE - allow namespace overridden

0 commit comments

Comments
 (0)