How to display debug info or console.log equivalent in Lua - debugging

I am creating many games using Lua and LOVE2D, but whenever I implement a new function and want to test it out, or simply want to know a value of a variable in Lua, I either display it on the game screen or just hope that it works.
Now my question is...
IS THERE A WAY TO DISPLAY SOME INFO, such as A VARIABLE VALUE or something else into the terminal or somewhere else? Just like console.log in javascript which displays some content in the javascript console in the browser. So, is there a way to do this is Lua?? using LOVE2D?
I am using a Mac, so I have a terminal and not a command prompt. Is there a way to display some content there? Anywhere else would also be fine, I just need to see if those values are as expected or not.

Use a conf.lua file to enable the console, then you should be able to use a standard print(). You can read the wiki entry here.
Note: You have to run Lua and Love2D via the terminal for this to work. Running Lua and Love2D like this is required for the print statements to show:
/Applications/love.app/Contents/MacOS/love "/Users/myuser/Desktop/love2d-test-proj"
You just need to add a conf.lua file to the same location where your main.lua. Your file may be as simple as this:
function love.conf(t)
t.console = true
end
But feel free to copy the whole configuration file from the above link and edit what you need.
I can't be completely sure about this, because I have no access to Mac, but the console is disabled by default and even on Windows, no prints are shown until you turn it on.
Alternatively You can also display debug info in the game itself like some games do.
What I like to do is add something like debugVariable = {} for logging events that happen in each loop and debugPermanent = {} for events that happen rarely. Possibly add convenience functions for writing to the variables:
function debugAddVariable(str)
table.insert(debugVariable, str)
end
--..and similarly for debugPermanent
Now a function to draw our debug info:
function debugDraw()
love.graphics.push() --remember graphics state
love.graphics.origin() --clear any previous transforms
love.graphics.setColor(--[[select color for debug info]])
love.graphics.setFont(--[[select font for debug info]])
for i, v in ipairs(debugPermanent) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
for i, v in ipairs(debugVariable) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
debugVariable = {} --clear debugVariable to prepare it for the next loop
love.graphics.pop() --recall graphics state
end
And we just call this draw function at the end of our love.draw() and the texts should appear.
Obviously, this method can be refined further and further almost infinitely, displaying specific variables, and adding graphs for some other variables to clarify the information you want to show, but that's kind of outside of the scope of the question.
Lastly Feel free to check here for debug libraries submitted by users.

Related

"Watch" long array from Julia REPL?

