Skip to content

Commit

Permalink
Merge pull request #2 from BlockProject3D/v3-rewrite
Browse files Browse the repository at this point in the history
V3 rewrite
  • Loading branch information
Yuri6037 authored Jun 17, 2024
2 parents 330bbf0 + 0f7e73f commit f6fc3b8
Show file tree
Hide file tree
Showing 14 changed files with 1,062 additions and 604 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
matrix:
os:
- ubuntu-20.04
- macos-11
- macos-12
- windows-2019
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Build
Expand All @@ -36,7 +36,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
Expand All @@ -54,7 +54,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Install Audit Tool
Expand All @@ -73,7 +73,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
Expand All @@ -94,7 +94,7 @@ jobs:
ispre: ${{ steps.version.outputs.ispre }}
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Get Version
id: version
uses: bp3d-actions/cargo-version@main
Expand All @@ -109,7 +109,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Setup cargo
Expand All @@ -132,7 +132,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Create Pull Request
uses: repo-sync/pull-request@master
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
ispre: ${{ steps.version.outputs.ispre }}
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Get Version
id: version
uses: bp3d-actions/cargo-version@main
Expand All @@ -31,7 +31,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Setup cargo
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[package]
name = "bp3d-logger"
version = "2.0.0-rc.1.0.0"
version = "2.0.0-rc.2.0.0"
authors = ["Yuri Edward <yuri6037@outlook.com>"]
edition = "2021"
description = "A flexible Log implementation intended to be used with BP3D software."
license = "BSD-3-Clause"
repository = "https://gitlab.com/bp3d/logger"
repository = "https://github.com/BlockProject3D/debug.logger"
readme = "./README.MD"
keywords = []
categories = []

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "0.4.14"
bp3d-os = { version = "1.0.0-rc.1.2.1", features=["dirs", "time"] }
bp3d-os = { version = "1.0.0-rc.3.0.2", features=["dirs", "time"] }
bp3d-util = { version = "1.0.0", features = ["format"] }
crossbeam-channel = "0.5.2"
crossbeam-queue = "0.3.8"
once_cell = "1.10.0"
Expand Down
200 changes: 200 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Copyright (c) 2024, BlockProject 3D
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of BlockProject 3D nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::handler::{FileHandler, Handler, StdHandler};
use crate::internal::Logger;
use crate::level::LevelFilter;
use crate::GetLogs;

/// Enum of the different color settings when printing to stdout/stderr.
#[derive(Debug, Copy, Clone)]
pub enum Colors {
/// Color printing is always enabled.
Enabled,

/// Color printing is always disabled.
Disabled,

/// Color printing is automatic (if current terminal is a tty, print with colors, otherwise
/// print without colors).
Auto,
}

impl Default for Colors {
fn default() -> Self {
Self::Disabled
}
}

/// The base logger builder/initializer.
///
/// # Examples
///
/// The following example shows basic initialization of this logger.
/// ```
/// use bp3d_logger::{Builder, Level, LogMsg, Location};
///
/// fn main() {
/// let logger = Builder::new().add_stdout().add_file("my-app").start();
/// logger.log(&LogMsg::from_msg(Location::new("bp3d-logger", "test.c", 1), Level::Info, "Example message"));
/// }
/// ```
///
/// The following example shows initialization of this logger and use of the log buffer.
/// ```
/// use bp3d_logger::{Builder, Level, LogMsg, Location, LevelFilter, handler::{LogQueue, LogQueueHandler}};
///
/// fn main() {
/// let queue = LogQueue::default();
/// let logger = Builder::new().add_stdout().add_handler(LogQueueHandler::new(queue.clone())).start();
///
/// //... application code with log redirect pump.///
/// logger.log(&LogMsg::from_msg(Location::new("bp3d-logger", "test.c", 1), Level::Info, "Example message"));
/// logger.set_filter(LevelFilter::None);
/// logger.log(&LogMsg::from_msg(Location::new("bp3d-logger", "test.c", 1), Level::Info, "Dropped message"));
/// logger.raw_log(&LogMsg::from_msg(Location::new("bp3d-logger", "test.c", 1), Level::Info, "Example message 1"));
/// logger.set_filter(LevelFilter::Info);
///
/// logger.flush();
/// let l = queue.pop().unwrap(); // Capture the last log message.
/// // We can't test for equality because log messages contains a timestamp...
/// assert_eq!(l.msg(), "Example message");
/// let l = queue.pop().unwrap();
/// assert_eq!(l.msg(), "Example message 1");
/// //... application code without log redirect pump.
/// }
/// ```
pub struct Builder {
pub(crate) colors: Colors,
pub(crate) smart_stderr: bool,
pub(crate) buf_size: Option<usize>,
pub(crate) handlers: Vec<Box<dyn Handler>>,
pub(crate) filter: LevelFilter,
}

