Project Alice
Loading...
Searching...
No Matches
pcp_logger.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2014 by Cisco Systems, Inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef PCP_LOGGER_H_
27#define PCP_LOGGER_H_
28
29#define ERR_BUF_LEN 256
30
31#include "pcp.h"
32
33#ifdef NDEBUG
34#undef DEBUG
35#endif
36
37void pcp_logger_init(void);
38
39#ifdef WIN32
40void pcp_logger(pcp_loglvl_e log_level, const char* fmt, ...);
41#else
42void pcp_logger(pcp_loglvl_e log_level, const char* fmt, ...)
43 __attribute__((format(printf, 2, 3)));
44#endif
45
46#ifdef DEBUG
47
48#ifndef PCP_MAX_LOG_LEVEL
49// Maximal log level for compile-time check
50#define PCP_MAX_LOG_LEVEL PCP_LOGLVL_DEBUG
51#endif
52
53#define PCP_LOG(level, fmt, ...) { if (level<=PCP_MAX_LOG_LEVEL) \
54pcp_logger(level, "FILE: %s:%d; Func: %s:\n " fmt,\
55__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__); }
56
57#define PCP_LOG_END(level) { if (level<=PCP_MAX_LOG_LEVEL) \
58pcp_logger(level, "FILE: %s:%d; Func: %s: END \n " ,\
59__FILE__, __LINE__, __FUNCTION__); }
60
61#define PCP_LOG_BEGIN(level) { if (level<=PCP_MAX_LOG_LEVEL) \
62pcp_logger(level, "FILE: %s:%d; Func: %s: BEGIN \n ",\
63__FILE__, __LINE__, __FUNCTION__); }
64
65#else //DEBUG
66#ifndef PCP_MAX_LOG_LEVEL
67// Maximal log level for compile-time check
68#define PCP_MAX_LOG_LEVEL PCP_LOGLVL_INFO
69#endif
70
71#define PCP_LOG(level, fmt, ...)
72
73#define PCP_LOG_END(level)
74
75#define PCP_LOG_BEGIN(level)
76
77#endif // DEBUG
78#if PCP_MAX_LOG_LEVEL>=PCP_LOGLVL_DEBUG
79#define PCP_LOG_DEBUG(fmt, ...) PCP_LOG(PCP_LOGLVL_DEBUG, fmt, __VA_ARGS__)
80#else
81#define PCP_LOG_DEBUG(fmt, ...)
82#endif
83
84#if PCP_MAX_LOG_LEVEL>=PCP_LOGLVL_INFO
85#define PCP_LOG_FLOW(f, msg) \
86do { \
87 if (pcp_log_level >= PCP_LOGLVL_INFO) { \
88 char src_buf[INET6_ADDRSTRLEN]="Unknown"; \
89 char dst_buf[INET6_ADDRSTRLEN]="Unknown"; \
90 char pcp_buf[INET6_ADDRSTRLEN]="Unknown"; \
91\
92 inet_ntop(AF_INET6, &f->kd.src_ip, src_buf, sizeof(src_buf)); \
93 inet_ntop(AF_INET6, &f->kd.map_peer.dst_ip, dst_buf, \
94 sizeof(dst_buf)); \
95 inet_ntop(AF_INET6, &f->kd.pcp_server_ip, pcp_buf, sizeof(pcp_buf)); \
96 PCP_LOG(PCP_LOGLVL_INFO, \
97 "%s(PCP server: %s; Int. addr: [%s]:%d; Dest. addr: [%s]:%d; Key bucket: %d)", \
98 msg, pcp_buf, src_buf, ntohs(f->kd.map_peer.src_port), dst_buf, ntohs(f->kd.map_peer.dst_port), f->key_bucket); \
99 } \
100} while(0)
101#else
102#define PCP_LOG_FLOW(f, msg) do{} while(0)
103#endif
104
105void pcp_strerror(int errnum, char *buf, size_t buflen);
106
107#endif /* PCP_LOGGER_H_ */
pcp_loglvl_e
Definition: pcp.h:84
void pcp_strerror(int errnum, char *buf, size_t buflen)
Definition: pcp_logger.c:165
void pcp_logger_init(void)
Definition: pcp_logger.c:50
void pcp_logger(pcp_loglvl_e log_level, const char *fmt,...) __attribute__((format(printf