1 /*
2 
3    honggfuzz - logging
4    -----------------------------------------
5 
6    Copyright 2014 Google Inc. All Rights Reserved.
7 
8    Licensed under the Apache License, Version 2.0 (the "License");
9    you may not use this file except in compliance with the License.
10    You may obtain a copy of the License at
11 
12      http://www.apache.org/licenses/LICENSE-2.0
13 
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19 
20 */
21 
22 #ifndef _LOG_H
23 #define _LOG_H
24 
25 #include <stdbool.h>
26 
27 #define LOG_RAW(...) dprintf(logGetFD(), __VA_ARGS__);
28 
29 #define LOG_HELP(...) logLog(HELP, __FUNCTION__, __LINE__, false, __VA_ARGS__);
30 #define LOG_HELP_BOLD(...) logLog(HELP_BOLD, __FUNCTION__, __LINE__, false, __VA_ARGS__);
31 
32 #define LOG_D(...) if (logGetLogLevel() >= DEBUG) { logLog(DEBUG, __FUNCTION__, __LINE__, false, __VA_ARGS__); }
33 #define LOG_I(...) if (logGetLogLevel() >= INFO) { logLog(INFO, __FUNCTION__, __LINE__, false, __VA_ARGS__); }
34 #define LOG_W(...) if (logGetLogLevel() >= WARNING) { logLog(WARNING, __FUNCTION__, __LINE__, false, __VA_ARGS__); }
35 #define LOG_E(...) if (logGetLogLevel() >= ERROR) { logLog(ERROR, __FUNCTION__, __LINE__, false, __VA_ARGS__); }
36 #define LOG_F(...) if (logGetLogLevel() >= FATAL) { logLog(FATAL, __FUNCTION__, __LINE__, false, __VA_ARGS__); }
37 
38 #define PLOG_D(...) if (logGetLogLevel() >= DEBUG) { logLog(DEBUG, __FUNCTION__, __LINE__, true, __VA_ARGS__); }
39 #define PLOG_I(...) if (logGetLogLevel() >= INFO) { logLog(INFO, __FUNCTION__, __LINE__, true, __VA_ARGS__); }
40 #define PLOG_W(...) if (logGetLogLevel() >= WARNING) { logLog(WARNING, __FUNCTION__, __LINE__, true, __VA_ARGS__); }
41 #define PLOG_E(...) if (logGetLogLevel() >= ERROR) { logLog(ERROR, __FUNCTION__, __LINE__, true, __VA_ARGS__); }
42 #define PLOG_F(...) if (logGetLogLevel() >= FATAL) { logLog(FATAL, __FUNCTION__, __LINE__, true, __VA_ARGS__); }
43 
44 enum llevel_t {
45     FATAL = 0,
46     ERROR,
47     WARNING,
48     INFO,
49     DEBUG,
50     HELP,
51     HELP_BOLD
52 };
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 void logSetLogLevel(enum llevel_t);
58 enum llevel_t logGetLogLevel(void);
59 int logGetFD();
60 bool logInitLogFile(const char *logfile, enum llevel_t ll);
61 void logLog(enum llevel_t ll, const char *fn, int ln, bool perr, const char *fmt, ...)
62     __attribute__ ((format(printf, 5, 6)));
63 void logStop(int sig);
64 #ifdef __cplusplus
65 }
66 #endif
67 
68 // courtesy of @pwntester on github
69 #if defined(__APPLE__)
70   #if !defined(__NR_gettid)
71     #define __NR_gettid SYS_gettid
72   #endif
73 #endif
74 
75 #endif                          /* _LOG_H */
76