![](/img/trans.png)
[英]How to create a command line application like awscli with python in windows?
[英]xcode create command line window like windows does
我有一个Windows应用程序,将其通过GLFW移植到Mac。 在Win32中,我通过::AllocConsole();
创建一个cmd窗口::AllocConsole();
。 我用它来调试脚本。 但是在Mac中,似乎无法从正在运行的进度创建cmd窗口。 lldb适合C ++部分,但是对我的python脚本部分没有帮助。 我试图创建一个GLFW窗口来伪造此窗口,但是GLFW只能运行一个实例,如果挂断我的应用程序,则所有窗口都将暂停。 因此,我正在寻找一种在子线程中从我的应用程序创建窗口的方法,并且该窗口可用作与我的应用程序交互的调试工具。
谢谢你的提问。 我花了一些时间研究出可以在任何类Unix系统上使用的东西。
这是一个起点。 我将它留给其他人进行完善。
要点:
将进程分叉到主程序和子程序中。
主程序将unix域套接字iostream连接到其选择的unix套接字名称。
子进程产生一个xterm进程,该进程使用特殊的命令行选项运行相同的程序,并为其指定unix域套接字的名称。
在xterm下运行的程序的衍生版本在unix套接字上侦听并重复接收到的所有数据以输出到std(在xterm窗口中)。
现在,原始程序可以将数据记录到unix域io_stream来发出调试数据。
该程序也应在cygwin下的Windows中运行。
#include <iostream>
#include <memory>
#include <chrono>
#include <system_error>
#include <thread>
#include <stdlib.h>
#include <unistd.h>
#include <boost/asio.hpp>
using namespace std;
namespace asio = boost::asio;
asio::io_service io_service;
asio::local::stream_protocol::iostream log_stream;
int run_program(int argc, char** argv)
{
for (int i = 0 ; i < 100 ; ++i) {
cout << "logging first" << endl;
log_stream << i << " hello" << endl;
cout << "logged" << endl;
this_thread::sleep_for(chrono::milliseconds(400));
}
return 0;
}
auto main(int argc, char** argv) -> int
{
if (argc == 3
&& strcmp(argv[1], "--repeat") == 0)
{
auto socket_name = string(argv[2]);
cout << "listening on " << socket_name << endl;
::unlink(socket_name.c_str()); // Remove previous binding.
asio::local::stream_protocol::endpoint ep(socket_name);
asio::local::stream_protocol::acceptor acceptor(io_service, ep);
asio::local::stream_protocol::socket socket(io_service);
acceptor.accept(socket);
cout << "accepted" << endl;
while (1) {
char buf[100];
auto bytes_read = socket.read_some(asio::buffer(buf));
if (bytes_read > 0) {
cout.write(buf, bytes_read);
cout.flush();
}
else {
socket.close();
exit(0);
}
}
}
else {
const auto socket_name = "/tmp/foo"s;
cout << "forking" << endl;
auto client_pid = fork();
if (client_pid == 0) {
cout << "in client" << endl;
ostringstream ss;
ss << "xterm -e " << argv[0] << " --repeat " << socket_name;
auto s = ss.str();
auto err = system(s.c_str());
if (err) {
throw system_error(errno, system_category(), "logger child execution");
}
}
else if (client_pid == -1) {
throw system_error(errno, system_category(), "forking");
}
else {
cout << "pause to allow xterm client to start" << endl;
this_thread::sleep_for(chrono::seconds(2));
cout << "making endpoint " << socket_name << endl;
asio::local::stream_protocol::endpoint endpoint(socket_name);
cout << "connecting to " << endpoint << endl;
log_stream.connect(endpoint);
cout << "connected" << endl;
auto ret = run_program(argc, argv);
log_stream.close();
exit(ret);
}
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.