isla_lib/
log.rs

1// BSD 2-Clause License
2//
3// Copyright (c) 2019, 2020 Alasdair Armstrong
4//
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// 2. Redistributions in binary form must reproduce the above copyright
15// notice, this list of conditions and the following disclaimer in the
16// documentation and/or other materials provided with the distribution.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30use std::sync::atomic::{AtomicU32, Ordering::*};
31
32pub static FLAGS: AtomicU32 = AtomicU32::new(0);
33
34pub fn color(tid: usize) -> &'static str {
35    match tid % 14 {
36        0 => "\x1b[91m",
37        1 => "\x1b[92m",
38        2 => "\x1b[93m",
39        3 => "\x1b[94m",
40        4 => "\x1b[95m",
41        5 => "\x1b[96m",
42        6 => "\x1b[97m",
43        7 => "\x1b[91m\x1b[4m",
44        8 => "\x1b[92m\x1b[4m",
45        9 => "\x1b[93m\x1b[4m",
46        10 => "\x1b[94m\x1b[4m",
47        11 => "\x1b[95m\x1b[4m",
48        12 => "\x1b[96m\x1b[4m",
49        13 => "\x1b[97m\x1b[4m",
50        _ => unreachable!(),
51    }
52}
53
54pub const VERBOSE: u32 = 1u32;
55pub const MEMORY: u32 = 2u32;
56pub const FORK: u32 = 4u32;
57pub const LITMUS: u32 = 8u32;
58pub const PROBE: u32 = 16u32;
59pub const CACHE: u32 = 32u32;
60
61pub fn set_flags(flags: u32) {
62    FLAGS.store(flags, SeqCst);
63}
64
65#[macro_export]
66macro_rules! log {
67    ($flags: expr, $msg: expr) => {
68        if log::FLAGS.load(std::sync::atomic::Ordering::Relaxed) & $flags > 0u32 {
69            eprintln!("[log]: {}", $msg)
70        }
71    };
72}
73
74#[macro_export]
75macro_rules! log_from {
76    ($tid: expr, $flags: expr, $msg: expr) => {
77        if log::FLAGS.load(std::sync::atomic::Ordering::Relaxed) & $flags > 0u32 {
78            eprintln!("[{}{:<3}\x1b[0m]: {}", log::color($tid), $tid, $msg)
79        }
80    };
81}
82
83#[macro_export]
84macro_rules! if_logging {
85    ($flags: expr, $body:block) => {
86        if log::FLAGS.load(std::sync::atomic::Ordering::Relaxed) & $flags > 0u32 $body
87    };
88}