1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

//!# [stderr](https://github.com/biluohc/stderr)
//!  A library that using macro to write to `io::stderr()` like `print!()/println!()`.
//!
//!## Usage
//!
//! On Cargo.toml:
//!
//! ```toml
//!  [dependencies]
//!  stderr = "0.7.1"
//! ```
//!
//!## About stderr
//!Usage as same as `print!/println!`.
//!
//!1. `err!`/`errln!`: Panics if writing to `io::stderr()` fails.
//!2. `errst!`/`errstln!`: Do nothing if writing to `io::stderr()` fails(silent->st).
//!
//!
//!## Example
//!
//!```Rust
//!#[macro_use]
//!extern crate stderr;
//!
//!fn main() {
//!  println!("err!/errln!/errst!/errstln!()@stderr !");
//!  let vec = vec![1, 2, 3, 4, 5];
//!  let s = std::env::args().nth(0).unwrap();
//!
//!  err!("err!(expr\\n)\n");
//!  err!("err!(String: vec![1, 2, 3, 4, 5])\n{}: {:?}", s, vec);
//!  errln!();
//!  errln!("errln!(expr)");
//!
//!  println!();
//!
//!  errst!("errst!(expr\\n)\n");
//!  errst!("errst!(String: vec![1, 2, 3, 4, 5])\n{}: {:?}", s, vec);
//!  errstln!();
//!  errstln!("errstln!(expr)");
//!
//!  // If you need to use `dbxxx!`,you must run `Loger::init(module_path!())` or `init!()` before use them on current process.
//!  // Otherwise you should ignore the following.
//!
//!  use stderr::Loger;  // `dbxxx!` belongs the module.
//!  Loger::init(module_path!());
//!  dbln!();
//!  dbln!("db!/dbln!()@Loger !");
//!  db!("{}\n", s);
//!  dbln!("{:?}", vec);
//!
//!  dbstln!();
//!  dbstln!("dbst!/dbstln!()@Loger !");
//!  dbst!("{}\n", s);
//!  dbstln!("{:?}", vec);
//!}
//!```
//!
//!## About stderr::Loger
//!`db!()`/`dbln!()`/`dbst!()`/`dbstln!()` print message while command line arguments conntains `-log` or `--log`(follows as [`module_path!()`](https://doc.rust-lang.org/std/macro.module_path.html),
//!if it has no followers(None) or followers by `''`,will print the message them occurred on current project(Use a stderr crate.)
//!
//!or environment variable 'LOG' as [`module_path!()`](https://doc.rust-lang.org/std/macro.module_path.html).
//!
//!if Value is None(`LOG=`) or (`LOG=''`),as same as above.
//!
//!Ps: fish can not set None.
//!### Example(if crate'name is 'app', a dependency's name is crate_lib):
//!
//!### Bash
//!
//!```bash
//!   env LOG=app  app_file_path
//!   env LOG=     app_file_path
//!   env LOG=''   app_file_path
//!   env LOG=app::mod1         app_file_path
//!   env LOG=app::mod1::mod2   app_file_path
//!   env LOG=* app_file_path   #use '*' to match all.
//!   env LOG=app,crate_lib   app_file_path     #print the message occurred on app or crate_lib
//!
//!   app_file_path --log app
//!   app_file_path --log
//!   app_file_path --log '' # use '' to avoid shell explain it.
//!
//!   app_file_path --log app::mod1
//!   app_file_path --log app::mod1::mod2
//!   app_file_path --log '*' # use '' to avoid shell explain it.
//!   app_file_path --log app,crate_lib
//!```
//!### Fish
//!```fish
//! set -x LOG app ; and app_file_path
//! set -x LOG '' ;and app_file_path
//! set -x LOG app::mod1 ;and app_file_path
//! set -x app::mod1::mod2 ;and app_file_path
//! set -x '*' ; and app_file_path
//! set -x 'app,crate_lib' ; and app_file_path
//!```
//!
//!If you neend to use `db!(),dbln!()` ,etc:
//!
//!You must use `Loger::init(module_path!())` or `init!()` before use them on the current process.
//!
extern crate time;
#[macro_use]
mod log;
pub use log::Loger;

// err!,errln!
#[macro_export]
macro_rules! err {
    ($($arg:tt)*) => {
        {
        use std::io::{self, Write};
        write!(&mut io::stderr(),$($arg)*).unwrap();
        // Panics if writing to io::stdout() fails.        
        }
        };
}
#[macro_export]
macro_rules! errln {
       () => (err!("\n"));
       ($fmt:expr) => (err!(concat!($fmt, "\n")));
        ($fmt:expr, $($arg:tt)*) => (err!(concat!($fmt, "\n"), $($arg)*));
}

// errst!,errstln!
#[macro_export]
macro_rules! errst {
    ($($arg:tt)*) => {
        {
        use std::io::{self, Write};
        if let Ok(..) =  write!(&mut io::stderr(),$($arg)* ) {};
        // Do nothing if writing to io::stdout() fails(silent->st).
        }
        };
}
#[macro_export]
macro_rules! errstln {
       () => (errst!("\n"));
       ($fmt:expr) => (errst!(concat!($fmt, "\n")));
        ($fmt:expr, $($arg:tt)*) => (errst!(concat!($fmt, "\n"), $($arg)*));
}