aboutsummaryrefslogtreecommitdiff
path: root/src/io
diff options
context:
space:
mode:
authorJ08nY2017-05-19 18:00:00 +0200
committerJ08nY2017-05-19 18:00:00 +0200
commit132a768b5718cef1ff621380f2dcf21cd0553404 (patch)
tree77acc82153e0d0b73351258feaa504ce0d3bbd74 /src/io
parentbaa18605f6b9e6c6627d118a9ffc6c1c683ac12d (diff)
downloadecgen-132a768b5718cef1ff621380f2dcf21cd0553404.tar.gz
ecgen-132a768b5718cef1ff621380f2dcf21cd0553404.tar.zst
ecgen-132a768b5718cef1ff621380f2dcf21cd0553404.zip
Add debug logging with time, refactor allocation
Diffstat (limited to 'src/io')
-rw-r--r--src/io/input.c10
-rw-r--r--src/io/output.c22
-rw-r--r--src/io/output.h29
3 files changed, 33 insertions, 28 deletions
diff --git a/src/io/input.c b/src/io/input.c
index ead0bf2..2b18a53 100644
--- a/src/io/input.c
+++ b/src/io/input.c
@@ -4,8 +4,8 @@
*/
#define _POSIX_C_SOURCE 200809L
#include "input.h"
-#include <parson/parson.h>
#include "output.h"
+#include "util/memory.h"
FILE *in;
int delim;
@@ -29,11 +29,7 @@ static GEN input_i(const char *prompt, unsigned long bits) {
;
if (len <= 3 || !(line[0] == '0' && (line[1] == 'x' || line[1] == 'X'))) {
- char *new_line = realloc(line, (size_t)(len + 2));
- if (!new_line) {
- perror("Couldn't alloc.");
- exit(1);
- }
+ char *new_line = try_realloc(line, (size_t)(len + 2));
memmove(new_line + 2, new_line, (size_t)len);
new_line[0] = '0';
new_line[1] = 'x';
@@ -113,8 +109,6 @@ GEN input_param(param_t param, const char *prompt, unsigned long bits) {
}
void input_init(const config_t *cfg) {
- json_set_allocation_functions(pari_malloc, pari_free);
-
if (cfg->input) {
in = fopen(cfg->input, "r");
delim = ',';
diff --git a/src/io/output.c b/src/io/output.c
index 749d008..dafbf14 100644
--- a/src/io/output.c
+++ b/src/io/output.c
@@ -6,16 +6,13 @@
#include "output.h"
#include <parson/parson.h>
#include "math/field.h"
+#include "util/memory.h"
FILE *out;
FILE *verbose;
char *output_malloc(const char *what) {
- char *s = pari_malloc(sizeof(char) * (strlen(what) + 1));
- if (!s) {
- perror("Couldn't malloc.");
- exit(1);
- }
+ char *s = try_calloc(sizeof(char) * (strlen(what) + 1));
strcpy(s, what);
return s;
}
@@ -62,8 +59,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
len += strlen(gens[i]);
}
size_t lenn = sizeof(char) * (len + curve->ngens);
- params[OFFSET_GENERATORS] = pari_malloc(lenn);
- params[OFFSET_GENERATORS][0] = '\0';
+ params[OFFSET_GENERATORS] = try_calloc(lenn);
for (size_t i = 0; i < curve->ngens; ++i) {
if (i > 0) strncat(params[OFFSET_GENERATORS], ",", lenn - 1);
strncat(params[OFFSET_GENERATORS], gens[i], lenn - 1);
@@ -87,8 +83,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
len += strlen(points[i]);
}
size_t lenn = sizeof(char) * (len + curve->npoints);
- params[OFFSET_POINTS] = pari_malloc(lenn);
- params[OFFSET_POINTS][0] = '\0';
+ params[OFFSET_POINTS] = try_calloc(lenn);
for (size_t i = 0; i < curve->npoints; ++i) {
if (i > 0) strncat(params[OFFSET_POINTS], ",", lenn - 1);
strncat(params[OFFSET_POINTS], points[i], lenn - 1);
@@ -105,8 +100,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
}
}
size_t lenn = sizeof(char) * (len + count);
- char *result = pari_malloc(lenn);
- result[0] = '\0';
+ char *result = try_calloc(lenn);
for (int i = OFFSET_FIELD; i < OFFSET_END; ++i) {
if (params[i]) {
@@ -299,7 +293,7 @@ void output_f_end(FILE *out, const config_t *cfg) {
void output_o_end(const config_t *cfg) { output_f_end(out, cfg); }
void output_init(const config_t *cfg) {
- json_set_allocation_functions(pari_malloc, pari_free);
+ json_set_allocation_functions(try_malloc, pari_free);
if (cfg->output) {
out = fopen(cfg->output, cfg->append ? "a" : "w");
@@ -315,11 +309,11 @@ void output_init(const config_t *cfg) {
if (cfg->verbose_log) {
verbose = fopen(cfg->verbose_log, "w");
if (!verbose) {
- verbose = stdout;
+ verbose = stderr;
perror("Failed to open verbose output file.");
}
} else {
- verbose = stdout;
+ verbose = stderr;
}
setvbuf(verbose, NULL, _IONBF, 0);
diff --git a/src/io/output.h b/src/io/output.h
index ef36f99..5713b37 100644
--- a/src/io/output.h
+++ b/src/io/output.h
@@ -12,6 +12,29 @@
#include <stdbool.h>
#include "math/types.h"
+#ifdef DEBUG
+
+#include "time.h"
+
+#define debug(...) fprintf(verbose, __VA_ARGS__)
+#define debug_log(...) \
+ fprintf(verbose, " - %lu %s\n", time(NULL), __VA_ARGS__);
+#define debug_log_start(...) \
+ fprintf(verbose, "[ ] %lu %s\n", time(NULL), __VA_ARGS__)
+#define debug_log_end(...) \
+ fprintf(verbose, "[*] %lu %s\n", time(NULL), __VA_ARGS__)
+#else
+#define debug(...)
+#define debug_log(...)
+#define debug_log_start(...)
+#define debug_log_end(...)
+#endif // DEBUG
+
+#define verbose_log(...) \
+ if (cfg->verbose) fprintf(verbose, __VA_ARGS__)
+
+#define output_log(...) fprintf(out, __VA_ARGS__)
+
/**
* @brief Output curve to a pari_malloc'ed string in CSV format.
* @param curve
@@ -178,10 +201,4 @@ void output_init(const config_t *cfg);
*/
void output_quit(void);
-#ifdef DEBUG
-#define debug(...) fprintf(out, __VA_ARGS__)
-#else
-#define debug(...)
-#endif
-
#endif // ECGEN_OUTPUT_H