blob: a9b005b59f687b7171ee68dfaf90c05ade1e660c [file] [log] [blame]
Shawn Willden0a4df7e2014-08-28 16:09:05 -06001/*
2 * Copyright 2014 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 SYSTEM_KEYMASTER_LOGGER_H_
18#define SYSTEM_KEYMASTER_LOGGER_H_
19
Shawn Willden538b0652014-12-30 23:23:40 -070020#include <stdarg.h>
21
Shawn Willden0a4df7e2014-08-28 16:09:05 -060022namespace keymaster {
23
24class Logger {
25 public:
26 Logger() {}
27 virtual ~Logger() {}
Shawn Willden538b0652014-12-30 23:23:40 -070028
29 enum LogLevel {
30 DEBUG_LVL, // Messages used only for debugging
31 INFO_LVL, // Informational messages; something is unusual but not wrong
32 WARNING_LVL, // There's an indication of trouble, but it may be okay.
33 ERROR_LVL, // A problem has occurred, but processing can continue
34 SEVERE_LVL, // A severe problem has occurred; likely indicates a defect.
35 };
36
37 virtual int log_msg(LogLevel level, const char* fmt, va_list args) const = 0;
38
Shawn Willden538b0652014-12-30 23:23:40 -070039 static int Log(LogLevel level, const char* fmt, va_list args);
40 static int Log(LogLevel level, const char* fmt, ...);
41 static int Debug(const char* fmt, ...);
42 static int Info(const char* fmt, ...);
43 static int Warning(const char* fmt, ...);
44 static int Error(const char* fmt, ...);
45 static int Severe(const char* fmt, ...);
46
47 protected:
48 static void set_instance(Logger* logger) { instance_ = logger; }
Shawn Willden0a4df7e2014-08-28 16:09:05 -060049
50 private:
51 // Disallow copying.
52 Logger(const Logger&);
53 void operator=(const Logger&);
Shawn Willden538b0652014-12-30 23:23:40 -070054
55 static Logger* instance_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060056};
57
Seth Moore27accf52022-02-14 10:30:04 -080058#define __KM_STR(x) #x
59#define __KM_STRINGIFY(x) __KM_STR(x)
60#define __KM_FILE_LINE __FILE__ ", Line " __KM_STRINGIFY(__LINE__) ": "
Shawn Willden538b0652014-12-30 23:23:40 -070061
Seth Moore27accf52022-02-14 10:30:04 -080062#define LOG_D(fmt, ...) Logger::Debug(__KM_FILE_LINE fmt, __VA_ARGS__)
63#define LOG_I(fmt, ...) Logger::Info(__KM_FILE_LINE fmt, __VA_ARGS__)
64#define LOG_W(fmt, ...) Logger::Warning(__KM_FILE_LINE fmt, __VA_ARGS__)
65#define LOG_E(fmt, ...) Logger::Error(__KM_FILE_LINE fmt, __VA_ARGS__)
66#define LOG_S(fmt, ...) Logger::Severe(__KM_FILE_LINE fmt, __VA_ARGS__)
Shawn Willden538b0652014-12-30 23:23:40 -070067
Shawn Willden0a4df7e2014-08-28 16:09:05 -060068} // namespace keymaster
69
70#endif // SYSTEM_KEYMASTER_LOGGER_H_