From ab4614316b615e5b19f2a435a3cca53016583d22 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Michal=20Mal=C3=BD?= <madcatxster@devoid-pointer.net>
Date: Sun, 21 Dec 2014 03:27:38 +0100
Subject: [PATCH] - Add logging system - Gracefully exit on SIGINT

---
 src/formatting_helpers.adb |  2 --
 src/global_types.ads       |  2 +-
 src/launcher.adb           | 46 ++++++++++++++++++++++++++++++++++++++
 src/launcher.ads           | 23 +++++++++++++++++++
 src/nine_q.adb             | 18 ++-------------
 5 files changed, 72 insertions(+), 19 deletions(-)
 create mode 100644 src/launcher.adb
 create mode 100644 src/launcher.ads

diff --git a/src/formatting_helpers.adb b/src/formatting_helpers.adb
index 76c8074..182619d 100644
--- a/src/formatting_helpers.adb
+++ b/src/formatting_helpers.adb
@@ -1,6 +1,5 @@
 with Ada.Numerics.Generic_Elementary_Functions;
 with Ada.Strings.Fixed;
-with Ada.Strings.Unbounded;
 --with Ada.Text_IO;
 
 package body Formatting_Helpers is
@@ -100,7 +99,6 @@ package body Formatting_Helpers is
 
   procedure Prepend_Zeros_To_Text(Num: in FH_Float; Decimals: in FH_Float; Text: in out UB_Text) is
     package FHEF is new Ada.Numerics.Generic_Elementary_Functions(FH_Float);
-    use Ada.Strings.Unbounded;
     use FHEF;
 
     Log_Dec: constant FH_Float := Log(Base => 10.0, X => Decimals);
diff --git a/src/global_types.ads b/src/global_types.ads
index d80f381..67e93e8 100644
--- a/src/global_types.ads
+++ b/src/global_types.ads
@@ -4,7 +4,7 @@ with Ada.Strings.Unbounded;
 package Global_Types is
 
   type Problem_ID is new Ada.Containers.Count_Type;
-  type RetCode is (OK, E_NOTFOUND, E_UNKW, E_INVAL, E_NOMEM, E_NULLPTR); 
+  type RetCode is (OK, E_NOTFOUND, E_UNKW, E_INVAL, E_NOMEM, E_NULLPTR, E_FAIL); 
   type Unique_ID is new Ada.Containers.Count_Type;
   subtype HTML_Code is Ada.Strings.Unbounded.Unbounded_String;
   subtype UB_Text is Ada.Strings.Unbounded.Unbounded_String;
diff --git a/src/launcher.adb b/src/launcher.adb
new file mode 100644
index 0000000..61be8a7
--- /dev/null
+++ b/src/launcher.adb
@@ -0,0 +1,46 @@
+with Ada.Text_IO;
+with AWS.Config.Ini;
+
+with Global_Types;
+with Handlers;
+with Logging_System;
+
+use Global_Types;
+
+package body Launcher is
+
+  procedure Launch is
+    RC: RetCode;
+  begin
+    RC := Logging_System.Initialize;
+    if RC /= OK then
+      Ada.Text_IO.Put_Line("Nine-Q will not start without a log file");
+      return;
+    end if;
+
+    AWS.Config.Ini.Read(Server_Config, "nine_q_config.ini");
+
+    AWS.Server.Start(Web_Server => Web_Server,
+		     Dispatcher => Handlers.Get_Dispatchers,
+		     Config => Server_Config);
+
+    Ada.Text_IO.Put_Line("Nine-Q server started");
+    Logging_System.Log("Nine-Q server started", Logging_System.DEBUG);
+
+    AWS.Server.Wait(AWS.Server.No_Server);
+    Logging_System.Log("Nine-Q server shut down", Logging_System.DEBUG);
+    Ada.Text_IO.Put_Line("Nine-Q server shut down");
+
+    Logging_System.Close;
+  end Launch;
+
+  protected body Signal_Handlers is
+
+    procedure Shutdown_On_Signal is
+    begin
+      AWS.Server.Shutdown(Web_Server);
+    end Shutdown_On_Signal;
+
+  end Signal_Handlers;
+
+end Launcher;
diff --git a/src/launcher.ads b/src/launcher.ads
new file mode 100644
index 0000000..896f5e0
--- /dev/null
+++ b/src/launcher.ads
@@ -0,0 +1,23 @@
+with Ada.Interrupts;
+with Ada.Interrupts.Names;
+with AWS.Config;
+with AWS.Server;
+
+package Launcher is
+  pragma Unreserve_All_Interrupts;
+
+  procedure Launch;
+
+private
+  Server_Config: AWS.Config.Object;
+  Web_Server: AWS.Server.HTTP;
+
+  protected Signal_Handlers is
+    procedure Shutdown_On_Signal;
+
+    pragma Attach_Handler(Shutdown_On_Signal, Ada.Interrupts.Names.SIGINT);
+  end Signal_Handlers;
+
+end Launcher;
+
+
diff --git a/src/nine_q.adb b/src/nine_q.adb
index 004df67..37e070e 100644
--- a/src/nine_q.adb
+++ b/src/nine_q.adb
@@ -1,20 +1,6 @@
-with AWS.Config;
-with AWS.Config.Ini;
-with AWS.Server;
-
-with Handlers;
+with Launcher;
 
 procedure Nine_Q is
-  Server_Config: AWS.Config.Object;
-  Web_Server: AWS.Server.HTTP;
 begin
-  AWS.Config.Ini.Read(Server_Config, "nine_q_config.ini");
-
-  AWS.Server.Start(Web_Server => Web_Server,
-		   Dispatcher => Handlers.Get_Dispatchers,
-		   Config => Server_Config);
-
-  AWS.Server.Wait(AWS.Server.Forever);
-  AWS.Server.Shutdown(Web_Server);
-
+  Launcher.Launch;
 end Nine_Q;
-- 
2.43.5