neo-project / neo

NEO Smart Economy
MIT License
3.47k stars 1.03k forks source link

node: Create a locker for multiple Console.Write #2995

Open shargon opened 11 months ago

shargon commented 11 months ago

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 ... }

cschuchardt88 commented 11 months ago

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 commented 11 months ago

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

cschuchardt88 commented 11 months ago

this can be closed. has one built-in

Jim8y commented 11 months ago

@shargon

shargon commented 11 months ago

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

Where is syncRoot?

cschuchardt88 commented 11 months ago

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

shargon commented 11 months ago

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

cschuchardt88 commented 11 months ago

Its a partial class

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

shargon commented 11 months ago

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

cschuchardt88 commented 11 months ago

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

cschuchardt88 commented 1 month ago

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