aboutsummaryrefslogtreecommitdiff
path: root/cmnext-cli/src/ConsoleLogProvider.cs
blob: b38491c7d96dad26a97ffafb716e4f491cdbb3e6 (plain)
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
/*
* Copyright (c) 2024 Vaughn Nugent
* 
* Package: CMNext.Cli
* File: Program.cs 
*
* CMNext.Cli is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* CMNext.Cli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with CMNext.Cli. If not, see http://www.gnu.org/licenses/.
*/


using System;
using VNLib.Utils.Logging;
using Typin.Console;

namespace CMNext.Cli
{
    public sealed class ConsoleLogProvider(IConsole console) : ILogProvider
    {
        private LogLevel _level = LogLevel.Information;

        ///<inheritdoc/>
        public void Flush() => console.Output.Flush();

        ///<inheritdoc/>
        public object GetLogProvider() => console.Output;

        ///<inheritdoc/>
        public bool IsEnabled(LogLevel level) => level >= _level;

     
        public void SetLogLevel(LogLevel level) => _level = level;
        public void SetVerbose(bool verbose) => SetLogLevel(verbose ? LogLevel.Verbose : LogLevel.Information);
        public void SetDebug(bool debug) => SetLogLevel(debug ? LogLevel.Debug : LogLevel.Information);

        ///<inheritdoc/>
        public void Write(LogLevel level, string value)
        {
            if (!IsEnabled(level)) return;

            console.Output.WriteLine("[{0}]: {1}", level, value);
        }

        ///<inheritdoc/>
        public void Write(LogLevel level, Exception exception, string value = "")
        {
            if (!IsEnabled(level)) return;

            console.Output.WriteLine("[{0}]: {1}", level, $"{value}\n{exception}");
        }

        ///<inheritdoc/>
        public void Write(LogLevel level, string value, params object?[] args)
        {
            if (!IsEnabled(level)) return;

            console.Output.WriteLine("[{0}]: {1}", level, string.Format(value, args));
        }

        ///<inheritdoc/>
        public void Write(LogLevel level, string value, params ValueType[] args)
        {
            if (!IsEnabled(level)) return;

            console.Output.WriteLine("[{0}]: {1}", level, string.Format(value, args));
        }
    }
}