1 /*
2  * Copyright (C) 2021 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 "EncoderDebug.h"
18 
19 #include <string>
20 #include <fstream>
21 #include <streambuf>
22 
23 #include <android/log.h>
24 
25 namespace {
26 
encoderShouldLog()27 bool encoderShouldLog() {
28 #if defined(ENABLE_ENCODER_DEBUG_LOGGING_FOR_ALL_APPS)
29     return true;
30 #elif defined(ENABLE_ENCODER_DEBUG_LOGGING_FOR_APP)
31     static const bool sEnabled = []() {
32         std::ifstream cmdlineStream("/proc/self/cmdline");
33         const std::string cmdline((std::istreambuf_iterator<char>(cmdlineStream)),
34                                    std::istreambuf_iterator<char>());
35 
36         if (cmdline.find(ENABLE_ENCODER_DEBUG_LOGGING_FOR_APP) != std::string::npos) {
37             const std::string message = "Enabling gfxstream encoder logging for " + cmdline;
38             __android_log_write(ANDROID_LOG_DEBUG, "gfxstream", message.c_str());
39             return true;
40         } else {
41             const std::string message = "Not enabling gfxstream encoder logging for " + cmdline;
42             __android_log_write(ANDROID_LOG_DEBUG, "gfxstream", message.c_str());
43             return false;
44         }
45     }();
46     return sEnabled;
47 #else
48     return false;
49 #endif
50 }
51 
52 }  // namespace
53 
encoderLog(const char * format,...)54 void encoderLog(const char* format, ...) {
55 #if defined(ENABLE_ENCODER_DEBUG_LOGGING_FOR_ALL_APPS) || \
56     defined(ENABLE_ENCODER_DEBUG_LOGGING_FOR_APP)
57     if (!encoderShouldLog()) {
58         return;
59     }
60 
61     char buffer[2048];
62 
63     va_list args;
64     va_start(args, format);
65     vsnprintf(buffer, 2048, format, args);
66     va_end(args);
67 
68     __android_log_write(ANDROID_LOG_DEBUG, "gfxstream", buffer);
69 #else
70     (void)format;
71 #endif
72 }