Esempio n. 1
0
        public static void Initiate()
        {
            if (!initiated)
            {
                logging = new LoggingSection("Services");

                CommandHandler = new CommandHandler();
                Scanners       = new Scanners();

                OCRProvider = new ConcurrentResourceProvider <TesseractEngine>();
                TesseractEngine[] tesseractEngine = new TesseractEngine[Environment.ProcessorCount];
                for (int i = 0; i < tesseractEngine.Length; i++)
                {
                    tesseractEngine[i] = new TesseractEngine(@"./Tesseract/tessdata", "deu");
                }
                OCRProvider.Setup(tesseractEngine);

                servicesToBeDisposed = new List <IDisposable>()
                {
                    CommandHandler,
                    OCRProvider
                };

                initiated = true;
                logging.Info("Services initiation complete");
            }
        }
Esempio n. 2
0
        public void LoggingSection_CorrectNameShouldBeSetFromObjectWithGenericType()
        {
            //Arrange
            List <object>  list           = new List <object>();
            LoggingSection loggingSection = new LoggingSection(list);

            //Act
            loggingSection.Info("Test");

            //Assert
            Assert.Equal("List`1<Object>", loggingSection.Name);
        }
Esempio n. 3
0
        public void LogMessages_ShouldHaveOneEntryAfterLogging()
        {
            //Arrange
            LoggingSection loggingSection = new LoggingSection(this);

            //Act
            loggingSection.Info("Test");

            //Assert
            Assert.NotNull(loggingSection.GetLogMessages());
            Assert.True(loggingSection.GetLogMessages().Count == 1);
        }
Esempio n. 4
0
        public void Log_ReturnedLogMessage_ShouldHaveLogLevelInfo()
        {
            //Arrange
            LoggingSection loggingSection = new LoggingSection(this);
            LogMessage     logMessage;

            //Act
            logMessage = loggingSection.Info("Test");

            //Assert
            Assert.NotNull(logMessage);
            Assert.Equal(LogLevel.INFO, logMessage.LogLevel);
        }
Esempio n. 5
0
        public void LogHandler_ShouldNotTriggerIfLogLevelDisabled()
        {
            //Arrange
            TestLogHandler testLogHandler = new TestLogHandler(new LogLevel[0]);

            Logger.LogHandlers += testLogHandler.OnLog;
            LoggingSection loggingSection = new LoggingSection(this);

            //Act
            loggingSection.Info("Test");

            //Assert
            Assert.False(testLogHandler.Triggered);
        }
Esempio n. 6
0
        public void LogHandler_ShouldTriggerIfLogLevelEnabled()
        {
            //Arrange
            TestLogHandler testLogHandler = new TestLogHandler(new LogLevel[] { LogLevel.INFO });

            Logger.LogHandlers += testLogHandler.OnLog;
            LoggingSection loggingSection = new LoggingSection(this);

            //Act
            loggingSection.Info("Test");

            //Assert
            Assert.True(testLogHandler.Triggered);
        }
Esempio n. 7
0
        internal static void Dispose()
        {
            if (initiated)
            {
                logging.Info("Disposing Services");

                for (int i = servicesToBeDisposed.Count - 1; i >= 0; i--)
                {
                    Disable(servicesToBeDisposed[i]);
                }

                logging.Dispose();
                initiated = false;
            }
        }
Esempio n. 8
0
 public Scanners()
 {
     logging  = new LoggingSection(this);
     scanners = new List <Scanner>();
     logging.Info("Initializing scanners");
     foreach (Type type in AppDomain.CurrentDomain.GetAssemblies()
              .SelectMany(s => s.GetTypes())
              .Where(p => !p.IsInterface && p != typeof(Scanner) && typeof(Scanner).IsAssignableFrom(p))
              .Where(t => t.GetConstructors().All(c => c.GetParameters().Length == 0)))
     {
         Scanner scanner = (Scanner)Activator.CreateInstance(type);
         logging.Debug($"Created instance of {type.Name}");
         scanners.Add(scanner);
     }
 }
Esempio n. 9
0
        public void Log_ShouldTriggerLogHandlers()
        {
            //Arrange
            bool hasLogged = false;
            EventHandler <LogEventArgs> logHandler = (source, args) =>
            {
                hasLogged = true;
            };
            LoggingSection loggingSection = new LoggingSection(this);

            //Act
            Logger.LogHandlers += logHandler;
            loggingSection.Info("Test");

            //Assert
            Assert.True(hasLogged);
        }
Esempio n. 10
0
        public CommandHandler()
        {
            logging    = new LoggingSection(this);
            commands   = new List <ICommand>();
            hasControl = false;

            logging.Info("Initializing commands");
            foreach (Type type in AppDomain.CurrentDomain.GetAssemblies()
                     .SelectMany(s => s.GetTypes())
                     .Where(p => !p.IsInterface && typeof(ICommand).IsAssignableFrom(p))
                     .Where(t => t.GetConstructors().All(c => c.GetParameters().Length == 0)))
            {
                ICommand cmd = (ICommand)Activator.CreateInstance(type);
                logging.Debug($"Initialized command {cmd.Name}");
                commands.Add(cmd);
            }
            logging.Debug($"Initialized {commands.Count} commands");
        }
Esempio n. 11
0
        public void LogMessage_ShouldHaveCorrectTimestamp()
        {
            //Arrange
            LoggingSection loggingSection = new LoggingSection(this);
            LogMessage     logMessage;
            DateTime       now;

            //Act
            logMessage = loggingSection.Info("Test");
            now        = DateTime.Now;

            //Assert
            Assert.NotNull(logMessage);
            //TODO: Remove out-commented code if below solution proves to work consistently
            //Assert.Equal(now.Minute, logMessage.Timestamp.Minute);
            //Assert.Equal(now.Hour, logMessage.Timestamp.Hour);
            //Assert.Equal(now.Day, logMessage.Timestamp.Day);
            //Assert.Equal(now.Month, logMessage.Timestamp.Month);
            //Assert.Equal(now.Year, logMessage.Timestamp.Year);
            Assert.True(now.Ticks / TimeSpan.TicksPerMillisecond - logMessage.Timestamp.Ticks / TimeSpan.TicksPerMillisecond < 1000);
        }
Esempio n. 12
0
        public void HandleInput(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return;
            }

            input = input.Trim();

            string[] split       = input.Split(' ');
            string   commandName = split[0];

            ICommand cmd = commands.FirstOrDefault(c => c.Name.ToLower() == commandName.ToLower() || c.Alias.Any(a => a.ToLower() == commandName.ToLower()));

            if (cmd != null)
            {
                string[] args = new string[split.Length - 1];
                for (int i = 1; i < split.Length; i++)
                {
                    args[i - 1] = split[i];
                }

                try
                {
                    cmd.OnTrigger(args);
                }
                catch (Exception e)
                {
                    logging.Error($"" +
                                  $"Unhandled exception while running command {cmd.Name}: {e.Message}" +
                                  $"\nSource: {(e.Source != null ? e.Source : "Unknown")}" +
                                  $"\nStackTrace: {e.StackTrace}");
                    throw;
                }
            }
            else
            {
                logging.Info($"No command found for \"{commandName}\".");
            }
        }