Home | History | Annotate | Download | only in libcommon
      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 _HF_LOG_H_
     23 #define _HF_LOG_H_
     24 
     25 #include <getopt.h>
     26 #include <pthread.h>
     27 #include <stdbool.h>
     28 
     29 #include "common.h"
     30 
     31 enum llevel_t { FATAL = 0, ERROR, WARNING, INFO, DEBUG, HELP, HELP_BOLD };
     32 
     33 extern enum llevel_t log_level;
     34 
     35 #define LOG_HELP(...) logLog(HELP, __FUNCTION__, __LINE__, false, __VA_ARGS__);
     36 #define LOG_HELP_BOLD(...) logLog(HELP_BOLD, __FUNCTION__, __LINE__, false, __VA_ARGS__);
     37 
     38 #define LOG_D(...)                                                 \
     39     if (log_level >= DEBUG) {                                      \
     40         logLog(DEBUG, __FUNCTION__, __LINE__, false, __VA_ARGS__); \
     41     }
     42 #define LOG_I(...)                                                \
     43     if (log_level >= INFO) {                                      \
     44         logLog(INFO, __FUNCTION__, __LINE__, false, __VA_ARGS__); \
     45     }
     46 #define LOG_W(...)                                                   \
     47     if (log_level >= WARNING) {                                      \
     48         logLog(WARNING, __FUNCTION__, __LINE__, false, __VA_ARGS__); \
     49     }
     50 #define LOG_E(...)                                                 \
     51     if (log_level >= ERROR) {                                      \
     52         logLog(ERROR, __FUNCTION__, __LINE__, false, __VA_ARGS__); \
     53     }
     54 #define LOG_F(...)                                                 \
     55     if (log_level >= FATAL) {                                      \
     56         logLog(FATAL, __FUNCTION__, __LINE__, false, __VA_ARGS__); \
     57     }
     58 
     59 #define PLOG_D(...)                                               \
     60     if (log_level >= DEBUG) {                                     \
     61         logLog(DEBUG, __FUNCTION__, __LINE__, true, __VA_ARGS__); \
     62     }
     63 #define PLOG_I(...)                                              \
     64     if (log_level >= INFO) {                                     \
     65         logLog(INFO, __FUNCTION__, __LINE__, true, __VA_ARGS__); \
     66     }
     67 #define PLOG_W(...)                                                 \
     68     if (log_level >= WARNING) {                                     \
     69         logLog(WARNING, __FUNCTION__, __LINE__, true, __VA_ARGS__); \
     70     }
     71 #define PLOG_E(...)                                               \
     72     if (log_level >= ERROR) {                                     \
     73         logLog(ERROR, __FUNCTION__, __LINE__, true, __VA_ARGS__); \
     74     }
     75 #define PLOG_F(...)                                               \
     76     if (log_level >= FATAL) {                                     \
     77         logLog(FATAL, __FUNCTION__, __LINE__, true, __VA_ARGS__); \
     78     }
     79 
     80 extern bool logInitLogFile(const char* logfile, enum llevel_t ll);
     81 
     82 extern void logLog(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...)
     83     __attribute__((format(printf, 5, 6)));
     84 
     85 extern void logStop(int sig);
     86 
     87 extern bool logIsTTY(void);
     88 
     89 extern int logFd(void);
     90 
     91 extern pthread_mutex_t* logMutexGet(void);
     92 
     93 void logMutexReset(void);
     94 
     95 #endif /* _HF_LOG_H_ */
     96