blob: ba0bd8b5aef3dffa87386db1eee9d7c5647b2197 [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// 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 Hamaji776ca302015-06-06 03:52:48 +090015#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 Hamaji54e52dd2015-06-27 17:14:06 +090023#include "flags.h"
Dan Willemsene41c7552017-02-22 14:31:16 -080024#include "log.h"
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +090025#include "stringprintf.h"
26
Shinichiro Hamajie9f7e672015-07-03 15:57:45 +090027using namespace std;
28
29extern bool g_log_no_exit;
30extern string* g_last_error;
31
Shinichiro Hamaji8d503012015-07-03 16:26:15 +090032// Useful for logging-only arguments.
33#define UNUSED __attribute__((unused))
34
Shinichiro Hamaji6f04aed2015-06-27 05:48:27 +090035#ifdef NOLOG
36#define LOG(args...)
37#else
Dan Willemsen3ce083f2017-10-11 22:17:48 -070038#define LOG(args...) \
39 do { \
40 fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \
41 } while (0)
Shinichiro Hamaji6f04aed2015-06-27 05:48:27 +090042#endif
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090043
Dan Willemsen3ce083f2017-10-11 22:17:48 -070044#define LOG_STAT(args...) \
45 do { \
46 if (g_flags.enable_stat_logs) \
47 fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \
Shinichiro Hamaji7cf19352016-01-26 15:32:44 +090048 } while (0)
49
Dan Willemsen3ce083f2017-10-11 22:17:48 -070050#define PLOG(...) \
51 do { \
52 fprintf(stderr, "%s: %s\n", StringPrintf(__VA_ARGS__).c_str(), \
53 strerror(errno)); \
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +090054 } while (0)
55
Dan Willemsen3ce083f2017-10-11 22:17:48 -070056#define PERROR(...) \
57 do { \
58 PLOG(__VA_ARGS__); \
59 exit(1); \
60 } while (0)
61
62#define WARN(...) \
63 do { \
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +090064 fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \
65 } while (0)
66
Dan Willemsen3ce083f2017-10-11 22:17:48 -070067#define KATI_WARN(...) \
68 do { \
69 if (g_flags.enable_kati_warnings) \
70 fprintf(stderr, "%s\n", StringPrintf(__VA_ARGS__).c_str()); \
Shinichiro Hamaji644d6b92015-11-17 14:47:56 +090071 } while (0)
72
Dan Willemsen3ce083f2017-10-11 22:17:48 -070073#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 Hamaji776ca302015-06-06 03:52:48 +090080 } while (0)
81
Dan Willemsen3ce083f2017-10-11 22:17:48 -070082#define CHECK(c) \
83 if (!(c)) \
84 ERROR("%s:%d: %s", __FILE__, __LINE__, #c)
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090085
Dan Willemsene41c7552017-02-22 14:31:16 -080086// Set of logging functions that will automatically colorize lines that have
87// location information when --color_warnings is set.
Dan Willemsen3ce083f2017-10-11 22:17:48 -070088void ColorWarnLog(const char* file, int line, const char* msg);
89void ColorErrorLog(const char* file, int line, const char* msg);
Dan Willemsene41c7552017-02-22 14:31:16 -080090
Dan Willemsen3ce083f2017-10-11 22:17:48 -070091#define WARN_LOC(loc, ...) \
92 do { \
Dan Willemsene41c7552017-02-22 14:31:16 -080093 ColorWarnLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \
94 } while (0)
95
Dan Willemsen3ce083f2017-10-11 22:17:48 -070096#define KATI_WARN_LOC(loc, ...) \
97 do { \
Dan Willemsene41c7552017-02-22 14:31:16 -080098 if (g_flags.enable_kati_warnings) \
99 ColorWarnLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \
Dan Willemsen3ce083f2017-10-11 22:17:48 -0700100 } while (0)
Dan Willemsene41c7552017-02-22 14:31:16 -0800101
Dan Willemsen3ce083f2017-10-11 22:17:48 -0700102#define ERROR_LOC(loc, ...) \
103 do { \
Dan Willemsene41c7552017-02-22 14:31:16 -0800104 ColorErrorLog(LOCF(loc), StringPrintf(__VA_ARGS__).c_str()); \
105 } while (0)
106
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900107#endif // LOG_H_