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" |
Shinichiro Hamaji | 9db4cb8 | 2015-06-22 16:51:04 +0900 | [diff] [blame] | 24 | #include "stringprintf.h" |
| 25 | |
Shinichiro Hamaji | e9f7e67 | 2015-07-03 15:57:45 +0900 | [diff] [blame] | 26 | using namespace std; |
| 27 | |
| 28 | extern bool g_log_no_exit; |
| 29 | extern string* g_last_error; |
| 30 | |
Shinichiro Hamaji | 8d50301 | 2015-07-03 16:26:15 +0900 | [diff] [blame] | 31 | // Useful for logging-only arguments. |
| 32 | #define UNUSED __attribute__((unused)) |
| 33 | |
Shinichiro Hamaji | 6f04aed | 2015-06-27 05:48:27 +0900 | [diff] [blame] | 34 | #ifdef NOLOG |
| 35 | #define LOG(args...) |
| 36 | #else |
Shinichiro Hamaji | 9db4cb8 | 2015-06-22 16:51:04 +0900 | [diff] [blame] | 37 | #define LOG(args...) do { \ |
| 38 | fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \ |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 39 | } while(0) |
Shinichiro Hamaji | 6f04aed | 2015-06-27 05:48:27 +0900 | [diff] [blame] | 40 | #endif |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 41 | |
Shinichiro Hamaji | 54e52dd | 2015-06-27 17:14:06 +0900 | [diff] [blame] | 42 | #define LOG_STAT(args...) do { \ |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 43 | if (g_flags.enable_stat_logs) \ |
Shinichiro Hamaji | 54e52dd | 2015-06-27 17:14:06 +0900 | [diff] [blame] | 44 | fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \ |
| 45 | } while(0) |
| 46 | |
Shinichiro Hamaji | 7cf1935 | 2016-01-26 15:32:44 +0900 | [diff] [blame] | 47 | #define PLOG(...) do { \ |
Shinichiro Hamaji | 9db4cb8 | 2015-06-22 16:51:04 +0900 | [diff] [blame] | 48 | fprintf(stderr, "%s: %s\n", StringPrintf(__VA_ARGS__).c_str(), \ |
| 49 | strerror(errno)); \ |
Shinichiro Hamaji | 7cf1935 | 2016-01-26 15:32:44 +0900 | [diff] [blame] | 50 | } while (0) |
| 51 | |
| 52 | #define PERROR(...) do { \ |
| 53 | PLOG(__VA_ARGS__); \ |
Shinichiro Hamaji | 9db4cb8 | 2015-06-22 16:51:04 +0900 | [diff] [blame] | 54 | exit(1); \ |
| 55 | } while (0) |
| 56 | |
| 57 | #define WARN(...) do { \ |
| 58 | fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \ |
| 59 | } while (0) |
| 60 | |
Shinichiro Hamaji | 644d6b9 | 2015-11-17 14:47:56 +0900 | [diff] [blame] | 61 | #define KATI_WARN(...) do { \ |
| 62 | if (g_flags.enable_kati_warnings) \ |
| 63 | fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \ |
| 64 | } while (0) |
| 65 | |
Shinichiro Hamaji | 383cfe0 | 2015-07-27 16:12:45 +0900 | [diff] [blame] | 66 | #define ERROR(...) do { \ |
| 67 | if (!g_log_no_exit) { \ |
| 68 | fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \ |
| 69 | exit(1); \ |
| 70 | } \ |
| 71 | g_last_error = new string(StringPrintf(__VA_ARGS__)); \ |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 72 | } while (0) |
| 73 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 74 | #define CHECK(c) if (!(c)) ERROR("%s:%d: %s", __FILE__, __LINE__, #c) |
| 75 | |
| 76 | #endif // LOG_H_ |