我有一个 windows 应用程序,我用 GLFW 将它移植到 mac。在 Win32 中,我通过 ::AllocConsole(); 创建了一个 cmd 窗口。我用它来调试我的脚本。但在 mac 中,似乎无法从运行进度创建 cmd 窗口。 lldb 适用于 C++ 部分,但对我的 python 脚本部分没有帮助。我试图创建一个 GLFW 窗口来伪造它,但是 GLFW 只能运行一个实例,如果我挂断我的应用程序,所有窗口都会暂停。因此,我正在寻找一种在子线程中从我的应用创建窗口的方法,它可以用作与我的应用交互的调试工具。
最佳答案
感谢您的精彩提问。我花了一些时间才想出可以在任何类 unix 系统上运行的东西。
这是一个起点。我会留给其他人完善。
要点:
将进程 fork 到主程序和子程序中。
主程序将 unix 域套接字 iostream 连接到它选择的 unix 套接字名称。
子进程生成一个 xterm 进程,该进程使用特殊的命令行选项运行相同的程序,并为其指定 unix 域套接字的名称。
在 xterm 下运行的程序的生成版本在 unix 套接字上监听并将它接收到的所有数据重复到标准输出(在 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;
}
关于python - xcode像windows一样创建命令行窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31135068/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法