Skip to content

Commit

Permalink
Introduce our own Logging package and use throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
SMerrony committed Mar 15, 2022
1 parent 2ed4070 commit b38eac0
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 226 deletions.
16 changes: 9 additions & 7 deletions Src/bdf_font.adb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Copyright (C) 2021 Steve Merrony
-- Copyright ©2021,2022 Steve Merrony

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
Expand All @@ -17,8 +17,10 @@
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.

with Ada.Text_IO; use Ada.Text_IO;
with Interfaces; use Interfaces;
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces; use Interfaces;

with Logging; use Logging;

package body BDF_Font is

Expand Down Expand Up @@ -98,14 +100,14 @@ package body BDF_Font is

loop
Get_Line (Font_File, Font_Line, Font_Line_Length);
-- Put_Line ("DEBUG: " & Font_Line (1 .. Font_Line_Length));
-- Log (DEBUG, "" & Font_Line (1 .. Font_Line_Length));
exit when Font_Line (1 .. Font_Line_Length) = "ENDPROPERTIES";
end loop;
Get_Line (Font_File, Font_Line, Font_Line_Length);
if Font_Line (1 .. 5) /= "CHARS" then
raise BDF_DECODE with "ERROR: BDF_Font - CHARS line not found";
end if;
Put_Line ("DEBUG: BDF Font " & Font_Line (1 .. Font_Line_Length));
Log (INFO, "BDF Font " & Font_Line (1 .. Font_Line_Length));

Char_Count := Positive'Value (Font_Line (7 .. Font_Line_Length));

