Read terminal output

Hi,

So I’m trying to get the output of a corona sdk run, but I can’t seem to get it right.

If I run the /Applications/CoronaSDK/simulator and pipe the output to something, all I get is the corona copyright message and the text from the “print()” statements in the project doesn’t show up until I quit the simulator.

If I run the simulator with the -project flag I only get the copyright notice no matter what.

If I just try to read the output of the run (not piping it to anything) I only get the copyright notice as well (and errors) but not the print()'s

How should I go about this problem? [import]uid: 43027 topic_id: 15621 reply_id: 315621[/import]

Hey,

instead open the Corona Simulator, try oppening the Corona TERMINAL as it will automatically call the Simulator and the both will work together as well as you can open the Debugger and it will call the Simulator.
Hope that helps,
Rodrigo. [import]uid: 89165 topic_id: 15621 reply_id: 57675[/import]

Thanks, but I’m trying to capture the console output in my own application, but it gets lost somewhere in the void.

Manually opening the corona simulator (not the .app, the one in the .app that the corona terminal opens) or opening the corona terminal works fine. It’s when I try to intercept the output it goes wrong. [import]uid: 43027 topic_id: 15621 reply_id: 57676[/import]

Sorry,

I did not understand you well so.

Anyway maybe someone reading this will let you a help!
Luck!
Rodrigo. [import]uid: 89165 topic_id: 15621 reply_id: 57679[/import]

@tomas, you have the solution in your post. It was staring you in the eye and you have missed it all along.

Did you see what the console says when you spawn the terminal? The command used to spawn the simulator is

/Users/{path}/CoronaSDK/Corona\ Terminal ; exit;

and you said that you start to get the data *only* after you exit… so you add the exit like ansca does in the command to start getting the data flowing…

hope that works for you ,

mit glueck
cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15621 reply_id: 57690[/import]

If it only were so…

the “code” for the Corona Terminal app in the Corona SDK folder looks like this:

[bash]
#!/bin/bash

path=dirname "$0"
if [[-f “$path/Corona Simulator.app/Contents/MacOS/Corona Simulator”]]
then
“$path/Corona Simulator.app/Contents/MacOS/Corona Simulator” $*
else
echo “ERROR: Could not find “Corona Simulator.app” in: $path”
fi
[/bash]

so adding a “exit;” unfortunately doesn’t help me :frowning:

I’m trying to do this in C (alltough I hardly know any :wink: )

the following is my code:
[cpp]
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {
FILE *fp;
char line[1024];

// Open the command for reading.
fp = popen("/Applications/CoronaSDK/simulator -project /path/Tester 2>&1", “r”);

if (fp == NULL) {
printf(“Failed to run command\n” );
exit;
}

fprintf(stdout, “%s”, “This will show!”);
fflush(stdout);

// Read the output a line at a time - output it.
while (fgets(line, sizeof line, fp) != NULL) {
printf(“out: %s”, line);
fflush(stdout);
}

fflush(fp);
fprintf(stdout, “%s”, “Death to me! \o/\n”);

// close
pclose(fp);

return 0;
}
[/cpp]

and it outputs:


This will show!
out: Copyright © 2009-2011 A n s c a , I n c .
out: Version: 2.0.0
out: Build: 2011.591
out: The file sandbox for this project is located at the following folder:
out: (/path/Tester-AE5A86F38C0D11BB4ACCDA74C52FB74D)

– this is were i cmd+q it:

Death to me! \o/



the test project I’m running just does a bunch of “print()”: s but they never show up.
if I change to:
[cpp]fp = popen("/Applications/CoronaSDK/simulator 2>&1", “r”);[/cpp]

I get the same output, but I also get the “print()”: s, when I press cmd+q (and also I have to pick the project manually, which is a deal-breaker)

…the quest goes on [import]uid: 43027 topic_id: 15621 reply_id: 57696[/import] </stdlib.h></stdio.h>

and here’s the video to prove that it works…

on youtube

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15621 reply_id: 57699[/import]

Ah…

sorry I forgot to mention that I also got it to work in xcode, but not if I run the c-app from the command line in a terminal.

I did get it to work in bash, but only if I added “io.flush()” after my prints.

[import]uid: 43027 topic_id: 15621 reply_id: 57700[/import]

Duh me… the exit was just to close the terminal at the end.

About can you use this in a console, yes you can, you need to change a few things in your code.

  1. Do not use popen
  2. read from the stdout rather than reading from a pipe

well, if you just keep your read and write to the simple stdin and stdout, your app will interact with the console…

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15621 reply_id: 57706[/import]

here’s the console output

TheiMac:~ username$ /Users/username/Desktop/runSimulator/build/Debug/runSimulator;

Copyright (C) 2009-2011 A n s c a , I n c .
 Version: 2.0.0
 Build: 2011.612
The file sandbox for this project is located at the following folder:
 (/Users/username/Library/Application Support/Corona Simulator/Vector Graphics-642B983D6AC38A95AAD8B61F9603BA07)
