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
813namespace {
914thread_local int current_group_depth = 0 ;
@@ -14,43 +19,52 @@ thread_local std::unordered_map<std::string, unsigned long long> counters;
1419
1520std::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
2438namespace chromatic ::js {
2539
2640void console::log (const std::string &message) {
27- print_styled (stdout , " " , message);
41+ print_styled (false , " " , message);
2842}
2943
3044void 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
3549void 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
4054void 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
4458void 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
4862void 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
5266void 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
104118void 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
110124void console::countReset (const std::string &message) {
@@ -119,26 +133,26 @@ void console::dir(const std::string &message) { log(message); }
119133void console::dirxml (const std::string &message) { log (message); }
120134
121135void 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
126140void 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
131145void 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
135149void 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
140154void 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
0 commit comments