Code Monkey home page Code Monkey logo

glib_logger's Introduction

A simple logger that integrates with glib message logging mechanism. The logger is useful when one wants to integrate a piece of Rust code into a larger application which is already using glib/gio stack.

Example

use std::env;

use log;

fn main() {
    env::set_var("G_MESSAGES_DEBUG", "all");

    glib_logger::init(&glib_logger::SIMPLE);
    log::set_max_level(log::LevelFilter::Debug);

    log::info!("info message: {}", 2);
    log::warn!("warning message: {}", "foobar");
    log::debug!("Hello, world!");
}

Equivalent Vala code:

public void main() {
    Environment.set_variable ("G_MESSAGES_DEBUG", "all", false);

    info("info message: %d", 2);
    warning("warning message: %s", "foobar");
    debug("Hello, world!");
}

Running:

$ ./glib_logger_test
** INFO: 20:18:34.074: src/main.rs:12: info message: 2

** (process:39403): WARNING **: 20:18:34.076: src/main.rs:13: warning message: foobar
** (process:39403): DEBUG: 20:18:34.076: src/main.rs:15: Hello, world!

Details

Due to slight differences between the meaning of respective log levels, the crate takes certain liberties. Specifically the log level mappings are:

  • Level::Trace, Level::Debug => G_LOG_LEVEL_DEBUG
  • Level::Error => G_LOG_LEVEL_CRITICAL
  • Level::Info => G_LOG_LEVEL_INFO
  • Level::Warn => G_LOG_LEVEL_WARNING

The G_LOG_LEVEL_ERROR (as produced via g_error() macro in C) is not mapped to any of log::Level enum values. The reason is that g_error() is fatal, while log::error!() is not.

The formatting is done fully in Rust. However, log filtering based on level is done in Glib. It is advisable to set G_MESSAGES_DEBUG=all environment variable.

Using Glib a domain can be set per file by using #define G_LOG_DOMAIN "my-domain" directly in C code. This functionality is not avaialble when using glib_logger, all logs are emitted with a NULL domain.

glib_logger's People

Contributors

bboozzoo avatar

Watchers

 avatar  avatar

Forkers

shutton xanathar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.