Skip to content

Commit a16308d

Browse files
committed
feat: add in the Setlogcallback function for desktop analytics
1 parent 4263601 commit a16308d

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

analytics/integration_test/src/integration_test.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,25 @@ TEST_F(FirebaseAnalyticsTest, TestSetProperties) {
242242
InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber(hashed_phone);
243243
}
244244

245+
#if defined(_WIN32)
246+
TEST_F(FirebaseAnalyticsTest, TestSetLogCallback) {
247+
bool log_callback_called = false;
248+
firebase::analytics::SetLogCallback(
249+
[&](firebase::analytics::LogLevel log_level, const char* message) {
250+
log_callback_called = true;
251+
});
252+
// Log an event with an invalid parameter to trigger a log message.
253+
const firebase::analytics::Parameter kInvalidParameters[] = {
254+
firebase::analytics::Parameter("invalid_character_!", 5),
255+
};
256+
firebase::analytics::LogEvent(
257+
"invalid_event", kInvalidParameters,
258+
sizeof(kInvalidParameters) / sizeof(kInvalidParameters[0]));
259+
EXPECT_TRUE(log_callback_called);
260+
firebase::analytics::SetLogCallback(nullptr);
261+
}
262+
#endif // defined(_WIN32)
263+
245264
TEST_F(FirebaseAnalyticsTest, TestLogEvents) {
246265
// Log an event with no parameters.
247266
firebase::analytics::LogEvent(firebase::analytics::kEventLogin);

analytics/src/analytics_android.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ void ResetAnalyticsData() {
609609
util::CheckAndClearJniExceptions(env);
610610
}
611611

612+
// NO-OP in Android and iOS. Only used in Windows.
613+
void SetLogCallback(const LogCallback&) {}
614+
612615
// NO-OP in Android and iOS. Only used in Windows.
613616
void NotifyAppLifecycleChange(AppLifecycleState) {}
614617

analytics/src/analytics_desktop.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <map>
16+
#include <mutex>
1617
#include <sstream>
1718
#include <string>
1819
#include <vector>
@@ -44,6 +45,8 @@ static HMODULE g_analytics_module = 0;
4445
// Future data for analytics.
4546
// This is initialized in `Initialize()` and cleaned up in `Terminate()`.
4647
static bool g_initialized = false;
48+
static LogCallback g_log_callback;
49+
static std::mutex g_log_callback_mutex;
4750
static int g_fake_instance_id = 0;
4851
static bool g_analytics_collection_enabled = true;
4952
static std::string g_app_id;
@@ -385,6 +388,41 @@ void ResetAnalyticsData() {
385388
g_fake_instance_id++;
386389
}
387390

391+
// C-style callback that will be passed to the Google Analytics C API.
392+
static void GoogleAnalyticsWraperLogCallback(GoogleAnalytics_LogLevel log_level,
393+
const char* message) {
394+
if (g_log_callback) {
395+
LogLevel sdk_log_level;
396+
switch (log_level) {
397+
case kDebug:
398+
sdk_log_level = kLogLevelDebug;
399+
break;
400+
case kInfo:
401+
sdk_log_level = kLogLevelInfo;
402+
break;
403+
case kWarning:
404+
sdk_log_level = kLogLevelWarning;
405+
break;
406+
case kError:
407+
sdk_log_level = kLogLevelError;
408+
break;
409+
default:
410+
sdk_log_level = kLogLevelInfo;
411+
}
412+
g_log_callback(sdk_log_level, message);
413+
}
414+
}
415+
416+
// Allows the passing of a callback to be used when the SDK logs any
417+
// messages regarding its behavior. The callback must be thread-safe.
418+
void SetLogCallback(const LogCallback& callback) {
419+
FIREBASE_ASSERT_RETURN_VOID(internal::IsInitialized());
420+
// The C API does not support user data, so we must use a global variable.
421+
std::lock_guard<std::mutex> lock(g_log_callback_mutex);
422+
g_log_callback = callback;
423+
GoogleAnalytics_SetLogCallback(GoogleAnalyticsWraperLogCallback);
424+
}
425+
388426
// Notify the Analytics SDK about the current state of the app's lifecycle.
389427
void NotifyAppLifecycleChange(AppLifecycleState state) {
390428
FIREBASE_ASSERT_RETURN_VOID(internal::IsInitialized());

analytics/src/analytics_ios.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ void ResetAnalyticsData() {
379379
g_resetter->Reset();
380380
}
381381

382+
// No-Op on iOS and Android. Only used in Windows desktop apps.
383+
void SetLogCallback(const LogCallback&) {}
384+
382385
// No-Op on iOS and Android. Only used in Windows desktop apps.
383386
void NotifyAppLifecycleChange(AppLifecycleState) {}
384387

analytics/src/include/firebase/analytics.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <cstddef>
2121
#include <cstdint>
22+
#include <functional>
2223
#include <map>
2324
#include <string>
2425
#include <vector>
@@ -558,6 +559,28 @@ void SetSessionTimeoutDuration(int64_t milliseconds);
558559
/// instance id.
559560
void ResetAnalyticsData();
560561

562+
/// @brief The log level of the message logged by the SDK.
563+
enum LogLevel {
564+
kLogLevelDebug,
565+
kLogLevelInfo,
566+
kLogLevelWarning,
567+
kLogLevelError,
568+
};
569+
570+
/// @brief The callback type for logging messages from the SDK.
571+
///
572+
/// The callback is invoked whenever the SDK logs a message.
573+
///
574+
/// @param[in] log_level The log level of the message.
575+
/// @param[in] message The message logged by the SDK.
576+
using LogCallback = std::function<void(LogLevel, const char*)>;
577+
578+
/// @brief Allows the passing of a callback to be used when the SDK logs any
579+
/// messages regarding its behavior. The callback must be thread-safe.
580+
///
581+
/// @param[in] callback The callback to use. Must be thread-safe.
582+
void SetLogCallback(const LogCallback& callback);
583+
561584
/// @brief The state of an app in its lifecycle.
562585
///
563586
/// kUnknown is an invalid state that is used to capture uninitialized values.

0 commit comments

Comments
 (0)