• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 NVRAM_CORE_LOGGER_H_
18 #define NVRAM_CORE_LOGGER_H_
19 
20 // Numeric constants for log levels.
21 #define NVRAM_LOG_LEVEL_ERR 1
22 #define NVRAM_LOG_LEVEL_WARN 2
23 #define NVRAM_LOG_LEVEL_INFO 3
24 #define NVRAM_LOG_LEVEL_DEBUG 4
25 
26 #if !defined(NVRAM_LOG_LEVEL)
27 // By default, log only warnings and errors.
28 #define NVRAM_LOG_LEVEL NVRAM_LOG_LEVEL_WARN
29 #endif
30 
31 #ifdef __ANDROID__
32 
33 #define LOG_TAG "NVRAM"
34 #include <log/log.h>
35 
36 // Maps NVRAM log levels to Android log priorities.
37 #define NVRAM_ANDROID_LOG_PRI_ERR ANDROID_LOG_ERROR
38 #define NVRAM_ANDROID_LOG_PRI_WARN ANDROID_LOG_WARN
39 #define NVRAM_ANDROID_LOG_PRI_INFO ANDROID_LOG_INFO
40 #define NVRAM_ANDROID_LOG_PRI_DEBUG ANDROID_LOG_DEBUG
41 
42 // Send log output to Android's logging system.
43 #define NVRAM_LOG_EMIT(level, fmt, ...) \
44   LOG_PRI(NVRAM_ANDROID_LOG_PRI_##level, LOG_TAG, fmt, ##__VA_ARGS__)
45 
46 #else  // __ANDROID__
47 
48 extern "C" {
49 #include <stdio.h>
50 }
51 
52 // By default, send log output to stderr.
53 #define NVRAM_LOG_EMIT(level, fmt, ...) \
54   fprintf(stderr, "NVRAM: " fmt "\n", ##__VA_ARGS__)
55 
56 #endif  // !__ANDROID__
57 
58 // NVRAM_LOG is the central log macro. It checks whether the log level is
59 // effective, adds file and line information and calls the platform-specific
60 // NVRAM_LOG_EMIT.
61 #define NVRAM_STR(arg) #arg
62 #define NVRAM_STRINGIFY(arg) NVRAM_STR(arg)
63 #define NVRAM_LOG(level, fmt, ...)                                           \
64   do {                                                                       \
65     if (NVRAM_LOG_LEVEL_##level <= NVRAM_LOG_LEVEL) {                        \
66       NVRAM_LOG_EMIT(level, __FILE__ ":" NVRAM_STRINGIFY(__LINE__) ": " fmt, \
67                      ##__VA_ARGS__);                                         \
68     }                                                                        \
69   } while (0)
70 
71 // Convenience logging macros.
72 #define NVRAM_LOG_ERR(fmt, ...) NVRAM_LOG(ERR, fmt, ##__VA_ARGS__)
73 #define NVRAM_LOG_WARN(fmt, ...) NVRAM_LOG(WARN, fmt, ##__VA_ARGS__)
74 #define NVRAM_LOG_INFO(fmt, ...) NVRAM_LOG(INFO, fmt, ##__VA_ARGS__)
75 #define NVRAM_LOG_DEBUG(fmt, ...) NVRAM_LOG(DEBUG, fmt, ##__VA_ARGS__)
76 
77 #endif  // NVRAM_CORE_LOGGER_H_
78