Skip to content

mclo.gs upload #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/api/mclogs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::collections::HashMap;

use eyre::Result;
use serde::{Deserialize, Serialize};

use super::{HttpClient, HttpClientExt};

const MCLOGS: &str = "https://api.mclo.gs/1";
const UPLOAD: &str = "/log";
const RAW: &str = "/raw";

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PostLogResponse {
pub success: bool,
pub id: Option<String>,
pub url: Option<String>,
pub raw: Option<String>,
pub error: Option<String>,
}

pub async fn upload_log(http: &HttpClient, content: &str) -> Result<PostLogResponse> {
let url = format!("{MCLOGS}{UPLOAD}");
let request = http
.post(url)
.form(&HashMap::from([("content", content)]))
.build()?;

Ok(http.execute(request).await?.json().await?)
}

pub async fn raw_log(http: &HttpClient, id: &str) -> Result<String> {
let url = format!("{MCLOGS}{RAW}/{id}");

Ok(http.get_request(&url).await?.text().await?)
}
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use reqwest::Response;

pub mod dadjoke;
pub mod github;
pub mod mclogs;
pub mod paste_gg;
pub mod pluralkit;
pub mod prism_meta;
Expand Down
83 changes: 83 additions & 0 deletions src/handlers/event/analyze_logs/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::sync::OnceLock;

use log::trace;
use regex::Regex;

// in future, we can add extra data to display in log analysis like Java version
pub enum Info {
Game,
Launcher,
}

pub fn find(log: &str) -> Option<Info> {
if looks_like_launcher_log(log) {
// launcher logs can sometimes seem like a game log
Some(Info::Launcher)
} else if looks_like_game_log(log) {
Some(Info::Game)
} else {
None
}
}

fn looks_like_launcher_log(log: &str) -> bool {
static QT_LOG_REGEX: OnceLock<Regex> = OnceLock::new();

trace!("Guessing whether log is launcher log");

let qt_log = QT_LOG_REGEX.get_or_init(|| Regex::new(r"\d+\.\d{3} [CDFIW] \|").unwrap());
qt_log.is_match(log)
}

fn looks_like_game_log(log: &str) -> bool {
static LOG4J_REGEX: OnceLock<Regex> = OnceLock::new();

trace!("Guessing whether log is Minecraft log");

if log.contains("Prism Launcher version: ") || log.contains("Minecraft process ID: ") {
return true;
}

// present in almost every Minecraft version
if log.contains("Setting user: ") || log.contains("Minecraft Version: ") {
return true;
}

if log.contains("Exception in thread ")
|| log.contains("Exception: ")
|| log.contains("Error: ")
|| log.contains("Throwable: ")
|| log.contains("Caused by: ")
{
return true;
}

if log.contains("org.prismlauncher.EntryPoint.main(EntryPoint.java")
|| log.contains("java.lang.Thread.run(Thread.java")
{
return true;
}

let log4j = LOG4J_REGEX.get_or_init(|| {
Regex::new(r"\[\d{2}:\d{2}:\d{2}\] \[.+?/(FATAL|ERROR|WARN|INFO|DEBUG|TRACE)\] ").unwrap()
});

if log4j.is_match(log) {
return true;
}

if log.contains("[INFO]")
|| log.contains("[CONFIG]")
|| log.contains("[FINE]")
|| log.contains("[FINER]")
|| log.contains("[FINEST]")
|| log.contains("[SEVERE]")
|| log.contains("[STDERR]")
|| log.contains("[WARNING]")
|| log.contains("[DEBUG]")
{
return true;
}

false
}
Loading
Loading