From 0e117bc2f97a21049f90a8639362183844f6aeea Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 3 Nov 2021 12:39:23 +0100 Subject: [PATCH] move from printf to logX() - unfinished. --- Makefile | 85 ++++++++++++++++++++++++++++++++++++++++------------- make.deps | 10 +++++++ src/debug.c | 39 ++++++++++++++++-------- src/debug.h | 40 ++++++++++++++++--------- src/fen.c | 23 ++++++++------- src/move.c | 11 ++++--- src/move.h | 6 ++-- src/piece.c | 4 +-- src/pool.c | 53 ++++++++++++++++++--------------- 9 files changed, 178 insertions(+), 93 deletions(-) create mode 100644 make.deps diff --git a/Makefile b/Makefile index 3aaaaaf..bcaae7f 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,28 @@ +# Makefile - GNU make only. +# +# Copyright (C) 2021 Bruno Raoult ("br") +# Licensed under the GNU General Public License v3.0 or later. +# Some rights reserved. See COPYING. +# * You should have received a copy of the GNU General Public License along with this +# program. If not, see . +# +# SPDX-License-Identifier: GPL-3.0-or-later # -BINDIR=./bin -SRCDIR=./src +BINDIR := ./bin +SRCDIR := ./src +OBJDIR := ./obj +DEPS := make.deps SRC=$(wildcard $(SRCDIR)/*.c) INC=$(wildcard $(SRCDIR)/*.h) +SRC_S=$(notdir $(SRC)) -BIN=fen pool piece move +CC=gcc + +.SECONDEXPANSION: +OBJ=$(addprefix $(OBJDIR)/,$(SRC_S:.c=.o)) +BIN=fen pool piece move debug CFLAGS += -std=gnu99 CFLAGS += -g @@ -17,28 +33,55 @@ CFLAGS += -Wextra #CFLAGS += -Werror CFLAGS += -Wmissing-declarations -all: clean $(BIN) +##################################### DEBUG flags +CFLAGS += -DDEBUG # global +CFLAGS += -DDEBUG_POOL # memory pools management +CFLAGS += -DDEBUG_FEN # FEN decoding + +#CFLAGS += -DDEBUG_MAINSLEEP # sleep 1 sec within main loop (SIGINTR test) +#CFLAGS += -DDEBUG_EVAL2 # eval 2 +#CFLAGS += -DDEBUG_EVAL3 # eval 3 +#CFLAGS += -DDEBUG_MEM # malloc + +all: $(OBJ) $(BIN) + @echo CFLAGS used: $(CFLAGS) + +$(DEPS): $(SRC) $(INC) + @echo generating dependancies. + @$(CC) -MM $(SRC) > $@ + @sed -i "s|\(.*\.o\):|${OBJDIR}/\0:|" $@ + +include $(DEPS) .PHONY: clean clean: - rm -rf *.o core $(BIN) + rm -rf $(OBJ) core $(BIN) $(DEPS) -fen: CFLAGS+=-DFENBIN -fen: $(SRC) - echo SRC=$(SRC) - $(CC) $(CFLAGS) $? -o $@ +#$(OBJ): $(OBJDIR)/%.o: $(SRCDIR)/%.c +# @mkdir -p $(@D) +# $(CC) -c $(CFLAGS) -o $@ $< +$(OBJDIR)/%.o: $(SRCDIR)/%.c + @mkdir -p $(@D) + @echo compiling $@. + @$(CC) -c $(CFLAGS) -o $@ $< -pool: CFLAGS+=-DPOOLBIN -pool: $(SRC) - echo SRC=$(SRC) - $(CC) $(CFLAGS) $? -o $@ +#fen: CFLAGS+=-DBIN_$$@ +$(BIN): $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c + @echo compiling $@. + @$(CC) -DBIN_$@ $(CFLAGS) $^ -o $@ -piece: CFLAGS+=-DPIECEBIN -piece: $(SRC) - echo SRC=$(SRC) - $(CC) $(CFLAGS) $? -o $@ +#pool: CFLAGS+=-DPOOLBIN +#pool: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c +# $(CC) $(CFLAGS) $^ -o $@ -move: CFLAGS+=-DMOVEBIN -move: $(SRC) - echo SRC=$(SRC) - $(CC) $(CFLAGS) $? -o $@ +# piece: CFLAGS+=-DPIECEBIN +# piece: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c +# $(CC) $(CFLAGS) $^ -o $@ + +# move: CFLAGS+=-DMOVEBIN +# move: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c +# $(CC) $(CFLAGS) $^ -o $@ + +# debug: CFLAGS+=-DDEBUGBIN +# debug: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c +# $(CC) $(CFLAGS) $^ -o $@ diff --git a/make.deps b/make.deps new file mode 100644 index 0000000..9e9963b --- /dev/null +++ b/make.deps @@ -0,0 +1,10 @@ +./obj/debug.o:: src/debug.c src/debug.h +./obj/fen.o:: src/fen.c src/debug.h src/chessdefs.h src/position.h src/board.h \ + src/list.h src/fen.h src/piece.h src/pool.h +./obj/move.o:: src/move.c src/chessdefs.h src/piece.h src/board.h src/list.h \ + src/position.h src/pool.h src/move.h +./obj/piece.o:: src/piece.c src/chessdefs.h src/piece.h src/board.h src/list.h \ + src/position.h src/pool.h +./obj/pool.o:: src/pool.c src/list.h src/pool.h src/debug.h +./obj/position.o:: src/position.c src/chessdefs.h src/position.h src/board.h \ + src/list.h src/fen.h src/piece.h src/pool.h diff --git a/src/debug.c b/src/debug.c index 5546d08..4a78ffb 100644 --- a/src/debug.c +++ b/src/debug.c @@ -21,19 +21,27 @@ #define MILLISEC 1000000 /* milli sec in sec */ static int64_t timer_start; /* in nanosecond */ +static uint32_t debug_level=0; -int clock_gettime(clockid_t clockid, struct timespec *tp); +void debug_level_set(uint32_t level) +{ + debug_level = level;; -void debug_init() + log(0, "debug level set to %u\n", level); +} + +void debug_init(uint32_t level) { struct timespec timer; + + debug_level_set(level); if (!clock_gettime(CLOCK_MONOTONIC, &timer)) { timer_start = timer.tv_sec * NANOSEC + timer.tv_nsec; } else { timer_start = 0; } - log("timer started.\n"); + log(0, "timer started.\n"); } inline static int64_t timer_elapsed() @@ -51,9 +59,12 @@ inline static int64_t timer_elapsed() * @src : source filename (or NULL) * @line : line number */ -void debug(bool timestamp, uint32_t indent, const char *src, +void debug(uint32_t level, bool timestamp, uint32_t indent, const char *src, uint32_t line, const char *fmt, ...) { + if (level > debug_level) + return; + va_list ap; if (indent) @@ -68,28 +79,30 @@ void debug(bool timestamp, uint32_t indent, const char *src, if (src) { if (line) printf("[%s:%u] ", src, line); - else { + else printf("[%s] ", src); - } } va_start(ap, fmt); vprintf(fmt, ap); va_end(ap); } -#ifdef DEBUGBIN +#ifdef BIN_debug #include int main() { int foo=1; - debug_init(); + debug_init(5); - log("log=%d\n", foo++); - log_i(1, "log_i=%d\n", foo++); - log_i(2, "log_i=%d\n", foo++); - log_it(3, "log_it=%d\n", foo++); - log_f("log_f=%d\n", foo++); + log(0, "log0=%d\n", foo++); + log(1, "log1=%d\n", foo++); + log(2, "log2=%d\n", foo++); + log_i(2, "log_i 2=%d\n", foo++); + log_i(5, "log_i 5=%d\n", foo++); + log_i(6, "log_i 6=%d\n", foo++); + log_it(4, "log_it 4=%d\n", foo++); + log_f(1, "log_f 5=%d\n", foo++); } #endif diff --git a/src/debug.h b/src/debug.h index 5fd4214..0b8cbeb 100644 --- a/src/debug.h +++ b/src/debug.h @@ -17,38 +17,50 @@ #include #include -#ifdef DEBUG - -void debug_init(void); - -void debug(bool timestamp, uint32_t indent, +void debug_init(uint32_t level); +void debug_level_set(uint32_t level); +void debug_devel_set(uint32_t level); +void debug(uint32_t level, bool timestamp, uint32_t indent, const char *src, uint32_t line, const char *, ...); -/* format: func name, no indent, no timestamp +#ifdef DEBUG + +/* format: only printf + */ +#define log(level, fmt, args...) \ + debug((level), false, 0, NULL, 0, fmt, ##args) + +/* format: func name, no line number, indent, no timestamp * foo:15 val=2 */ -#define log(fmt, args...) debug(false, 0, __func__, __LINE__, fmt, ##args) +#define log_f(level, fmt, args...) \ + debug((level), false, 0, __func__, 0, fmt, ##args) /* format : func name, indent, no timestamp * foo:15 val=2 */ -#define log_i(i, fmt, args...) debug(false, (i), __func__, __LINE__, fmt, args) +#define log_i(level, fmt, args...) \ + debug((level), false, (level), __func__, __LINE__, fmt, args) /* format : func name, indent, timestamp * []foo:15 val=2 */ -#define log_it(i, fmt, args...) debug(true, (i), __func__, __LINE__, fmt, args) +#define log_it(level, fmt, args...) \ + debug((level), true, (level), __func__, __LINE__, fmt, args) /* format: file name, no indent, no timestamp * foo:15 val=2 + * + * #define log_f(level, fmt, args...) \ + * debug((level), false, 0, __FILE__, __LINE__, fmt, args) */ -#define log_f(fmt, args...) debug(false, 0, __FILE__, __LINE__, fmt, args) - - #else -#define log(...) -#define f +#define log(level, fmt, args...) +#define log_i(...) +#define log_it(...) +#define log_f(...) + #endif /* DEBUG */ #endif /* DEBUG_H */ diff --git a/src/fen.c b/src/fen.c index caa3e2b..f3e0cf4 100644 --- a/src/fen.c +++ b/src/fen.c @@ -17,6 +17,7 @@ #include #include +#include "debug.h" #include "chessdefs.h" #include "position.h" #include "board.h" @@ -77,14 +78,13 @@ pos_t *fen2pos(pos_t *pos, char *fen) piece = KING; //goto set_square; set_square: - /*printf("f=%d r=%d *p=%c piece=%c color=%d\n", +# ifdef DEBUG_FEN + log_i(5, "f=%d r=%d *p=%c piece=%c color=%d\n", file, rank, *p, cp, color); - */ +# endif piece |= color; - //board[SQ88(file, rank)]->piece = piece; board[SQ88(file, rank)].piece = piece; piece_add(pos, piece, SQUARE(file, rank)); - //board[SQ88(file, rank)]->piece |= isupper(*p)? WHITE: BLACK; file++; break; case '/': @@ -98,12 +98,14 @@ pos_t *fen2pos(pos_t *pos, char *fen) } } } - /*for (rank = 7; rank >= 0; --rank) { +# ifdef DEBUG_FEN + for (rank = 7; rank >= 0; --rank) { for (file = 0; file < 8; ++file) { - printf("%2x ", board[SQ88(file, rank)]->piece); + log(5, "%02x ", board[SQ88(file, rank)].piece); } - putchar('\n'); - }*/ + log(5, "\n"); + } +# endif /* 2) next move color */ @@ -150,16 +152,17 @@ pos_t *fen2pos(pos_t *pos, char *fen) * 6) current move number */ SKIP_BLANK(p); - //printf("pos=%d\n", (int)(p-fen)); + log_i(5, "pos=%d\n", (int)(p-fen)); sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove); return pos; } -#ifdef FENBIN +#ifdef BIN_fen int main(int ac, char**av) { pos_t *pos; + debug_init(3); piece_pool_init(); pos = pos_create(); if (ac == 1) { diff --git a/src/move.c b/src/move.c index e56a9b0..9456f2a 100644 --- a/src/move.c +++ b/src/move.c @@ -244,8 +244,6 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece) int pseudo_moves_castle(pos_t *pos) { - //piece_t piece = PIECE(ppiece->piece); - //square_t square = ppiece->square, new; unsigned char color = pos->turn; board_t *board = pos->board; unsigned char rank1, castle_K, castle_Q; @@ -302,8 +300,8 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece) move_t *move; int count = 0; - //printf("square=%#04x piece=%#04x color = %s\n", square, piece, color==WHITE? "white": "black"); - /*printf("%s: pos:%p piece:%d [%s] at %#04x[%c%c]\n", __func__, pos, piece, + /*printf("square=%#04x piece=%#04x color = %s\n", square, piece, color==WHITE? "white": "black"); + printf("%s: pos:%p piece:%d [%s] at %#04x[%c%c]\n", __func__, pos, piece, P_NAME(piece), square, FILE2C(GET_F(square)), @@ -373,17 +371,18 @@ int moves_get(pos_t *pos) }; */ -#ifdef MOVEBIN +#ifdef BIN_move #include "fen.h" +#include "debug.h" int main(int ac, char**av) { pos_t *pos; + debug_init(1); pos = pos_create(); piece_pool_init(); moves_pool_init(); - if (ac == 1) { pos_startpos(pos); } else { diff --git a/src/move.h b/src/move.h index 5d57c51..e2399da 100644 --- a/src/move.h +++ b/src/move.h @@ -11,8 +11,8 @@ * */ -#ifndef ROOK_H -#define ROOK_H +#ifndef MOVE_H +#define MOVE_H #include "chessdefs.h" #include "position.h" @@ -54,4 +54,4 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *piece); int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece); int moves_get(pos_t *pos); -#endif +#endif /* MODE_H */ diff --git a/src/piece.c b/src/piece.c index a88ee9c..f7e3b0e 100644 --- a/src/piece.c +++ b/src/piece.c @@ -95,13 +95,11 @@ piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square) return new; } -#ifdef PIECEBIN +#ifdef BIN_piece #include "fen.h" int main(int ac, char**av) { - //size_t i; pos_t *pos; - //piece_list_t *plist; pos = pos_create(); piece_pool_init(); diff --git a/src/pool.c b/src/pool.c index af866ae..70917e0 100644 --- a/src/pool.c +++ b/src/pool.c @@ -22,16 +22,16 @@ #include #include "list.h" #include "pool.h" +#include "debug.h" void pool_stats(pool_t *pool) { if (pool) { - printf("[%s] pool [%p]: ", pool->name, (void *)pool); - printf("avail:%u ", pool->available); - printf("alloc:%u ", pool->allocated); - printf("grow:%u ", pool->growsize); - printf("eltsize:%lu ", pool->eltsize); - printf("\n"); +# ifdef DEBUG_POOL + log_f(1, "[%s] pool [%p]: avail:%u alloc:%u grow:%u eltsize:%lu\n", + pool->name, (void *)pool, pool->available, pool->allocated, + pool->growsize, pool->eltsize); +# endif } } @@ -39,8 +39,10 @@ pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize) { pool_t *pool; - printf("%s: name=[%s] growsize=%u eltsize=%lu\n", - __func__, name, growsize, eltsize); +# ifdef DEBUG_POOL + log_f(1, "name=[%s] growsize=%u eltsize=%lu\n", + name, growsize, eltsize); +# endif /* we need at least this space in struct */ if (eltsize < sizeof (struct list_head)) return NULL; @@ -57,15 +59,14 @@ pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize) static uint32_t _pool_add(pool_t *pool, struct list_head *elt) { - /* - printf("%s: pool=%p &head=%p elt=%p off1=%lu off2=%lu\n", - __func__, +# ifdef DEBUG_POOL + log_f(10, "pool=%p &head=%p elt=%p off1=%lu off2=%lu\n", (void *)pool, (void *)&pool->head, (void *)elt, (void *)&pool->head-(void *)pool, offsetof(pool_t, head)); - */ +# endif list_add(elt, &pool->head); return ++pool->available; @@ -81,7 +82,6 @@ static struct list_head *_pool_get(pool_t *pool) struct list_head *res = pool->head.next; pool->available--; list_del(res); - // printf("%s: res=%p\n", __func__, (void *)res); return res; } @@ -93,28 +93,34 @@ void *pool_get(pool_t *pool) void *alloc = malloc(pool->eltsize * pool->growsize); void *cur; uint32_t i; - printf("+++ %s [%s]: growing pool from %u to %u elements.\n", __func__, +# ifdef DEBUG_POOL + log_f(2, "[%s]: growing pool from %u to %u elements.\n", pool->name, pool->allocated, pool->allocated + pool->growsize); - +# endif if (!alloc) return NULL; - //printf(" (old=%u)\n", pool->allocated); +# ifdef DEBUG_POOL + log_f(5, " (old=%u)\n", pool->allocated); +# endif pool->allocated += pool->growsize; - //printf(" (new=%u)\n", pool->allocated); +# ifdef DEBUG_POOL + log_f(5, " (new=%u)\n", pool->allocated); +# endif for (i = 0; i < pool->growsize; ++i) { cur = alloc + i * pool->eltsize; - //printf("%s: alloc=%p cur=%p\n", __func__, alloc, cur); +# ifdef DEBUG_POOL + log_f(5, "alloc=%p cur=%p\n", alloc, cur); +# endif _pool_add(pool, (struct list_head *)cur); } pool_stats(pool); } - //printf("%s: returning %p pointer\n", __func__, res); return _pool_get(pool); } -#ifdef POOLBIN +#ifdef BIN_pool struct d { uint16_t data1; char c; @@ -132,7 +138,8 @@ int main(int ac, char**av) char ccur='z'; struct d *elt; - printf("%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt), + debug_init(3); + log_f(1, "%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt), sizeof(*elt), offsetof(struct d, list)); if ((pool = pool_init("dummy", 3, sizeof(*elt)))) { @@ -140,7 +147,7 @@ int main(int ac, char**av) for (int cur=1; curdata1 = icur++; @@ -150,7 +157,7 @@ int main(int ac, char**av) pool_stats(pool); action = 1; } else { /* remove one elt from list */ - printf("deleting %d elements\n", total); + log_f(2, "deleting %d elements\n", total); for (int i = 0; i < total; ++i) { if (!list_empty(&head)) { elt = list_last_entry(&head, struct d, list);