impl Default for Builder {
fn default() -> Self {
Self {
colors: Colors::default(),
smart_stderr: true,
buf_size: None,
handlers: Vec::new(),
filter: LevelFilter::Info,
}
}
}

impl Builder {
/// Creates a new instance of a logger builder.
pub fn new() -> Builder {
Builder::default()
}

/// Sets the colors state when logging to stdout/stderr.
///
/// The default behavior is to disable colors.
pub fn colors(mut self, state: Colors) -> Self {
self.colors = state;
self
}

/// Sets the default level filter when initializing the logger.
///
/// The default is [Info](LevelFilter::Info).
pub fn filter(mut self, filter: LevelFilter) -> Self {
self.filter = filter;
self
}

/// Enables or disables automatic redirection of error logs to stderr.
///
/// The default for this flag is true.
pub fn smart_stderr(mut self, flag: bool) -> Self {
self.smart_stderr = flag;
self
}

/// Sets the buffer size.
///
/// # Arguments
///
/// * `buf_size`: the buffer size.
///
/// returns: Builder
pub fn buffer_size(mut self, buf_size: usize) -> Self {
self.buf_size = Some(buf_size);
self
}

/// Adds a new log [Handler](Handler).
///
/// # Arguments
///
/// * `handler`: the new handler implementation to add.
///
/// returns: Builder
pub fn add_handler<T: Handler + 'static>(mut self, handler: T) -> Self {
self.handlers.push(Box::new(handler));
self
}

/// Enables stdout logging.
pub fn add_stdout(self) -> Self {
let motherfuckingrust = self.smart_stderr;
let motherfuckingrust1 = self.colors;
self.add_handler(StdHandler::new(motherfuckingrust, motherfuckingrust1))
}

/// Enables file logging to the given application.
///
/// The application is given as a reference to [GetLogs](GetLogs) to allow obtaining
/// a log directory from various sources.
///
/// If the log directory could not be found the function prints an error to stderr.
pub fn add_file<T: GetLogs>(self, app: T) -> Self {
if let Some(logs) = app.get_logs() {
self.add_handler(FileHandler::new(logs))
} else {
eprintln!("Failed to obtain application log directory");
self
}
}

/// Initializes the log implementation with this current configuration.
///
/// NOTE: This returns an instance of [Logger](Logger) which is the main entry point for all
/// logging based operations. This instance also acts as a guard to flush all log buffers
/// before returning. It is necessary to flush log buffers because this implementation
/// uses threads to avoid blocking the main thread when issuing logs.
///
/// NOTE 2: There are no safety concerns with running twice this function in the same
/// application, only that calling this function may be slow due to thread management.
pub fn start(self) -> Logger {
Logger::new(self)
}
}
4 changes: 2 additions & 2 deletions src/easy_termcolor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021, BlockProject 3D
// Copyright (c) 2024, BlockProject 3D
//
// All rights reserved.
//
Expand Down Expand Up @@ -26,7 +26,7 @@
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use log::Level;
use crate::Level;
use std::fmt::Display;
use termcolor::{Color, ColorSpec};

Expand Down
Loading

0 comments on commit f6fc3b8

Please sign in to comment.