1 /*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "log.h"
18
19 #include <fmt/color.h>
20 #include <fmt/core.h>
21
22 #include <array>
23 #include <chrono>
24 #include <cstdio>
25 #include <cstdlib>
26 #include <cstring>
27 #include <ctime>
28 #include <optional>
29
30 namespace rootcanal::log {
31
32 // Enable flag for log styling.
33 static bool enable_log_color = true;
34
SetLogColorEnable(bool enable)35 void SetLogColorEnable(bool enable) { enable_log_color = enable; }
36
37 static std::array<char, 5> verbosity_tag = {'D', 'I', 'W', 'E', 'F'};
38
39 static std::array<fmt::text_style, 5> text_style = {
40 fmt::fg(fmt::color::dim_gray),
41 fmt::fg(fmt::color::floral_white),
42 fmt::emphasis::bold | fmt::fg(fmt::color::yellow),
43 fmt::emphasis::bold | fmt::fg(fmt::color::orange_red),
44 fmt::emphasis::bold | fmt::fg(fmt::color::red),
45 };
46
47 static std::array<fmt::color, 16> text_color = {
48 fmt::color::cadet_blue, fmt::color::aquamarine,
49 fmt::color::indian_red, fmt::color::blue_violet,
50 fmt::color::chartreuse, fmt::color::medium_sea_green,
51 fmt::color::deep_pink, fmt::color::medium_orchid,
52 fmt::color::green_yellow, fmt::color::dark_orange,
53 fmt::color::golden_rod, fmt::color::medium_slate_blue,
54 fmt::color::coral, fmt::color::lemon_chiffon,
55 fmt::color::wheat, fmt::color::turquoise,
56 };
57
VLog(Verbosity verb,char const * file,int line,std::optional<int> instance,char const * format,fmt::format_args args)58 void VLog(Verbosity verb, char const* file, int line,
59 std::optional<int> instance, char const* format,
60 fmt::format_args args) {
61 // Generate the time label.
62 auto now = std::chrono::system_clock::now();
63 auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
64 auto now_t = std::chrono::system_clock::to_time_t(now);
65 char time_str[19]; // "mm-dd_HH:MM:SS.mmm\0" is 19 byte long
66 auto n = std::strftime(time_str, sizeof(time_str), "%m-%d %H:%M:%S",
67 std::localtime(&now_t));
68 snprintf(time_str + n, sizeof(time_str) - n, ".%03u",
69 static_cast<unsigned int>(now_ms.time_since_epoch().count() % 1000));
70
71 // Generate the file label.
72 char delimiter = '/';
73 char const* file_name = ::strrchr(file, delimiter);
74 file_name = file_name == nullptr ? file : file_name + 1;
75 char file_str[40]; // file:line limited to 40 characters
76 snprintf(file_str, sizeof(file_str), "%.35s:%d", file_name, line);
77
78 fmt::print("root-canal {} {} {:<35.35} ", verbosity_tag[verb], time_str,
79 file_str);
80
81 if (instance.has_value() && enable_log_color) {
82 fmt::color instance_color = text_color[*instance % text_color.size()];
83 fmt::print(fmt::bg(instance_color) | fmt::fg(fmt::color::black), " {:>2} ",
84 *instance);
85 fmt::print(" ");
86 } else if (instance.has_value()) {
87 fmt::print(" {:>2} ", *instance);
88 } else {
89 fmt::print(" ");
90 }
91
92 if (enable_log_color) {
93 fmt::text_style style = text_style[verb];
94 fmt::vprint(stdout, style, format, args);
95 } else {
96 fmt::vprint(stdout, format, args);
97 }
98
99 fmt::print("\n");
100
101 if (verb == Verbosity::kFatal) {
102 std::abort();
103 }
104 }
105
106 } // namespace rootcanal::log
107