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 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 23 #include "flags.h" 24 #include "stringprintf.h" 25 26 using namespace std; 27 28 extern bool g_log_no_exit; 29 extern string* g_last_error; 30 31 // Useful for logging-only arguments. 32 #define UNUSED __attribute__((unused)) 33 34 #ifdef NOLOG 35 #define LOG(args...) 36 #else 37 #define LOG(args...) do { \ 38 fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \ 39 } while(0) 40 #endif 41 42 #define LOG_STAT(args...) do { \ 43 if (g_flags.enable_stat_logs) \ 44 fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \ 45 } while(0) 46 47 #define PLOG(...) do { \ 48 fprintf(stderr, "%s: %s\n", StringPrintf(__VA_ARGS__).c_str(), \ 49 strerror(errno)); \ 50 } while (0) 51 52 #define PERROR(...) do { \ 53 PLOG(__VA_ARGS__); \ 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 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 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__)); \ 72 } while (0) 73 74 #define CHECK(c) if (!(c)) ERROR("%s:%d: %s", __FILE__, __LINE__, #c) 75 76 #endif // LOG_H_ 77