Skip to content

Commit a55f069

Browse files
committed
feat(loglevel): configurable logging verbosity
1 parent 10caada commit a55f069

6 files changed

+22
-10
lines changed

build/pbf2json.darwin-x64

0 Bytes
Binary file not shown.

build/pbf2json.linux-arm

0 Bytes
Binary file not shown.

build/pbf2json.linux-x64

0 Bytes
Binary file not shown.

build/pbf2json.win32-x64

0 Bytes
Binary file not shown.

index.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@ var util = require('util'),
77
child = require('child_process'),
88
exec = path.join(__dirname, 'build', util.format( 'pbf2json.%s-%s', os.platform(), os.arch() ) );
99

10-
function errorHandler( name ){
10+
// custom log levels can be detected for lines with the format:
11+
// [level] message
12+
// supported levels (listed from least verbose to most verbose):
13+
// error, warn, info
14+
function getLogLevel( line ){
15+
if( line.indexOf('[warn]') > -1 ){ return 1; }
16+
if( line.indexOf('[info]') > -1 ){ return 2; }
17+
return 0;
18+
}
19+
20+
function errorHandler( name, level ){
1121
return function( data ){
1222
data.toString('utf8').trim().split('\n').forEach( function( line ){
13-
console.error( util.format( '[%s]:', name ), line );
23+
if( getLogLevel( line ) <= level ){
24+
console.error( util.format( '[%s]:', name ), line );
25+
}
1426
});
1527
};
1628
}
@@ -44,10 +56,10 @@ function createReadStream( config ){
4456
.pipe( decoder );
4557

4658
// print error and exit on decoder pipeline error
47-
decoder.on( 'error', errorHandler( 'decoder' ) );
59+
decoder.on( 'error', errorHandler( 'decoder', config.loglevel || 0 ) );
4860

4961
// print error and exit on stderr
50-
proc.stderr.on( 'data', errorHandler( 'pbf2json' ) );
62+
proc.stderr.on( 'data', errorHandler( 'pbf2json', config.loglevel || 0 ) );
5163

5264
// terminate the process and pipeline
5365
decoder.kill = function(){

pbf2json.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func print(d *osmpbf.Decoder, masks *BitmaskMap, db *leveldb.DB, config settings
303303

304304
// no ways found, skip relation
305305
if len(memberWayLatLons) == 0 {
306-
log.Println("denormalize failed for relation:", v.ID, "no ways found")
306+
log.Println("[warn] denormalize failed for relation:", v.ID, "no ways found")
307307
continue
308308
}
309309

@@ -321,7 +321,7 @@ func print(d *osmpbf.Decoder, masks *BitmaskMap, db *leveldb.DB, config settings
321321

322322
// if for any reason we failed to find a valid bounds
323323
if nil == wayBounds {
324-
log.Println("failed to calculate bounds for relation member way")
324+
log.Println("[warn] failed to calculate bounds for relation member way")
325325
continue
326326
}
327327

@@ -337,7 +337,7 @@ func print(d *osmpbf.Decoder, masks *BitmaskMap, db *leveldb.DB, config settings
337337

338338
// if for any reason we failed to find a valid bounds
339339
if nil == bounds {
340-
log.Println("denormalize failed for relation:", v.ID, "no valid bounds")
340+
log.Println("[warn] denormalize failed for relation:", v.ID, "no valid bounds")
341341
continue
342342
}
343343

@@ -350,7 +350,7 @@ func print(d *osmpbf.Decoder, masks *BitmaskMap, db *leveldb.DB, config settings
350350

351351
default:
352352

353-
log.Fatalf("unknown type %T\n", v)
353+
log.Fatalf("[error] unknown type %T\n", v)
354354

355355
}
356356
}
@@ -503,7 +503,7 @@ func cacheLookupNodes(db *leveldb.DB, way *osmpbf.Way) ([]map[string]string, err
503503

504504
data, err := db.Get([]byte(stringid), nil)
505505
if err != nil {
506-
log.Println("denormalize failed for way:", way.ID, "node not found:", stringid)
506+
log.Println("[warn] denormalize failed for way:", way.ID, "node not found:", stringid)
507507
return make([]map[string]string, 0), err
508508
}
509509

@@ -521,7 +521,7 @@ func cacheLookupWayNodes(db *leveldb.DB, wayid int64) ([]map[string]string, erro
521521
// look up way bytes
522522
reldata, err := db.Get([]byte(stringid), nil)
523523
if err != nil {
524-
log.Println("lookup failed for way:", wayid, "noderefs not found:", stringid)
524+
log.Println("[warn] lookup failed for way:", wayid, "noderefs not found:", stringid)
525525
return make([]map[string]string, 0), err
526526
}
527527

0 commit comments

Comments
 (0)