Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 10b301da authored by Sam Leffler's avatar Sam Leffler
Browse files

add C wrapper for libmetrics

TEST=unit tests + build&test crosmetrics plugin for flimflam

Review URL: http://codereview.chromium.org/2832008
parent 1bb904ed
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ TESTDAEMON_OBJS = \
	metrics_daemon.o \
	metrics_daemon_test.o
LIB_OBJS = \
	c_metrics_library.o \
	metrics_library.o
TESTLIB_OBJS = \
	metrics_library.o \
+44 −0
Original line number Diff line number Diff line
// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//
// C wrapper to libmetrics
//

#include "c_metrics_library.h"
#include "metrics_library.h"

extern "C" CMetricsLibrary CMetricsLibraryNew(void) {
  MetricsLibrary* lib = new MetricsLibrary;
  return reinterpret_cast<CMetricsLibrary>(lib);
}

extern "C" void CMetricsLibraryDelete(CMetricsLibrary handle) {
  MetricsLibrary* lib = reinterpret_cast<MetricsLibrary*>(handle);
  delete lib;
}

extern "C" void CMetricsLibraryInit(CMetricsLibrary handle) {
  MetricsLibrary* lib = reinterpret_cast<MetricsLibrary*>(handle);
  if (lib != NULL)
    lib->Init();
}

extern "C" int CMetricsLibrarySendToUMA(CMetricsLibrary handle,
					const char* name, int sample,
					int min, int max, int nbuckets) {
  MetricsLibrary* lib = reinterpret_cast<MetricsLibrary*>(handle);
  if (lib == NULL)
    return 0;
  return lib->SendToUMA(std::string(name), sample, min, max, nbuckets);
}

extern "C" int CMetricsLibrarySendEnumToUMA(CMetricsLibrary handle,
					    const char* name, int sample,
					    int max) {
  MetricsLibrary* lib = reinterpret_cast<MetricsLibrary*>(handle);
  if (lib == NULL)
    return 0;
  return lib->SendEnumToUMA(std::string(name), sample, max);
}
+33 −0
Original line number Diff line number Diff line
// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef C_METRICS_LIBRARY_H_
#define C_METRICS_LIBRARY_H_

#if defined(__cplusplus)
extern "C" {
#endif
typedef struct CMetricsLibraryOpaque* CMetricsLibrary;

// C wrapper for MetricsLibrary::MetricsLibrary.
CMetricsLibrary CMetricsLibraryNew(void);

// C wrapper for MetricsLibrary::~MetricsLibrary.
void CMetricsLibraryDelete(CMetricsLibrary handle);

// C wrapper for MetricsLibrary::Init.
void CMetricsLibraryInit(CMetricsLibrary handle);

// C wrapper for MetricsLibrary::SendToUMA.
int CMetricsLibrarySendToUMA(CMetricsLibrary handle,
			     const char* name, int sample,
			     int min, int max, int nbuckets);

// C wrapper for MetricsLibrary::SendEnumToUMA.
int CMetricsLibrarySendEnumToUMA(CMetricsLibrary handle,
				 const char* name, int sample, int max);
#if defined(__cplusplus)
}
#endif
#endif	// C_METRICS_LIBRARY_H_
+1 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ class MetricsLibrary : public MetricsLibraryInterface {
  static bool SendToAutotest(const std::string& name, int value);

 private:
  friend class CMetricsLibraryTest;
  friend class MetricsLibraryTest;
  FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
  FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
+45 −2
Original line number Diff line number Diff line
@@ -2,13 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "metrics_library.h"

#include <cstring>

#include <base/file_util.h>
#include <gtest/gtest.h>

#include "c_metrics_library.h"
#include "metrics_library.h"

static const FilePath kTestUMAEventsFile("test-uma-events");

class MetricsLibraryTest : public testing::Test {
@@ -83,6 +84,48 @@ TEST_F(MetricsLibraryTest, SendToUMA) {
  EXPECT_EQ(0, memcmp(exp, buf, kLen));
}

class CMetricsLibraryTest : public testing::Test {
 protected:
  virtual void SetUp() {
    lib_ = CMetricsLibraryNew();
    MetricsLibrary& ml = *reinterpret_cast<MetricsLibrary*>(lib_);
    EXPECT_EQ(NULL, ml.uma_events_file_);
    CMetricsLibraryInit(lib_);
    EXPECT_TRUE(NULL != ml.uma_events_file_);
    ml.uma_events_file_ = kTestUMAEventsFile.value().c_str();
  }

  virtual void TearDown() {
    CMetricsLibraryDelete(lib_);
    file_util::Delete(kTestUMAEventsFile, false);
  }

  CMetricsLibrary lib_;
};

TEST_F(CMetricsLibraryTest, SendEnumToUMA) {
  char buf[100];
  const int kLen = 40;
  EXPECT_TRUE(CMetricsLibrarySendEnumToUMA(lib_, "Test.EnumMetric", 1, 3));
  EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100));

  char exp[kLen];
  sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3",
          kLen, 0, 0, 0, 0);
  EXPECT_EQ(0, memcmp(exp, buf, kLen));
}

TEST_F(CMetricsLibraryTest, SendToUMA) {
  char buf[100];
  const int kLen = 37;
  EXPECT_TRUE(CMetricsLibrarySendToUMA(lib_, "Test.Metric", 2, 1, 100, 50));
  EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100));

  char exp[kLen];
  sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0);
  EXPECT_EQ(0, memcmp(exp, buf, kLen));
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();