示例#1
0
        public void LoggerConsole_checkMessageTypeColor_WarningYellow()
        {
            LoggerConsole lc = new LoggerConsole();

            lc.Log("Warning", "simple message");

            Assert.IsTrue(lc.ColorResult == ConsoleColor.Yellow);
        }
示例#2
0
        public static void Run()
        {
            // Bridge Pattern allows independent variation between abstraction (logger) and implementation (formatting functionality)

            var standardFormatter = new FormatterStandard();
            var allCapsFormatter  = new FormatterAllCaps();

            var consoleLoggerInStandardFormat = new LoggerConsole(standardFormatter);
            var consoleLoggerInAllCaps        = new LoggerConsole(allCapsFormatter);

            var fileLoggerInStandardFormat = new LoggerFile(standardFormatter);
            var fileLoggerInAllCaps        = new LoggerFile(allCapsFormatter);

            const string title   = "The Title";
            const string message = "This is the message.";

            consoleLoggerInStandardFormat.Log(title, message);
            consoleLoggerInAllCaps.Log(title, message);
            fileLoggerInStandardFormat.Log(title, message);
            fileLoggerInAllCaps.Log(title, message);
        }
示例#3
0
        public bool SendFile(string path, string directory, SendFunction send)
        {
            if (!File.Exists(path))
            {
                return(false);
            }

            FileStream fileStream;

            try { fileStream = File.Open(path, FileMode.Open, FileAccess.Read); }
            catch
            {
                Logger.Log(LogLevel.Error, "Could not open file: \"{0}\"", path);
                return(false);
            }

            string destinationPath;

            if (directory.Length <= 0)
            {
                destinationPath = Path.GetFileName(path);
            }
            else
            {
                destinationPath = path.Replace(directory + "\\", "");
            }

            var fileSize             = fileStream.Length;
            int requiredSizeFilename = destinationPath.Length + sizeof(int);

            while (fileStream.Position != fileStream.Length)
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var writer = new BinaryWriter(memoryStream))
                    {
                        const int fileSizeLength  = 8;
                        const int boolFlagLenth   = 1;
                        const int chunkSizeLength = 4;

                        long leftover     = fileStream.Length - fileStream.Position;
                        int  maxChunkSize = Constants.MaxPacketSize - (requiredSizeFilename + fileSizeLength + (2 * boolFlagLenth) + chunkSizeLength);

                        if (maxChunkSize <= 0)
                        {
                            Logger.Log(LogLevel.Error, "Filename \"{0}\" size exceeding packet limit!", path);
                            return(false);
                        }

                        bool createNew = fileStream.Position == 0;
                        int  chunkSize;

                        if (leftover > int.MaxValue)
                        {
                            chunkSize = maxChunkSize;
                        }
                        else
                        {
                            chunkSize = Math.Min((int)leftover, maxChunkSize);
                        }

                        bool lastChunk = chunkSize == leftover;

                        byte[] chunk = new byte[chunkSize];

                        fileStream.Read(chunk, 0, chunk.Length);

                        writer.Write(destinationPath);
                        writer.Write(fileSize);
                        writer.Write(createNew);
                        writer.Write(lastChunk);
                        writer.Write(chunkSize);
                        writer.Write(chunk);

                        Logger.Log(LogLevel.Information, "Sending chunk with {0} byte of data", chunkSize);
                        if (!send(memoryStream.ToArray()))
                        {
                            fileStream.Dispose();
                            return(false);
                        }
                    }
                }
            }

            fileStream.Dispose();

            return(true);
        }
示例#4
0
        public bool ReceiveFile(byte[] packet, ref string filePath)
        {
            using (var stream = new MemoryStream(packet))
            {
                using (var reader = new BinaryReader(stream))
                {
                    while (stream.Position < stream.Length)
                    {
                        if (filePath == "")
                        {
                            filePath = reader.ReadString();
                        }
                        else if (filePath != reader.ReadString())
                        {
                            Logger.Log(LogLevel.Error, "Header data of file \"{0}\" corrupt!", filePath);
                            continue;
                        }

                        var  dataSize   = reader.ReadInt64();
                        bool createFile = reader.ReadBoolean();
                        bool lastChunk  = reader.ReadBoolean();
                        int  chunkSize  = reader.ReadInt32();

                        long currentSize;

                        if (createFile)
                        {
                            Logger.Log(LogLevel.Priority, "Receiving file \"{0}\"", filePath);
                        }

                        Logger.Log(LogLevel.Information, "Decoding chunk with {0} bytes.", chunkSize);

                        var chunk = new byte[chunkSize];
                        var read  = reader.Read(chunk, 0, chunkSize);

                        string directory = Path.GetDirectoryName(DirectoryPath + "\\" + filePath);

                        m_directoryLock.Execute(() =>
                        {
                            if (!Directory.Exists(directory))
                            {
                                Directory.CreateDirectory(directory);
                                MakeFolderWritable(directory);
                            }
                        });

                        try
                        {
                            using (var filestream = new FileStream(DirectoryPath + "\\" + filePath, createFile ? FileMode.Create : FileMode.Append))
                            {
                                filestream.Write(chunk, 0, chunk.Length);
                                currentSize = filestream.Length;
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(LogLevel.Error, "Could not write chunk: " + ex.Message);
                            continue;
                        }

                        if (lastChunk)
                        {
                            Logger.Log(LogLevel.Priority, "Receiving of file \"{0}\" complete.", filePath);
                            return(true);
                        }
                    }

                    return(false);
                }
            }
        }
        private static void LogGridPanel(GridPanel gridPanel)
        {
            ILogger logger = new LoggerConsole(new GridPanelMapper());

            logger.Log(gridPanel);
        }