-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogger.rs
More file actions
45 lines (37 loc) · 1.26 KB
/
Copy pathlogger.rs
File metadata and controls
45 lines (37 loc) · 1.26 KB
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
//! A simple logger used from lua,
//! but initialization is opaque for the user.
use chrono::Utc;
use mlua::{Lua, Result as LuaResult, Value};
pub fn setup(lua: &Lua) -> LuaResult<()> {
lua.globals().set(
"logger_init",
lua.create_function(|lua, file_name: Option<String>| {
let logger: Value = lua.globals().get("__INTERNAL_LOGGER__")?;
if logger != Value::Nil {
// Already initialized, do nothing.
return Ok(logger);
}
let date = Utc::now().format("%A, %B %e, %Y %H:%M:%S").to_string();
let logfile = file_name.unwrap_or("kipt.out".to_string());
lua.load(format!(
"__INTERNAL_LOGGER__ = io.open(\"{}\", \"w\")
__INTERNAL_LOGGER__:write(\"-- {} --\", \"\\n\\n\")
",
logfile, date,
))
.exec()?;
let logger: mlua::Value = lua.globals().get("__INTERNAL_LOGGER__")?;
Ok(logger)
})?,
)?;
Ok(())
}
pub fn write(lua: &Lua, data: &str) -> LuaResult<()> {
lua.load(format!("__INTERNAL_LOGGER__:write(\"{}\", \"\\n\")", data))
.exec()?;
Ok(())
}
pub fn close(lua: &Lua) -> LuaResult<()> {
lua.load("__INTERNAL_LOGGER__:close()").exec()?;
Ok(())
}