1/*
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
20#include <stdarg.h>
21
22namespace keymaster {
23
24class Logger {
25  public:
26    Logger() {}
27    virtual ~Logger() {}
28
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
39    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; }
49
50  private:
51    // Disallow copying.
52    Logger(const Logger&);
53    void operator=(const Logger&);
54
55    static Logger* instance_;
56};
57
58#define STR(x) #x
59#define STRINGIFY(x) STR(x)
60#define FILE_LINE __FILE__ ", Line " STRINGIFY(__LINE__) ": "
61
62#ifdef DEBUG
63#define DEBUG_LOGS 1
64#else
65#define DEBUG_LOGS 0
66#endif
67
68#define LOG_D(fmt, ...)                                                                            \
69    do {                                                                                           \
70        if (DEBUG_LOGS)                                                                            \
71            Logger::Debug(FILE_LINE fmt, __VA_ARGS__);                                             \
72    } while (0)
73#define LOG_I(fmt, ...)                                                                            \
74    do {                                                                                           \
75        if (DEBUG_LOGS)                                                                            \
76            Logger::Info(FILE_LINE fmt, __VA_ARGS__);                                              \
77    } while (0)
78
79#define LOG_W(fmt, ...) Logger::Warning(FILE_LINE fmt, __VA_ARGS__)
80#define LOG_E(fmt, ...) Logger::Error(FILE_LINE fmt, __VA_ARGS__)
81#define LOG_S(fmt, ...) Logger::Severe(FILE_LINE fmt, __VA_ARGS__)
82
83}  // namespace keymaster
84
85#endif  // SYSTEM_KEYMASTER_LOGGER_H_
86