Skip to content

Commit cd12d35

Browse files
Logs are now configurable
1 parent 37b2922 commit cd12d35

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "cynthiaweb"
33
description = "A simple web server and generator based on plain file editing. I hate WordPress."
44
documentation = "https://cynthia-docs.strawmelonjuice.com/"
55
authors = ["MLC Bloeiman <[email protected]>"]
6-
version = "2.0.6-alpha.1"
6+
version = "2.0.6-alpha.2"
77
edition = "2021"
88
license = "GPL-3.0 OR MIT"
99

src/logger.rs

+94
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,32 @@ const DATE_FORMAT_STR: &str = "[year]-[month]-[day]-[hour]:[minute]:[second]:[su
88
const SPACES: usize = 32;
99
const DIVVER: &str = "\t";
1010
pub(crate) fn general_log(msg: String) {
11+
let log_enabled: bool = match std::env::var("LOG_ENABLED") {
12+
Ok(g) => g.parse::<bool>().unwrap(),
13+
Err(_) => true,
14+
};
15+
if !log_enabled {return};
1116
// General log item -- '[log]'
1217
log_by_act_num(1, msg)
1318
}
1419

1520
pub(crate) fn cache_log(msg: String) {
21+
let log_enabled: bool = match std::env::var("LOG_CACHE_ENABLED") {
22+
Ok(g) => g.parse::<bool>().unwrap(),
23+
Err(_) => false,
24+
};
25+
if !log_enabled {return};
1626
// Log item for caching -- '[cache]'
1727
log_by_act_num(31, msg)
1828
}
1929

2030
pub(crate) fn general_error(msg: String) {
31+
// Check if these log items are enabled
32+
let log_enabled: bool = match std::env::var("LOG_ERROR_ENABLED") {
33+
Ok(g) => g.parse::<bool>().unwrap(),
34+
Err(_) => true,
35+
};
36+
if !log_enabled {return};
2137
// General error item -- '[ERROR]'
2238
log_by_act_num(5, msg)
2339
}
@@ -27,31 +43,87 @@ pub(crate) fn fatal_error(msg: String) {
2743
}
2844

2945
pub(crate) fn general_warn(msg: String) {
46+
// Check if these log items are enabled
47+
let log_enabled: bool = match std::env::var("LOG_WARN_ENABLED") {
48+
Ok(g) => g.parse::<bool>().unwrap(),
49+
Err(_) => true,
50+
};
51+
if !log_enabled {return};
3052
// General warning item -- '[WARN]'
3153
log_by_act_num(15, msg)
3254
}
3355

3456
pub(crate) fn jsr_error(msg: String) {
57+
// Check if these log items are enabled
58+
let log_enabled: bool = match std::env::var("LOG_JSR_ERROR_ENABLED") {
59+
Ok(g) => g.parse::<bool>().unwrap(),
60+
Err(_) => true,
61+
};
62+
if !log_enabled {return};
3563
// Error in JavaScript runtime
3664
log_by_act_num(12, msg)
3765
}
3866

3967
pub(crate) fn general_info(msg: String) {
68+
// Check if these log items are enabled
69+
let log_enabled: bool = match std::env::var("LOG_JSR_INFO_ENABLED") {
70+
Ok(g) => g.parse::<bool>().unwrap(),
71+
Err(_) => true,
72+
};
73+
if !log_enabled {return};
4074
// General info item -- '[INFO]'
4175
log_by_act_num(10, msg)
4276
}
4377

4478
pub(crate) fn req_ok(msg: String) {
79+
// Check if these log items are enabled
80+
let log_enabled: bool = match std::env::var("LOG_REQUESTS_ENABLED") {
81+
Ok(g) => g.parse::<bool>().unwrap(),
82+
Err(_) => true,
83+
};
84+
if !log_enabled {return};
4585
// Request that on Cynthia's part succeeded (and is so responded to) -- '[CYNGET/OK]'
4686
log_by_act_num(200, msg)
4787
}
4888

