• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "log.h"
25 #include "stringprintf.h"
26 
27 using namespace std;
28 
29 extern bool g_log_no_exit;
30 extern string* g_last_error;
31 
32 // Useful for logging-only arguments.
33 #define UNUSED __attribute__((unused))
34 
35 #ifdef NOLOG
36 #define LOG(args...)
37 #else
38 #define LOG(args...)                                             \
39   do {                                                           \
40     fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \
41   } while (0)
42 #endif
43 
44 #define LOG_STAT(args...)                                          \
45   do {                                                             \
46     if (g_flags.enable_stat_logs)                                  \
47       fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \
48   } while (0)
49 
50 #define PLOG(...)                                                  \
51   do {                                                             \
52     fprintf(stderr, "%s: %s\n", StringPrintf(__VA_ARGS__).c_str(), \
53             strerror(errno));                                      \
54   } while (0)
55 
56 #define PERROR(...)    \
57   do {                 \
58     PLOG(__VA_ARGS__); \
59     exit(1);           \
60   } while (0)
61 
62 #define WARN(...)                                               \
63   do {                                                          \
64     fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \
65   } while (0)
66 
67 #define KATI_WARN(...)                                            \
68   do {                                                            \
69     if (g_flags.enable_kati_warnings)                             \
70       fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \
71   } while (0)
72 
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__));         \
80   } while (0)
81 
82 #define CHECK(c) \
83   if (!(c))      \
84   ERROR("%s:%d: %s", __FILE__, __LINE__, #c)
85 
86 // Set of logging functions that will automatically colorize lines that have
87 // location information when --color_warnings is set.
88 void ColorWarnLog(const char* file, int line, const char* msg);
89 void ColorErrorLog(const char* file, int line, const char* msg);
90 
91 #define WARN_LOC(loc, ...)                                      \
92   do {                                                          \
93     ColorWarnLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \
94   } while (0)
95 
96 #define KATI_WARN_LOC(loc, ...)                                   \
97   do {                                                            \
98     if (g_flags.enable_kati_warnings)                             \
99       ColorWarnLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \
100   } while (0)
101 
102 #define ERROR_LOC(loc, ...)                                      \
103   do {                                                           \
104     ColorErrorLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \
105   } while (0)
106 
107 #endif  // LOG_H_
108