Skip to content

Commit

Permalink
Add info debugging.
Browse files Browse the repository at this point in the history
It is useful, particularly when something seems to be going wrong with
snare or github settings, to have a verbosity level that tells you when
a request has been received.
  • Loading branch information
ltratt committed Feb 5, 2024
1 parent a61aec7 commit eba8bac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 1 addition & 2 deletions snare.1
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ This can be useful for debugging.
.It Fl v
enables more verbose logging.
.Fl v
can be used up to 4 times, with each repetition increasing the quantity
of logging.
may be specified multiple times, with each increasing the quantity of logging.
.El
.Sh INTEGRATION WITH GITHUB
.Nm
Expand Down
3 changes: 2 additions & 1 deletion src/httpserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,12 @@ fn request(snare: &Arc<Snare>, mut stream: TcpStream) {
}
drop(conf);

let repo_id = format!("github/{}/{}", owner, repo);
snare.info(&format!("Received {event_type} for {repo_id}"));
if event_type == "ping" {
return;
}

let repo_id = format!("github/{}/{}", owner, repo);
let qj = QueueJob::new(
repo_id,
owner.to_owned(),
Expand Down
18 changes: 16 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ use std::{
};

use getopts::Options;
use libc::{c_char, openlog, syslog, LOG_CONS, LOG_CRIT, LOG_DAEMON, LOG_ERR, LOG_WARNING};
use libc::{
c_char, openlog, syslog, LOG_CONS, LOG_CRIT, LOG_DAEMON, LOG_ERR, LOG_INFO, LOG_WARNING,
};
use nix::{
fcntl::OFlag,
unistd::{daemon, pipe2, setresgid, setresuid, Gid, Uid},
Expand All @@ -45,6 +47,7 @@ const SNARE_CONF_PATH: &str = "/etc/snare/snare.conf";
pub enum LogLevel {
Error,
Warn,
Info,
}

pub(crate) struct Snare {
Expand Down Expand Up @@ -100,6 +103,7 @@ impl Snare {
let syslog_level = match self.log_level {
LogLevel::Error => LOG_ERR,
LogLevel::Warn => LOG_WARNING,
LogLevel::Info => LOG_INFO,
};
unsafe {
syslog(syslog_level, fmt.as_ptr(), msg.as_ptr());
Expand All @@ -126,6 +130,15 @@ impl Snare {
pub fn warn(&self, msg: &str) {
self.log(msg, LogLevel::Warn);
}

/// Log `msg` as an informational message.
///
/// # Panics
///
/// If `msg` contains a `NUL` byte.
pub fn info(&self, msg: &str) {
self.log(msg, LogLevel::Info);
}
}

/// Try to find a `snare.conf` file.
Expand Down Expand Up @@ -239,7 +252,8 @@ pub fn main() {

let log_level = match matches.opt_count("v") {
0 => LogLevel::Error,
_ => LogLevel::Warn,
1 => LogLevel::Warn,
_ => LogLevel::Info,
};

change_user(&conf);
Expand Down

0 comments on commit eba8bac

Please sign in to comment.