diff options
author | sanine <sanine.not@pm.me> | 2022-01-26 12:00:35 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-01-26 12:00:35 -0600 |
commit | 5734102b0adf54933230264e0b43e74f5eabf718 (patch) | |
tree | 484d6cf93a1c536c98ddfe3f60efbb475560cb94 /experimental | |
parent | 6b687b91e788b4e91a6ed30640a2477b9596b954 (diff) |
add basic logging and CLI options
Diffstat (limited to 'experimental')
-rw-r--r-- | experimental/tectonics/Makefile | 4 | ||||
-rw-r--r-- | experimental/tectonics/geometry.h | 34 | ||||
-rw-r--r-- | experimental/tectonics/logging.c | 42 | ||||
-rw-r--r-- | experimental/tectonics/logging.h | 16 | ||||
-rw-r--r-- | experimental/tectonics/tectonics.c | 61 | ||||
-rw-r--r-- | experimental/tectonics/world.c | 3 |
6 files changed, 149 insertions, 11 deletions
diff --git a/experimental/tectonics/Makefile b/experimental/tectonics/Makefile index ab8fb0d..a0245f3 100644 --- a/experimental/tectonics/Makefile +++ b/experimental/tectonics/Makefile @@ -5,8 +5,8 @@ DEPS=tectonics.h %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) -all: world.o util.o tectonics.o quadtree.o drawing.o - $(CC) -o tectonics world.o util.o tectonics.o quadtree.o drawing.o $(CFLAGS) +all: world.o util.o tectonics.o quadtree.o drawing.o logging.o + $(CC) -o tectonics world.o util.o tectonics.o logging.o quadtree.o drawing.o $(CFLAGS) test: all quadtree.o quadtree.test.o tests.o $(CC) -o tests world.o util.o quadtree.o quadtree.test.o tests.o $(CFLAGS) diff --git a/experimental/tectonics/geometry.h b/experimental/tectonics/geometry.h index de01042..fdad15a 100644 --- a/experimental/tectonics/geometry.h +++ b/experimental/tectonics/geometry.h @@ -9,6 +9,13 @@ struct point_t { }; +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * quadtree + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + struct quad_region_t { struct point_t center; double half_dim; @@ -25,13 +32,6 @@ struct quadtree_node_t { }; -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * - * quadtree - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - */ - /* check if a region contains a point */ bool quad_contains_point(struct quad_region_t region, struct point_t pt); @@ -65,4 +65,24 @@ int quadtree_get_closest(struct quadtree_node_t *root, */ void quadtree_free(struct quadtree_node_t *root); + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * mesh + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +struct triangle_t { + struct point_t p1, p2, p3; +}; + +struct triangle_array_t { + int n_triangles; + struct triangle_t *triangles; +}; + + + + #endif diff --git a/experimental/tectonics/logging.c b/experimental/tectonics/logging.c new file mode 100644 index 0000000..ec416d6 --- /dev/null +++ b/experimental/tectonics/logging.c @@ -0,0 +1,42 @@ +#include <stdio.h> +#include <stdarg.h> + +#include "logging.h" + +int global_log_level; + +const char *level_string(int level) +{ + switch(level) { + case FATAL: + return "FATAL"; + case ERROR: + return "ERROR"; + case WARN: + return "WARN"; + case INFO: + return "INFO"; + case DEBUG: + return "DEBUG"; + case TRACE: + return "TRACE"; + + default: + if (level > TRACE) + return "TRACE"; + return "[BAD LOG LEVEL]"; + } +} + + +void log_msg(int level, const char *format_string, ...) +{ + if (global_log_level >= level) { + va_list args; + va_start(args, format_string); + fprintf(stderr, "[%s] ", level_string(level)); + vfprintf(stderr, format_string, args); + fprintf(stderr, "\n"); + va_end(args); + } +} diff --git a/experimental/tectonics/logging.h b/experimental/tectonics/logging.h new file mode 100644 index 0000000..47e09e7 --- /dev/null +++ b/experimental/tectonics/logging.h @@ -0,0 +1,16 @@ +#ifndef ARGENT_LOGGING_H +#define ARGENT_LOGGING_H + +#define FATAL 0 +#define ERROR 1 +#define WARN 2 +#define INFO 3 +#define DEBUG 4 +#define TRACE 5 + +extern int global_log_level; + +const char *level_string(int level); +void log_msg(int level, const char *format_string, ...); + +#endif diff --git a/experimental/tectonics/tectonics.c b/experimental/tectonics/tectonics.c index 7d5342a..0a81b45 100644 --- a/experimental/tectonics/tectonics.c +++ b/experimental/tectonics/tectonics.c @@ -2,12 +2,21 @@ #include "tectonics.h" #include "drawing.h" +#include "logging.h" #define X_RES 1024 #define Y_RES X_RES -int main() + +int parse_options(char **output_file, int argc, char **argv); + + +int main(int argc, char **argv) { + char *output_file; + if (parse_options(&output_file, argc, argv)) + return 1; + cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, X_RES, Y_RES); cairo_t *cr = cairo_create(surface); @@ -23,3 +32,53 @@ int main() cairo_surface_destroy(surface); return 0; } + + +#include <stdio.h> +#include <unistd.h> + +static void print_usage(const char *progname) +{ + printf("Usage: %s [-c config_file] [-v[v[v]]] [-q[q[q]]] [-h] [-s script]\n" + " -v Increase output verbosity (-vvv displays every log message)\n" + " -q Decrease output verbosity (-qqq suppresses even fatal errors)\n" + " -c Specify configuration file to read (default 'config.lua')\n" + " -s Override the built-in Lua script\n" + " -h Print this help message and exit\n", + progname); +} + +int parse_options(char **output_file, int argc, char **argv) +{ + int log_level = WARN; + *output_file = "output.png"; + + int opt; + while ((opt = getopt(argc, argv, "qvo:h")) != -1) { + switch (opt) { + case 'q': + log_level -= 1; + break; + + case 'v': + log_level += 1; + break; + + case 'o': + *output_file = optarg; + + case 'h': + print_usage(argv[0]); + return 2; + + default: + print_usage(argv[0]); + return 1; + } + } + + // ew, globals (sorry) + global_log_level = log_level; + + return 0; +} diff --git a/experimental/tectonics/world.c b/experimental/tectonics/world.c index 5aa485c..f5b6efb 100644 --- a/experimental/tectonics/world.c +++ b/experimental/tectonics/world.c @@ -4,6 +4,7 @@ #include "tectonics.h" #include "geometry.h" #include "util.h" +#include "logging.h" static void rebuild_tree(struct world_t *world) { @@ -114,7 +115,7 @@ void create_world(struct world_t *world, int n_points) rebuild_tree(world); - for (int i=0; i<3; i++) + for (int i=0; i<15; i++) relax_points(world, 10*world->n_points); } |