Suppose I have a long array.
> using MakieGallery
> size(database)
(210,)
If I do
> [d.title for d=database]
it will print it truncated, and if I show it, it will print it into a mess:
> show([d.title for d=database])
I don't know how, but probably I could print values into a column and it would scroll my console far up.
All this is bad. Is it possible to do some sort of simple "watch" of a variable? I.e. open some small widget in separate window with a list control, diplaying an array, which I could scroll as needed?
Internally Julia uses Base.show to display the values in the REPL, you can simply extend this function in any way you like (this example is just a really simple implementation to print every element of array in a new line and you probably shouldn't use it):
Base.show(io::IO, ::MIME"text/plain", x::Array) = x .|> println
You can then go on and add your function to .julia/config/startup.jl to load this every time you start the REPL. Just make sure to have a really solid implementation to handle various edge cases where it might not function properly.
Pluto.jl has a very nice viewer for tabular data (including arrays). It truncats the output per default, but offers a button to show more.
Furthermore, the view automatically updates when you change the data in another cell.

Change the way an object is displayed in debugger/inspector variable-value table

I would like to know if there is a message I can override in Pharo so that my custom classes display more descriptive information in the inspector/debuger much like simple variable types do, like Integers or Strings. For instance:
Instead of that, I would like it to show a more custom and informative description consisting of its internal variales so as to have a tighter/tidier view of the variables instead of having to click on it and open another chart (therefore losing sight of the information on the previous chart). I know you can increase the amount of charts shown below, but that is not the point of the question. I would like to achieve something like this:
I have browsed the pharo forums and found nothing, I have also tried overriding over 30 methods hoping that one of them changed the output. Only the class message seemed to change the output, but I could only return an instance of Metaclass and besides messing with this message would break a lot of stuff. Finally I tried to reverse engineer the debugger and then the inspector to see at which point is the table constructed and what values are used or which messages are sent to build said values, but it was just too much for me, the callstack kept growing and I couldn't even scratch the surface.
Luckily, doing this in any Smalltalk is very easy. Types inherited from Object are expected to answer to the message printString, and ultimately printOn: aStream. Those messages are expected to give a description of the object. So, you should just override printOn: in your class (printString uses printOn:) and all the browsers and inspectors will automatically use it. There other possibilities in Pharo, if you want to provide more complex information in different tabs, but I think printOn: will suffice for you.
An example would be:
MyPoint>>printOn: aStream
aStream nextPut: ${.
x printOn: aStream.
aStream nextPutAll: ', '
y printOn: aStream.
aStream nextPut: $}
In Smalltalk, every time you observe something you don't like or understand, you ask the question: Which message is doing this?
In your case, the question would be: Which message creates the string a MyPoint that I see everywhere?
Next, to answer your question you need to find a good place for inserting a halt and then debug from there until you find the culprit. To do this just find the simplest expression that would reproduce the issue and debug it. In your case the right-click command in the Playground will do. So,
Write and select (MyPoint on: 14 and: -5) halt in a Playground.
Right-click and issue the Print it command (I'm assuming you already checked that this command produces the string 'a MyPoint').
Debug
Go over the evaluation of #DoIt, which answers the result
Continue this way alternating between Into and Over to make sure you follow the result to where it's being taken
Eventually you will reach the implementation of Object >> #printString. Bingo!
Now you can open a System Browser and take a look at this method, study how it's been implemented in different classes, etc. Your investigation should show you that the most basic message for printing is #printOn:. You may also want to take a look at other implementors so to better understand what people usually do. (Bear in mind that writing good #printOn:s is a minimalist art)
Overriding printOn: will work for simple cases where you want to just change description.
Pharo allows a lot more than that!
Due the extensible (moldable) nature of our inspector, you do not need to override a method to get your own visualisation of the object.
For example, look this array visualisation:
This is obtained adding this method to Collection:
gtInspectorItemsIn: composite
<gtInspectorPresentationOrder: 0>
^ composite fastList
title: 'Items';
display: [ self asOrderedCollection ];
beMultiple;
format: [ :each | GTObjectPrinter asTruncatedTextFrom: each ];
send: [ :result |
result
ifNil: [ nil ]
ifNotNil: [ result size = 1
ifTrue: [ result anyOne ]
ifFalse: [ self species withAll: result ]
]
]
if you browse for senders of gtInspectorPresentationOrder: you will see there are already a lot of special visualisations in the image.
You can take those as an example on how to create your own, adapted exactly to what you need :)

Printing VBScript variables in QTP UFT

How can I check values of my variables at run time when using QTP UFT?
I simply want to create a variable, do logic and fill it with data, set a breakpoint in the line following the variable and then check or output its value somewhere.
I have tried:
print variableName
WScript.Echo variableName
The first produces error: Print function type mismatch
The second produces error: Object required: "WScript"
I'm not sure where the problem lies as I've just started to get into both UFT and VBScript (mostly did C# and javascript and everything is quite different here). Could someone tell me the correct solution and perhaps also explain these errors to me?
If you wanted to see all variables use the debug viewer in QTP.
View -> Debug Viewer
There you can list all the variables you want to watch. You should be able to see them in break points.
Note : Ensure you have Windows script debugger installed to use the debug viewer.
You can also add the variable to watch .. Insert a breakpoint>> start the script then right click on the variable under test and add it to watch(Add to Watch) . After Adding you will see a watch window and the value of the variable will be displayed.
I obsess over my variables... so much so that I sprinkly my code with print statements... Actually, I abstract print into my own UDF (so that I can optionally add logging to a file if needed)...
Here's my function: (it's actually a sub because it doesn't return anything)
Sub say(textToSay)
If talkative Then 'note that if I set global var "talkative" to false, then the whole log is disabled
print textToSay
End If
End Sub
Then, ALL my code typically looks like this...
say "click submit button"
Broswer("WebSite").Page("Search").WebButton("Submit").click
say "check for result"
if not Browser("WebSite").Page("Results").Exist then
say "results page didn't appear after exist timeout"
failtest
else
say "successfully found results page"
end if
Honestly, I'm shocked that your "print variableName" statement gave an error, print is actually available in the VBScript api, so it should have worked.
All of my output goes to the Output pane in UFT. I would give my right-arm to find a way to programmatically clear that output pane between runs, but noone seems to know how to do it.
The benefit of all this logging is that I can watch my code run and see EVERY branch taken by the code, and I can add statements to print my variables.
Here's an example that shows how I would answer your question:
result = browser("WebSite").Page("Results").WebElement("Result").GetROProperty("innertext")
say "result:" & result
if result = "Approved" then
Reporter.ReportEvent micPass, "Check for approved", "Approved!"
else
Reporter.ReportEvent micFail, "Check for approved", "NOT Approved!"
End If
Notice the say statement in there - I'll be able to see that immediately during code execution without waiting until the results are shown at the end.

How to debug a spreadsheet custom function in google script?

I am writing a custom function to be used in a spreadsheet and I would like to be able to at least display some data. Few things seem to work, for example Browser.msgBox doesn't find the appropriate permissions.
Breakpoints don't interrupt execution.
I had some hope for this code
function test() {
var s = "test"
Logger.log(s)
return s + s
}
But when I set a cell in the spreadsheet to "=test()" the cell properly shows the value "testtest" but when I return to the script editor and use view>execution transcript or view>logs I don't see anything.
Perhaps this logging goes to a special file somewhere?
When you use a custom function as a formula, it can be evaluated and re-evaluated at many times. Therefore, it is not appropriate to fill up the Logging output or the Execution Transcript with this. If you want to debug, you must run (or debug) the script manually from the script editor.
Take an example, where you have two custom functions - f1() and f2()
And say, in cell A1, you enter the formula =f1() and in A2, you enter =f2(A1).
In such a case, both the cells will be re-evaluated. So what should the logger output show ?

What is the best way to get keyboard events (input without press 'enter') in a Ruby console application?

I've been looking for this answer in the internet for a while and have found other people asking the same thing, even here. So this post will be a presentation of my case and a response to the "solutions" that I have found.
I am such new in Ruby, but for learning purposes I decided to create a gem, here.
I am trying to implement a keyboard navigation to this program, that will allow the user use short-cuts to select what kind of request he want to see. And in the future, arrow navigations, etc.
My problem: I can't find a consistent way to get the keyboard events from the user's console with Ruby.
Solutions that I have tried:
Highline gem: Seems do not support this feature anymore. Anyway it uses the STDIN, keep reading.
STDIN.getch: I need to run it in a parallel loop, because at the same time that the user can use a short-cut, more data can be created and the program needs to show it. And well, I display formated text in the console, (Rails log). When this loop is running, my text lost the all the format.
Curses: Cool but I need to set position(x,y) to display my text every time? It will get confusing.
Here is where I am trying to do it.
You may note that I am using "stty -raw echo" (turns raw off) before show my text and "stty raw -echo" (turns raw on) after. That keeps my text formated.
But my key listener loop is not working. I mean, It works in sometimes but is not consistent. If a press a key twice it don't work anymore and sometimes it stops alone too.
Let me put one part of the code here:
def run
# Two loops run in parallel using Threads.
# stream_log loops like a normal stream in the file, but it also parser the text.
# break it into requests and store in #requests_queue.
# stream_parsed_log stream inside the #requests_queue and shows it in the screen.
#requests_queue = Queue.new
#all_requests = Array.new
# It's not working yet.
Thread.new { listen_keyboard }
Thread.new { stream_log }
stream_parsed_log
end
def listen_keyboard
# not finished
loop do
char = STDIN.getch
case char
when 'q'
puts "Exiting."
exit
when 'a'
#types_to_show = ['GET', 'POST', 'PUT', 'DELETE', 'ASSET']
requests_to_show = filter_to_show(#all_requests)
command = true
when 'p'
#types_to_show = ['POST']
requests_to_show = filter_to_show(#all_requests)
command = true
end
clear_screen if command
#requests_queue += requests_to_show if command
command = false
end
end
I need a light in my path, what should I do?
That one was my mistake.
It's just a logic error in another part of code that was running in another thread so the ruby don't shows the error by default. I used ruby -d and realized what was wrong. This mistake was messing my keyboard input.
So now it's fixed and I am using STDIN.getch with no problem.
I just turn the raw mode off before show any string. And everything is ok.
You can check here, or in the gem itself.
That's it.

Resources