<commit_before>// Cymbalum, an extensible molecular simulation engine
// Copyright (C) 2015-2016 <NAME> — BSD license

//! Logging configuration
//!
//! This module provides a `Logger` struct which can be used for logging
//! messages from Cymbalum. One and only one of the initilialization function
//! should be called, and it should only be called once. Any call to
//! initializing function after the first one will be ignored.
//!
//! Other [log](https://github.com/rust-lang/log) compatible crates can be used
//! for logging if you whant.

use std::io;
use std::io::prelude::*;
use std::fs::OpenOptions;
use std::path::Path;
use std::sync::{Arc, Mutex};

use log::{Log, LogRecord, LogMetadata, set_logger};
pub use log::LogLevel;

/// Logger with capacity to write to the standard output stream, the standard
/// error stream or a file.
 pub struct Logger {
     level: LogLevel,
     writer: Arc<Mutex<Box<Write + Send + Sync>>>,
 }

 impl Logger{
     fn new<T>(level: LogLevel, handle: T) -> Logger where T: Write + Send + Sync + 'static {
         ::chemfiles::Logger::get().log_callback(|level, message| {
             match level {
                 ::chemfiles::LogLevel::Error => error!("{}", message),
                 ::chemfiles::LogLevel::Warning => warn!("{}", message),
                 ::chemfiles::LogLevel::Info => info!("{}", message),
                 ::chemfiles::LogLevel::Debug => debug!("{}", message),
             }
         }).expect("Could not set the chemfiles logging callback");
         Logger{
             level: level,
             writer: Arc::new(Mutex::new(Box::new(handle))),
         }
     }

     fn init(logger: Logger) {
         let res = set_logger(|max_log_level| {
             max_log_level.set(logger.level.to_log_level_filter());
             Box::new(logger)
         });

         if let Err(_) = res {
             warn!("Logger was initialized more than once! This call is ignored.");
         }
     }

     /// Initialize the global logger to use the standard output stream.
     pub fn stdout() {
         let logger = Logger::new(levels::DEFAULT_LEVEL, io::stdout());
         Logger::init(logger);
     }

     /// Initialize the global logger to use the standard output stream, with
     /// the maximum log level of `level`
     pub fn stdout_at_level(level: LogLevel) {
         let logger = Logger::new(level, io::stdout());
         Logger::init(logger);
     }

     /// Initialize the global logger to use the standard error stream.
     pub fn stderr() {
         let logger = Logger::new(levels::DEFAULT_LEVEL, io::stderr());
         Logger::init(logger);
     }

     /// Initialize the global logger to use the standard error stream, with
     /// the maximum log level of `level`
     pub fn stderr_at_level(level: LogLevel) {
         let logger = Logger::new(level, io::stderr());
         Logger::init(logger);
     }

     /// Initialize the global logger to write to the file at `path`, creating
     /// the file if needed. If the file already exists, it is not removed.
     pub fn file<P: AsRef<Path>>(path: P) -> Result<(), io::Error> {
         let file = try!(OpenOptions::new().write(true).append(true).open(path));
         let logger = Logger::new(levels::DEFAULT_LEVEL, file);
         Logger::init(logger);
         Ok(())
     }

     /// Initialize the global logger to write to the file at `path`, creating
     /// the file if needed. If the file already exists, it is not removed. The
     /// maximum log level is set to `level`.
     pub fn file_at_level<P: AsRef<Path>>(path: P, level: LogLevel) -> Result<(), io::Error> {
         let file = try!(OpenOptions::new().write(true).append(true).open(path));
         let logger = Logger::new(level, file);
         Logger::init(logger);
         Ok(())
     }
 }
<commit_msg>Factorize Logger method, and add missing create<commit_after>// Cymbalum, an extensible molecular simulation engine
// Copyright (C) 2015-2016 <NAME> — BSD license

//! Logging configuration
//!
//! This module provides a `Logger` struct which can be used for logging
//! messages from Cymbalum. One and only one of the initilialization function
//! should be called, and it should only be called once. Any call to
//! initializing function after the first one will be ignored.
//!
//! Other [log](https://github.com/rust-lang/log) compatible crates can be used
//! for logging if you whant.

use std::io;
use std::io::prelude::*;
use std::fs::OpenOptions;
use std::path::Path;
use std::sync::{Arc, Mutex};

use log::{Log, LogRecord, LogMetadata, set_logger};
pub use log::LogLevel;

/// Logger with capacity to write to the standard output stream, the standard
/// error stream or a file.
 pub struct Logger {
     level: LogLevel,
     writer: Arc<Mutex<Box<Write + Send + Sync>>>,
 }

 impl Logger{
     fn new<T>(level: LogLevel, handle: T) -> Logger where T: Write + Send + Sync + 'static {
         ::chemfiles::Logger::get().log_callback(|level, message| {
             match level {
                 ::chemfiles::LogLevel::Error => error!("{}", message),
                 ::chemfiles::LogLevel::Warning => warn!("{}", message),
                 ::chemfiles::LogLevel::Info => info!("{}", message),
                 ::chemfiles::LogLevel::Debug => debug!("{}", message),
             }
         }).expect("Could not set the chemfiles logging callback");
         Logger{
             level: level,
             writer: Arc::new(Mutex::new(Box::new(handle))),
         }
     }

     fn init(logger: Logger) {
         let res = set_logger(|max_log_level| {
             max_log_level.set(logger.level.to_log_level_filter());
             Box::new(logger)
         });

         if let Err(_) = res {
             warn!("Logger was initialized more than once! This call is ignored.");
         }
     }

     /// Initialize the global logger to use the standard output stream.
     pub fn stdout() {
         Logger::stdout_at_level(levels::DEFAULT_LEVEL)
     }

     /// Initialize the global logger to use the standard output stream, with
     /// the maximum log level of `level`
     pub fn stdout_at_level(level: LogLevel) {
         let logger = Logger::new(level, io::stdout());
         Logger::init(logger);
     }

     /// Initialize the global logger to use the standard error stream.
     pub fn stderr() {
         Logger::stderr_at_level(levels::DEFAULT_LEVEL)
     }

     /// Initialize the global logger to use the standard error stream, with
     /// the maximum log level of `level`
     pub fn stderr_at_level(level: LogLevel) {
         let logger = Logger::new(level, io::stderr());
         Logger::init(logger);
     }

     /// Initialize the global logger to write to the file at `path`, creating
     /// the file if needed. If the file already exists, it is not removed.
     pub fn file<P: AsRef<Path>>(path: P) -> Result<(), io::Error> {
         Logger::file_at_level(path, levels::DEFAULT_LEVEL)
     }

     /// Initialize the global logger to write to the file at `path`, creating
     /// the file if needed. If the file already exists, it is not removed. The
     /// maximum log level is set to `level`.
     pub fn file_at_level<P: AsRef<Path>>(path: P, level: LogLevel) -> Result<(), io::Error> {
         let file = try!(OpenOptions::new().write(true).create(true).append(true).open(path));
         let logger = Logger::new(level, file);
         Logger::init(logger);
         Ok(())
     }
 }

{'fraction_non_alphanumeric': 0.13778789501874666, 'fraction_numerical': 0.0021424745581146223, 'mean_word_length': 2.602990834539315, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 39, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7341105', 'n_tokens_mistral': 2098, 'n_tokens_neox': 2060, 'n_words': 715}