Skip to content

Commit

Permalink
Merge pull request #155 from lars-t-hansen/w-152-log
Browse files Browse the repository at this point in the history
For #152 - Remove env_logger and log dependencies
  • Loading branch information
bast authored Apr 4, 2024
2 parents cb4349a + 6e47e60 commit e572e25
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 102 deletions.
81 changes: 0 additions & 81 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ edition = "2021"
[dependencies]
subprocess = "0.2"
clap = { version = "4.5", features = ["derive"] }
log = "0.4"
env_logger = "0.11"
libc = "0.2"
signal-hook = { version = "0.3", default-features = false, features = [] }
13 changes: 13 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// A trivial logging package, that can be replaced by something more interesting if necessary.

pub fn init() {
// Currently nothing
}

pub fn info(s: &str) {
eprintln!("Info: {s}");
}

pub fn error(s: &str) {
eprintln!("Error: {s}");
}
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate env_logger;

use clap::{Parser, Subcommand};

mod amd;
Expand All @@ -8,6 +6,7 @@ mod command;
mod gpu;
mod hostname;
mod jobs;
mod log;
mod nvidia;
mod procfs;
mod procfsapi;
Expand Down Expand Up @@ -83,7 +82,7 @@ fn main() {
// improperly reflects the true order in which they were obtained. See #100.
let timestamp = time::now_iso8601();

env_logger::init();
log::init();

let cli = Cli::parse();

Expand Down
16 changes: 7 additions & 9 deletions src/ps.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]

extern crate log;

use crate::amd;
use crate::hostname;
use crate::jobs;
use crate::log;
use crate::nvidia;
use crate::procfs;
use crate::procfsapi;
Expand Down Expand Up @@ -258,10 +257,10 @@ pub fn create_snapshot(jobs: &mut dyn jobs::JobManager, opts: &PsOptions, timest
}

if skip {
log::info!("Lockfile present, exiting");
log::info("Lockfile present, exiting");
}
if failed {
log::error!("Unable to properly manage or delete lockfile");
log::error("Unable to properly manage or delete lockfile");
}
} else {
do_create_snapshot(jobs, opts, timestamp, &interrupted);
Expand Down Expand Up @@ -289,16 +288,15 @@ fn do_create_snapshot(
let memtotal_kib = match procfs::get_memtotal_kib(&fs) {
Ok(n) => n,
Err(e) => {
log::error!("Could not get installed memory: {}", e);
log::error(&format!("Could not get installed memory: {e}"));
return;
}
};

let procinfo_output = match procfs::get_process_information(&fs, memtotal_kib) {
Ok(result) => result,
Err(msg) => {
eprintln!("procfs failed: {msg}");
log::error!("procfs failed: {msg}");
log::error(&format!("procfs failed: {msg}"));
return;
}
};
Expand Down Expand Up @@ -348,7 +346,7 @@ fn do_create_snapshot(
gpu_status = GpuStatus::UnknownFailure;
// This is a soft failure, surfaced through dashboards; we do not want mail about it
// under normal circumstances.
//log::error!("GPU (Nvidia) process listing failed: {:?}", e);
//log::error(&format!("GPU (Nvidia) process listing failed: {:?}", e));
}
Ok(ref nvidia_output) => {
for proc in nvidia_output {
Expand Down Expand Up @@ -383,7 +381,7 @@ fn do_create_snapshot(
gpu_status = GpuStatus::UnknownFailure;
// This is a soft failure, surfaced through dashboards; we do not want mail about it
// under normal circumstances.
//log::error!("GPU (AMD) process listing failed: {:?}", e);
//log::error(&format!("GPU (AMD) process listing failed: {:?}", e));
}
Ok(ref amd_output) => {
for proc in amd_output {
Expand Down
21 changes: 14 additions & 7 deletions src/sysinfo.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
extern crate log;

use crate::amd;
use crate::gpu;
use crate::hostname;
use crate::log;
use crate::nvidia;
use crate::procfs;
use crate::procfsapi;
Expand All @@ -16,14 +15,18 @@ pub fn show_system(timestamp: &str) {
match do_show_system(&mut writer, &fs, timestamp) {
Ok(_) => {}
Err(e) => {
log::error!("sysinfo failed: {e}");
log::error(&format!("sysinfo failed: {e}"));
}
}
}

const GIB: usize = 1024 * 1024 * 1024;

fn do_show_system(writer: &mut dyn io::Write, fs: &dyn procfsapi::ProcfsAPI, timestamp: &str) -> Result<(), String> {
fn do_show_system(
writer: &mut dyn io::Write,
fs: &dyn procfsapi::ProcfsAPI,
timestamp: &str,
) -> Result<(), String> {
let (model, sockets, cores_per_socket, threads_per_core) = procfs::get_cpu_info(fs)?;
let mem_by = procfs::get_memtotal_kib(fs)? * 1024;
let mem_gib = (mem_by as f64 / GIB as f64).round() as i64;
Expand Down Expand Up @@ -78,13 +81,16 @@ fn do_show_system(writer: &mut dyn io::Write, fs: &dyn procfsapi::ProcfsAPI, tim
};
let timestamp = util::json_quote(timestamp);
let hostname = util::json_quote(&hostname);
let description = util::json_quote(&format!("{sockets}x{cores_per_socket}{ht} {model}, {mem_gib} GiB{gpu_desc}"));
let description = util::json_quote(&format!(
"{sockets}x{cores_per_socket}{ht} {model}, {mem_gib} GiB{gpu_desc}"
));
let cpu_cores = sockets * cores_per_socket * threads_per_core;

// Note the field names here are used by decoders that are developed separately, and they should
// be considered set in stone.

let s = format!(r#"{{
let s = format!(
r#"{{
"timestamp": "{timestamp}",
"hostname": "{hostname}",
"description": "{description}",
Expand All @@ -93,7 +99,8 @@ fn do_show_system(writer: &mut dyn io::Write, fs: &dyn procfsapi::ProcfsAPI, tim
"gpu_cards": {gpu_cards},
"gpumem_gb": {gpumem_gb}
}}
"#);
"#
);

// Ignore I/O errors.

Expand Down

0 comments on commit e572e25

Please sign in to comment.