~garritfra/taurus

Nicer logs and logger module v1 APPLIED

Alexey Yerin: 1
 Nicer logs and logger module

 2 files changed, 16 insertions(+), 7 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~garritfra/taurus/patches/14949/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] Nicer logs and logger module Export this patch

Signed-off-by: Alexey Yerin <yerinalexey98fd@gmail.com>
---
 src/logger.rs |  9 +++++++++
 src/main.rs   | 14 +++++++-------
 2 files changed, 16 insertions(+), 7 deletions(-)
 create mode 100644 src/logger.rs

diff --git a/src/logger.rs b/src/logger.rs
new file mode 100644
index 0000000..9408dfa
--- /dev/null
+++ b/src/logger.rs
@@ -0,0 +1,9 @@
use std::fmt::Display;

pub fn info(message: impl Display) {
    println!("[info] {}", message);
}

pub fn error(message: impl Display) {
    println!("[err] {}", message);
}
diff --git a/src/main.rs b/src/main.rs
index 0e85963..86d899b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,6 +4,7 @@ extern crate url;
mod config;
mod error;
mod gemini;
mod logger;

use error::{TaurusError, TaurusResult};
use gemini::{GeminiRequest, GeminiResponse};
@@ -21,7 +22,7 @@ use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg

fn main() {
    if let Err(e) = run() {
        println!("Error: {}", e);
        logger::error(e);
        std::process::exit(1);
    }
}
@@ -66,7 +67,7 @@ fn run() -> TaurusResult<()> {
    let acceptor = TlsAcceptor::new(identity).unwrap();
    let acceptor = Arc::new(acceptor);

    println!("Info: Listening on port {}", port);
    logger::info(format!("Listening on port {}", port));

    for stream in listener.incoming() {
        match stream {
@@ -77,15 +78,15 @@ fn run() -> TaurusResult<()> {
                thread::spawn(move || match acceptor.accept(stream) {
                    Ok(stream) => {
                        if let Err(e) = handle_client(stream, &static_root) {
                            println!("Error: can't handle client: {}", e);
                            logger::error(format!("Can't handle client: {}", e));
                        }
                    }
                    Err(e) => {
                        println!("Error: can't handle stream: {}", e);
                        logger::error(format!("Can't handle stream: {}", e));
                    }
                });
            }
            Err(err) => println!("Error: {}", err),
            Err(err) => logger::error(err),
        }
    }

@@ -119,8 +120,7 @@ fn write_file(path: &str) -> TaurusResult<GeminiResponse> {
        Ok(buf) => Ok(GeminiResponse::success(buf, mime_type)),
        Err(err) => {
            // Cannot read file or it doesn't exist

            println!("Error [{}]: {}", path, err);
            logger::error(format!("{}: {}", path, err));

            Ok(GeminiResponse::not_found())
        }
-- 
2.29.2
Applied, thanks!

Unfortunately, builds.sr.ht can not yet pick up patches without a
project specifier. For future patches, could you add the project to
the subject line (e.g. [PATCH taurus]), to trigger a build?

"Patches have to have their subject prefix configured to match the
name of the repository being tested. For example, “[PATCH example v2]”
will be matched to a repository named “example”." See: [1].

[1]: https://sourcehut.org/blog/2020-07-14-setting-up-ci-for-mailing-lists/