debug.h
Go to the documentation of this file.
1 /* vim: set ts=2 expandtab: */
2 /**
3  * @file debug.h
4  * @brief debug macros for the Commotion daemon
5  *
6  * @author Josh King (jheretic), jking@chambana.net
7  *
8  * @internal
9  * Created 03/07/2013
10  * Revision $Id: doxygen.commotion.templates,v 0.1 2013/01/01 09:00:00 jheretic Exp $
11  * Compiler gcc/g++
12  * Company The Open Technology Institute
13  * Copyright Copyright (c) 2013, Josh King
14  *
15  * This file is part of Commotion, Copyright (c) 2013, Josh King
16  *
17  * Commotion is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Affero General Public License as
19  * published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * Commotion is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Affero General Public License for more details.
26  *
27  * You should have received a copy of the GNU Affero General Public License
28  * along with Commotion. If not, see <http://www.gnu.org/licenses/>.
29  * ------------------------------------------------------------------
30  * Copyright (c) 2010, Zed A. Shaw and Mongrel2 Project Contributors.
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions are
35  * met:
36  *
37  * * Redistributions of source code must retain the above copyright
38  * notice, this list of conditions and the following disclaimer.
39  *
40  * * Redistributions in binary form must reproduce the above copyright
41  * notice, this list of conditions and the following disclaimer in the
42  * documentation and/or other materials provided with the distribution.
43  *
44  * * Neither the name of the Mongrel2 Project, Zed A. Shaw, nor the names
45  * of its contributors may be used to endorse or promote products
46  * derived from this software without specific prior written
47  * permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
50  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
51  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
53  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
54  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
55  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
56  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
57  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
58  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  *
61  * =====================================================================================
62  */
63 
64 #ifndef __DEBUG_H__
65 #define __DEBUG_H__
66 
67 #include <stdio.h>
68 #include <errno.h>
69 #include <string.h>
70 
71 #undef LOG
72 #undef DEBUG
73 #undef CLEAN_ERRNO
74 #undef ERROR
75 #undef WARN
76 #undef INFO
77 #undef CHECK
78 #undef SENTINEL
79 #undef CHECK_MEM
80 #undef CHECK_DEBUG
81 
82 #ifdef USESYSLOG
83 #include <syslog.h>
84 #define LOG(M, ...) syslog(M, ##__VA_ARGS__)
85 #else
86 #define LOG(M, N, ...) fprintf(stderr, "["M"] " N, ##__VA_ARGS__)
87 #define LOG_INFO "LOG_INFO"
88 #define LOG_WARNING "LOG_WARNING"
89 #define LOG_ERR "LOG_ERR"
90 #define LOG_DEBUG "LOG_DEBUG"
91 #endif
92 
93 #if defined(NDEBUG)
94 #define DEBUG(M, ...)
95 #else
96 #define DEBUG(M, ...) LOG(LOG_DEBUG, "(%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
97 #endif
98 
99 #define CLEAN_ERRNO() (errno == 0 ? "None" : strerror(errno))
100 
101 #define ERROR(M, ...) LOG(LOG_ERR, "(%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, CLEAN_ERRNO(), ##__VA_ARGS__)
102 
103 #define WARN(M, ...) LOG(LOG_WARNING, "(%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, CLEAN_ERRNO(), ##__VA_ARGS__)
104 
105 #define INFO(M, ...) LOG(LOG_INFO, "(%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
106 
107 #define CHECK(A, M, ...) do { if(!(A)) { ERROR(M, ##__VA_ARGS__); errno=0; goto error; } } while (0)
108 
109 #define SENTINEL(M, ...) { ERROR(M, ##__VA_ARGS__); errno=0; goto error; }
110 
111 #define CHECK_MEM(A) CHECK((A), "Out of memory.")
112 
113 #define CHECK_DEBUG(A, M, ...) do { if(!(A)) { DEBUG(M, ##__VA_ARGS__); errno=0; goto error; } } while(0)
114 
115 #endif