4989
pub(crate) fn req_notfound(msg: String) {
90+
// Check if these log items are enabled
91+
let log_enabled: bool = match std::env::var("LOG_REQUESTS_ENABLED") {
92+
Ok(g) => g.parse::<bool>().unwrap(),
93+
Err(_) => true,
94+
};
95+
if !log_enabled {return};
5096
// Request for an item that does not exist Cynthia published.jsonc
5197
log_by_act_num(404, msg)
5298
}
5399

100+
pub(crate) fn req_serve_proxied(msg: String) {
101+
// Check if these log items are enabled
102+
let log_enabled: bool = match std::env::var("LOG_PROXY_REQUESTS_ENABLED") {
103+
Ok(g) => g.parse::<bool>().unwrap(),
104+
Err(_) => true,
105+
};
106+
if !log_enabled {return};
107+
// Proxying a request to a plugin
108+
log_by_act_num(49038, msg)
109+
}
110+
pub(crate) fn req_serve_plugin_asset(msg: String) {
111+
// Check if these log items are enabled
112+
let log_enabled: bool = match std::env::var("LOG_PLUGIN_ASSET_REQUESTS_ENABLED") {
113+
Ok(g) => g.parse::<bool>().unwrap(),
114+
Err(_) => true,
115+
};
116+
if !log_enabled {return};
117+
// Serving a plugin asset
118+
log_by_act_num(293838, msg)
119+
}
120+
54121
pub(crate) fn log_by_act_num(act: i32, msg: String) {
122+
let log_enabled: bool = match std::env::var("LOG_ENABLED") {
123+
Ok(g) => g.parse::<bool>().unwrap(),
124+
Err(_) => true,
125+
};
126+
if !log_enabled {return};
55127
/*
56128
57129
Acts:
@@ -132,6 +204,28 @@ pub(crate) fn log_by_act_num(act: i32, msg: String) {
132204
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
133205
eprintln!("{0}{1}", preq, msg.bright_red().on_bright_yellow());
134206
}
207+
49038 => {
208+
let name = format!("[{} - [PROXY]", times);
209+
let spaceleft = if name.chars().count() < SPACES {
210+
SPACES - name.chars().count()
211+
} else {
212+
0
213+
};
214+
let title = format!("{}", name.bold().bright_magenta());
215+
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
216+
println!("{0}❕{DIVVER}{1}", preq, msg.bright_green());
217+
}
218+
293838 => {
219+
let name = format!("[{} - [PLUGIN ASSET]", times);
220+
let spaceleft = if name.chars().count() < SPACES {
221+
SPACES - name.chars().count()
222+
} else {
223+
0
224+
};
225+
let title = format!("{}", name.bold().bright_magenta());
226+
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
227+
println!("{0}❕{DIVVER}{1}", preq, msg.bright_green());
228+
}
135229
10 => {
136230
let name = format!("[{} - [NOTE]", times);
137231
let spaceleft = if name.chars().count() < SPACES {

src/main.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ async fn serves_e(
163163
if id.starts_with(&**l) {
164164
let fid = id.replace(&**l, "");
165165
let fileb = format!("./plugins/{}/{}/{fid}", plugin.name, s[0]);
166-
let file = Path::new(&fileb);
167-
logger::general_info(
168-
format!("Serving {}", file.canonicalize().unwrap().display()),
166+
let file: &Path = Path::new(&fileb);
167+
logger::req_serve_plugin_asset(
168+
format!("Serving '{}' for '{}'.", file.canonicalize().unwrap().display(), plugin.name),
169169
);
170170
return NamedFile::open(file);
171171
};
@@ -196,7 +196,6 @@ async fn serves_es(req: HttpRequest, pluginsmex: Data<Mutex<Vec<PluginMeta>>>) -
196196
None => {}
197197
}
198198
}
199-
200199
HttpResponse::Ok().body(body)
201200
}
202201

0 commit comments

Comments
 (0)