Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 15 | #ifndef LOG_H_ |
| 16 | #define LOG_H_ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
Shinichiro Hamaji | 54e52dd | 2015-06-27 17:14:06 +0900 | [diff] [blame] | 23 | #include "flags.h" |
Dan Willemsen | e41c755 | 2017-02-22 14:31:16 -0800 | [diff] [blame] | 24 | #include "log.h" |
Shinichiro Hamaji | 9db4cb8 | 2015-06-22 16:51:04 +0900 | [diff] [blame] | 25 | #include "stringprintf.h" |
| 26 | |
Shinichiro Hamaji | e9f7e67 | 2015-07-03 15:57:45 +0900 | [diff] [blame] | 27 | using namespace std; |
| 28 | |
| 29 | extern bool g_log_no_exit; |
| 30 | extern string* g_last_error; |
| 31 | |
Shinichiro Hamaji | 8d50301 | 2015-07-03 16:26:15 +0900 | [diff] [blame] | 32 | // Useful for logging-only arguments. |
| 33 | #define UNUSED __attribute__((unused)) |
| 34 | |
Shinichiro Hamaji | 6f04aed | 2015-06-27 05:48:27 +0900 | [diff] [blame] | 35 | #ifdef NOLOG |
| 36 | #define LOG(args...) |
| 37 | #else |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 38 | #define LOG(args...) \ |
| 39 | do { \ |
| 40 | fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \ |
| 41 | } while (0) |
Shinichiro Hamaji | 6f04aed | 2015-06-27 05:48:27 +0900 | [diff] [blame] | 42 | #endif |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 43 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 44 | #define LOG_STAT(args...) \ |
| 45 | do { \ |
| 46 | if (g_flags.enable_stat_logs) \ |
| 47 | fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \ |
Shinichiro Hamaji | 7cf1935 | 2016-01-26 15:32:44 +0900 | [diff] [blame] | 48 | } while (0) |
| 49 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 50 | #define PLOG(...) \ |
| 51 | do { \ |
| 52 | fprintf(stderr, "%s: %s\n", StringPrintf(__VA_ARGS__).c_str(), \ |
| 53 | strerror(errno)); \ |
Shinichiro Hamaji | 9db4cb8 | 2015-06-22 16:51:04 +0900 | [diff] [blame] | 54 | } while (0) |
| 55 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 56 | #define PERROR(...) \ |
| 57 | do { \ |
| 58 | PLOG(__VA_ARGS__); \ |
| 59 | exit(1); \ |
| 60 | } while (0) |
| 61 | |
| 62 | #define WARN(...) \ |
| 63 | do { \ |
Shinichiro Hamaji | 9db4cb8 | 2015-06-22 16:51:04 +0900 | [diff] [blame] | 64 | fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \ |
| 65 | } while (0) |
| 66 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 67 | #define KATI_WARN(...) \ |
| 68 | do { \ |
| 69 | if (g_flags.enable_kati_warnings) \ |
| 70 | fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \ |
Shinichiro Hamaji | 644d6b9 | 2015-11-17 14:47:56 +0900 | [diff] [blame] | 71 | } while (0) |
| 72 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 73 | #define ERROR(...) \ |
| 74 | do { \ |
| 75 | if (!g_log_no_exit) { \ |
| 76 | fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \ |
| 77 | exit(1); \ |
| 78 | } \ |
| 79 | g_last_error = new string(StringPrintf(__VA_ARGS__)); \ |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 80 | } while (0) |
| 81 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 82 | #define CHECK(c) \ |
| 83 | if (!(c)) \ |
| 84 | ERROR("%s:%d: %s", __FILE__, __LINE__, #c) |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 85 | |
Dan Willemsen | e41c755 | 2017-02-22 14:31:16 -0800 | [diff] [blame] | 86 | // Set of logging functions that will automatically colorize lines that have |
| 87 | // location information when --color_warnings is set. |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 88 | void ColorWarnLog(const char* file, int line, const char* msg); |
| 89 | void ColorErrorLog(const char* file, int line, const char* msg); |
Dan Willemsen | e41c755 | 2017-02-22 14:31:16 -0800 | [diff] [blame] | 90 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 91 | #define WARN_LOC(loc, ...) \ |
| 92 | do { \ |
Dan Willemsen | e41c755 | 2017-02-22 14:31:16 -0800 | [diff] [blame] | 93 | ColorWarnLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \ |
| 94 | } while (0) |
| 95 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 96 | #define KATI_WARN_LOC(loc, ...) \ |
| 97 | do { \ |
Dan Willemsen | e41c755 | 2017-02-22 14:31:16 -0800 | [diff] [blame] | 98 | if (g_flags.enable_kati_warnings) \ |
| 99 | ColorWarnLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \ |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 100 | } while (0) |
Dan Willemsen | e41c755 | 2017-02-22 14:31:16 -0800 | [diff] [blame] | 101 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 102 | #define ERROR_LOC(loc, ...) \ |
| 103 | do { \ |
Dan Willemsen | e41c755 | 2017-02-22 14:31:16 -0800 | [diff] [blame] | 104 | ColorErrorLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \ |
| 105 | } while (0) |
| 106 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 107 | #endif // LOG_H_ |