1 /*
2  * Copyright (C) 2016 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 #ifndef LOG_H_
18 #define LOG_H_
19 
20 #include <stdarg.h>
21 
22 #include <chrono>
23 #include <vector>
24 
25 namespace android {
26 
27 /*
28  * Prefer to use these macros instead of calling Log::Error, etc. directly, in
29  * case we want to add tracing of the source file and line number, or compile
30  * out logging completely, etc.
31  */
32 #define LOGE(fmt, ...) Log::Error(fmt, ##__VA_ARGS__)
33 #define LOGW(fmt, ...) Log::Warn(fmt, ##__VA_ARGS__)
34 #define LOGI(fmt, ...) Log::Info(fmt, ##__VA_ARGS__)
35 #define LOGD(fmt, ...) Log::Debug(fmt, ##__VA_ARGS__)
36 
37 #define LOGD_BUF(buf, len) Log::DebugBuf((const uint8_t *) buf, len)
38 #define LOGD_VEC(vec) Log::DebugBuf(vec)
39 
40 // Interface for a log output method
41 class Logger {
42   public:
~Logger()43     virtual ~Logger() {};
44     virtual void Output(const char *str) = 0;
45     virtual void Output(const char *format, va_list arg_list) = 0;
46 };
47 
48 // Singleton used to log messages to an arbitrary output
49 class Log {
50   public:
51     enum class LogLevel {
52         // Use with SetLevel to disable logging
53         Disable,
54         Error,
55         Warn,
56         Info,
57         Debug,
58     };
59 
60     // Define the logging mechanism and minimum log level that will be printed
61     static void Initialize(Logger *logger, LogLevel level);
62 
63     __attribute__((__format__ (printf, 1, 2)))
64     static void Error(const char *format, ...);
65 
66     __attribute__((__format__ (printf, 1, 2)))
67     static void Warn(const char *format, ...);
68 
69     __attribute__((__format__ (printf, 1, 2)))
70     static void Info(const char *format, ...);
71 
72     __attribute__((__format__ (printf, 1, 2)))
73     static void Debug(const char *format, ...);
74 
75     static void DebugBuf(std::vector<uint8_t> vec);
76     static void DebugBuf(const uint8_t *buffer, size_t size);
77 
78     // Allows for updating the logging level after initialization
79     static void SetLevel(LogLevel level);
80 
81   private:
82     static char LevelAbbrev(LogLevel level);
83     static void LogEx(LogLevel level, const char *format, va_list arg_list);
84 
85     static Logger* logger_;
86     static LogLevel level_;
87     static std::chrono::time_point<std::chrono::steady_clock> init_time_;
88 };
89 
90 class PrintfLogger : public Logger {
91   public:
92     void Output(const char *str);
93     void Output(const char *format, va_list arg_list);
94 };
95 
96 }  // namespace android
97 
98 #endif // LOG_H_
99