Skip to content

Commit

Permalink
First version of pantsd log
Browse files Browse the repository at this point in the history
  • Loading branch information
Borja Lorente committed Dec 10, 2018
1 parent 6e6bdc9 commit a6651a3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/python/pants/init/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def setup_logging_from_options(bootstrap_options):
return setup_logging(level, console_stream=sys.stderr, log_dir=bootstrap_options.logdir)


def setup_logging(level, console_stream=None, log_dir=None, scope=None, log_name=None):
def setup_logging(level, console_stream=None, log_dir=None, scope=None, log_name=None, native=None):
"""Configures logging for a given scope, by default the global scope.
:param str level: The logging level to enable, must be one of the level names listed here:
Expand Down Expand Up @@ -95,10 +95,15 @@ def trace(self, message, *args, **kwargs):
logger.addHandler(console_handler)

if log_dir:

safe_mkdir(log_dir)
log_filename = os.path.join(log_dir, log_name or 'pants.log')
file_handler = FileHandler(log_filename)

if native:
print("BL: setup_logging with native, filename {}".format(log_filename))
native.lib.setup_pantsd_logging(log_filename, level)

class GlogFormatter(Formatter):
LEVEL_MAP = {
logging.FATAL: 'F',
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/pantsd/pants_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def _pantsd_logging(self):
# for further forks.
with stdio_as(stdin_fd=-1, stdout_fd=-1, stderr_fd=-1):
# Reinitialize logging for the daemon context.
result = setup_logging(self._log_level, log_dir=self._log_dir, log_name=self.LOG_NAME)
result = setup_logging(self._log_level, log_dir=self._log_dir, log_name=self.LOG_NAME, native=self._native)

# Do a python-level redirect of stdout/stderr, which will not disturb `0,1,2`.
# TODO: Consider giving these pipes/actual fds, in order to make them "deep" replacements
Expand Down
20 changes: 17 additions & 3 deletions src/rust/engine/logging/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ use simplelog::Config;
use simplelog::WriteLogger;
use std::fs::File;
use std::io::{stderr, Stderr, Write};
use std::path::PathBuf;
use std::io::Error;

lazy_static! {
static ref LOGGER: Logger = Logger::new(LevelFilter::Off);
pub static ref LOGGER: Logger = Logger::new(LevelFilter::Off);
}

// This is a hard-coding of constants in the standard logging python package.
// TODO: Switch from CustomTryInto to TryFromPrimitive when try_from is stable.
#[derive(Debug, Eq, PartialEq, CustomTryInto)]
#[repr(u8)]
#[repr(u64)]
enum PythonLogLevel {
NotSet = 0,
// Trace doesn't exist in a Python world, so set it to "a bit lower than Debug".
Expand Down Expand Up @@ -76,7 +78,7 @@ impl Logger {
}

pub fn init(max_level: u8) {
let max_python_level = max_level.try_into_PythonLogLevel();
let max_python_level = (max_level as u64).try_into_PythonLogLevel();
match max_python_level {
Ok(python_level) => {
let level: log::LevelFilter = python_level.into();
Expand All @@ -97,6 +99,18 @@ impl Logger {
pub fn set_stderr_log_level(&self, level: LevelFilter) {
self.stderr_log.lock().set_level(level);
}

pub fn set_pantsd_logger(&self, log_file_path: PathBuf, python_level: u64) -> Result<(), String> {
let res = python_level.try_into_PythonLogLevel().and_then( |level| {
// File::create(log_file_path).map(|file| {
File::create(log_file_path.clone()).map(|file| {
*self.pantsd_log.lock() = MaybeWriteLogger::new(file, level.into());
}).map_err(|err| format!("{}", err))
});

info!("BL: Set pantsd logger at {:?}", log_file_path);
res
}
}

impl Log for Logger {
Expand Down
11 changes: 11 additions & 0 deletions src/rust/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ use scheduler::{ExecutionRequest, RootResult, Scheduler, Session};
use tasks::Tasks;
use types::Types;

use logging::logger::LOGGER;

// TODO: Consider renaming and making generic for collections of PyResults.
#[repr(C)]
pub struct RawNodes {
Expand Down Expand Up @@ -783,6 +785,15 @@ pub extern "C" fn materialize_directories(
.into()
}

#[no_mangle]
pub extern "C" fn setup_pantsd_logging(log_file_ptr: *const raw::c_char, level: u64) -> PyResult {
let path_str = unsafe { CStr::from_ptr(log_file_ptr).to_string_lossy().into_owned() };
let path = PathBuf::from(path_str);
eprintln!("BL: Setup pantsd logger");
LOGGER.set_pantsd_logger(path, level)
.into()
}

fn graph_full(scheduler: &Scheduler, subject_types: Vec<TypeId>) -> RuleGraph {
let graph_maker = GraphMaker::new(&scheduler.core.tasks, subject_types);
graph_maker.full_graph()
Expand Down

0 comments on commit a6651a3

Please sign in to comment.