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
27using namespace std;
28
29extern bool g_log_no_exit;
30extern 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...) do {                                           \
39    fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str());    \
40  } while(0)
41#endif
42
43#define LOG_STAT(args...) do {                                      \
44    if (g_flags.enable_stat_logs)                                   \
45      fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str());  \
46  } while(0)
47
48#define PLOG(...) do {                                              \
49    fprintf(stderr, "%s: %s\n", StringPrintf(__VA_ARGS__).c_str(),  \
50            strerror(errno));                                       \
51  } while (0)
52
53#define PERROR(...) do {                                            \
54    PLOG(__VA_ARGS__);                                              \
55    exit(1);                                                        \
56  } while (0)
57
58#define WARN(...) do {                                          \
59    fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \
60  } while (0)
61
62#define KATI_WARN(...) do {                                             \
63    if (g_flags.enable_kati_warnings)                                   \
64      fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str());       \
65  } while (0)
66
67#define ERROR(...) do {                                                 \
68    if (!g_log_no_exit) {                                               \
69      fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str());       \
70      exit(1);                                                          \
71    }                                                                   \
72    g_last_error = new string(StringPrintf(__VA_ARGS__));               \
73  } while (0)
74
75#define CHECK(c) if (!(c)) ERROR("%s:%d: %s", __FILE__, __LINE__, #c)
76
77// Set of logging functions that will automatically colorize lines that have
78// location information when --color_warnings is set.
79void ColorWarnLog(const char* file, int line, const char *msg);
80void ColorErrorLog(const char* file, int line, const char *msg);
81
82#define WARN_LOC(loc, ...) do {                                 \
83    ColorWarnLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \
84  } while (0)
85
86#define KATI_WARN_LOC(loc, ...) do {                                  \
87    if (g_flags.enable_kati_warnings)                             \
88      ColorWarnLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \
89  } while(0)
90
91#define ERROR_LOC(loc, ...) do {                                 \
92    ColorErrorLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \
93  } while (0)
94
95#endif  // LOG_H_
96