Ejemplo 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");
            }
        }
Ejemplo 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);
        }
Ejemplo 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);
        }
Ejemplo 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);
        }
Ejemplo 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);
        }
Ejemplo 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);
        }
Ejemplo 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;
            }
        }
Ejemplo 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);
     }
 }
Ejemplo 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);
        }
Ejemplo 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");
        }
Ejemplo 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);
        }
Ejemplo 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}\".");
            }
        }