Skip to content

Commit 00caa8e

Browse files
committed
Add log apis and example log.
1 parent 4605749 commit 00caa8e

File tree

20 files changed

+260
-7
lines changed

20 files changed

+260
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ members = [
99

1010
# internal
1111
"examples/hello",
12+
"examples/log",
1213
# "examples/mini-curl",
1314
]

examples/hello/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE

examples/log/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "log"
3+
version = "0.0.0"
4+
authors = ["jmjoy <[email protected]>"]
5+
edition = "2018"
6+
publish = false
7+
license = "Unlicense"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[lib]
12+
crate-type = ["cdylib"]
13+
14+
[dependencies]
15+
phper = { version = "0.2.0-alpha.2", path = "../../phper" }
16+
17+
[dev-dependencies]
18+
phper-test = { version = "0.2.0-alpha.2", path = "../../phper-test" }
19+
20+
[build-dependencies]
21+
phper-build = { version = "0.2.0-alpha.2", path = "../../phper-build" }

examples/log/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE

examples/log/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# hello
2+
3+
Log example.
4+
5+
## Environment
6+
7+
```bash
8+
# Specify if php isn't installed globally.
9+
export PHP_CONFIG = <Your path of php-config>
10+
```
11+
12+
## Build
13+
14+
```bash
15+
cargo build --release
16+
```
17+
18+
## Test
19+
20+
```bash
21+
cargo test --release
22+
```
23+
24+
## Install
25+
26+
```bash
27+
cargo run --release -- install
28+
```

examples/log/src/lib.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use phper::{
2+
deprecated, error, functions::Argument, modules::Module, notice, php_get_module, values::Val,
3+
warning,
4+
};
5+
6+
#[php_get_module]
7+
pub fn get_module(module: &mut Module) {
8+
// set module metadata
9+
module.set_name(env!("CARGO_PKG_NAME"));
10+
module.set_version(env!("CARGO_PKG_VERSION"));
11+
module.set_author(env!("CARGO_PKG_AUTHORS"));
12+
13+
module.add_function(
14+
"log_notice",
15+
|params: &mut [Val]| {
16+
let message = params[0].as_string();
17+
notice!("Something happened: {}", message);
18+
},
19+
vec![Argument::by_val("message")],
20+
);
21+
22+
module.add_function(
23+
"log_warning",
24+
|params: &mut [Val]| {
25+
let message = params[0].as_string();
26+
warning!("Something warning: {}", message);
27+
},
28+
vec![Argument::by_val("message")],
29+
);
30+
31+
module.add_function(
32+
"log_error",
33+
|params: &mut [Val]| {
34+
let message = params[0].as_string();
35+
error!("Something gone failed: {}", message);
36+
},
37+
vec![Argument::by_val("message")],
38+
);
39+
40+
module.add_function(
41+
"log_deprecated",
42+
|params: &mut [Val]| {
43+
let message = params[0].as_string();
44+
deprecated!("Something deprecated: {}", message);
45+
},
46+
vec![Argument::by_val("message")],
47+
);
48+
}

examples/log/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use phper::cmd::make;
2+
3+
fn main() {
4+
make();
5+
}

examples/log/tests/integration.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use phper_test::test_php_scripts_with_condition;
2+
use std::{env, path::Path, str};
3+
4+
#[test]
5+
fn test_php() {
6+
test_php_scripts_with_condition(
7+
env!("CARGO_BIN_EXE_log"),
8+
&[Path::new(env!("CARGO_MANIFEST_DIR"))
9+
.join("tests")
10+
.join("php")
11+
.join("test_php_notice.php")],
12+
|output| {
13+
let stdout = str::from_utf8(&output.stdout).unwrap();
14+
stdout.contains("Notice:")
15+
&& stdout.contains("Something happened: just for test")
16+
&& output.status.success()
17+
},
18+
);
19+
20+
test_php_scripts_with_condition(
21+
env!("CARGO_BIN_EXE_log"),
22+
&[Path::new(env!("CARGO_MANIFEST_DIR"))
23+
.join("tests")
24+
.join("php")
25+
.join("test_php_warning.php")],
26+
|output| {
27+
let stdout = str::from_utf8(&output.stdout).unwrap();
28+
stdout.contains("Warning:")
29+
&& stdout.contains("Something warning: just for test")
30+
&& output.status.success()
31+
},
32+
);
33+
34+
test_php_scripts_with_condition(
35+
env!("CARGO_BIN_EXE_log"),
36+
&[Path::new(env!("CARGO_MANIFEST_DIR"))
37+
.join("tests")
38+
.join("php")
39+
.join("test_php_error.php")],
40+
|output| {
41+
let stdout = str::from_utf8(&output.stdout).unwrap();
42+
stdout.contains("Fatal error:")
43+
&& stdout.contains("Something gone failed: just for test")
44+
},
45+
);
46+
47+
test_php_scripts_with_condition(
48+
env!("CARGO_BIN_EXE_log"),
49+
&[Path::new(env!("CARGO_MANIFEST_DIR"))
50+
.join("tests")
51+
.join("php")
52+
.join("test_php_deprecated.php")],
53+
|output| {
54+
let stdout = str::from_utf8(&output.stdout).unwrap();
55+
stdout.contains("Deprecated:")
56+
&& stdout.contains("Something deprecated: just for test")
57+
&& output.status.success()
58+
},
59+
);
60+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
ini_set("display_errors", "On");
4+
ini_set("display_startup_errors", "On");
5+
error_reporting(E_ALL);
6+
7+
log_deprecated("just for test");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
ini_set("display_errors", "On");
4+
ini_set("display_startup_errors", "On");
5+
error_reporting(E_ALL);
6+
7+
log_error("just for test");

0 commit comments

Comments
 (0)