Skip to content

Commit

Permalink
Add file logger (#853)
Browse files Browse the repository at this point in the history
  • Loading branch information
wkozyra95 authored Nov 14, 2024
1 parent f7597ae commit 01e55fd
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 72 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
### ✨ New features

- Add `loop` option for MP4 input. ([#699](https://github.com/software-mansion/live-compositor/pull/699) by [@WojciechBarczynski](https://github.com/WojciechBarczynski))
- Add `LIVE_COMPOSITOR_LOG_FILE` environment variable to enable logging to file ([#853](https://github.com/software-mansion/live-compositor/pull/853) by [@wkozyra95](https://github.com/wkozyra95))
- Add border, border radius and box shadow options to `Rescaler` and `View` components. ([#815](https://github.com/software-mansion/live-compositor/pull/815) by [@WojciechBarczynski](https://github.com/WojciechBarczynski), ([#839](https://github.com/software-mansion/live-compositor/pull/839), [#842](https://github.com/software-mansion/live-compositor/pull/842), [#858](https://github.com/software-mansion/live-compositor/pull/858) by [@wkozyra95](https://github.com/wkozyra95))


### 🐛 Bug fixes

Expand Down
4 changes: 4 additions & 0 deletions docs/pages/deployment/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,7 @@ Increasing this value always increases the latency of the stream by the same amo
:::

Defaults to `80ms` (about 5 frames in 60 fps).

### `LIVE_COMPOSITOR_LOG_FILE`

Path to the file were Live Compositor logs should be written. Setting this option does not disable logging to the standard output.
13 changes: 2 additions & 11 deletions generate/src/compositor_instance.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use anyhow::{anyhow, Result};
use crossbeam_channel::Sender;
use live_compositor::{
config::{read_config, LoggerConfig, LoggerFormat},
logger::{self, FfmpegLogLevel},
server::run_api,
state::ApiState,
};
use live_compositor::{config::read_config, logger, server::run_api, state::ApiState};
use reqwest::StatusCode;
use std::{
env,
Expand Down Expand Up @@ -113,10 +108,6 @@ fn init_compositor_prerequisites() {
GLOBAL_PREREQUISITES_INITIALIZED.get_or_init(|| {
env::set_var("LIVE_COMPOSITOR_WEB_RENDERER_ENABLE", "0");
ffmpeg_next::format::network::init();
logger::init_logger(LoggerConfig {
ffmpeg_logger_level: FfmpegLogLevel::Info,
format: LoggerFormat::Compact,
level: "warn,wgpu_hal=warn,wgpu_core=warn".to_string(),
});
logger::init_logger(read_config().logger);
});
}
14 changes: 3 additions & 11 deletions integration_tests/examples/encoded_channel_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ use compositor_render::{
InputId, OutputId, Resolution,
};
use integration_tests::examples::download_file;
use live_compositor::{
config::{read_config, LoggerConfig, LoggerFormat},
logger::{self, FfmpegLogLevel},
state::ApiState,
};
use live_compositor::{config::read_config, logger, state::ApiState};

const BUNNY_FILE_URL: &str =
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
Expand All @@ -38,13 +34,9 @@ const BUNNY_FILE_PATH: &str = "examples/assets/BigBuckBunny.mp4";
// Data read from channels are dumped into files as it is without any timestamp data.
fn main() {
ffmpeg_next::format::network::init();
logger::init_logger(LoggerConfig {
ffmpeg_logger_level: FfmpegLogLevel::Info,
format: LoggerFormat::Compact,
level: "info,wgpu_hal=warn,wgpu_core=warn".to_string(),
});
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let mut config = read_config();
logger::init_logger(config.logger.clone());
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
config.queue_options.ahead_of_time_processing = true;
// no chromium support, so we can ignore _event_loop
let (state, _event_loop) = ApiState::new(config).unwrap_or_else(|err| {
Expand Down
10 changes: 3 additions & 7 deletions integration_tests/examples/raw_channel_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,17 @@ use compositor_render::{
};
use integration_tests::{gstreamer::start_gst_receive_tcp, test_input::TestInput};
use live_compositor::{
config::{read_config, LoggerConfig, LoggerFormat},
logger::{self, FfmpegLogLevel},
config::read_config,
logger::{self},
};

const VIDEO_OUTPUT_PORT: u16 = 8002;

// Start simple pipeline with input that sends PCM audio and wgpu::Textures via Rust channel.
fn main() {
ffmpeg_next::format::network::init();
logger::init_logger(LoggerConfig {
ffmpeg_logger_level: FfmpegLogLevel::Info,
format: LoggerFormat::Compact,
level: "info,wgpu_hal=warn,wgpu_core=warn".to_string(),
});
let config = read_config();
logger::init_logger(config.logger);
let ctx = GraphicsContext::new(false, Default::default(), Default::default()).unwrap();
let (wgpu_device, wgpu_queue) = (ctx.device.clone(), ctx.queue.clone());
// no chromium support, so we can ignore _event_loop
Expand Down
10 changes: 3 additions & 7 deletions integration_tests/examples/raw_channel_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use crossbeam_channel::bounded;
use image::{codecs::png::PngEncoder, ColorType, ImageEncoder};
use integration_tests::{examples::download_file, read_rgba_texture};
use live_compositor::{
config::{read_config, LoggerConfig, LoggerFormat},
logger::{self, FfmpegLogLevel},
config::read_config,
logger::{self},
};

const BUNNY_FILE_URL: &str =
Expand All @@ -50,11 +50,7 @@ fn root_dir() -> PathBuf {
// - read audio samples and write raw value using debug formatting
fn main() {
ffmpeg_next::format::network::init();
logger::init_logger(LoggerConfig {
ffmpeg_logger_level: FfmpegLogLevel::Info,
format: LoggerFormat::Compact,
level: "info,wgpu_hal=warn,wgpu_core=warn".to_string(),
});
logger::init_logger(read_config().logger);
let mut config = read_config();
config.queue_options.ahead_of_time_processing = true;
let ctx = GraphicsContext::new(false, Default::default(), Default::default()).unwrap();
Expand Down
10 changes: 3 additions & 7 deletions integration_tests/examples/vulkan.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use anyhow::Result;
use integration_tests::examples::download_all_assets;
use live_compositor::{
config::{LoggerConfig, LoggerFormat},
logger::{self, FfmpegLogLevel},
config::read_config,
logger::{self},
};

fn main() {
ffmpeg_next::format::network::init();
logger::init_logger(LoggerConfig {
ffmpeg_logger_level: FfmpegLogLevel::Info,
format: LoggerFormat::Compact,
level: "info,wgpu_hal=warn,wgpu_core=warn".to_string(),
});
logger::init_logger(read_config().logger);

download_all_assets().unwrap();

Expand Down
11 changes: 4 additions & 7 deletions integration_tests/src/compositor_instance.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::{anyhow, Result};
use crossbeam_channel::Sender;
use live_compositor::{
config::{read_config, Config, LoggerConfig, LoggerFormat},
logger::{self, FfmpegLogLevel},
config::{read_config, Config},
logger::{self},
server::run_api,
state::ApiState,
};
Expand Down Expand Up @@ -112,10 +112,7 @@ fn init_compositor_prerequisites() {
GLOBAL_PREREQUISITES_INITIALIZED.get_or_init(|| {
env::set_var("LIVE_COMPOSITOR_WEB_RENDERER_ENABLE", "0");
ffmpeg_next::format::network::init();
logger::init_logger(LoggerConfig {
ffmpeg_logger_level: FfmpegLogLevel::Info,
format: LoggerFormat::Compact,
level: "info,wgpu_hal=warn,wgpu_core=warn".to_string(),
});
let config = read_config();
logger::init_logger(config.logger);
});
}
15 changes: 14 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::{env, path::PathBuf, str::FromStr, time::Duration};
use std::{
env,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
time::Duration,
};

use compositor_pipeline::queue::{self, QueueOptions};
use compositor_render::{web_renderer::WebRendererInitOptions, Framerate, WgpuFeatures};
Expand All @@ -25,6 +31,7 @@ pub struct LoggerConfig {
pub ffmpeg_logger_level: FfmpegLogLevel,
pub format: LoggerFormat,
pub level: String,
pub log_file: Option<Arc<Path>>,
}

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -182,13 +189,19 @@ fn try_read_config() -> Result<Config, String> {
Err(_) => queue::DEFAULT_BUFFER_DURATION,
};

let log_file = match env::var("LIVE_COMPOSITOR_LOG_FILE") {
Ok(path) => Some(Arc::from(PathBuf::from(path))),
Err(_) => None,
};

let config = Config {
instance_id,
api_port,
logger: LoggerConfig {
ffmpeg_logger_level,
format: logger_format,
level: logger_level,
log_file,
},
queue_options: QueueOptions {
default_buffer_duration,
Expand Down
64 changes: 43 additions & 21 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
use std::{str::FromStr, sync::OnceLock};
use std::{
fmt::Debug,
fs::{self, File},
str::FromStr,
sync::OnceLock,
};

use tracing_subscriber::{
fmt::{self},
layer::SubscriberExt,
util::SubscriberInitExt,
Layer, Registry,
};

use crate::config::{read_config, LoggerConfig, LoggerFormat};

Expand Down Expand Up @@ -58,27 +70,37 @@ extern "C" fn ffmpeg_log_callback(
}

pub fn init_logger(opts: LoggerConfig) {
let env_filter = tracing_subscriber::EnvFilter::new(opts.level);
match opts.format {
LoggerFormat::Pretty => {
tracing_subscriber::fmt()
.pretty()
.with_env_filter(env_filter)
.init();
}
LoggerFormat::Json => {
tracing_subscriber::fmt()
.json()
.with_env_filter(env_filter)
.init();
}
LoggerFormat::Compact => {
tracing_subscriber::fmt()
.compact()
.with_env_filter(env_filter)
.init();
}
let env_filter = tracing_subscriber::EnvFilter::new(opts.level.clone());

let stdout_layer = match opts.format {
LoggerFormat::Pretty => fmt::Layer::default().pretty().boxed(),
LoggerFormat::Json => fmt::Layer::default().json().boxed(),
LoggerFormat::Compact => fmt::Layer::default().compact().boxed(),
};

let file_layer = if let Some(log_file) = opts.log_file {
if log_file.exists() {
fs::remove_file(&log_file).unwrap()
};
fs::create_dir_all(log_file.parent().unwrap()).unwrap();
let writer = File::create(log_file).unwrap();
Some(fmt::Layer::default().json().with_writer(writer))
} else {
None
};

match file_layer {
Some(file_layer) => Registry::default()
.with(stdout_layer)
.with(file_layer)
.with(env_filter)
.init(),
None => Registry::default()
.with(stdout_layer)
.with(env_filter)
.init(),
}

unsafe {
ffmpeg_next::sys::av_log_set_callback(Some(ffmpeg_log_callback));
}
Expand Down

0 comments on commit 01e55fd

Please sign in to comment.