sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

dbg.h (2717B)


      1 /*
      2  * BSD 2-Clause License
      3  *
      4  * Copyright (C) 2014-2016, Lazaros Koromilas <lostd@2f30.org>
      5  * Copyright (C) 2014-2016, Dimitris Papastamos <sin@2f30.org>
      6  * Copyright (C) 2016-2022, Arun Prakash Jana <engineerarun@gmail.com>
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions are met:
     11  *
     12  * * Redistributions of source code must retain the above copyright notice, this
     13  *   list of conditions and the following disclaimer.
     14  *
     15  * * Redistributions in binary form must reproduce the above copyright notice,
     16  *   this list of conditions and the following disclaimer in the documentation
     17  *   and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #pragma once
     32 
     33 #ifdef DEBUG
     34 static int DEBUG_FD;
     35 
     36 static int xprintf(int fd, const char *fmt, ...)
     37 {
     38 	char buf[BUFSIZ];
     39 	int r;
     40 	va_list ap;
     41 
     42 	va_start(ap, fmt);
     43 	r = vsnprintf(buf, sizeof(buf), fmt, ap);
     44 	if (r > 0 && (unsigned int)r < sizeof(buf))
     45 		r = write(fd, buf, r);
     46 	va_end(ap);
     47 	return r;
     48 }
     49 
     50 static int enabledbg(void)
     51 {
     52 	FILE *fp = fopen("/tmp/nnndbg", "w");
     53 
     54 	if (!fp) {
     55 		perror("dbg(1)");
     56 
     57 		fp = fopen("./nnndbg", "w");
     58 		if (!fp) {
     59 			perror("dbg(2)");
     60 			return -1;
     61 		}
     62 	}
     63 
     64 	DEBUG_FD = dup(fileno(fp));
     65 	fclose(fp);
     66 	if (DEBUG_FD == -1) {
     67 		perror("dbg(3)");
     68 		return -1;
     69 	}
     70 
     71 	return 0;
     72 }
     73 
     74 static void disabledbg(void)
     75 {
     76 	close(DEBUG_FD);
     77 }
     78 
     79 #define STRINGIFY(x) #x
     80 #define TOSTRING(x) STRINGIFY(x)
     81 
     82 #define DPRINTF_D(x) xprintf(DEBUG_FD, "ln " TOSTRING(__LINE__) ": " #x "=%d\n", x)
     83 #define DPRINTF_U(x) xprintf(DEBUG_FD, "ln " TOSTRING(__LINE__) ": " #x "=%u\n", x)
     84 #define DPRINTF_S(x) xprintf(DEBUG_FD, "ln " TOSTRING(__LINE__) ": " #x "=%s\n", x)
     85 #define DPRINTF_P(x) xprintf(DEBUG_FD, "ln " TOSTRING(__LINE__) ": " #x "=%p\n", x)
     86 #else
     87 #define DPRINTF_D(x)
     88 #define DPRINTF_U(x)
     89 #define DPRINTF_S(x)
     90 #define DPRINTF_P(x)
     91 #endif /* DEBUG */