Skip to content

Commit d449b82

Browse files
fix: android log
1 parent 1347925 commit d449b82

3 files changed

Lines changed: 41 additions & 25 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules
55
dist
66
.lldb_cmds
77
output.log
8+
/test

src/core/bindings/console.cc

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
#include <fmt/color.h>
55
#include <fmt/core.h>
66
#include <unordered_map>
7+
#include <vector>
8+
9+
#ifdef __ANDROID__
10+
#include <android/log.h>
11+
#endif
712

813
namespace {
914
thread_local int current_group_depth = 0;
@@ -14,43 +19,52 @@ thread_local std::unordered_map<std::string, unsigned long long> counters;
1419

1520
std::string get_indent() { return std::string(current_group_depth * 2, ' '); }
1621

17-
void print_styled(FILE *out, const std::string &prefix,
22+
void print_styled(bool is_stderr, const std::string &prefix,
1823
const std::string &message,
1924
fmt::text_style style = fmt::text_style()) {
20-
fmt::print(out, style, "{}{}{}\n", get_indent(), prefix, message);
25+
auto res = fmt::format(style, "{}{}{}\n", get_indent(), prefix, message);
26+
#ifdef _WIN32
27+
OutputDebugStringA(res.c_str());
28+
fmt::print(is_stderr ? stderr : stdout, "{}", res);
29+
#elif defined(__ANDROID__)
30+
__android_log_write(is_stderr ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO,
31+
"chromatic", res.c_str());
32+
#else
33+
fmt::print(is_stderr ? stderr : stdout, "{}", res);
34+
#endif
2135
}
2236
} // namespace
2337

2438
namespace chromatic::js {
2539

2640
void console::log(const std::string &message) {
27-
print_styled(stdout, "", message);
41+
print_styled(false, "", message);
2842
}
2943

3044
void console::error(const std::string &message) {
31-
print_styled(stderr, "", message,
45+
print_styled(true, "", message,
3246
fmt::fg(fmt::color::red) | fmt::emphasis::bold);
3347
}
3448

3549
void console::warn(const std::string &message) {
36-
print_styled(stderr, "", message,
50+
print_styled(true, "", message,
3751
fmt::fg(fmt::color::yellow) | fmt::emphasis::bold);
3852
}
3953

4054
void console::info(const std::string &message) {
41-
print_styled(stdout, "", message, fmt::fg(fmt::color::cyan));
55+
print_styled(false, "", message, fmt::fg(fmt::color::cyan));
4256
}
4357

4458
void console::debug(const std::string &message) {
45-
print_styled(stdout, "", message, fmt::fg(fmt::color::gray));
59+
print_styled(false, "", message, fmt::fg(fmt::color::gray));
4660
}
4761

4862
void console::trace(const std::string &message) {
49-
print_styled(stdout, "Trace: ", message, fmt::fg(fmt::color::magenta));
63+
print_styled(false, "Trace: ", message, fmt::fg(fmt::color::magenta));
5064
}
5165

5266
void console::group(const std::string &message) {
53-
print_styled(stdout, "", message, fmt::emphasis::bold);
67+
print_styled(false, "", message, fmt::emphasis::bold);
5468
current_group_depth++;
5569
}
5670

@@ -84,7 +98,7 @@ void console::timeEnd(const std::string &message) {
8498
auto end = std::chrono::high_resolution_clock::now();
8599
auto duration =
86100
std::chrono::duration<double, std::milli>(end - it->second).count();
87-
print_styled(stdout, "", fmt::format("{}: {} ms", label, duration));
101+
print_styled(false, "", fmt::format("{}: {} ms", label, duration));
88102
timers.erase(it);
89103
}
90104

@@ -98,13 +112,13 @@ void console::timeLog(const std::string &message) {
98112
auto end = std::chrono::high_resolution_clock::now();
99113
auto duration =
100114
std::chrono::duration<double, std::milli>(end - it->second).count();
101-
print_styled(stdout, "", fmt::format("{}: {} ms", label, duration));
115+
print_styled(false, "", fmt::format("{}: {} ms", label, duration));
102116
}
103117

104118
void console::count(const std::string &message) {
105119
const std::string label = message.empty() ? "default" : message;
106120
auto val = ++counters[label];
107-
print_styled(stdout, "", fmt::format("{}: {}", label, val));
121+
print_styled(false, "", fmt::format("{}: {}", label, val));
108122
}
109123

110124
void console::countReset(const std::string &message) {
@@ -119,26 +133,26 @@ void console::dir(const std::string &message) { log(message); }
119133
void console::dirxml(const std::string &message) { log(message); }
120134

121135
void console::profile(const std::string &message) {
122-
print_styled(stdout, "Profile: ", message + " started",
136+
print_styled(false, "Profile: ", message + " started",
123137
fmt::fg(fmt::color::light_green));
124138
}
125139

126140
void console::profileEnd(const std::string &message) {
127-
print_styled(stdout, "Profile: ", message + " ended",
141+
print_styled(false, "Profile: ", message + " ended",
128142
fmt::fg(fmt::color::light_green));
129143
}
130144

131145
void console::timeStamp(const std::string &message) {
132-
print_styled(stdout, "TimeStamp: ", message, fmt::fg(fmt::color::light_blue));
146+
print_styled(false, "TimeStamp: ", message, fmt::fg(fmt::color::light_blue));
133147
}
134148

135149
void console::timeline(const std::string &message) {
136-
print_styled(stdout, "Timeline: ", message + " started",
150+
print_styled(false, "Timeline: ", message + " started",
137151
fmt::fg(fmt::color::light_blue));
138152
}
139153

140154
void console::timelineEnd(const std::string &message) {
141-
print_styled(stdout, "Timeline: ", message + " ended",
155+
print_styled(false, "Timeline: ", message + " ended",
142156
fmt::fg(fmt::color::light_blue));
143157
}
144158

src/core/typescript/src/process.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NativeProcess as NP } from 'chromatic';
22
import { NativePointer } from './native-pointer';
33
import type { ModuleInfo, RangeInfo } from './types';
4+
import { Module } from './module';
45

56
/**
67
* Process — provides information about the current process.
@@ -78,34 +79,34 @@ export const Process = {
7879
* Find the module that contains the given address.
7980
*
8081
* @param address - A memory address to look up.
81-
* @returns {@link ModuleInfo} if found, or `null`.
82+
* @returns {@link Module} if found, or `null`.
8283
*/
83-
findModuleByAddress(address: NativePointer | string): ModuleInfo | null {
84+
findModuleByAddress(address: NativePointer | string): Module | null {
8485
const ptr = new NativePointer(address);
8586
const m = NP.findModuleByAddress(ptr.toString());
8687
if (!m) return null;
87-
return {
88+
return new Module({
8889
name: m.name,
8990
base: new NativePointer(m.base),
9091
size: m.size,
9192
path: m.path
92-
};
93+
});
9394
},
9495

9596
/**
9697
* Find a loaded module by its short name.
9798
*
9899
* @param name - Module name (e.g. `"libSystem.B.dylib"`).
99-
* @returns {@link ModuleInfo} if found, or `null`.
100+
* @returns {@link Module} if found, or `null`.
100101
*/
101-
findModuleByName(name: string): ModuleInfo | null {
102+
findModuleByName(name: string): Module | null {
102103
const m = NP.findModuleByName(name);
103104
if (!m) return null;
104-
return {
105+
return new Module({
105106
name: m.name,
106107
base: new NativePointer(m.base),
107108
size: m.size,
108109
path: m.path
109-
};
110+
});
110111
}
111112
};

0 commit comments

Comments
 (0)