|
16 | 16 |
|
17 | 17 | #include "app/src/log.h" |
18 | 18 |
|
| 19 | +#include <assert.h> |
19 | 20 | #include <stdarg.h> |
20 | 21 | #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" |
21 | 30 |
|
22 | 31 | #if !defined(FIREBASE_NAMESPACE) |
23 | 32 | #define FIREBASE_NAMESPACE firebase |
24 | 33 | #endif |
25 | 34 |
|
26 | 35 | namespace FIREBASE_NAMESPACE { |
27 | 36 |
|
| 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 | + |
28 | 52 | // Initializes the logging module. |
29 | 53 | void LogInitialize() {} |
30 | 54 |
|
| 55 | +// Set the platform specific SDK log level. |
| 56 | +void LogSetPlatformLevel(LogLevel level) {} |
| 57 | + |
31 | 58 | // Log a firebase message. |
32 | 59 | 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); |
52 | 63 | vprintf(format, args); |
53 | 64 | 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 |
54 | 78 | } |
55 | 79 |
|
56 | 80 | // NOLINTNEXTLINE - allow namespace overridden |
|
0 commit comments