Skip to content

Commit

Permalink
android: redirect stdout/stderr to logcat
Browse files Browse the repository at this point in the history
add the 'redirectOutputToLogcat' optional argument
move shared code to src/common/
update readme
update sample
  • Loading branch information
enricogior committed Feb 20, 2018
1 parent 4e4d2ae commit adbe081
Show file tree
Hide file tree
Showing 21 changed files with 417 additions and 492 deletions.
43 changes: 37 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cordova plugin add nodejs-mobile-cordova

## Reporting Issues

We have a [central repo](https://github.com/janeasystems/nodejs-mobile/issues) where we manage all the issues related to Node.js for Mobile Apps, including specific issues of the Node.js for Mobile Cordova plugin.
We have a [central repo](https://github.com/janeasystems/nodejs-mobile/issues) where we manage all the issues related to Node.js for Mobile Apps, including specific issues of the Node.js for Mobile Apps Cordova plugin.
So please, open the issue [there](https://github.com/janeasystems/nodejs-mobile/issues).

## Cordova Methods
Expand All @@ -32,26 +32,52 @@ So please, open the issue [there](https://github.com/janeasystems/nodejs-mobile/
### nodejs.start

```js
nodejs.start(scriptFileName, callback);
nodejs.start(scriptFileName, callback [, options]);
```
| Param | Type |
| --- | --- |
| scriptFileName | <code>string</code> |
| callback | <code>function</code> |
| options | <code>[StartupOptions](#cordova.StartupOptions)</code> |


### nodejs.startWithScript

```js
nodejs.startWithScript(scriptBody, callback);
nodejs.startWithScript(scriptBody, callback [, options]);
```
| Param | Type |
| --- | --- |
| scriptBody | <code>string</code> |
| callback | <code>function</code> |
| options | <code>[StartupOptions](#cordova.StartupOptions)</code> |

### nodejs.channel.setListener

```js
nodejs.channel.setListener(listenerCallback);
```
| Param | Type |
| --- | --- |
| listenerCallback | <code>function</code> |

### nodejs.channel.send

```js
nodejs.channel.send(message);
```
| Param | Type |
| --- | --- |
| message | <code>string</code> |

<a name="cordova.StartupOptions"></a>
### StartupOptions: <code>object</code>
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| redirectOutputToLogcat | <code>boolean</code> | <code>true</code> | Allows to disable the redirection of the Node stdout/stderr to the Android logcat |

Note: the stdout/stderr redirection is applied to the whole application, the side effect is that some undesired/duplicated output may appear in the logcat.
For example, the Chromium console output `I/chromium: [INFO:CONSOLE(xx)]` is also sent to stderr and will show up in logcat has well, with the `NODEJS-MOBILE` log tag.

## Node.js Methods

Expand All @@ -67,13 +93,19 @@ So please, open the issue [there](https://github.com/janeasystems/nodejs-mobile/
```
cordova.channel.send(message);
```
| Param | Type |
| --- | --- |
| message | <code>string</code> |

### cordova.channel.on

```
cordova.channel.on('message', listnerCallback);
```

| Param | Type |
| --- | --- |
| 'message' | <code>const string</code> |
| listnerCallback | <code>function</code> |

## Usage

Expand Down Expand Up @@ -111,8 +143,7 @@ Switch to Xcode:
* enlarge the `Console` area, at the end of the console log it should show:

```
2017-10-02 18:49:18.606100+0200 HelloCordova[2182:1463518] NodeJs Engine Started
2017-10-02 18:49:18.606100+0200 HelloCordova[2182:1463518] Node.js Mobile Engine Started
[node] received: Hello from Cordova!
2017-10-02 18:49:18.690132+0200 HelloCordova[2182:1463518] [cordova] received: Replying to this message: Hello from Cordova!
```

8 changes: 4 additions & 4 deletions install/hooks/ios/after-plugin-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module.exports = function(context) {
xcodeProject.parseSync();
var firstTargetUUID = xcodeProject.getFirstTarget().uuid;

//Adds a build phase to rebuild native modules
var rebuildNativeModulesBuildPhaseName = 'Build NodeJS Mobile Native Modules';
// Adds a build phase to rebuild native modules.
var rebuildNativeModulesBuildPhaseName = 'Build Node.js Mobile Native Modules';
var rebuildNativeModulesBuildPhaseScript = `
set -e
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
Expand Down Expand Up @@ -55,8 +55,8 @@ popd
);
}

//Adds a build phase to sign native modules
var signNativeModulesBuildPhaseName = 'Sign NodeJS Mobile Native Modules';
// Adds a build phase to sign native modules.
var signNativeModulesBuildPhaseName = 'Sign Node.js Mobile Native Modules';
var signNativeModulesBuildPhaseScript = `
set -e
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
Expand Down
12 changes: 6 additions & 6 deletions install/hooks/ios/before-plugin-uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var fs = require('fs');
module.exports = function(context) {
var xcode = context.requireCordovaModule('xcode');

//Adds a custom function to remove script build phases, which is not supported on cordova's Xcode module yet.
// Adds a custom function to remove script build phases, which is not supported on cordova's Xcode module yet.
xcode.project.prototype.myRemovePbxScriptBuildPhase = function (buildPhaseName, target) {
var buildPhaseTargetUuid = target || this.getFirstTarget().uuid;

Expand All @@ -14,7 +14,7 @@ module.exports = function(context) {
throw new Error("Couldn't find the build script phase to remove: " + buildPhaseName );
}

// remove the '_comment' suffix to get the actual uuid
// Remove the '_comment' suffix to get the actual uuid.
var buildPhaseUuid=buildPhaseUuid_comment.split('_')[0];

// Remove from the pbxBuildPhaseObjects
Expand Down Expand Up @@ -49,15 +49,15 @@ module.exports = function(context) {
xcodeProject.parseSync();
var firstTargetUUID = xcodeProject.getFirstTarget().uuid;

//Removes the build phase to rebuild native modules
var rebuildNativeModulesBuildPhaseName = 'Build NodeJS Mobile Native Modules';
// Removes the build phase to rebuild native modules.
var rebuildNativeModulesBuildPhaseName = 'Build Node.js Mobile Native Modules';
var rebuildNativeModulesBuildPhase = xcodeProject.buildPhaseObject('PBXShellScriptBuildPhase', rebuildNativeModulesBuildPhaseName, firstTargetUUID);
if (rebuildNativeModulesBuildPhase) {
xcodeProject.myRemovePbxScriptBuildPhase(rebuildNativeModulesBuildPhaseName, firstTargetUUID);
}

//Removes the build phase to sign native modules
var signNativeModulesBuildPhaseName = 'Sign NodeJS Mobile Native Modules';
// Removes the build phase to sign native modules.
var signNativeModulesBuildPhaseName = 'Sign Node.js Mobile Native Modules';
var signNativeModulesBuildPhase = xcodeProject.buildPhaseObject('PBXShellScriptBuildPhase', signNativeModulesBuildPhaseName, firstTargetUUID);
if (signNativeModulesBuildPhase) {
xcodeProject.myRemovePbxScriptBuildPhase(signNativeModulesBuildPhaseName, firstTargetUUID);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cordova-bridge",
"version": "0.1.0",
"description": "NodeJS for Mobile Native Bridge",
"description": "Node.js for Mobile Apps Native Bridge",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
31 changes: 17 additions & 14 deletions install/sample-project/www/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ var app = {
app.initialize();

function channelListener(msg) {
console.log("[cordova] received:", msg);
}

console.log('[cordova] received: ' + msg);
};

function startupCallback(err) {
if (err) {
console.log(err);
} else {
console.log ('Node.js Mobile Engine Started');
nodejs.channel.send('Hello from Cordova!');
}
};

function startNodeProject() {
nodejs.channel.setListener(channelListener);
nodejs.start("main.js",
function(err) {
if (err) {
console.log(err);
} else {
console.log ("NodeJs Engine Started");
nodejs.channel.send("Hello from Cordova!");
}
});
}
nodejs.channel.setListener(channelListener);
nodejs.start('main.js', startupCallback);
// To disable the stdout/stderr redirection to the Android logcat:
// nodejs.start('main.js', startupCallback, { redirectOutputToLogcat: false });
};
2 changes: 1 addition & 1 deletion install/sample-project/www/nodejs-project/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nodejs-mobile-sample-project",
"version": "0.1.0",
"description": "NodeJS for Mobile sample project",
"description": "Node.js for Mobile Apps sample project",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
14 changes: 7 additions & 7 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
id="nodejs-mobile-cordova"
version="0.1.2">

<name>NodeJS Mobile</name>
<description>NodeJS for Mobile Cordova Plugin</description>
<name>Node.js Mobile</name>
<description>Node.js for Mobile Apps Cordova Plugin</description>
<license>MIT</license>
<keywords>cordova,mobile,nodejs,node.js</keywords>

Expand Down Expand Up @@ -35,8 +35,8 @@

<header-file src="src/ios/CDVNodeJS.hh" />
<source-file src="src/ios/CDVNodeJS.mm" />
<header-file src="src/ios/NativeModule.hpp" />
<source-file src="src/ios/NativeModule.cpp" />
<header-file src="src/common/cordova-bridge/cordova-bridge.h" />
<source-file src="src/common/cordova-bridge/cordova-bridge.cpp" />
<header-file src="src/ios/NodeJSRunner.hh" />
<source-file src="src/ios/NodeJSRunner.mm" />
<header-file src="libs/ios/nodemobile/include" />
Expand Down Expand Up @@ -69,9 +69,9 @@

<source-file src="src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java" target-dir="src/com/janeasystems/cdvnodejsmobile/" />

<source-file src="src/android/jni/cordova-bridge.h" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="src/android/jni/cordova-bridge.cpp" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="src/android/jni/native-module.cpp" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="src/common/cordova-bridge/cordova-bridge.h" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="src/common/cordova-bridge/cordova-bridge.cpp" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="src/android/jni/native-lib.cpp" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />

<source-file src="libs/android/libnode/" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />

Expand Down
6 changes: 3 additions & 3 deletions src/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ cmake_minimum_required(VERSION 3.4.1)
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
native-module
nodejs-mobile-cordova-native-lib

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
native-module.cpp
native-lib.cpp
cordova-bridge.cpp
)

Expand Down Expand Up @@ -54,7 +54,7 @@ find_library( # Sets the name of the path variable.
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
native-module
nodejs-mobile-cordova-native-lib

libnode

Expand Down
Loading

0 comments on commit adbe081

Please sign in to comment.