how to handle different input/output in one console? - c++

I am working on a windows sever program using c++, when the program started it stays for several days and output important logs to the default windows console.
now I want to add some control function to the console, like I cound enter something like query or stop, and the program ouput the variable number or stop accept requests. So there is the problem, I got two output stream(log and query response) and one input stream both mixed in one single console.
How do I seperate the three different stream in one single console? Maybe I cound write my own console to replace the default windows console?
I believe this is a very normal need and a lot of server application has implemented this but I could not find any source code...
I know I could use ncurses, but I think ncurses seems too low level for this. Any suggestion will be apprecitated.

Related

Creating a Launcher and sending info to second program

I have created a program that extracts data from a racing game and sends it to a speed gauge cluster. I call it the transfer program.
I need a simple user-friendly User Interface to start the transfer program, set some variables and choose a COM Port. At the moment I'm trying to do it with a C++ Windows Forms Application in a CLR Project on Microsoft Visual Studio 2015. When I tried to do it directly (creating the UI in the same project as the transfer program) I just get too many errors that I have no idea where they come from or why they are there.
So I've decided maybe I could try creating some sort of launcher, i.e. a completely different program that is only the UI to start the transfer program and send a few user-set variables to it on startup as well as choose a COM Port to communicate on.
Any idea on how to start on this? How do I execute the transfer program from the Launcher? How do I send variables and data to it?
Thank you very much!
There are basically two ways to pass information to your transfer program.
For simple use cases, just pass the values along on the command line. If you're still using the CLR, this is done using System.Diagnostics.Process. A nice example can be found in this answer: https://stackoverflow.com/a/33633148/127826
Use a shared configuration file. So the user interface loads the values from the config and also saves the file before executing the transfer program. The transfer program then reads the same configuration file to get the values it needs.
The second approach is far more flexible and is what I would use.

Keep a running C++ program console on top?

Let's say I was using cin in my program to allow the user to input into the console. That is simple enough but what if they were typing into, let's say, a web browser and I wanted them to input that into the console at the same time? When I click away the C++ program console window and have something else on top, the input obviously does not go into the console. How can I make it so the console is always running on top so that even when I were to input something into a web browser, it would also go into the console?
in order capture keyboard input when your application is not focused, you need to use windows hook, see:
http://msdn.microsoft.com/en-us/library/ms644959%28v=vs.85%29.aspx#wh_keyboardhook
Example code can be found here:
http://www.codeguru.com/cpp/w-p/system/keyboard/article.php/c5699/Hooking-the-Keyboard.htm
I think you may want to take a look at hooking the keyboard to get the input?

Prevent mixing of console output and written text

I have a C++ console application that prints some output constantly while it also accepts commands (using std::cin) from the user - output and input happen in separate threads.
If I write a text while some output appears the written text is mixed with application output. How can I prevent this behaviour?
To solve this problem, I need to display the program one line above the line where the text is typed. I'd inspire myself in Minecraft Bukkit server's solution - however I need the same for C++.
Assuming you want the output to appear while things are being typed, you'll need some screen control facilities to have the output go somewhere different than the input area. If I were tasked to implement something like this writing to a terminal I would refresh my ncurses experience. I realize you are on a Windows console and I have no idea if the Windows console is capable of the screen control needed to make it happen.
You can possibly tie custom stream buffers into std::cin and std::cout using the curses functionality under the hood but it may not be worth it. In any case, it isn't entirely trivial.
There's a windows port of ncurses called pdcurses. But if you are using visual studio there's a simple function provided called SetConsoleCursorPosition()

Windows Console2 coolType

As you can see in u413.com: Text comes on the screen, letter by letter, which looks kind of cool,
I want to do the same thing in Console
I got the source code of version 1.2; because I dont need all the complexity of version 2;
I just need a simple Command Prompt, with text that comes on to the screen letter by letter.
I dont need most of the builtin functions offered by Console, like Transparency, Taskbar Icon, etc.
The source code base is pretty small with only about 5 files.
The main class file seems to be Console.cpp;
Since console is like a GUI application, things dont get written to the STDOUT.
but heres what happens;
A Handle is called;
And that handle apparently writes to the console;
m_hStdOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
Now what I want to be able to do; Is somehow read the handle; see what text it has; And throw in a loop with a ::Sleep(20) method in it; to make sure that text appears letter by letter.
#Alf P. Steinbach I wrote the psuedo code for making the sleep command(in java) I also made use of it in every other java program I wrote, but the disadvantage was that it will work only for my programs and not every program run in the command prompt, but what I dont know is code to make a console, a windows console subsystem program, I wish it was possible with java, so that I could use it on linux too, but now, let me ask you exactly what I had in mind...
A program which simply, took input from the screen, send it to cmd.exe for processing, and sent back the reply, and all I have to do is throw in a sleep command between every character...
All I need is help, in getting this done, I wish you could start me up with this, and possible provide links and refrences to get this done...
This Console.cpp seems like a console emulator, meaning it runs OTHER programs. You don't need the source code, you can run existing console applications.
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE and STD_ERROR_HANDLE are the windows handles for STDIN, STDOUT and STDERR. They basicly does the same with only a few differences in how to use it.
If you want to integrate the console into your code, then you have to understand it, and find its own output function and call that. But it won't be any standard handle or things like that.

Async Console Output

I have a problem with my application win32 console.
The console is used to give commands to my application. However, at the same time it is used to output log messages which mostly comes from asynchronous threads. This becomes a problem when the user tries to write some input and simultaneously an async log message is printed, thus thrashing the display of the users input.
I would like to have some advice in regards to how to handle such a situtation?
Is it possible for example to dedicate the last line in the console to input, similarly to how it looks in the in-game consoles for some games?
You can use SetConsoleMode to disable input echo and line editing mode. You can then echo back input whenever your program is ready to do so. Note that this means you will need to implement things like backspace manually. And don't forget to reset the mode back when you're done with the console!
This is possible using the Console API, but it involves quite a bit of work and all the threads that use the console will have to cooperate by calling your output method rather than directly calling the Console API functions or the runtime library output functions.
The basic idea is to have your common output function write to the console screen buffer, and scroll the buffer in code rather than letting the text flow onto the last line and scroll automatically. As I recall, you'll have to parse the output for newlines and other control characters, and handle them correctly.
You might be able to get away with using "cooked" console input on the last line, although in doing so you risk problems if the user enters more text than will fit on a single line. Also, the user hitting Enter at the end of the line might cause it to scroll up. Probably best in this situation to use raw console input.
You'll want to become very familiar with Windows consoles.
Any time you have asyncronous threads trying to update the same device at once, you are going to have issues like this unless something synchronizes them.
If you have access to everyone's source code, the thing to do would probably be to create some kind of sync object that every task must use to access the console (semaphore, etc).