Skip to content

Commit ea297ad

Browse files
committed
N-API mapnik.Logger + tests (WIP) [skip ci]
1 parent d55e480 commit ea297ad

File tree

5 files changed

+72
-86
lines changed

5 files changed

+72
-86
lines changed

binding.gyp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'target_name': '<(module_name)',
1010
'product_dir': '<(module_path)',
1111
'sources': [
12-
#"src/mapnik_logger.cpp",
12+
"src/mapnik_logger.cpp",
1313
"src/node_mapnik.cpp",
1414
"src/blend.cpp",
1515
"src/mapnik_map.cpp",
@@ -46,9 +46,6 @@
4646
#"src/mapnik_cairo_surface.cpp",
4747
#"src/mapnik_vector_tile.cpp"
4848
],
49-
"msvs_disabled_warnings": [
50-
4267
51-
],
5249
'include_dirs': [
5350
'./mason_packages/.link/include/',
5451
'./mason_packages/.link/include/freetype2',

src/mapnik_logger.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1-
#include "utils.hpp"
1+
//#include "utils.hpp"
22
#include "mapnik_logger.hpp"
33
#include <mapnik/debug.hpp>
44

55
Napi::FunctionReference Logger::constructor;
66

7+
Napi::Object Logger::Initialize(Napi::Env env, Napi::Object exports)
8+
{
9+
Napi::Function func = DefineClass(env, "Logger", {
10+
StaticMethod<&Logger::get_severity>("getSeverity"),
11+
StaticMethod<&Logger::set_severity>("setSeverity"),
12+
StaticValue("NONE", Napi::Number::New(env, mapnik::logger::severity_type::none), napi_enumerable),
13+
StaticValue("ERROR", Napi::Number::New(env, mapnik::logger::severity_type::error), napi_enumerable),
14+
StaticValue("DEBUG", Napi::Number::New(env, mapnik::logger::severity_type::debug), napi_enumerable),
15+
StaticValue("WARN", Napi::Number::New(env, mapnik::logger::severity_type::warn), napi_enumerable)
16+
});
17+
// What about booleans like:
18+
// ENABLE_STATS
19+
// ENABLE_LOG
20+
// DEFAULT_LOG_SEVERITY
21+
// RENDERING_STATS
22+
// DEBUG
23+
constructor = Napi::Persistent(func);
24+
constructor.SuppressDestruct();
25+
exports.Set("Logger", func);
26+
return exports;
27+
}
28+
729
/**
830
* **`mapnik.Logger`**
931
*
@@ -15,39 +37,12 @@ Napi::FunctionReference Logger::constructor;
1537
* var log = mapnik.Logger.get_severity();
1638
* console.log(log); // 3
1739
*/
18-
void Logger::Initialize(Napi::Object target) {
19-
Napi::HandleScope scope(env);
20-
21-
Napi::FunctionReference lcons = Napi::Function::New(env, Logger::New);
22-
23-
lcons->SetClassName(Napi::String::New(env, "Logger"));
24-
25-
// Static methods
26-
Napi::SetMethod(Napi::GetFunction(lcons).As<Napi::Object>(), "getSeverity", Logger::get_severity);
27-
Napi::SetMethod(Napi::GetFunction(lcons).As<Napi::Object>(), "setSeverity", Logger::set_severity);
28-
29-
// Constants
30-
NODE_MAPNIK_DEFINE_CONSTANT(Napi::GetFunction(lcons),"NONE",mapnik::logger::severity_type::none);
31-
NODE_MAPNIK_DEFINE_CONSTANT(Napi::GetFunction(lcons),"ERROR",mapnik::logger::severity_type::error);
32-
NODE_MAPNIK_DEFINE_CONSTANT(Napi::GetFunction(lcons),"DEBUG",mapnik::logger::severity_type::debug);
33-
NODE_MAPNIK_DEFINE_CONSTANT(Napi::GetFunction(lcons),"WARN",mapnik::logger::severity_type::warn);
34-
35-
// What about booleans like:
36-
// ENABLE_STATS
37-
// ENABLE_LOG
38-
// DEFAULT_LOG_SEVERITY
39-
// RENDERING_STATS
40-
// DEBUG
41-
42-
// Not sure if needed...
43-
(target).Set(Napi::String::New(env, "Logger"), Napi::GetFunction(lcons));
44-
constructor.Reset(lcons);
45-
46-
}
4740

48-
Napi::Value Logger::New(Napi::CallbackInfo const& info){
41+
Logger::Logger(Napi::CallbackInfo const& info)
42+
: Napi::ObjectWrap<Logger>(info)
43+
{
44+
Napi::Env env = info.Env();
4945
Napi::Error::New(env, "a mapnik.Logger cannot be created directly - rather you should ....").ThrowAsJavaScriptException();
50-
return env.Null();
5146
}
5247

5348
/**
@@ -57,9 +52,11 @@ Napi::Value Logger::New(Napi::CallbackInfo const& info){
5752
* @static
5853
* @returns {number} severity level
5954
*/
60-
Napi::Value Logger::get_severity(Napi::CallbackInfo const& info){
55+
Napi::Value Logger::get_severity(Napi::CallbackInfo const& info)
56+
{
57+
Napi::Env env = info.Env();
6158
int severity = mapnik::logger::instance().get_severity();
62-
return Napi::New(env, severity);
59+
return Napi::Number::New(env, severity);
6360
}
6461

6562
/**
@@ -73,13 +70,16 @@ Napi::Value Logger::get_severity(Napi::CallbackInfo const& info){
7370
* @param {number} severity - severity level
7471
* @returns {number} severity level
7572
*/
76-
Napi::Value Logger::set_severity(Napi::CallbackInfo const& info){
77-
if (info.Length() != 1 || !info[0].IsNumber()) {
73+
Napi::Value Logger::set_severity(Napi::CallbackInfo const& info)
74+
{
75+
Napi::Env env = info.Env();
76+
if (info.Length() != 1 || !info[0].IsNumber())
77+
{
7878
Napi::TypeError::New(env, "requires a severity level parameter").ThrowAsJavaScriptException();
79-
return env.Null();
79+
return env.Undefined();
8080
}
8181

8282
int severity = info[0].As<Napi::Number>().Int32Value();
8383
mapnik::logger::instance().set_severity(static_cast<mapnik::logger::severity_type>(severity));
84-
return;
84+
return env.Undefined();
8585
}

src/mapnik_logger.hpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
1-
#ifndef __NODE_MAPNIK_LOGGER_H__
2-
#define __NODE_MAPNIK_LOGGER_H__
3-
1+
#pragma once
42
#include <napi.h>
5-
#include <uv.h>
63

7-
//Forward declaration of mapnik logger
84
namespace mapnik { class logger; }
95

10-
class Logger : public Napi::ObjectWrap<Logger> {
6+
class Logger : public Napi::ObjectWrap<Logger>
7+
{
118
public:
12-
// V8 way of...
13-
static Napi::FunctionReference constructor;
14-
15-
// Initialize function is needed for all addons
16-
static void Initialize(Napi::Object target);
9+
// initializer
10+
static Napi::Object Initialize(Napi::Env env, Napi::Object exports);
1711

18-
// Nan_Method when new Logger object is instantiated
19-
static Napi::Value New(Napi::CallbackInfo const& info);
12+
// ctor
13+
explicit Logger(Napi::CallbackInfo const& info);
2014

2115
// Get and set functions
2216
// Are these the only methods available in logger?
2317
static Napi::Value get_severity(Napi::CallbackInfo const& info);
2418
static Napi::Value set_severity(Napi::CallbackInfo const& info);
25-
static Napi::Value evoke_error(Napi::CallbackInfo const& info);
26-
19+
//Napi::Value evoke_error(Napi::CallbackInfo const& info);
2720
private:
28-
// Default Constructor
29-
Logger();
30-
// Deconstructor
31-
~Logger();
21+
static Napi::FunctionReference constructor;
3222
};
33-
34-
#endif

src/node_mapnik.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "mapnik_map.hpp"
77
#include "mapnik_color.hpp"
88
#include "mapnik_geometry.hpp"
9-
//#include "mapnik_logger.hpp"
9+
#include "mapnik_logger.hpp"
1010
#include "mapnik_feature.hpp"
1111
#include "mapnik_fonts.hpp"
1212
#include "mapnik_plugins.hpp"
@@ -320,6 +320,7 @@ Napi::Object init(Napi::Env env, Napi::Object exports)
320320
Layer::Initialize(env, exports);
321321
Map::Initialize(env, exports);
322322
Expression::Initialize(env, exports);
323+
Logger::Initialize(env, exports);
323324
// enums
324325
init_image_types(env, exports);
325326
init_image_scalings(env, exports);

test/logger.test.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
"use strict";
22

3+
var test = require('tape');
34
var mapnik = require('../');
4-
var assert = require('assert');
55

6-
describe('logger', function() {
7-
8-
it('get_severity should return default', function(done) {
9-
assert.equal(mapnik.Logger.getSeverity(), mapnik.Logger.ERROR);
10-
done();
11-
});
126

13-
it('test that you cant initialize a logger', function() {
14-
assert.throws(function() { var l = new mapnik.Logger(); });
15-
});
7+
test('get_severity should return default', (assert) => {
8+
assert.equal(mapnik.Logger.getSeverity(), mapnik.Logger.ERROR);
9+
assert.end();
10+
});
11+
12+
test('test that you cant initialize a logger', (assert) => {
13+
assert.throws(function() { var l = new mapnik.Logger(); });
14+
assert.end();
15+
});
1616

17-
it('set_severity should fail with bad input', function() {
18-
assert.throws(function() { mapnik.Logger.setSeverity(); });
19-
assert.throws(function() { mapnik.Logger.setSeverity(null); });
20-
assert.throws(function() { mapnik.Logger.setSeverity(2,3); });
21-
});
17+
test('set_severity should fail with bad input', (assert) => {
18+
assert.throws(function() { mapnik.Logger.setSeverity(); });
19+
assert.throws(function() { mapnik.Logger.setSeverity(null); });
20+
assert.throws(function() { mapnik.Logger.setSeverity(2,3); });
21+
assert.end();
22+
});
2223

23-
it('set_severity should set mapnik.logger', function(done) {
24-
var orig_severity = mapnik.Logger.getSeverity();
25-
mapnik.Logger.setSeverity(mapnik.Logger.NONE);
26-
assert.equal(mapnik.Logger.getSeverity(), mapnik.Logger.NONE);
27-
mapnik.Logger.setSeverity(orig_severity);
28-
done();
29-
});
24+
test('set_severity should set mapnik.logger', (assert) => {
25+
var orig_severity = mapnik.Logger.getSeverity();
26+
mapnik.Logger.setSeverity(mapnik.Logger.NONE);
27+
assert.equal(mapnik.Logger.getSeverity(), mapnik.Logger.NONE);
28+
mapnik.Logger.setSeverity(orig_severity);
29+
assert.end();
3030
});

0 commit comments

Comments
 (0)