Skip to content

Commit ebbe7e7

Browse files
committed
propagate silent flag and add --log-file
1 parent 92f769e commit ebbe7e7

File tree

6 files changed

+88
-47
lines changed

6 files changed

+88
-47
lines changed

edge_detect.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ class post_process {
2020
if (application_progress_callback) {
2121
(*application_progress_callback)(f);
2222
}
23-
if (progress_callback) {
24-
(*progress_callback)(static_cast<int>(f * 100.));
23+
if (!silent) {
24+
if (progress_callback) {
25+
(*progress_callback)(static_cast<int>(f * 100.));
26+
}
2527
}
2628
}
2729

2830
public:
31+
bool silent = false;
32+
2933
void set_progress_callback(boost::optional<std::function<void(int)>> cb) {
3034
progress_callback = cb;
3135
}
@@ -52,7 +56,7 @@ class threaded_post_process : public post_process {
5256
{}
5357

5458
regular_voxel_storage* operator()(regular_voxel_storage* storage) {
55-
progress_writer p(typeid(T).name());
59+
progress_writer p(typeid(T).name(), silent);
5660

5761
std::vector<std::thread> ts;
5862
std::vector<regular_voxel_storage*> results(n_);

progress_writer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
static std::mutex progress_mtx;
77

88
void threaded_progress_writer::operator()(int n, int i) {
9+
if (silent_) return;
10+
911
if (ps_[n] == i) {
1012
return;
1113
}

progress_writer.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ class threaded_progress_writer {
1313
std::vector<int> ps_;
1414
std::string item_;
1515
mutable bool ended_;
16+
bool silent_;
1617
public:
1718
boost::optional<std::function<void(float)>> application_progress_callback;
1819

19-
threaded_progress_writer(const std::string& item, int n)
20-
: item_(item), ended_(false)
20+
threaded_progress_writer(const std::string& item, int n, bool silent=false)
21+
: item_(item), ended_(false), silent_(silent)
2122
{
2223
ps_.resize(n);
2324
}
@@ -29,6 +30,7 @@ class threaded_progress_writer {
2930
void operator()(int n, int i);
3031

3132
void end() const {
33+
if (silent_) return;
3234
if (!ended_) {
3335
std::cerr << std::endl;
3436
ended_ = true;
@@ -46,8 +48,8 @@ class progress_writer {
4648

4749
progress_writer() : silent_(true) {}
4850

49-
progress_writer(const std::string& item)
50-
: item_(item), ended_(false), silent_(false) {}
51+
progress_writer(const std::string& item, bool silent = false)
52+
: item_(item), ended_(false), silent_(silent) {}
5153

5254
~progress_writer() {
5355
end();
@@ -71,7 +73,7 @@ class progress_writer {
7173
}
7274

7375
threaded_progress_writer thread(int n) {
74-
return threaded_progress_writer(item_, n);
76+
return threaded_progress_writer(item_, n, silent_);
7577
}
7678
};
7779

voxec.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,37 +105,44 @@ std::ostream& operator<<(std::ostream& a, const statement_type& b) {
105105
return a;
106106
}
107107

108-
class print_visitor {
108+
class log_visitor {
109+
private:
110+
json_logger::meta_value v_;
109111
public:
112+
const json_logger::meta_value& value() const { return v_; }
113+
110114
typedef void result_type;
111115

112-
void operator()(const boost::blank&) const {
113-
std::cerr << "(void:)" << std::endl;
116+
void operator()(const boost::blank&) {
117+
v_ = std::string("void");
114118
}
115119

116-
void operator()(int v) const {
117-
std::cerr << "(integer:)" << v << std::endl;
120+
void operator()(int v) {
121+
v_ = (long) v;
118122
}
119123

120-
void operator()(double v) const {
121-
std::cerr << "(real:)" << v << std::endl;
124+
void operator()(double v) {
125+
v_ = v;
122126
}
123127

124-
void operator()(const std::string& v) const {
128+
void operator()(const std::string& v) {
125129
if (v.at(0) == '"') {
126-
std::cerr << "(string:)" << v.substr(1, v.size() - 2) << std::endl;
130+
v_ = v.substr(1, v.size() - 2);
131+
}
132+
else {
133+
v_ = v;
127134
}
128135
}
129136

130-
void operator()(const function_arg_value_type& v) const {
137+
void operator()(const function_arg_value_type& v) {
131138
v.apply_visitor(*this);
132139
}
133140

134-
void operator()(filtered_files_t const v) const { }
141+
void operator()(filtered_files_t const v) { }
135142

136-
void operator()(geometry_collection_t* const v) const { }
143+
void operator()(geometry_collection_t* const v) { }
137144

138-
void operator()(abstract_voxel_storage* const v) const { }
145+
void operator()(abstract_voxel_storage* const v) { }
139146
};
140147

141148

@@ -180,6 +187,7 @@ scope_map run(const std::vector<statement_type>& statements, double size, size_t
180187

181188
voxel_operation_runner op(st);
182189
op.application_progress_callback = apfn;
190+
op.silent = with_progress_on_cout;
183191
context[st.assignee()] = op.run(st, context);
184192

185193
if (context[st.assignee()].which() == HAS_VOXELS) {
@@ -205,12 +213,7 @@ scope_map run(const std::vector<statement_type>& statements, double size, size_t
205213

206214
json_logger::message(json_logger::LOG_NOTICE, "storing {value} in {variable}", {
207215
{"variable", {{"name", st.assignee()}}},
208-
{"value", {
209-
{"count", (long) voxels->count()},
210-
{"grid", left.format() + " - " + right.format()},
211-
{"bounds", voxels->bounds()[0].format() + " - " + voxels->bounds()[1].format()},
212-
{"world", left_world.format() + " - " + right_world.format()},
213-
}},
216+
{"value", dump_info(voxels)},
214217
});
215218
}
216219
}
@@ -223,8 +226,12 @@ scope_map run(const std::vector<statement_type>& statements, double size, size_t
223226
}
224227

225228
if (statements.size()) {
226-
print_visitor v;
227-
context[statements.back().assignee()].apply_visitor(v);
229+
log_visitor v;
230+
context[statements.back().assignee()].apply_visitor(std::ref(v));
231+
232+
json_logger::message(json_logger::LOG_NOTICE, "scripted finished with {variable}", {
233+
{"variable", {{"value", v.value()}}},
234+
});
228235
} else {
229236
json_logger::message(json_logger::LOG_WARNING, "no operations in input", {});
230237
}

voxec.h

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "offset.h"
88
#include "fill_gaps.h"
99
#include "traversal.h"
10+
#include "json_logger.h"
1011

1112
#ifdef WITH_IFC
1213
#include <ifcparse/IfcFile.h>
@@ -138,6 +139,7 @@ struct argument_spec {
138139
class voxel_operation {
139140
public:
140141
boost::optional<std::function<void(float)>> application_progress_callback;
142+
bool silent = false;
141143

142144
virtual const std::vector<argument_spec>& arg_names() const = 0;
143145
virtual symbol_value invoke(const scope_map& scope) const = 0;
@@ -384,20 +386,22 @@ class op_create_geometry : public voxel_operation {
384386
}
385387

386388
if (old_progress != iterator.progress()) {
387-
std::cerr << "\rparsing geometry " << iterator.progress();
388389
old_progress = iterator.progress();
390+
if (application_progress_callback) {
391+
(*application_progress_callback)(old_progress / 100.f);
392+
}
389393
}
390394

391395
if (!iterator.next()) {
392-
std::cerr << std::endl;
393396
break;
394397
}
395398
}
396399

397-
}
400+
}
398401

399402
if (!at_least_one_succesful) {
400-
throw std::runtime_error("Failed to generate geometry");
403+
json_logger::message(json_logger::LOG_FATAL, "Failed to generate geometry");
404+
abort();
401405
}
402406

403407
std::random_shuffle(geometries->begin(), geometries->end());
@@ -415,7 +419,7 @@ namespace {
415419
return voxels;
416420
}
417421

418-
abstract_voxel_storage* voxelize(geometry_collection_t* surfaces, double vsize, int chunksize, const boost::optional<int>& threads) {
422+
abstract_voxel_storage* voxelize(geometry_collection_t* surfaces, double vsize, int chunksize, const boost::optional<int>& threads, bool silent = false) {
419423
double x1, y1, z1, x2, y2, z2;
420424
int nx, ny, nz;
421425

@@ -439,7 +443,7 @@ namespace {
439443
nz += PADDING * 2;
440444

441445
if (threads) {
442-
progress_writer progress("voxelize");
446+
progress_writer progress("voxelize", silent);
443447
threaded_processor p(x1, y1, z1, vsize, nx, ny, nz, chunksize, *threads, progress);
444448
p.process(surfaces->begin(), surfaces->end(), SURFACE(), output(MERGED()));
445449
return p.voxels();
@@ -471,7 +475,7 @@ class op_voxelize : public voxel_operation {
471475
// Just some arbitrary empty region
472476
return (abstract_voxel_storage*) new chunked_voxel_storage<bit_t>(make_vec<long>(0,0,0), vsize, cs, make_vec<size_t>(1U,1U,1U));
473477
} else {
474-
return voxelize(surfaces, vsize, cs, t);
478+
return voxelize(surfaces, vsize, cs, t, silent);
475479
}
476480
}
477481
};
@@ -493,7 +497,7 @@ class op_print_components : public voxel_operation {
493497
};
494498

495499
namespace {
496-
void dump_info(abstract_voxel_storage* voxels) {
500+
json_logger::meta_data dump_info(abstract_voxel_storage* voxels) {
497501
if (dynamic_cast<abstract_chunked_voxel_storage*>(voxels)) {
498502
auto left = dynamic_cast<abstract_chunked_voxel_storage*>(voxels)->grid_offset();
499503
auto nc = dynamic_cast<abstract_chunked_voxel_storage*>(voxels)->num_chunks();
@@ -502,14 +506,15 @@ namespace {
502506
auto szl = (long)dynamic_cast<abstract_chunked_voxel_storage*>(voxels)->chunk_size();
503507
auto left_world = ((voxels->bounds()[0].as<long>() + left * szl).as<double>() * sz);
504508
auto right_world = ((voxels->bounds()[1].as<long>() + left * szl).as<double>() * sz);
505-
std::cerr << "voxels: "
506-
<< left.format() << " - " << right.format()
507-
<< " ; count: " << voxels->count()
508-
<< " ; bounds: " << voxels->bounds()[0].format()
509-
<< " - " << voxels->bounds()[1].format() << std::endl
510-
<< " ; world: " << left_world.format()
511-
<< " - " << right_world.format() << std::endl;
509+
510+
return {
511+
{"count", (long)voxels->count()},
512+
{"grid", left.format() + " - " + right.format()},
513+
{"bounds", voxels->bounds()[0].format() + " - " + voxels->bounds()[1].format()},
514+
{"world", left_world.format() + " - " + right_world.format()},
515+
};
512516
}
517+
return {};
513518
}
514519

515520
template <typename Fn, typename Fn2>
@@ -811,6 +816,7 @@ class op_fill_gaps : public voxel_operation {
811816
abstract_voxel_storage* voxels = scope.get_value<abstract_voxel_storage*>("input");
812817
threaded_post_process<fill_gaps> tpp(scope.get_value<int>("THREADS"));
813818
tpp.set_progress_callback(application_progress_callback);
819+
tpp.silent = silent;
814820
return tpp((regular_voxel_storage*) voxels);
815821
}
816822
};
@@ -825,6 +831,7 @@ class op_offset : public voxel_operation {
825831
abstract_voxel_storage* voxels = scope.get_value<abstract_voxel_storage*>("input");
826832
threaded_post_process< offset<> > tpp(scope.get_value<int>("THREADS"));
827833
tpp.set_progress_callback(application_progress_callback);
834+
tpp.silent = silent;
828835
return tpp((regular_voxel_storage*)voxels);
829836
}
830837
};
@@ -853,7 +860,10 @@ class op_offset_xy : public voxel_operation {
853860
#else
854861
auto current = (regular_voxel_storage*) voxels->copy();
855862
while (d) {
856-
auto ofs = threaded_post_process< offset<1, 2> >(scope.get_value<int>("THREADS"))(current);
863+
threaded_post_process< offset<1, 2> > tpp(scope.get_value<int>("THREADS"));
864+
// @todo progress not propagated as it's within a loop
865+
tpp.silent = silent;
866+
auto ofs = tpp(current);
857867
auto next = (regular_voxel_storage*) current->boolean_union(ofs);
858868
delete ofs;
859869
delete current;
@@ -877,6 +887,7 @@ class op_outmost : public voxel_operation {
877887
abstract_voxel_storage* voxels = scope.get_value<abstract_voxel_storage*>("input");
878888
threaded_post_process<keep_outmost> tpp(1);
879889
tpp.set_progress_callback(application_progress_callback);
890+
tpp.silent = silent;
880891
return tpp((regular_voxel_storage*)voxels);
881892
}
882893
};
@@ -904,6 +915,7 @@ class op_volume : public voxel_operation {
904915
abstract_voxel_storage* voxels = scope.get_value<abstract_voxel_storage*>("input");
905916
threaded_post_process<traversal_voxel_filler_separate_components> tpp(1);
906917
tpp.set_progress_callback(application_progress_callback);
918+
tpp.silent = silent;
907919
return tpp((regular_voxel_storage*)voxels);
908920
}
909921
};
@@ -921,6 +933,7 @@ class op_volume_2 : public voxel_operation {
921933
}
922934
threaded_post_process<traversal_voxel_filler_inverse> tpp(1);
923935
tpp.set_progress_callback(application_progress_callback);
936+
tpp.silent = silent;
924937
return ((regular_voxel_storage*)voxels);
925938
}
926939
};
@@ -935,6 +948,7 @@ class op_volume_inverted : public voxel_operation {
935948
abstract_voxel_storage* voxels = scope.get_value<abstract_voxel_storage*>("input");
936949
threaded_post_process<traversal_voxel_filler_inverted> tpp(1);
937950
tpp.set_progress_callback(application_progress_callback);
951+
tpp.silent = silent;
938952
return tpp((regular_voxel_storage*)voxels);
939953
}
940954
};
@@ -1327,13 +1341,15 @@ class voxel_operation_runner {
13271341
public:
13281342
const statement_type& statement_;
13291343
boost::optional<std::function<void(float)>> application_progress_callback;
1344+
bool silent = false;
13301345

13311346
voxel_operation_runner(const statement_type& statement) :
13321347
statement_(statement) {}
13331348

13341349
symbol_value run(const statement_type& st, scope_map& context) {
13351350
voxel_operation* op = voxel_operation_map::create(statement_.call().name());
13361351
op->application_progress_callback = application_progress_callback;
1352+
op->silent = silent;
13371353

13381354
bool has_keyword = false;
13391355
std::map<std::string, function_arg_value_type> function_values;

voxec_main.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
namespace po = boost::program_options;
1111

1212
int main(int argc, char** argv) {
13-
// json_logger::register_output(json_logger::FMT_JSON, &std::cerr);
14-
json_logger::register_output(json_logger::FMT_TEXT, &std::cerr);
13+
std::unique_ptr<std::ofstream> log_file;
1514

1615
po::options_description opts("Command line options");
1716
opts.add_options()
1817
("quiet,q", "limit output on stdout to progress indication")
18+
("log-file", po::value<std::string>(), "filename to log json message")
1919
("threads,t", po::value<size_t>(), "number of parallel processing threads")
2020
("size,d", po::value<double>(), "voxel size in meters")
2121
("chunk,c", po::value<size_t>(), "chunk size in number of voxels")
@@ -63,10 +63,20 @@ int main(int argc, char** argv) {
6363
chunk = vmap["chunk"].as<size_t>();
6464
}
6565

66+
if (vmap.count("log-file")) {
67+
const std::string log_filename = vmap["log-file"].as<std::string>();
68+
log_file = std::make_unique<std::ofstream>(log_filename.c_str());
69+
json_logger::register_output(json_logger::FMT_TEXT, &*log_file);
70+
}
71+
6672
const std::string input_filename = vmap["input-file"].as<std::string>();
6773
const bool with_mesh = vmap.count("mesh") != 0;
6874
const bool quiet = vmap.count("quiet") != 0;
6975

76+
if (!quiet) {
77+
json_logger::register_output(json_logger::FMT_TEXT, &std::cerr);
78+
}
79+
7080
std::ifstream ifs(input_filename.c_str(), std::ios::binary);
7181
if (!ifs.good()) {
7282
json_logger::message(json_logger::LOG_FATAL, "unable to open file {file}", {

0 commit comments

Comments
 (0)