blob: 11cf0e5215176d667c8325d9c3f99b0c7e789cab [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"
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +090024#include "stringprintf.h"
25
Shinichiro Hamajie9f7e672015-07-03 15:57:45 +090026using namespace std;
27
28extern bool g_log_no_exit;
29extern string* g_last_error;
30
Shinichiro Hamaji8d503012015-07-03 16:26:15 +090031// Useful for logging-only arguments.
32#define UNUSED __attribute__((unused))
33
Shinichiro Hamaji6f04aed2015-06-27 05:48:27 +090034#ifdef NOLOG
35#define LOG(args...)
36#else
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +090037#define LOG(args...) do { \
38 fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090039 } while(0)
Shinichiro Hamaji6f04aed2015-06-27 05:48:27 +090040#endif
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090041
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +090042#define LOG_STAT(args...) do { \
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +090043 if (g_flags.enable_stat_logs) \
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +090044 fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \
45 } while(0)
46
Shinichiro Hamaji7cf19352016-01-26 15:32:44 +090047#define PLOG(...) do { \
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +090048 fprintf(stderr, "%s: %s\n", StringPrintf(__VA_ARGS__).c_str(), \
49 strerror(errno)); \
Shinichiro Hamaji7cf19352016-01-26 15:32:44 +090050 } while (0)
51
52#define PERROR(...) do { \
53 PLOG(__VA_ARGS__); \
Shinichiro Hamaji9db4cb82015-06-22 16:51:04 +090054 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 Hamaji644d6b92015-11-17 14:47:56 +090061#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 Hamaji383cfe02015-07-27 16:12:45 +090066#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 Hamaji776ca302015-06-06 03:52:48 +090072 } while (0)
73
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090074#define CHECK(c) if (!(c)) ERROR("%s:%d: %s", __FILE__, __LINE__, #c)
75
76#endif // LOG_H_