Skip to content

Commit

Permalink
feat(loglevel): configurable logging verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Jun 10, 2019
1 parent 10caada commit a55f069
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
Binary file modified build/pbf2json.darwin-x64
Binary file not shown.
Binary file modified build/pbf2json.linux-arm
Binary file not shown.
Binary file modified build/pbf2json.linux-x64
Binary file not shown.
Binary file modified build/pbf2json.win32-x64
Binary file not shown.
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ var util = require('util'),
child = require('child_process'),
exec = path.join(__dirname, 'build', util.format( 'pbf2json.%s-%s', os.platform(), os.arch() ) );

function errorHandler( name ){
// custom log levels can be detected for lines with the format:
// [level] message
// supported levels (listed from least verbose to most verbose):
// error, warn, info
function getLogLevel( line ){
if( line.indexOf('[warn]') > -1 ){ return 1; }
if( line.indexOf('[info]') > -1 ){ return 2; }
return 0;
}

function errorHandler( name, level ){
return function( data ){
data.toString('utf8').trim().split('\n').forEach( function( line ){
console.error( util.format( '[%s]:', name ), line );
if( getLogLevel( line ) <= level ){
console.error( util.format( '[%s]:', name ), line );
}
});
};
}
Expand Down Expand Up @@ -44,10 +56,10 @@ function createReadStream( config ){
.pipe( decoder );

// print error and exit on decoder pipeline error
decoder.on( 'error', errorHandler( 'decoder' ) );
decoder.on( 'error', errorHandler( 'decoder', config.loglevel || 0 ) );

// print error and exit on stderr
proc.stderr.on( 'data', errorHandler( 'pbf2json' ) );
proc.stderr.on( 'data', errorHandler( 'pbf2json', config.loglevel || 0 ) );

// terminate the process and pipeline
decoder.kill = function(){
Expand Down
12 changes: 6 additions & 6 deletions pbf2json.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func print(d *osmpbf.Decoder, masks *BitmaskMap, db *leveldb.DB, config settings

// no ways found, skip relation
if len(memberWayLatLons) == 0 {
log.Println("denormalize failed for relation:", v.ID, "no ways found")
log.Println("[warn] denormalize failed for relation:", v.ID, "no ways found")
continue
}

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

// if for any reason we failed to find a valid bounds
if nil == wayBounds {
log.Println("failed to calculate bounds for relation member way")
log.Println("[warn] failed to calculate bounds for relation member way")
continue
}

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

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

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

default:

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

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

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

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

Expand Down

0 comments on commit a55f069

Please sign in to comment.