Master_T
Master_T

Reputation: 7987

Ada: can I have two different console windows?

I'm using GNAT 2014 for an Ada project and I have a simple (I hope) question. Can I have two "consoles" open at the same time?

Let me explain why: my program outputs a lot of logging data to the console. What I would like would be to have two separate consoles, one that prints the program output and one that takes user inputs.

Is this doable? Or is this something that runs on the OS side of things, and Ada has no control over? I could achieve what I want with GtkAda, but since I'd like to use as little 3rd party libraries as possible I wanted to know if there was a native way first (also considering that the commands I need to issue are very basic, like "quit" or "pause", a console input would suffice).

Upvotes: 0

Views: 270

Answers (2)

Jacob Sparre Andersen
Jacob Sparre Andersen

Reputation: 6611

Consider creating a file for each non-interactive "console" you want:

with Ada.Text_IO;

generic
   type Names is (<>);
package Generic_Consoles is
   procedure Open_Or_Create;
   function File (Name : in Names) return Ada.Text_IO.File_Access;
end Generic_Consoles;

package body Generic_Consoles is
   Files : array (Names) of aliased Ada.Text_IO.File_Type;

   procedure Open_Or_Create is
      use Ada.Text_IO;
   begin
      for Name in Files'Range loop
         begin
            Open (File => Files (Name),
                  Mode => Append_File,
                  Name => Names'Image (Name));
         exception
            when others =>
               Create (File => Files (Name),
                       Mode => Append_File,
                       Name => Names'Image (Name));
         end;
      end loop;
   end Open_Or_Create;

   function File (Name : in Names) return Ada.Text_IO.File_Access is
   begin
      return Files (Name)'Access;
   end File;
end Generic_Consoles;

Once you have instantiated the package and called Open_Or_Create, you can use the different "consoles" with all the usual Ada.Text_IO output procedures:

Put_Line (File => Consoles (Parser_Errors),
          Item => "Error 42: Could not parse statement.");

Upvotes: 2

manuBriot
manuBriot

Reputation: 2715

You could for instance use GNATCOLL.Traces for the logging framework (instead of, I assume, Put_Line). This will allow you to control precisely where the logs should go, and in particular some of the logs can be configured to be sent to a file, some to the terminal.

One thing you could do is send some of the logs to a file, and in your second terminal monitor that log file. This is also more convenient since it allows you to scroll up in the logs, whereas a terminal will tend to lose the older logs.

Of course, the same approach can be done with a simple Put_Line (Standard_Error, "...") or even to a file, but GNATCOLL.Traces is configurable dynamically which is often more convenient.

Upvotes: 1

Related Questions