1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! Logging methods for the output
use std::io::Write;
use std::{env, io};

use crate::util::EOL;

fn issue_command(command: &str, msg: &str) {
    let message = format!("::{} {}", command, msg);
    io::stdout()
        .write_all((message + EOL).as_bytes())
        .expect("Failed to write message")
}

/// Prints a debug message to the log.
///
/// Only visible if [debug logging is enabled](https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging)
///
/// ```rust
/// use actions_github::logger;
/// logger::debug_log("Initializing the project");
/// ```
pub fn debug_log(msg: &str) {
    let message = format!("::debug::{}", msg);
    io::stdout()
        .write_all((message + EOL).as_bytes())
        .expect("Failed to write debug message")
}

/// Logs regular information message
///
/// ```rust
/// use actions_github::logger;
/// logger::info(format!("Finished analyzing {}", "project").as_str());
/// ```
pub fn info(msg: &str) {
    io::stdout()
        .write_all((msg.to_owned() + EOL).as_bytes())
        .expect("Failed to write debug message")
}

/// Creates a warning message and prints the message to the log.
///
/// This message will create an annotation.
///
/// ```rust
/// use actions_github::logger;
/// logger::warn_log("Missing name of project");
/// ```
pub fn warn_log(msg: &str) {
    issue_command("warning", msg);
}

/// Creates an error message and prints the message to the log.
///
/// This message will create an annotation.
///
/// ```rust
/// use actions_github::logger;
/// logger::error_log("Did not find library");
/// ```
pub fn error_log(msg: &str) {
    issue_command("error", msg);
}

/// Creates a notice message and prints the message to the log.
///
/// This message will create an annotation.
///
/// ```rust
/// use actions_github::logger;
/// logger::notice_log("Step one is finished");
/// ```
pub fn notice_log(msg: &str) {
    issue_command("notice", msg);
}

/// Returns if it's running on a debug runner.
///
/// If the `RUNNER_DEBUG` variable is not defined, it'll always return true
///
/// ```rust
/// use actions_github::logger;
/// assert!(logger::is_debug());
/// ```
pub fn is_debug() -> bool {
    match env::var("RUNNER_DEBUG") {
        Ok(value) => value == "1",
        Err(_) => true,
    }
}