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,    fmt::color::indian_red,
49         fmt::color::blue_violet, fmt::color::chartreuse,    fmt::color::medium_sea_green,
50         fmt::color::deep_pink,   fmt::color::medium_orchid, fmt::color::green_yellow,
51         fmt::color::dark_orange, fmt::color::golden_rod,    fmt::color::medium_slate_blue,
52         fmt::color::coral,       fmt::color::lemon_chiffon, fmt::color::wheat,
53         fmt::color::turquoise,
54 };
55 
VLog(Verbosity verb,char const * file,int line,std::optional<int> instance,char const * format,fmt::format_args args)56 void VLog(Verbosity verb, char const* file, int line, std::optional<int> instance,
57           char const* format, fmt::format_args args) {
58   // Generate the time label.
59   auto now = std::chrono::system_clock::now();
60   auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
61   auto now_t = std::chrono::system_clock::to_time_t(now);
62   char time_str[19];  // "mm-dd_HH:MM:SS.mmm\0" is 19 byte long
63   auto n = std::strftime(time_str, sizeof(time_str), "%m-%d %H:%M:%S", std::localtime(&now_t));
64   snprintf(time_str + n, sizeof(time_str) - n, ".%03u",
65            static_cast<unsigned int>(now_ms.time_since_epoch().count() % 1000));
66 
67   // Generate the file label.
68   char delimiter = '/';
69   char const* file_name = ::strrchr(file, delimiter);
70   file_name = file_name == nullptr ? file : file_name + 1;
71   char file_str[40];  // file:line limited to 40 characters
72   snprintf(file_str, sizeof(file_str), "%.35s:%d", file_name, line);
73 
74   fmt::print("root-canal {} {} {:<35.35} ", verbosity_tag[verb], time_str, file_str);
75 
76   if (instance.has_value() && enable_log_color) {
77     fmt::color instance_color = text_color[*instance % text_color.size()];
78     fmt::print(fmt::bg(instance_color) | fmt::fg(fmt::color::black), " {:>2} ", *instance);
79     fmt::print(" ");
80   } else if (instance.has_value()) {
81     fmt::print(" {:>2}  ", *instance);
82   } else {
83     fmt::print("     ");
84   }
85 
86   if (enable_log_color) {
87     fmt::text_style style = text_style[verb];
88     fmt::vprint(stdout, style, format, args);
89   } else {
90     fmt::vprint(stdout, format, args);
91   }
92 
93   fmt::print("\n");
94 
95   if (verb == Verbosity::kFatal) {
96     std::abort();
97   }
98 }
99 
100 }  // namespace rootcanal::log
101