1 // Copyright 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <chrono>
15 #include <cinttypes>
16 #include <cstdarg>
17 #include <cstring>
18 #include <sstream>
19 #include <thread>
20 
21 #include "absl/log/absl_log.h"
22 #include "absl/log/log.h"
23 #include "logging.h"
24 namespace {
25 bool sEnableVerbose = false;
26 }  // namespace
27 
set_gfxstream_logger(gfxstream_logger_t f)28 void set_gfxstream_logger(gfxstream_logger_t f) {}
set_gfxstream_fine_logger(gfxstream_logger_t f)29 void set_gfxstream_fine_logger(gfxstream_logger_t f) {}
set_gfxstream_enable_log_colors()30 void set_gfxstream_enable_log_colors() {}
set_gfxstream_enable_verbose_logs()31 void set_gfxstream_enable_verbose_logs() { sEnableVerbose = true; }
32 
gfx_stream_logger(char severity,const char * file,unsigned int line,int64_t timestamp_us,const char * msg)33 void gfx_stream_logger(char severity, const char* file, unsigned int line, int64_t timestamp_us,
34                        const char* msg) {
35 
36     switch (severity) {
37         case 'I':  // INFO
38             ABSL_LOG(INFO).AtLocation(file, line) << msg;
39             break;
40         case 'W':  // WARNING
41             ABSL_LOG(WARNING).AtLocation(file, line) << msg;
42             break;
43         case 'E':  // ERROR
44             ABSL_LOG(ERROR).AtLocation(file, line) << msg;
45             break;
46         case 'F':  // FATAL
47             ABSL_LOG(FATAL).AtLocation(file, line) << msg;
48             break;
49         case 'V':
50             VLOG(1).AtLocation(file, line) << msg;
51             break;
52         case 'D':
53             VLOG(2).AtLocation(file, line) << msg;
54             break;
55         default:
56             ABSL_LOG(INFO).AtLocation(file, line) << msg;
57             break;
58     };
59 }
60 
get_gfx_stream_logger()61 gfxstream_logger_t get_gfx_stream_logger() { return gfx_stream_logger; };
62 
OutputLog(FILE * stream,char severity,const char * file,unsigned int line,int64_t timestamp_us,const char * format,...)63 void OutputLog(FILE* stream, char severity, const char* file, unsigned int line,
64                int64_t timestamp_us, const char* format, ...) {
65     if (severity == 'V' && !sEnableVerbose) {
66         return;
67     }
68 
69     constexpr int bufferSize = 4096;
70     char buffer[bufferSize];
71     int strlen = bufferSize;
72     va_list args;
73     va_start(args, format);
74     int size = vsnprintf(buffer, bufferSize, format, args);
75     va_end(args);
76     if (size >= bufferSize) {
77         // Indicate trunctation.
78         strncpy(buffer + bufferSize - 3, "...", 3);
79     } else {
80         strlen = size;
81     }
82 
83     std::string_view msg(buffer, strlen);
84 
85     if (timestamp_us == 0) {
86         switch (severity) {
87             case 'I':  // INFO
88                 ABSL_LOG(INFO).AtLocation(file, line) << msg;
89                 break;
90             case 'W':  // WARNING
91                 ABSL_LOG(WARNING).AtLocation(file, line) << msg;
92                 break;
93             case 'E':  // ERROR
94                 ABSL_LOG(ERROR).AtLocation(file, line) << msg;
95                 break;
96             case 'F':  // FATAL
97                 ABSL_LOG(FATAL).AtLocation(file, line) << msg;
98                 break;
99             case 'V':
100                 VLOG(1).AtLocation(file, line) << msg;
101                 break;
102             case 'D':
103                 VLOG(2).AtLocation(file, line) << msg;
104                 break;
105         };
106     } else {
107         auto ts = absl::UnixEpoch() + absl::Microseconds(timestamp_us);
108         switch (severity) {
109             case 'I':  // INFO
110                 ABSL_LOG(INFO).AtLocation(file, line).WithTimestamp(ts) << msg;
111                 break;
112             case 'W':  // WARNING
113                 ABSL_LOG(WARNING).AtLocation(file, line).WithTimestamp(ts) << msg;
114                 break;
115             case 'E':  // ERROR
116                 ABSL_LOG(ERROR).AtLocation(file, line).WithTimestamp(ts) << msg;
117                 break;
118             case 'F':  // FATAL
119                 ABSL_LOG(FATAL).AtLocation(file, line).WithTimestamp(ts) << msg;
120                 break;
121             case 'V':
122                 VLOG(1).AtLocation(file, line).WithTimestamp(ts) << msg;
123                 break;
124             case 'D':
125                 VLOG(2).AtLocation(file, line).WithTimestamp(ts) << msg;
126                 break;
127         };
128     }
129 }
130