Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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: 34 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ zip = { version = "0.6", default-features = false }
zstd = "0.13"

# dist-server only
env_filter = "0.1.3"
hostname = "0.4.0"
nix = { version = "0.28.0", optional = true, features = [
"mount",
"user",
Expand Down
24 changes: 24 additions & 0 deletions docs/Distributed.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,30 @@ token = "preshared token"
type = "DANGEROUSLY_INSECURE"
```

# Logging

## stderr

You can set the `SCCACHE_LOG` environment variable to a comma-separated list of directives that specifies the levels and modules desired.

For example:
```
SCCACHE_LOG=info,sccache_dist=trace,sccache=trace,sccache_heartbeat=off
```


For more details, consult the [`env_logger`](https://docs.rs/env_logger/latest/env_logger/#enabling-logging) documentation.

## Syslog

Use the `--syslog` argument to enable logging to a syslog daemon.
The presence of the `SCCACHE_LOG` environment variable takes precedence over this argument.
The same syntax is accepted:

```
--syslog info,sccache_dist=debug,sccache=debug,sccache_heartbeat=off
```


# Building the Distributed Server Binaries

Expand Down
99 changes: 48 additions & 51 deletions src/bin/sccache-dist/cmdline/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{env, ffi::OsString, fmt, net::SocketAddr, path::PathBuf, str::FromStr};
use std::{
ffi::OsString,
fmt,
io::BufWriter,
net::{SocketAddr, TcpStream, UdpSocket},
path::PathBuf,
process,
};

use anyhow::{anyhow, bail};
use clap::{Arg, ArgGroup, Command as ClapCommand, ValueEnum};
use clap::{Arg, ArgGroup, Command as ClapCommand};
use sccache::{config, dist::ServerId};
use syslog::Facility;
use syslog::{unix, BasicLogger, Formatter3164, LoggerBackend};
use syslog::{Facility, Logger};

use crate::cmdline::{AuthSubcommand, Command};

#[derive(Debug, Clone)]
struct TokenLength(usize);

Expand Down Expand Up @@ -53,53 +60,14 @@ impl fmt::Display for TokenLength {
}
}

#[derive(Clone, Copy, ValueEnum)]
enum LogLevel {
Error,
Warn,
Info,
Debug,
Trace,
}

impl FromStr for LogLevel {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let variant = match s {
"error" => Self::Error,
"warn" => Self::Warn,
"info" => Self::Info,
"debug" => Self::Debug,
"trace" => Self::Trace,
_ => bail!("Unknown log level: {:?}", s),
};

Ok(variant)
}
}

impl From<LogLevel> for log::LevelFilter {
fn from(log_level: LogLevel) -> Self {
match log_level {
LogLevel::Error => Self::Error,
LogLevel::Warn => Self::Warn,
LogLevel::Info => Self::Info,
LogLevel::Debug => Self::Debug,
LogLevel::Trace => Self::Trace,
}
}
}

fn flag_infer_long(name: &'static str) -> Arg {
Arg::new(name).long(name)
}

fn get_clap_command() -> ClapCommand {
let syslog = flag_infer_long("syslog")
.help("Log to the syslog with LEVEL")
.value_name("LEVEL")
.value_parser(clap::value_parser!(LogLevel));
.value_name("LEVEL");
let config_with_help_message = |help: &'static str| {
flag_infer_long("config")
.help(help)
Expand Down Expand Up @@ -155,9 +123,38 @@ fn get_clap_command() -> ClapCommand {
]))
}

fn check_init_syslog(name: &str, log_level: LogLevel) {
let level = log::LevelFilter::from(log_level);
drop(syslog::init(Facility::LOG_DAEMON, level, Some(name)));
fn check_init_syslog(name: &str, log_filter: &str) {
let facility = Facility::LOG_DAEMON;
let process = name.to_string();
let pid = process::id();
let mut formatter = Formatter3164 {
facility,
hostname: None,
process,
pid,
};

let backend = if let Ok(logger) = unix(formatter.clone()) {
logger.backend
} else {
formatter.hostname = Some(hostname::get().unwrap().to_string_lossy().to_string());
if let Ok(tcp_stream) = TcpStream::connect(("127.0.0.1", 601)) {
LoggerBackend::Tcp(BufWriter::new(tcp_stream))
} else {
let udp_addr = "127.0.0.1:514".parse().unwrap();
let udp_stream = UdpSocket::bind(("127.0.0.1", 0)).unwrap();
LoggerBackend::Udp(udp_stream, udp_addr)
}
};

let mut builder = env_filter::Builder::new();
builder.parse(log_filter);
let filter = builder.build();
log::set_max_level(filter.filter());

let logger =
env_filter::FilteredLog::new(BasicLogger::new(Logger { formatter, backend }), filter);
drop(log::set_boxed_logger(Box::new(logger)));
}

/// Parse commandline `args` into a `Result<Command>` to execute.
Expand Down Expand Up @@ -217,9 +214,9 @@ pub fn try_parse_from(
Some(("scheduler", matches)) => {
if matches.contains_id("syslog") {
let log_level = matches
.get_one::<LogLevel>("syslog")
.get_one::<String>("syslog")
.expect("`syslog` is required");
check_init_syslog("sccache-scheduler", *log_level);
check_init_syslog("sccache-scheduler", log_level.as_str());
}

let config_path = matches
Expand All @@ -234,9 +231,9 @@ pub fn try_parse_from(
Some(("server", matches)) => {
if matches.contains_id("syslog") {
let log_level = matches
.get_one::<LogLevel>("syslog")
.get_one::<String>("syslog")
.expect("`syslog` is required");
check_init_syslog("sccache-buildserver", *log_level);
check_init_syslog("sccache-buildserver", log_level.as_str());
}

let config_path = matches
Expand Down
Loading