aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-06-17 17:39:06 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-06-17 17:39:30 +0200
commite986713657bc5f9880e91ed49dd9b0d0227f048d (patch)
tree908d371e60546795c4ca1f9a3b67571b964203c5 /src
parent02ce9adf6a9168ec66c322ce9e5a5cb9fe1e60a5 (diff)
downloadnissy-core-e986713657bc5f9880e91ed49dd9b0d0227f048d.tar.gz
nissy-core-e986713657bc5f9880e91ed49dd9b0d0227f048d.zip
Use callback function to log to stderr
Diffstat (limited to '')
-rw-r--r--src/cube.c22
-rw-r--r--src/cube.h4
-rw-r--r--src/cube_generic.h18
-rw-r--r--src/cube_public.h6
-rw-r--r--src/cube_transform_with_switch.h6
-rw-r--r--src/io_cube.h12
-rw-r--r--src/io_move_trans.h2
-rw-r--r--src/solve_generic.h26
-rw-r--r--src/solve_h48.h26
9 files changed, 72 insertions, 50 deletions
diff --git a/src/cube.c b/src/cube.c
index 97f7844..890e3fb 100644
--- a/src/cube.c
+++ b/src/cube.c
@@ -1,20 +1,32 @@
1#include <inttypes.h> 1#include <inttypes.h>
2#include <stdarg.h>
2#include <stdbool.h> 3#include <stdbool.h>
3#include <string.h> 4#include <string.h>
4 5
6void (*nissy_log)(const char *, va_list);
7
8void
9_log(const char *str, ...) /* TODO: rename */
10{
11 va_list args;
12
13 if (nissy_log != NULL) {
14 va_start(args, str);
15 nissy_log(str, args);
16 va_end(args);
17 }
18}
19
5#ifdef DEBUG 20#ifdef DEBUG
6#include <stdio.h>
7#define _static 21#define _static
8#define _static_inline 22#define _static_inline
9#define DBG_LOG(...) fprintf(stderr, __VA_ARGS__) 23#define DBG_WARN(condition, ...) if (!(condition)) _log(__VA_ARGS__);
10#define DBG_WARN(condition, ...) if (!(condition)) DBG_LOG(__VA_ARGS__);
11#define DBG_ASSERT(condition, retval, ...) \ 24#define DBG_ASSERT(condition, retval, ...) \
12 if (!(condition)) { DBG_LOG(__VA_ARGS__); return retval; } 25 if (!(condition)) { _log(__VA_ARGS__); return retval; }
13 26
14#else 27#else
15#define _static static 28#define _static static
16#define _static_inline static inline 29#define _static_inline static inline
17#define DBG_LOG(...)
18#define DBG_WARN(condition, ...) 30#define DBG_WARN(condition, ...)
19#define DBG_ASSERT(condition, retval, ...) 31#define DBG_ASSERT(condition, retval, ...)
20#endif 32#endif
diff --git a/src/cube.h b/src/cube.h
index fb96823..4c2c492 100644
--- a/src/cube.h
+++ b/src/cube.h
@@ -1,3 +1,5 @@
1/* include: inttypes, stdarg, stdbool, string */
2
1/* 3/*
2All the functions below return 0 in case of success and a positive 4All the functions below return 0 in case of success and a positive
3number in case of error, unless otherwise specified. See (TODO: 5number in case of error, unless otherwise specified. See (TODO:
@@ -81,3 +83,5 @@ int64_t nissy_solve(
81 const void *data, 83 const void *data,
82 char *solutions 84 char *solutions
83); 85);
86
87void nissy_setlogger(void (*logger_function)(const char *, va_list));
diff --git a/src/cube_generic.h b/src/cube_generic.h
index 7ff9992..d3080f7 100644
--- a/src/cube_generic.h
+++ b/src/cube_generic.h
@@ -65,16 +65,16 @@ isconsistent(cube_t cube)
65 return true; 65 return true;
66 66
67inconsistent_ep: 67inconsistent_ep:
68 DBG_LOG("Inconsistent EP\n"); 68 _log("Inconsistent EP\n");
69 return false; 69 return false;
70inconsistent_cp: 70inconsistent_cp:
71 DBG_LOG("Inconsistent CP\n"); 71 _log("Inconsistent CP\n");
72 return false; 72 return false;
73inconsistent_eo: 73inconsistent_eo:
74 DBG_LOG("Inconsistent EO\n"); 74 _log("Inconsistent EO\n");
75 return false; 75 return false;
76inconsistent_co: 76inconsistent_co:
77 DBG_LOG("Inconsistent CO\n"); 77 _log("Inconsistent CO\n");
78 return false; 78 return false;
79} 79}
80 80
@@ -114,13 +114,13 @@ issolvable(cube_t cube)
114 return true; 114 return true;
115 115
116issolvable_parity: 116issolvable_parity:
117 DBG_LOG("EP and CP parities are different\n"); 117 _log("EP and CP parities are different\n");
118 return false; 118 return false;
119issolvable_eo: 119issolvable_eo:
120 DBG_LOG("Odd number of flipped edges\n"); 120 _log("Odd number of flipped edges\n");
121 return false; 121 return false;
122issolvable_co: 122issolvable_co:
123 DBG_LOG("Sum of corner orientation is not multiple of 3\n"); 123 _log("Sum of corner orientation is not multiple of 3\n");
124 return false; 124 return false;
125} 125}
126 126
@@ -161,7 +161,7 @@ applymoves_finish:
161 return cube; 161 return cube;
162 162
163applymoves_error: 163applymoves_error:
164 DBG_LOG("applymoves error\n"); 164 _log("applymoves error\n");
165 return zero; 165 return zero;
166} 166}
167 167
@@ -238,7 +238,7 @@ move(cube_t c, uint8_t m)
238 case _move_B3: 238 case _move_B3:
239 return _move(B3, c); 239 return _move(B3, c);
240 default: 240 default:
241 DBG_LOG("move error, unknown move\n"); 241 _log("move error, unknown move\n");
242 return zero; 242 return zero;
243 } 243 }
244} 244}
diff --git a/src/cube_public.h b/src/cube_public.h
index 65d5d54..e2255c5 100644
--- a/src/cube_public.h
+++ b/src/cube_public.h
@@ -166,3 +166,9 @@ nissy_solve(
166 /* TODO: move solve_generic here? */ 166 /* TODO: move solve_generic here? */
167 return -1; 167 return -1;
168} 168}
169
170void
171nissy_setlogger(void (*log)(const char *, va_list))
172{
173 nissy_log = log;
174}
diff --git a/src/cube_transform_with_switch.h b/src/cube_transform_with_switch.h
index de62bf5..bc066fb 100644
--- a/src/cube_transform_with_switch.h
+++ b/src/cube_transform_with_switch.h
@@ -118,7 +118,7 @@ transform_edges(cube_t c, uint8_t t)
118 case _trans_BLm: 118 case _trans_BLm:
119 return _trans_edges_mirrored(BLm, c); 119 return _trans_edges_mirrored(BLm, c);
120 default: 120 default:
121 DBG_LOG("transform error, unknown transformation\n"); 121 _log("transform error, unknown transformation\n");
122 return zero; 122 return zero;
123 } 123 }
124} 124}
@@ -224,7 +224,7 @@ transform_corners(cube_t c, uint8_t t)
224 case _trans_BLm: 224 case _trans_BLm:
225 return _trans_corners_mirrored(BLm, c); 225 return _trans_corners_mirrored(BLm, c);
226 default: 226 default:
227 DBG_LOG("transform error, unknown transformation\n"); 227 _log("transform error, unknown transformation\n");
228 return zero; 228 return zero;
229 } 229 }
230} 230}
@@ -330,7 +330,7 @@ transform(cube_t c, uint8_t t)
330 case _trans_BLm: 330 case _trans_BLm:
331 return _trans_mirrored(BLm, c); 331 return _trans_mirrored(BLm, c);
332 default: 332 default:
333 DBG_LOG("transform error, unknown transformation\n"); 333 _log("transform error, unknown transformation\n");
334 return zero; 334 return zero;
335 } 335 }
336} 336}
diff --git a/src/io_cube.h b/src/io_cube.h
index 3e7880e..72113ff 100644
--- a/src/io_cube.h
+++ b/src/io_cube.h
@@ -49,7 +49,7 @@ readcube(const char *format, const char *buf)
49 if (!strcmp(format, ioformat[i].name)) 49 if (!strcmp(format, ioformat[i].name))
50 return ioformat[i].read(buf); 50 return ioformat[i].read(buf);
51 51
52 DBG_LOG("Cannot read cube in the given format\n"); 52 _log("Cannot read cube in the given format\n");
53 return zero; 53 return zero;
54} 54}
55 55
@@ -76,7 +76,7 @@ writecube(const char *format, cube_t cube, char *buf)
76 errormsg = "ERROR: format"; 76 errormsg = "ERROR: format";
77 77
78writecube_error: 78writecube_error:
79 DBG_LOG("writecube error, see stdout for details\n"); 79 _log("writecube error, see stdout for details\n");
80 len = strlen(errormsg); 80 len = strlen(errormsg);
81 memcpy(buf, errormsg, len); 81 memcpy(buf, errormsg, len);
82 buf[len] = '\n'; 82 buf[len] = '\n';
@@ -93,7 +93,7 @@ readco(const char *str)
93 if (*str == '2') 93 if (*str == '2')
94 return _ctwist_ccw; 94 return _ctwist_ccw;
95 95
96 DBG_LOG("Error reading CO\n"); 96 _log("Error reading CO\n");
97 return _error; 97 return _error;
98} 98}
99 99
@@ -107,7 +107,7 @@ readcp(const char *str)
107 !strncmp(str, cornerstralt[c], 3)) 107 !strncmp(str, cornerstralt[c], 3))
108 return c; 108 return c;
109 109
110 DBG_LOG("Error reading CP\n"); 110 _log("Error reading CP\n");
111 return _error; 111 return _error;
112} 112}
113 113
@@ -119,7 +119,7 @@ readeo(const char *str)
119 if (*str == '1') 119 if (*str == '1')
120 return _eflip; 120 return _eflip;
121 121
122 DBG_LOG("Error reading EO\n"); 122 _log("Error reading EO\n");
123 return _error; 123 return _error;
124} 124}
125 125
@@ -132,7 +132,7 @@ readep(const char *str)
132 if (!strncmp(str, edgestr[e], 2)) 132 if (!strncmp(str, edgestr[e], 2))
133 return e; 133 return e;
134 134
135 DBG_LOG("Error reading EP\n"); 135 _log("Error reading EP\n");
136 return _error; 136 return _error;
137} 137}
138 138
diff --git a/src/io_move_trans.h b/src/io_move_trans.h
index e36b8e5..14130c7 100644
--- a/src/io_move_trans.h
+++ b/src/io_move_trans.h
@@ -49,7 +49,7 @@ readtrans(const char *buf)
49 if (!strncmp(buf, transstr[t], 11)) 49 if (!strncmp(buf, transstr[t], 11))
50 return t; 50 return t;
51 51
52 DBG_LOG("readtrans error\n"); 52 _log("readtrans error\n");
53 return _error; 53 return _error;
54} 54}
55 55
diff --git a/src/solve_generic.h b/src/solve_generic.h
index 4e7833f..a8d6b39 100644
--- a/src/solve_generic.h
+++ b/src/solve_generic.h
@@ -49,11 +49,11 @@ solve(
49 solutions 49 solutions
50 ); 50 );
51 } else { 51 } else {
52 DBG_LOG("solve: unknown solver '%s'\n", solver); 52 _log("solve: unknown solver '%s'\n", solver);
53 return -1; 53 return -1;
54 } 54 }
55 55
56 DBG_LOG("solve: error\n"); 56 _log("solve: error\n");
57 return -1; 57 return -1;
58} 58}
59 59
@@ -63,7 +63,7 @@ solve_generic_appendsolution(dfsarg_generic_t *arg)
63 int strl; 63 int strl;
64 64
65 strl = writemoves(arg->moves, arg->depth, *arg->nextsol); 65 strl = writemoves(arg->moves, arg->depth, *arg->nextsol);
66 DBG_LOG("Solution found: %s\n", *arg->nextsol); 66 _log("Solution found: %s\n", *arg->nextsol);
67 *arg->nextsol += strl; 67 *arg->nextsol += strl;
68 **arg->nextsol = '\n'; 68 **arg->nextsol = '\n';
69 (*arg->nextsol)++; 69 (*arg->nextsol)++;
@@ -126,12 +126,12 @@ solve_generic(
126 int64_t ret, tmp, first; 126 int64_t ret, tmp, first;
127 127
128 if (!issolvable(cube)) { 128 if (!issolvable(cube)) {
129 DBG_LOG("solve: cube is not solvable\n"); 129 _log("solve: cube is not solvable\n");
130 return -1; 130 return -1;
131 } 131 }
132 132
133 if (issolved(cube)) { 133 if (issolved(cube)) {
134 DBG_LOG("solve: cube is already solved\n"); 134 _log("solve: cube is already solved\n");
135 sols[0] = '\n'; 135 sols[0] = '\n';
136 sols[1] = 0; 136 sols[1] = 0;
137 return 1; 137 return 1;
@@ -141,32 +141,32 @@ solve_generic(
141 "solve: NISS not implemented yet, 'nisstype' ignored\n"); 141 "solve: NISS not implemented yet, 'nisstype' ignored\n");
142 142
143 if (minmoves < 0) { 143 if (minmoves < 0) {
144 DBG_LOG("solve: 'minmoves' is negative, setting to 0\n"); 144 _log("solve: 'minmoves' is negative, setting to 0\n");
145 minmoves = 0; 145 minmoves = 0;
146 } 146 }
147 147
148 if (maxmoves < 0) { 148 if (maxmoves < 0) {
149 DBG_LOG("solve: invalid 'maxmoves', setting to 20\n"); 149 _log("solve: invalid 'maxmoves', setting to 20\n");
150 maxmoves = 20; 150 maxmoves = 20;
151 } 151 }
152 152
153 if (maxsols < 0) { 153 if (maxsols < 0) {
154 DBG_LOG("solve: 'maxsols' is negative\n"); 154 _log("solve: 'maxsols' is negative\n");
155 return -1; 155 return -1;
156 } 156 }
157 157
158 if (maxsols == 0) { 158 if (maxsols == 0) {
159 DBG_LOG("solve: 'maxsols' is 0\n"); 159 _log("solve: 'maxsols' is 0\n");
160 return 0; 160 return 0;
161 } 161 }
162 162
163 if (sols == NULL) { 163 if (sols == NULL) {
164 DBG_LOG("solve: return parameter 'sols' is NULL\n"); 164 _log("solve: return parameter 'sols' is NULL\n");
165 return -1; 165 return -1;
166 } 166 }
167 167
168 if (estimate == NULL) { 168 if (estimate == NULL) {
169 DBG_LOG("solve: 'estimate' is NULL\n"); 169 _log("solve: 'estimate' is NULL\n");
170 return -1; 170 return -1;
171 } 171 }
172 172
@@ -187,7 +187,7 @@ solve_generic(
187 if (tmp != 0) 187 if (tmp != 0)
188 first = arg.depth; 188 first = arg.depth;
189 189
190 DBG_LOG("Found %" PRId64 " solution%s at depth %" PRIu8 "\n", 190 _log("Found %" PRId64 " solution%s at depth %" PRIu8 "\n",
191 tmp, tmp == 1 ? "" : "s", arg.depth); 191 tmp, tmp == 1 ? "" : "s", arg.depth);
192 192
193 if (ret >= maxsols) 193 if (ret >= maxsols)
@@ -248,7 +248,7 @@ gendata(const char *solver, const char *options, void *data)
248 maxdepth = atoi(&options[i+1]); 248 maxdepth = atoi(&options[i+1]);
249 ret = gendata_h48(data, h, maxdepth); 249 ret = gendata_h48(data, h, maxdepth);
250 } else { 250 } else {
251 DBG_LOG("gendata: implemented only for H48 solver\n"); 251 _log("gendata: implemented only for H48 solver\n");
252 ret = -1; 252 ret = -1;
253 } 253 }
254 254
diff --git a/src/solve_h48.h b/src/solve_h48.h
index 8be67b6..fc3b9c9 100644
--- a/src/solve_h48.h
+++ b/src/solve_h48.h
@@ -146,13 +146,13 @@ gendata_cocsep(void *buf, uint64_t *selfsim, cube_t *rep)
146 .rep = rep 146 .rep = rep
147 }; 147 };
148 for (i = 0, n = 0, cc = 0; i < 10; i++) { 148 for (i = 0, n = 0, cc = 0; i < 10; i++) {
149 DBG_LOG("cocsep: generating depth %" PRIu8 "\n", i); 149 _log("cocsep: generating depth %" PRIu8 "\n", i);
150 memset(visited, 0, COCSEP_VISITEDSIZE); 150 memset(visited, 0, COCSEP_VISITEDSIZE);
151 arg.depth = 0; 151 arg.depth = 0;
152 arg.maxdepth = i; 152 arg.maxdepth = i;
153 cc = gendata_cocsep_dfs(&arg); 153 cc = gendata_cocsep_dfs(&arg);
154 info[i+2] = cc; 154 info[i+2] = cc;
155 DBG_LOG("found %" PRIu32 "\n", cc); 155 _log("found %" PRIu32 "\n", cc);
156 } 156 }
157 157
158 info[0] = (uint32_t)n; 158 info[0] = (uint32_t)n;
@@ -161,12 +161,12 @@ gendata_cocsep(void *buf, uint64_t *selfsim, cube_t *rep)
161 "cocsep: computed %" PRIu16 " symmetry classes, " 161 "cocsep: computed %" PRIu16 " symmetry classes, "
162 "expected %zu\n", n, COCSEP_CLASSES); 162 "expected %zu\n", n, COCSEP_CLASSES);
163 163
164 DBG_LOG("cocsep data computed\n"); 164 _log("cocsep data computed\n");
165 DBG_LOG("Symmetry classes: %" PRIu32 "\n", info[0]); 165 _log("Symmetry classes: %" PRIu32 "\n", info[0]);
166 DBG_LOG("Maximum pruning value: %" PRIu32 "\n", info[1]); 166 _log("Maximum pruning value: %" PRIu32 "\n", info[1]);
167 DBG_LOG("Pruning value distribution:\n"); 167 _log("Pruning value distribution:\n");
168 for (j = 0; j < 10; j++) 168 for (j = 0; j < 10; j++)
169 DBG_LOG("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+2]); 169 _log("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+2]);
170 170
171gendata_cocsep_return_size: 171gendata_cocsep_return_size:
172 return COCSEP_FULLSIZE; 172 return COCSEP_FULLSIZE;
@@ -264,21 +264,21 @@ gendata_h48(void *buf, uint8_t h, uint8_t maxdepth)
264 tot < esep_max && arg.depth <= maxdepth; 264 tot < esep_max && arg.depth <= maxdepth;
265 arg.depth++ 265 arg.depth++
266 ) { 266 ) {
267 DBG_LOG("esep: generating depth %" PRIu8 "\n", arg.depth); 267 _log("esep: generating depth %" PRIu8 "\n", arg.depth);
268 cc = gendata_esep_bfs(&arg); 268 cc = gendata_esep_bfs(&arg);
269 tot += cc; 269 tot += cc;
270 info[arg.depth+1] = cc; 270 info[arg.depth+1] = cc;
271 DBG_LOG("found %" PRIu64 "\n", cc); 271 _log("found %" PRIu64 "\n", cc);
272 } 272 }
273 273
274 info[0] = arg.depth-1; 274 info[0] = arg.depth-1;
275 infosize = 4 * (size_t)(info[0] + 2); 275 infosize = 4 * (size_t)(info[0] + 2);
276 276
277 DBG_LOG("h48 pruning table computed\n"); 277 _log("h48 pruning table computed\n");
278 DBG_LOG("Maximum pruning value: %" PRIu32 "\n", info[0]); 278 _log("Maximum pruning value: %" PRIu32 "\n", info[0]);
279 DBG_LOG("Pruning value distribution:\n"); 279 _log("Pruning value distribution:\n");
280 for (j = 0; j <= info[0]; j++) 280 for (j = 0; j <= info[0]; j++)
281 DBG_LOG("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+1]); 281 _log("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+1]);
282 282
283gendata_h48_return_size: 283gendata_h48_return_size:
284 return cocsepsize + ESEP_TABLESIZE(h, k) + infosize; 284 return cocsepsize + ESEP_TABLESIZE(h, k) + infosize;

Generated with cgit - Back to sebastiano.tronto.net