1 /***************************************************************************
2  * nsock_log.c -- nsock logging infrastructure.                            *
3  *                                                                         *
4  ***********************IMPORTANT NSOCK LICENSE TERMS***********************
5  *                                                                         *
6  * The nsock parallel socket event library is (C) 1999-2020 Insecure.Com   *
7  * LLC This library is free software; you may redistribute and/or          *
8  * modify it under the terms of the GNU General Public License as          *
9  * published by the Free Software Foundation; Version 2.  This guarantees  *
10  * your right to use, modify, and redistribute this software under certain *
11  * conditions.  If this license is unacceptable to you, Insecure.Com LLC   *
12  * may be willing to sell alternative licenses (contact                    *
13  * sales@insecure.com ).                                                   *
14  *                                                                         *
15  * As a special exception to the GPL terms, Insecure.Com LLC grants        *
16  * permission to link the code of this program with any version of the     *
17  * OpenSSL library which is distributed under a license identical to that  *
18  * listed in the included docs/licenses/OpenSSL.txt file, and distribute   *
19  * linked combinations including the two. You must obey the GNU GPL in all *
20  * respects for all of the code used other than OpenSSL.  If you modify    *
21  * this file, you may extend this exception to your version of the file,   *
22  * but you are not obligated to do so.                                     *
23  *                                                                         *
24  * If you received these files with a written license agreement stating    *
25  * terms other than the (GPL) terms above, then that alternative license   *
26  * agreement takes precedence over this comment.                           *
27  *                                                                         *
28  * Source is provided to this software because we believe users have a     *
29  * right to know exactly what a program is going to do before they run it. *
30  * This also allows you to audit the software for security holes.          *
31  *                                                                         *
32  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
33  * and add new features.  You are highly encouraged to send your changes   *
34  * to the dev@nmap.org mailing list for possible incorporation into the    *
35  * main distribution.  By sending these changes to Fyodor or one of the    *
36  * Insecure.Org development mailing lists, or checking them into the Nmap  *
37  * source code repository, it is understood (unless you specify otherwise) *
38  * that you are offering the Nmap Project (Insecure.Com LLC) the           *
39  * unlimited, non-exclusive right to reuse, modify, and relicense the      *
40  * code.  Nmap will always be available Open Source, but this is important *
41  * because the inability to relicense code has caused devastating problems *
42  * for other Free Software projects (such as KDE and NASM).  We also       *
43  * occasionally relicense the code to third parties as discussed above.    *
44  * If you wish to specify special license conditions of your               *
45  * contributions, just say so when you send them.                          *
46  *                                                                         *
47  * This program is distributed in the hope that it will be useful, but     *
48  * WITHOUT ANY WARRANTY; without even the implied warranty of              *
49  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
50  * General Public License v2.0 for more details                            *
51  * (http://www.gnu.org/licenses/gpl-2.0.html).                             *
52  *                                                                         *
53  ***************************************************************************/
54 
55 /* $Id: nsock_log.h 38071 2020-10-02 05:02:05Z fyodor $ */
56 
57 
58 #ifndef NSOCK_LOG_H
59 #define NSOCK_LOG_H
60 
61 #include "nsock.h"
62 
63 extern nsock_loglevel_t NsockLogLevel;
64 extern nsock_logger_t   NsockLogger;
65 
66 
67 #define NSOCK_LOG_WRAP(lvl, ...)  \
68     do { \
69         if (NsockLogger && (lvl) >= NsockLogLevel) { \
70             __nsock_log_internal((lvl), __FILE__, __LINE__, __func__, __VA_ARGS__); \
71         } \
72     } while (0)
73 
74 
nsock_loglevel2str(nsock_loglevel_t level)75 static inline const char *nsock_loglevel2str(nsock_loglevel_t level)
76 {
77   switch (level) {
78     case NSOCK_LOG_DBG_ALL:
79       return "FULL DEBUG";
80     case NSOCK_LOG_DBG:
81       return "DEBUG";
82     case NSOCK_LOG_INFO:
83       return "INFO";
84     case NSOCK_LOG_ERROR:
85       return "ERROR";
86     default:
87       return "???";
88   }
89 }
90 
91 /* -- Internal logging macros -- */
92 /**
93  * Most detailed debug messages, like allocating or moving objects.
94  */
95 #define nsock_log_debug_all(...) NSOCK_LOG_WRAP(NSOCK_LOG_DBG_ALL, __VA_ARGS__)
96 
97 /**
98  * Detailed debug messages, describing internal operations.
99  */
100 #define nsock_log_debug(...)     NSOCK_LOG_WRAP(NSOCK_LOG_DBG, __VA_ARGS__)
101 
102 /**
103  * High level debug messages, describing top level operations and external
104  * requests.
105  */
106 #define nsock_log_info(...)      NSOCK_LOG_WRAP(NSOCK_LOG_INFO, __VA_ARGS__)
107 
108 /**
109  * Error messages.
110  */
111 #define nsock_log_error(...)     NSOCK_LOG_WRAP(NSOCK_LOG_ERROR, __VA_ARGS__)
112 
113 
114 void __nsock_log_internal(nsock_loglevel_t loglevel, const char *file, int line,
115                           const char *func, const char *format, ...)
116                           __attribute__((format (printf, 5, 6)));
117 
118 #endif /* NSOCK_LOG_H */
119 
120