summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/LowLevelConsole.cs
blob: 29e69185ac6a047f0dcb7ac2fa5d601656fd3b4c (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Text;
using System.Security;
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;

namespace System
{
    //
    // Simple limited console class for internal printf-style debugging in System.Private.CoreLib
    // and low-level tests that want to call System.Private.CoreLib directly
    //

    public static class LowLevelConsole
    {
        private static readonly SafeFileHandle _outputHandle =
            new SafeFileHandle(Win32Native.GetStdHandle(Win32Native.STD_OUTPUT_HANDLE), false);

        public static unsafe void Write(string s)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(s);

            fixed (byte * pBytes = bytes)
            {
                int bytesWritten;
                Win32Native.WriteFile(_outputHandle, pBytes, bytes.Length, out bytesWritten, IntPtr.Zero);
            }
        }

        public static void WriteLine(string s)
        {
            Write(s + Environment.NewLine);
        }

        public static void WriteLine()
        {
            Write(Environment.NewLine);
        }
     }

     //
     // Internal wrapper with the regular name for convenience. Note that it cannot be public to avoid colliding 
     // with the full Console type.
     //
     internal static class Console
     {
        public static void Write(string s)
        {
            LowLevelConsole.Write(s);
        }

        public static void WriteLine(string s)
        {
            LowLevelConsole.WriteLine(s);
        }

        public static void WriteLine()
        {
            LowLevelConsole.WriteLine();
        }
    }
}