From f442e403c54ac2855883396a1e2bce0e3dbbb848 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 5 Jul 2022 12:01:56 +0800 Subject: [PATCH] add logging struct with -u json options flag, log tiling progress [#13] --- Makefile | 2 +- README.md | 1 + json_logger.cpp | 8 ++++++++ json_logger.hpp | 13 +++++++++++++ main.cpp | 7 +++++++ main.hpp | 3 +++ man/tippecanoe.1 | 2 ++ tile.cpp | 6 ++++++ 8 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 json_logger.cpp create mode 100644 json_logger.hpp diff --git a/Makefile b/Makefile index d212202..58a86c6 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ C = $(wildcard *.c) $(wildcard *.cpp) INCLUDES = -I/usr/local/include -I. LIBS = -L/usr/local/lib -tippecanoe: geojson.o jsonpull/jsonpull.o tile.o pool.o mbtiles.o geometry.o projection.o memfile.o mvt.o serial.o main.o text.o dirtiles.o plugin.o read_json.o write_json.o geobuf.o flatgeobuf.o evaluator.o geocsv.o csv.o geojson-loop.o +tippecanoe: geojson.o jsonpull/jsonpull.o tile.o pool.o mbtiles.o geometry.o projection.o memfile.o mvt.o serial.o main.o text.o dirtiles.o plugin.o read_json.o write_json.o geobuf.o flatgeobuf.o evaluator.o geocsv.o csv.o geojson-loop.o json_logger.o $(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread tippecanoe-enumerate: enumerate.o diff --git a/README.md b/README.md index 7d46176..706f067 100644 --- a/README.md +++ b/README.md @@ -529,6 +529,7 @@ the same layer, enclose them in an `all` expression so they will all be evaluate * `-q` or `--quiet`: Work quietly instead of reporting progress or warning messages * `-Q` or `--no-progress-indicator`: Don't report progress, but still give warnings * `-U` _seconds_ or `--progress-interval=`_seconds_: Don't report progress more often than the specified number of _seconds_. + * `-u` or `--json-progress`: like `-quiet` but logs progress as a JSON object. Use in combination with `-U`. * `-v` or `--version`: Report Tippecanoe's version number ### Filters diff --git a/json_logger.cpp b/json_logger.cpp new file mode 100644 index 0000000..0c3fbae --- /dev/null +++ b/json_logger.cpp @@ -0,0 +1,8 @@ +#include +#include + +#include "json_logger.hpp" + +void json_logger::progress_tile(double progress) { + fprintf(stderr,"{\"progress\":%3.1f}\n",progress); +} diff --git a/json_logger.hpp b/json_logger.hpp new file mode 100644 index 0000000..50f8489 --- /dev/null +++ b/json_logger.hpp @@ -0,0 +1,13 @@ +#include + +#ifndef LOGGING_HPP +#define LOGGING_HPP + + +struct json_logger { + bool json_enabled = false; + + void progress_tile(double progress); +}; + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index e69d48d..dcdebf4 100644 --- a/main.cpp +++ b/main.cpp @@ -69,6 +69,7 @@ static int min_detail = 7; int quiet = 0; int quiet_progress = 0; +json_logger logger; double progress_interval = 0; std::atomic last_progress(0); int geometry_scale = 0; @@ -2690,6 +2691,7 @@ int main(int argc, char **argv) { {"quiet", no_argument, 0, 'q'}, {"no-progress-indicator", no_argument, 0, 'Q'}, {"progress-interval", required_argument, 0, 'U'}, + {"json-progress", no_argument, 0, 'u'}, {"version", no_argument, 0, 'v'}, {"", 0, 0, 0}, @@ -3003,6 +3005,11 @@ int main(int argc, char **argv) { quiet_progress = 1; break; + case 'u': + quiet = 1; + logger.json_enabled = true; + break; + case 'U': progress_interval = atof_require(optarg, "Progress interval"); break; diff --git a/main.hpp b/main.hpp index 8d89ab6..41e6e64 100644 --- a/main.hpp +++ b/main.hpp @@ -5,6 +5,8 @@ #include #include +#include "json_logger.hpp" + struct index { long long start = 0; long long end = 0; @@ -38,6 +40,7 @@ void checkdisk(std::vector *r); extern int geometry_scale; extern int quiet; extern int quiet_progress; +extern json_logger logger; extern double progress_interval; extern std::atomic last_progress; diff --git a/man/tippecanoe.1 b/man/tippecanoe.1 index 9db7278..07fa0ce 100644 --- a/man/tippecanoe.1 +++ b/man/tippecanoe.1 @@ -687,6 +687,8 @@ If you don't specify, it will use \fB\fC/tmp\fR\&. .IP \(bu 2 \fB\fC\-U\fR \fIseconds\fP or \fB\fC\-\-progress\-interval=\fR\fIseconds\fP: Don't report progress more often than the specified number of \fIseconds\fP\&. .IP \(bu 2 +\fB\fC\-u\fR or \fB\fC\-\-json\-progress\fR: like \fB\fC\-quiet\fR but logs progress as a JSON object. Use in combination with \fB\fC\-U\fR\&. +.IP \(bu 2 \fB\fC\-v\fR or \fB\fC\-\-version\fR: Report Tippecanoe's version number .RE .SS Filters diff --git a/tile.cpp b/tile.cpp index cf41982..339cc32 100644 --- a/tile.cpp +++ b/tile.cpp @@ -1330,6 +1330,9 @@ serial_feature next_feature(FILE *geoms, std::atomic *geompos_in, cha if (!quiet && !quiet_progress && progress_time()) { fprintf(stderr, " %3.1f%% %d/%u/%u \r", progress, z, tx, ty); } + if (logger.json_enabled && progress_time()) { + logger.progress_tile(progress); + } *oprogress = progress; } @@ -2325,6 +2328,9 @@ long long write_tile(FILE *geoms, std::atomic *geompos_in, char *meta if (!quiet && !quiet_progress && progress_time()) { fprintf(stderr, " %3.1f%% %d/%u/%u \r", progress, z, tx, ty); } + if (logger.json_enabled && progress_time()) { + logger.progress_tile(progress); + } oprogress = progress; }