320 480
60 60
60 60
60 60
60 60
table: 0x1b757dd0
table: 0x1b757dd0
table: 0x1b757dd0
Runtime error
 /Users/username/Desktop/Vector Graphics/main.lua:446: attempt to index local 'te' (a nil value)
stack traceback:
 [C]: ?
 /Users/username/Desktop/Vector Graphics/main.lua:446: in main chunk
Runtime error: /Users/username/Desktop/Vector Graphics/main.lua:446: attempt to index local 'te' (a nil value)
stack traceback:
 [C]: ?
 /Users/username/Desktop/Vector Graphics/main.lua:446: in main chunk
This will show!Death to me! \o/
MyiMac:~ username$ 

the out: is not being appended, but then when working with a console (terminal) is that not what you are after, it does not matter if you have out: prepended or not.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15621 reply_id: 57707[/import]

This is what I get when I run it at my end

[Session started at 2011-09-27 13:00:25 +1000.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1510) (Wed Sep 22 02:45:02 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86\_64-apple-darwin".tty /dev/ttys002
Loading program into debugger…
Program loaded.
run
[Switching to process 62133]
Running…
This will show!out: 
out: Copyright (C) 2009-2011 A n s c a , I n c .
out: Version: 2.0.0
out: Build: 2011.612
out: The file sandbox for this project is located at the following folder:
out: (/Users/username/Library/Application Support/Corona Simulator/Vector Graphics-642B983D6AC38A95AAD8B61F9603BA07)
out: 320 480
out: 60 60
out: 60 60
out: 60 60
out: 60 60
out: table: 0x1b55bd50
out: table: 0x1b55bd50
out: table: 0x1b55bd50
out: Runtime error
out: /Users/username/Desktop/Vector Graphics/main.lua:446: attempt to index local 'te' (a nil value)
out: stack traceback:
out: [C]: ?
out: /Users/username/Desktop/Vector Graphics/main.lua:446: in main chunk
out: Runtime error: /Users/username/Desktop/Vector Graphics/main.lua:446: attempt to index local 'te' (a nil value)
out: stack traceback:
out: [C]: ?
out: /Users/username/Desktop/Vector Graphics/main.lua:446: in main chunk
Death to me! \o/

Debugger stopped.
Program exited with status value:0.

Now the issue is that you are not spawning it correctly. It is got to do with your Mac skills more than anything else.

You need to understand that the Mac .app is a directory that has sub directories inside of it that ultimately has an executable, which is what you need to spawn if you want to get access, you are calling the wrapper which will run but not give you direct control as you are wanting.

I did not even need the exit, I have spawned a project directly, and I am reading all the console output too from the program.

I will show you a video that it works…

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15621 reply_id: 57698[/import]

I wrote an article on two things,

  1. Mac App structure (for those that are new or have no idea on this)
  2. The code that will do what you are trying to achieve.

you can read it here

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15621 reply_id: 57709[/import]

Wow, that’s awesome, thank you very much for taking this much interest in the subject, however,

The whole issue for me is that I use the Titanium Appcelerator desktop framework and it will for some reason not give me the output that comes from the print() statements, only the copyright text and errors.

Therefore I had a theory that if I could take all the output from a corona sdk run and make sure they always gets printed on the stdout. It wasn’t that easy though, but the closest I came was my solution and in conjunction with it to put io.flush(); after I use a print(). If I do that, my code (posted above) works just fine. This is what my final code came to be:

[cpp]
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {

// We need the arguments
if (argc == 1) {
printf(“usage: %s <path_to_project> []\n”, argv[0]);
return 0;
}

FILE *fp;
char line[1024];
char run_string[256];

sprintf(run_string, “/Applications/CoronaSDK/simulator -project %s -skin %s 2>&1”, argv[1], argc == 3 ? argv[2] : “iPhone”);

fp = popen(run_string, “r”);

if (fp == NULL) {
printf(“Failed to run command\n” );
exit;
}

// Read the output a line at a time - output it.
while (fgets(line, sizeof line, fp) != NULL) {
fputs(line, stdout);
fflush(stdout);
}

// close
pclose(fp);

return 0;
}
[/cpp]

in some attempts, the print() statements got printed, but not until the simulator was closed, this was not an option for me as I wanted the output progressively as it occurred.

I will mos def revisit this issue, but this will have to do for now I guess (since it’s the closest to my goal) [import]uid: 43027 topic_id: 15621 reply_id: 57716[/import] </path_to_project></stdlib.h></stdio.h>

@thomas,
if what you are after is output from an app to feed into another, there are other ways to do so, there is a tutorial by Jay (not me) but Jay Whye @JAWhye on gamedevnation, he is posting the data to postbin, so if you can do something like that, push data to a site/web service, it will be much easier to use.

Anyways, if you find the solution you have above to be easier, well, then that’s what you have to do.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15621 reply_id: 57719[/import]