10a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden/*
20a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden * Copyright 2014 The Android Open Source Project
30a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden *
40a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden * Licensed under the Apache License, Version 2.0 (the "License");
50a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden * you may not use this file except in compliance with the License.
60a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden * You may obtain a copy of the License at
70a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden *
80a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden *      http://www.apache.org/licenses/LICENSE-2.0
90a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden *
100a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden * Unless required by applicable law or agreed to in writing, software
110a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden * distributed under the License is distributed on an "AS IS" BASIS,
120a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden * See the License for the specific language governing permissions and
140a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden * limitations under the License.
150a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden */
160a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden
170a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden#ifndef SYSTEM_KEYMASTER_LOGGER_H_
180a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden#define SYSTEM_KEYMASTER_LOGGER_H_
190a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden
20538b0654fd5096841e12da15271c74429a37be18Shawn Willden#include <stdarg.h>
21538b0654fd5096841e12da15271c74429a37be18Shawn Willden
220a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willdennamespace keymaster {
230a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden
240a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willdenclass Logger {
250a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden  public:
260a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden    Logger() {}
270a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden    virtual ~Logger() {}
28538b0654fd5096841e12da15271c74429a37be18Shawn Willden
29538b0654fd5096841e12da15271c74429a37be18Shawn Willden    enum LogLevel {
30538b0654fd5096841e12da15271c74429a37be18Shawn Willden        DEBUG_LVL,    // Messages used only for debugging
31538b0654fd5096841e12da15271c74429a37be18Shawn Willden        INFO_LVL,     // Informational messages; something is unusual but not wrong
32538b0654fd5096841e12da15271c74429a37be18Shawn Willden        WARNING_LVL,  // There's an indication of trouble, but it may be okay.
33538b0654fd5096841e12da15271c74429a37be18Shawn Willden        ERROR_LVL,    // A problem has occurred, but processing can continue
34538b0654fd5096841e12da15271c74429a37be18Shawn Willden        SEVERE_LVL,   // A severe problem has occurred; likely indicates a defect.
35538b0654fd5096841e12da15271c74429a37be18Shawn Willden    };
36538b0654fd5096841e12da15271c74429a37be18Shawn Willden
37538b0654fd5096841e12da15271c74429a37be18Shawn Willden    virtual int log_msg(LogLevel level, const char* fmt, va_list args) const = 0;
38538b0654fd5096841e12da15271c74429a37be18Shawn Willden
39538b0654fd5096841e12da15271c74429a37be18Shawn Willden    static int Log(LogLevel level, const char* fmt, va_list args);
40538b0654fd5096841e12da15271c74429a37be18Shawn Willden    static int Log(LogLevel level, const char* fmt, ...);
41538b0654fd5096841e12da15271c74429a37be18Shawn Willden    static int Debug(const char* fmt, ...);
42538b0654fd5096841e12da15271c74429a37be18Shawn Willden    static int Info(const char* fmt, ...);
43538b0654fd5096841e12da15271c74429a37be18Shawn Willden    static int Warning(const char* fmt, ...);
44538b0654fd5096841e12da15271c74429a37be18Shawn Willden    static int Error(const char* fmt, ...);
45538b0654fd5096841e12da15271c74429a37be18Shawn Willden    static int Severe(const char* fmt, ...);
46538b0654fd5096841e12da15271c74429a37be18Shawn Willden
47538b0654fd5096841e12da15271c74429a37be18Shawn Willden  protected:
48538b0654fd5096841e12da15271c74429a37be18Shawn Willden    static void set_instance(Logger* logger) { instance_ = logger; }
490a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden
500a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden  private:
510a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden    // Disallow copying.
520a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden    Logger(const Logger&);
530a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden    void operator=(const Logger&);
54538b0654fd5096841e12da15271c74429a37be18Shawn Willden
55538b0654fd5096841e12da15271c74429a37be18Shawn Willden    static Logger* instance_;
560a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden};
570a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden
58538b0654fd5096841e12da15271c74429a37be18Shawn Willden#define STR(x) #x
59538b0654fd5096841e12da15271c74429a37be18Shawn Willden#define STRINGIFY(x) STR(x)
60f35e8eea72e61a383bcbcbe204ff5628ae3bd4acShawn Willden#define FILE_LINE __FILE__ ", Line " STRINGIFY(__LINE__) ": "
61538b0654fd5096841e12da15271c74429a37be18Shawn Willden
62de7e66c3692073eb967f01cc8281441709701e2dShawn Willden#ifdef DEBUG
63de7e66c3692073eb967f01cc8281441709701e2dShawn Willden#define DEBUG_LOGS 1
64de7e66c3692073eb967f01cc8281441709701e2dShawn Willden#else
65de7e66c3692073eb967f01cc8281441709701e2dShawn Willden#define DEBUG_LOGS 0
66de7e66c3692073eb967f01cc8281441709701e2dShawn Willden#endif
67de7e66c3692073eb967f01cc8281441709701e2dShawn Willden
68de7e66c3692073eb967f01cc8281441709701e2dShawn Willden#define LOG_D(fmt, ...)                                                                            \
69de7e66c3692073eb967f01cc8281441709701e2dShawn Willden    do {                                                                                           \
70de7e66c3692073eb967f01cc8281441709701e2dShawn Willden        if (DEBUG_LOGS)                                                                            \
71de7e66c3692073eb967f01cc8281441709701e2dShawn Willden            Logger::Debug(FILE_LINE fmt, __VA_ARGS__);                                             \
72de7e66c3692073eb967f01cc8281441709701e2dShawn Willden    } while (0)
73de7e66c3692073eb967f01cc8281441709701e2dShawn Willden#define LOG_I(fmt, ...)                                                                            \
74de7e66c3692073eb967f01cc8281441709701e2dShawn Willden    do {                                                                                           \
75de7e66c3692073eb967f01cc8281441709701e2dShawn Willden        if (DEBUG_LOGS)                                                                            \
76de7e66c3692073eb967f01cc8281441709701e2dShawn Willden            Logger::Info(FILE_LINE fmt, __VA_ARGS__);                                              \
77de7e66c3692073eb967f01cc8281441709701e2dShawn Willden    } while (0)
78538b0654fd5096841e12da15271c74429a37be18Shawn Willden
79538b0654fd5096841e12da15271c74429a37be18Shawn Willden#define LOG_W(fmt, ...) Logger::Warning(FILE_LINE fmt, __VA_ARGS__)
80538b0654fd5096841e12da15271c74429a37be18Shawn Willden#define LOG_E(fmt, ...) Logger::Error(FILE_LINE fmt, __VA_ARGS__)
81538b0654fd5096841e12da15271c74429a37be18Shawn Willden#define LOG_S(fmt, ...) Logger::Severe(FILE_LINE fmt, __VA_ARGS__)
82538b0654fd5096841e12da15271c74429a37be18Shawn Willden
830a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden}  // namespace keymaster
840a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden
850a4df7e3a83a59e4a5abc3f605d7d7e9f636c682Shawn Willden#endif  // SYSTEM_KEYMASTER_LOGGER_H_
86