Coverage Report

Created: 2025-06-24 06:43

/src/hermes/lib/Platform/Logging.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
#include "hermes/Platform/Logging.h"
9
10
#ifdef __ANDROID__
11
#include <android/log.h>
12
#elif defined(__APPLE__)
13
#include <os/log.h>
14
#include <stdio.h>
15
#else
16
#include <cstdio>
17
#endif
18
19
#include <cstdarg>
20
#include <memory>
21
22
namespace hermes {
23
24
0
void hermesLog(const char *componentName, const char *fmt, ...) {
25
0
  va_list args;
26
0
  va_start(args, fmt);
27
#ifdef __ANDROID__
28
  __android_log_vprint(ANDROID_LOG_INFO, componentName, fmt, args);
29
#elif defined(__APPLE__)
30
  static os_log_t hermesLogger = os_log_create("dev.hermesengine", "Default");
31
  // Need to make a copy in order to do the vsprintf trick.
32
  va_list argsCopy;
33
  va_copy(argsCopy, args);
34
  int numChars = vsnprintf(nullptr, 0, fmt, argsCopy);
35
  va_end(argsCopy);
36
  std::unique_ptr<char[]> buffer{new char[numChars + 1]};
37
  int numCharsWritten = vsnprintf(buffer.get(), numChars + 1, fmt, args);
38
  (void)numCharsWritten;
39
  // Log all messages as public here. Hermes will never log private information.
40
  os_log_with_type(
41
      hermesLogger,
42
      OS_LOG_TYPE_INFO,
43
      "%{public}s: %{public}s",
44
      componentName,
45
      buffer.get());
46
#else
47
0
  fprintf(stderr, "%s: ", componentName);
48
0
  vfprintf(stderr, fmt, args);
49
0
  fprintf(stderr, "\n");
50
0
#endif
51
0
  va_end(args);
52
0
}
53
54
} // namespace hermes