コード例 #1
0
ファイル: Logger.cs プロジェクト: g0dsCookie/FastDLCompressor
        public bool Write(string msg, Severity severity = Severity.INFO)
        {
            if (severity < Severity)
            {
                return(false);
            }

            var sb = new StringBuilder();

            sb.AppendFormat("[{0}] [{1}] ", DateTime.Now.ToLongTimeString(), severity.GetName());
            sb.AppendLine(msg);

            var currentColor = SetColor(severity.GetColor());

            _mux.WaitOne();

            var tasks = new List <Task>();

            if (_file != null)
            {
                tasks.Add(_file.WriteAsync(sb.ToString()));
            }

            if (severity == Severity.ERROR)
            {
                tasks.Add(_stderr.WriteAsync(sb.ToString()));
            }
            else
            {
                tasks.Add(_stdout.WriteAsync(sb.ToString()));
            }

            foreach (var t in tasks)
            {
                t.Wait();
            }

            _mux.ReleaseMutex();

            SetColor(currentColor);

            return(true);
        }