~nerosnm/oslog

0e3d740f4b1730db51f26624a578323091660a7c — Søren Mortensen 3 years ago b83860e
Wrapper library allows actually calling into os_log

Signed-off-by: Søren Mortensen <soren@sorenmortensen.com>
11 files changed, 273 insertions(+), 7 deletions(-)

R .gitignore => os_log/.gitignore
R Cargo.toml => os_log/Cargo.toml
A os_log/build.rs
A os_log/include/os_log.h
A os_log/src/lib.rs
A os_log/src/os_log.rs
A os_log_wrapper/.gitignore
A os_log_wrapper/Makefile
A os_log_wrapper/include/os_log_wrapper.h
A os_log_wrapper/src/os_log_wrapper.c
D src/lib.rs
R .gitignore => os_log/.gitignore +0 -0
R Cargo.toml => os_log/Cargo.toml +4 -0
@@ 2,6 2,10 @@
name = "os_log"
version = "0.1.0"
authors = ["Søren Mortensen <soren@sorenmortensen.com>"]
build = "build.rs"

[dependencies]
log = { version = "0.4", features = ["std"] }

[build-dependencies]
bindgen = "0.37"

A os_log/build.rs => os_log/build.rs +39 -0
@@ 0,0 1,39 @@
//
//  build.rs
//  os_log
//
//  Created by Søren Mortensen on 28/07/2018.
//  Copyright © 2018 Søren Mortensen. All rights reserved.
//

extern crate bindgen;

use std::env;
use std::path::PathBuf;
use std::process::Command;

fn main() {
    let wrapper_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/../os_log_wrapper");

    Command::new("make")
        .current_dir(PathBuf::from(wrapper_dir))
        .status().unwrap();
    
    println!("cargo:rustc-link-search=native=../os_log_wrapper/out/");
    println!("cargo:rustc-link-lib=static=oslogwrapper");
    
    let bindings = bindgen::Builder::default()
        // The input header we would like to generate
        // bindings for.
        .header("include/os_log.h")
        // Finish the builder and generate the bindings.
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("os_log.rs"))
        .expect("Couldn't write bindings!");
}

A os_log/include/os_log.h => os_log/include/os_log.h +9 -0
@@ 0,0 1,9 @@
//
//  os_log.h
//  os_log
//
//  Created by Søren Mortensen on 28/07/2018.
//  Copyright © 2018 Søren Mortensen. All rights reserved.
//

#include "../../os_log_wrapper/include/os_log_wrapper.h"

A os_log/src/lib.rs => os_log/src/lib.rs +63 -0
@@ 0,0 1,63 @@
//
//  lib.rs
//  os_log
//
//  Created by Søren Mortensen on 28/07/2018.
//  Copyright © 2018 Søren Mortensen. All rights reserved.
//

extern crate log;

pub mod os_log;

use log::{Record, Level, Log, Metadata, SetLoggerError};

use std::ffi::CString;

use os_log::*;

struct OsLog {
    level: Level,
}

#[repr(u32)]
enum OsLogType {
    Default = OS_LOG_TYPE_DEFAULT,
    Info = OS_LOG_TYPE_INFO,
    Debug = OS_LOG_TYPE_DEBUG,
    Error = OS_LOG_TYPE_ERROR,
    Fault = OS_LOG_TYPE_FAULT,
}

impl Log for OsLog {
    fn enabled(&self, metadata: &Metadata) -> bool {
        metadata.level() <= self.level
    }

    fn log(&self, record: &Record) {
        if self.enabled(record.metadata()) {
            let string = format!("{}", record.args());
            let c_string = CString::new(string).unwrap();
            unsafe { _os_log_fault(c_string.as_ptr()); }
        }
    }

    fn flush(&self) {}
}

pub fn init_with_level(level: Level) -> Result<(), SetLoggerError> {
    log::set_boxed_logger(Box::new(OsLog { level }))
        .map(|()| log::set_max_level(level.to_level_filter()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_thing() {
        let string = format!("{}", "This is a test log from os_log");
        let c_string = CString::new(string).unwrap();
        unsafe { _os_log_fault(c_string.as_ptr()); }
    }
}

A os_log/src/os_log.rs => os_log/src/os_log.rs +14 -0
@@ 0,0 1,14 @@
//
//  os_log.rs
//  os_log
//
//  Created by Søren Mortensen on 28/07/2018.
//  Copyright © 2018 Søren Mortensen. All rights reserved.
//

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(safe_packed_borrows)]

include!(concat!(env!("OUT_DIR"), "/os_log.rs"));

A os_log_wrapper/.gitignore => os_log_wrapper/.gitignore +59 -0
@@ 0,0 1,59 @@

# Created by https://www.gitignore.io/api/c

### C ###
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf


# End of https://www.gitignore.io/api/c

A os_log_wrapper/Makefile => os_log_wrapper/Makefile +36 -0
@@ 0,0 1,36 @@
CC=clang
CFLAGS=-I$(IDIR)

IDIR=include

_DEPS = os_log_wrapper.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

ODIR=obj

_OBJ = os_log_wrapper.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

SDIR=src

_SRC = os_log_wrapper.c
SRC = $(patsubst %,$(SDIR)/%,$(_SRC))

OUTDIR=out

$(ODIR)/%.o: $(SDIR)/%.c $(DEPS)
	@echo "[Compile]" $<
	@$(CC) -c -o $@ $< $(CFLAGS)

$(OUTDIR)/liboslogwrapper.a: $(OBJ)
	@echo "[Link (Static)]" $<
	@mkdir -p $(OUTDIR)
	@ar rcs $@ $^

.PHONY: clean

clean:
	rm -f $(ODIR)/*.o *~ $(INCDIR)/*~
	rm -rf out

all: liboslogwrapper.a

A os_log_wrapper/include/os_log_wrapper.h => os_log_wrapper/include/os_log_wrapper.h +20 -0
@@ 0,0 1,20 @@
//
//  os_log_wrapper.h
//  os_log_wrapper
//
//  Created by Søren Mortensen on 29/07/2018.
//  Copyright © 2018 Søren Mortensen. All rights reserved.
//

#ifndef oslogwrapper_h
#define oslogwrapper_h

#include <os/log.h>

void _os_log(const char *str);
void _os_log_info(const char *str);
void _os_log_debug(const char *str);
void _os_log_error(const char *str);
void _os_log_fault(const char *str);

#endif /* oslogwrapper_h */

A os_log_wrapper/src/os_log_wrapper.c => os_log_wrapper/src/os_log_wrapper.c +29 -0
@@ 0,0 1,29 @@
//
//  os_log_wrapper.c
//  os_log_wrapper
//
//  Created by Søren Mortensen on 29/07/2018.
//  Copyright © 2018 Søren Mortensen. All rights reserved.
//

#include "os_log_wrapper.h"

void _os_log(const char *str) {
    os_log(OS_LOG_DEFAULT, "%{public}s", str);
}

void _os_log_info(const char *str) {
    os_log_info(OS_LOG_DEFAULT, "%{public}s", str);
}

void _os_log_debug(const char *str) {
    os_log_debug(OS_LOG_DEFAULT, "%{public}s", str);
}

void _os_log_error(const char *str) {
    os_log_error(OS_LOG_DEFAULT, "%{public}s", str);
}

void _os_log_fault(const char *str) {
    os_log_fault(OS_LOG_DEFAULT, "%{public}s", str);
}

D src/lib.rs => src/lib.rs +0 -7
@@ 1,7 0,0 @@
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}