Expand All @@ -121,7 +123,7 @@ package body BDF_Font is
Fill (Black_Pix_Buf, 16#000000ff#);

for CC in 0 .. Char_Count - 1 loop
-- Put_Line ("DEBUG: Loading char No. " & Integer'Image(CC));
-- Log (DEBUG, "Loading char No. " & Integer'Image(CC));

loop
Get_Line (Font_File, Font_Line, Font_Line_Length);
Expand All @@ -133,7 +135,7 @@ package body BDF_Font is
raise BDF_DECODE with "ERROR: BDF_Font - ENCODING line not found";
end if;
ASCII_Code := Natural'Value (Font_Line (10 .. Font_Line_Length));
-- Put_Line ("DEBUG: ... ASCII Code: " & ASCII_Code'Image);
-- Log (DEBUG, "... ASCII Code: " & ASCII_Code'Image);

-- skip 2 lines
Get_Line (Font_File, Font_Line, Font_Line_Length);
Expand Down
9 changes: 3 additions & 6 deletions Src/crt.adb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.

with Ada.Text_IO;

with Cairo; use Cairo;

with Gdk.Cairo;
with Gdk.Window;

with Display_P; use Display_P;
with Logging; use Logging;

package body Crt is

Expand All @@ -48,7 +47,7 @@ package body Crt is

procedure Init (Zoom : in BDF_Font.Zoom_T) is
begin
Ada.Text_IO.Put_Line ("DEBUG: Creating Crt");
Log (DEBUG, "Creating Crt");
BDF_Font.Load_Font (Font_Filename, Zoom);
Gtk.Drawing_Area.Gtk_New (Tube.DA);
Tube.DA.Set_Size_Request(BDF_Font.Decoded.Char_Width * Gint(Display.Get_Visible_Cols),
Expand Down Expand Up @@ -83,7 +82,7 @@ package body Crt is
is
pragma Unreferenced (Event);
begin
Ada.Text_IO.Put_Line ("DEBUG: Entering Configure_Event_CB");
Log (DEBUG, "Entering Configure_Event_CB");
if surface /= Cairo.Null_Surface then
Cairo.Surface_Destroy (surface);
end if;
Expand All @@ -109,7 +108,6 @@ package body Crt is
Blnk, Dm, Rv, Under, Prot : Boolean;
use Glib;
begin
-- Ada.Text_IO.Put_Line ("DEBUG: Draw_Crt called");
Cr := Cairo.Create (surface);

for Line in 0 .. Display.Get_Visible_Lines-1 loop
Expand Down Expand Up @@ -185,7 +183,6 @@ package body Crt is
is
pragma Unreferenced (Self);
begin
-- Ada.Text_IO.Put_Line ("DEBUG: Entering Draw_CB");
Draw_Crt;
Cairo.Set_Source_Surface (Cr, surface, 0.0, 0.0);
Cairo.Paint (Cr);
Expand Down
11 changes: 7 additions & 4 deletions Src/dashera.adb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ with Gtk.Main;
with Gtk.Window; use Gtk.Window;

with GUI;
with Logging; use Logging;

procedure Dashera is

Expand All @@ -41,7 +42,6 @@ procedure Dashera is
-- program args etc.
Arg_Ix : Natural := 1;
Host_Arg : Unbounded_String := Null_Unbounded_String;
Trace_Script : Boolean := false;
Trace_Xmodem : Boolean := false;

-- Builder : Gtkada.Builder.Gtkada_Builder;
Expand All @@ -51,6 +51,7 @@ procedure Dashera is
begin
Ada.Text_IO.Put_Line ("Usage of dashera:");
Ada.Text_IO.Put_Line (" -host <host:port> Host to connect with via Telnet");
Ada.Text_IO.Put_Line (" -debug Print debugging information on STDOUT");
Ada.Text_IO.Put_Line (" -tracescript Print trace of Mini-Expect script on STDOUT");
Ada.Text_IO.Put_Line (" -tracexmodem Show details of XMODEM file transfers on STDOUT");
Ada.Text_IO.Put_Line (" -version show the version number of dashera and exit");
Expand All @@ -65,8 +66,10 @@ begin
elsif Argument (Arg_Ix) = "-host" then
Host_Arg := To_Unbounded_String (Argument (Arg_Ix + 1));
Arg_Ix := Arg_Ix + 1;
elsif Argument (Arg_Ix) = "-debug" then
Set_Level (DEBUG);
elsif Argument (Arg_Ix) = "-tracescript" then
Trace_Script := true;
Set_Level (TRACE);
elsif Argument (Arg_Ix) = "-tracexmodem" then
Trace_Xmodem := true;
elsif Argument (Arg_Ix) = "-h" or Argument (Arg_Ix) = "-help" then
Expand All @@ -79,9 +82,9 @@ begin
Gdk.Threads.G_Init;
Gdk.Threads.Init;
Gtk.Main.Init;
Ada.Text_IO.Put_Line ( "DEBUG: Preparing to enter Main GTK event loop...");
Log (DEBUG, "Preparing to enter Main GTK event loop...");
Gdk.Threads.Enter;
Main_Window := Gui.Create_Window (Host_Arg, Trace_Script, Trace_Xmodem);
Main_Window := Gui.Create_Window (Host_Arg, Trace_Xmodem);
Main_Window.Show_All;
Gtk.Main.Main;
Gdk.Threads.Leave;
Expand Down
38 changes: 18 additions & 20 deletions Src/gui.adb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ with Gtkada.File_Selection;

with Text_IO.Unbounded_IO;

with BDF_Font; use BDF_Font;
with BDF_Font; use BDF_Font;
with Crt;
with Display_P; use Display_P;
with Display_P; use Display_P;
with Keyboard;
with Logging; use Logging;
with Mini_Expect;
with Session_Logger;
with Redirector; use Redirector;
with Redirector; use Redirector;
with Serial;
with Xmodem;

Expand All @@ -95,7 +96,7 @@ package body GUI is
procedure Window_Close_CB (Window : access Gtk_Widget_Record'Class) is
pragma Unreferenced (Window);
begin
Ada.Text_IO.Put_Line ("DEBUG: Calling Main_Quit at level: " & Gtk.Main.Main_Level'Image);
Log (DEBUG, "Calling Main_Quit at level: " & Gtk.Main.Main_Level'Image);
Gtk.Main.Main_Quit;
end Window_Close_CB;

Expand Down Expand Up @@ -318,7 +319,7 @@ package body GUI is
Unused_Buttons : Gtkada.Dialogs.Message_Dialog_Buttons;
begin
if Filename'Length > 1 then
Ada.Text_IO.Put_Line ("DEBUG: Chose template file: " & Filename);
Log (DEBUG, "Chose template file: " & Filename);
-- clear the labels
for K in 1 .. 17 loop
for R in 1 .. 4 loop
Expand All @@ -342,7 +343,7 @@ package body GUI is
Ada.Text_IO.Close (Templ_File);
Template_Revealer.Set_Reveal_Child (True);
else
Ada.Text_IO.Put_Line ("DEBUG: No Template file chosen");
Log (INFO, "No Template file chosen");
end if;
exception
when others =>
Expand All @@ -357,7 +358,7 @@ package body GUI is
Must_Exist => True);
begin
if Filename'Length > 1 then
Mini_Expect.Prepare (Filename, Trace_Script_Opt);
Mini_Expect.Prepare (Filename);
end if;
end Expect_CB;

Expand Down Expand Up @@ -632,7 +633,7 @@ package body GUI is
Dummy_Button := FC_Dialog.Add_Button (Stock_Cancel, Gtk_Response_Cancel);
Dummy_Button := FC_Dialog.Add_Button (Stock_Ok, Gtk_Response_OK);
if FC_Dialog.Run = Gtk_Response_OK then
Ada.Text_IO.Put_Line ("DEBUG: Chosen file for Xmodem Rx: " & FC_Dialog.Get_Filename);
Log (DEBUG, "Chosen file for Xmodem Rx: " & FC_Dialog.Get_Filename);
Xmodem.Receive (String(FC_Dialog.Get_Filename), Trace_Xmodem_Opt);
end if;
FC_Dialog.Destroy;
Expand Down Expand Up @@ -707,7 +708,7 @@ package body GUI is
Paste_Item,
About_Item : Gtk.Menu_Item.Gtk_Menu_Item;
begin
Ada.Text_IO.Put_Line ("DEBUG: Starting to Create_Menu_Bar");
Log (DEBUG, "Starting to Create_Menu_Bar");
Gtk_New (Menu_Bar);

-- File
Expand Down Expand Up @@ -879,7 +880,7 @@ package body GUI is
if Dest = Async then
Serial.Keyboard_Sender_Task.Send_Break;
else
Ada.Text_IO.Put_Line ("INFO: BREAK only implemented for Serial connections");
Log (INFO, "BREAK only implemented for Serial connections");
end if;
elsif Lab = "C1" then
Keyboard.Handle_Key_Release (GDK_F31);
Expand Down Expand Up @@ -962,7 +963,7 @@ package body GUI is
procedure Handle_FKey_Btn_CB (Btn : access Gtk.Button.Gtk_Button_Record'Class) is
Lab : constant String := Btn.Get_Label;
begin
Ada.Text_IO.Put_Line ("DEBUG: Handle_FKey_Btn_CB called for " & Lab);
Log (DEBUG, "Handle_FKey_Btn_CB called for " & Lab);
if Lab = "F1" then
Keyboard.Handle_Key_Release (GDK_F1);
elsif Lab = "F1" then
Expand Down Expand Up @@ -1030,7 +1031,7 @@ package body GUI is
Template_Rev.Add (Template_Grid);
Dummy := FProvider.Load_From_Data (CSS, Error'Access);
if not Dummy then
Ada.Text_IO.Put_Line ("ERROR: Loading CSS from data");
Log (Logging.ERROR, "Loading CSS from data");
end if;
Apply_Css (Widget => Template_Grid, Provider => +FProvider);
Template_Rev.Set_Reveal_Child (False);
Expand Down Expand Up @@ -1061,7 +1062,7 @@ package body GUI is
FKeys_Box.Set_Homogeneous (True);
Dummy := FProvider.Load_From_Data (CSS, Error'Access);
if not Dummy then
Ada.Text_IO.Put_Line ("ERROR: Loading CSS from data");
Log (Logging.ERROR, "Loading CSS from data");
end if;
for N in FKeys'Range loop
declare
Expand Down Expand Up @@ -1134,7 +1135,6 @@ package body GUI is
procedure Adj_Changed_CB (Self : access Gtk.Adjustment.Gtk_Adjustment_Record'Class) is
Posn : constant Natural := Natural(Self.Get_Value);
begin
-- Ada.Text_IO.Put_Line ("DEBUG: Adj changed to " & Posn'Image);
if Posn = Display_P.History_Lines then
Display_P.Display.Cancel_Scroll_Back;
else
Expand Down Expand Up @@ -1193,19 +1193,17 @@ package body GUI is
end Create_Status_Box;

function Create_Window (Host_Arg : in Unbounded_String;
Trace_Expect : in Boolean;
Trace_Xmodem : in Boolean) return Gtk.Window.Gtk_Window is
H_Grid : Gtk.Grid.Gtk_Grid;
Error : aliased Glib.Error.GError;
Unused_Buttons : Gtkada.Dialogs.Message_Dialog_Buttons;
begin
Ada.Text_IO.Put_Line ("DEBUG: Starting to Create_Window");
Trace_Script_Opt := Trace_Expect;
Log (DEBUG, "Starting to Create_Window");
Trace_Xmodem_Opt := Trace_Xmodem;

-- Gtk.Window.Initialize (Main_Window);
Gtk.Window.Gtk_New (Main_Window);
Ada.Text_IO.Put_Line ("DEBUG: New Window Created");
Log (DEBUG, "New Window Created");
Main_Window.Set_Title (App_Title);
Main_Window.On_Destroy (Window_Close_CB'Access);

Expand Down Expand Up @@ -1253,12 +1251,12 @@ package body GUI is

Gdk.Pixbuf.Gdk_New_From_File (Pixbuf => Icon_PB, Filename => App_Icon, Error => Error);
if Error /= null then
Ada.Text_IO.Put_Line ("WARNING: Could not find/load icon file: " & App_Icon);
Log (WARNING, "Could not find/load icon file: " & App_Icon);
else
Main_Window.Set_Icon (Icon_PB);
end if;

Ada.Text_IO.Put_Line ("DEBUG: Main Window Built");
Log (DEBUG, "Main Window Built");

if Host_Arg /= Null_Unbounded_String then
if Count (Host_Arg, ":") /= 1 then
Expand Down
1 change: 0 additions & 1 deletion Src/gui.ads
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ package GUI is
Trace_Script_Opt, Trace_Xmodem_Opt : Boolean;

function Create_Window (Host_Arg : in Unbounded_String;
Trace_Expect : in Boolean;
Trace_Xmodem : in Boolean) return Gtk_Window;

end GUI;
36 changes: 36 additions & 0 deletions Src/logging.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Copyright ©2022 Steve Merrony

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.

-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.

with Ada.Text_IO; use Ada.Text_IO;

package body Logging is

procedure Set_Level (Level : in Level_T) is
begin
Current_Level := Level;
end Set_Level;

procedure Log (Level : in Level_T; Message : in String) is
begin
if Level >= Current_Level then
Put_Line (Level'Image & ": " & Message);
end if;
end Log;

end Logging;
31 changes: 31 additions & 0 deletions Src/logging.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Copyright ©2022 Steve Merrony

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.

-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.

package Logging is

type Level_T is (DEBUG, TRACE, INFO, WARNING, ERROR);

procedure Set_Level (Level : in Level_T);
procedure Log (Level : in Level_T; Message : in String);

private

Current_Level : Level_T := INFO;

end Logging;
Loading

0 comments on commit b38eac0

Please sign in to comment.