neo icon indicating copy to clipboard operation
neo copied to clipboard

node: Create a locker for multiple Console.Write

Open shargon opened this issue 2 years ago • 11 comments

Currently we can receive from plugins some outputs that can be mixed with the entered command. We should be able to send these results together.

Related to https://github.com/neo-project/neo-node/pull/905#discussion_r1392224809

We need somthing like:

 public static class ConsoleHelper
    {
        public static readonly object Locker = new();
...
        public static void Info(params string[] values)
        {
            lock (Locker)
            {
                var currentColor = new ConsoleColorSet();

                for (int i = 0; i < values.Length; i++)
                {
                    if (i % 2 == 0)
                        InfoColor.Apply();
                    else
                        currentColor.Apply();
                    Console.Write(values[i]);
                }
                currentColor.Apply();
                Console.WriteLine();
            }
        }

In order to allow lock(ConsoleHelper.Lock){ ... multiple out ... }

shargon avatar Nov 15 '23 08:11 shargon

Wouldn't it be better to do this on neo-core

namespace Neo
{
    public delegate void LogEventHandler(string source, LogLevel level, object message);

    public static class Utility
    {
        internal class Logger : ReceiveActor
        {
            public Logger()
            {
                Receive<InitializeLogger>(_ => Sender.Tell(new LoggerInitialized()));
                Receive<LogEvent>(e => Log(e.LogSource, (LogLevel)e.LogLevel(), e.Message));
            }
        }

        public static event LogEventHandler Logging;

        public static Encoding StrictUTF8 { get; }

        static Utility()
        {
            StrictUTF8 = (Encoding)Encoding.UTF8.Clone();
            StrictUTF8.DecoderFallback = DecoderFallback.ExceptionFallback;
            StrictUTF8.EncoderFallback = EncoderFallback.ExceptionFallback;
        }

        public static object ConsoleLock { get; } = new(); // Add this line
        public static void Log(string source, LogLevel level, object message)
        {
            lock (ConsoleLock) // Add this line
                Logging?.Invoke(source, level, message);
        }
    }
}

Because the plugin you were talking about uses, Utility.Log

cschuchardt88 avatar Nov 15 '23 09:11 cschuchardt88

I did find this in neo-cli; syncRoot its for locking so you can sync root of console.

cschuchardt88 avatar Nov 15 '23 10:11 cschuchardt88

this can be closed. has one built-in

cschuchardt88 avatar Nov 23 '23 19:11 cschuchardt88

@shargon

Jim8y avatar Nov 24 '23 02:11 Jim8y

I did find this in neo-cli; syncRoot its for locking so you can sync root of console.

Where is syncRoot?

shargon avatar Nov 24 '23 11:11 shargon

It is in neo-cli https://github.com/neo-project/neo-node/blob/da786195cfb6d70236bb37611c37d7016bbc3aae/neo-cli/CLI/MainService.Logger.cs#L29

cschuchardt88 avatar Nov 24 '23 11:11 cschuchardt88

But is private, how can be used in https://github.com/neo-project/neo-node/pull/905 ?

shargon avatar Nov 24 '23 11:11 shargon

Its a partial class

https://github.com/neo-project/neo-node/blob/da786195cfb6d70236bb37611c37d7016bbc3aae/neo-cli/CLI/MainService.cs#L41

cschuchardt88 avatar Nov 24 '23 11:11 cschuchardt88

But we also use Console outside this repo, in plugins, we should have access to it

shargon avatar Nov 24 '23 11:11 shargon

We can make it public and do something like https://github.com/neo-project/neo/issues/2995

cschuchardt88 avatar Nov 24 '23 12:11 cschuchardt88

problem solved? with https://github.com/neo-project/neo/issues/2995#issuecomment-1844875392

cschuchardt88 avatar Sep 05 '24 18:09 cschuchardt88