__FILE__, __LINE__ and __FUNCTION__ for QML files? - qt

As a project I am working on is getting bigger I am getting tired of writing comprehensive log messages, needed to find out what went wrong and where.
So it would be extremely useful if I can incorporate that information into the messages automatically. For C++ we have the convenient __FILE__, __LINE__ and __FUNCTION__ macros, but I don't seem to find any for QML.
Note that there is console.trace() which outputs a trace in the console in the following format:
onCompleted (qrc:/main.qml:72)
So it includes function, file and line, which is all I need, therefore I assume there already exists a way to get those. Natural, console.trace() doesn't really quite cut it, because it directly outputs to the console, whereas I need those as strings to incorporate in my log messages.
So is there any way to get those?
Naturally I don't want to get them in the actual QML source, but the same way console.trace() does - on the spot where my Log.error() is invoked, so I can just Log.error("some error") instead of Log.error("some error in " + file + ", at " + line + " while executing " + func) which will actually be even more tedious than writing the whole thing manually.
Alternatively, I'd also appreciate if someone can point me to the implementation of the qml console, because I combed through the entire qtdeclarative code base for "console" and found nothing.
Note that I already found this, however it isn't of use to me, because I need that for a specific subset of messages and don't want to override the default message handler for all output.

console.log, console.debug, console.info, console.warn and console.error can be used to print debugging information to the console. The output is generated using the qDebug, qWarning, qCritical methods in C++ (see also Debugging Techniques and how such functions are implemented, especially this internal function). Setting the environment variable QML_CONSOLE_EXTENDED also prints the source code location of the call. For example,
export QT_MESSAGE_PATTERN="[%{type}] %{appname} (%{file}:%{line}) - %{message}"
Now the output looks like this,
This link contains details about customizing the QT_MESSAGE_PATTERN environment variable.

Related

R: Why does fs::dir_copy fail some of the time?

During a process, I need to copy a directory using fs::dir_copy(currentPath, newPath). Some of the time, I get the following error message:
Error in link_copy(links, path(new_path[[i]], path_rel(links, path[[i]])), :
all(is_link(path)) is not TRUE
This only happens some of the time, not always. What's more, if I replace my directory with a manual copy of itself (i.e. manually copy directory, delete original, rename the copy), then my code will work.
Could someone please explain why this could be happening? Is there a way I can sidestep that error once and for all?
This does not answer your question but may help you to further analyse what is going on. One of the great things of R is that you can easily inspect source code. Especially in a situation where unusual things appear to happen, looking at the code may be useful.
In your case, we can inspect the source code of fs::dir_copy and trace back to which code generates the error message. If you type fs::dir_copy (no parenthesis) into the console, R will print the R code of the function (if the function is not primitive). Doing that here will show you that the fs:dir_copy function calls the fs::link_copyfunction. Makes sense, as the error message comes from this function.
We can now print this function with fs::link_copy
This function generates the error message in this line:
stopifnot(all(is_link(path)))
From the error message we know that all(is_link(path)) returns FALSE. Next step is to have a look at the fs::is_link function. Here we see that the error may come from the call to the setNames function, which depends on the fs::file_info function: res <- file_info(path)
Here we see that setNames is called with a condition depending on what the file_infofunction returned:
setNames(!is.na(res$type) & res$type == "symlink", res$path)
This looks unusual as setNames takes an object and a character vector as formals. But then, I am not the developer of these functions ;-)
Perhaps there are issues with the file type on your machine and fs::file_info(path) returns something unexpected under some conditions.

How do I get pretty error messages in RStudio?

When working in RStudio (version 0.99.482 and 1.0.136), if I source a file and an error occur I just get the error message without any context. If the file is longer than a few lines of code, that informaiton is largely useless. What I want to know when an error occurs is the following:
What function threw the error? Preferably what function I called from my script, not something burried deep inside a package.
On what line of my file did the error occur?
This seems like a very basic thing for a scripting language to do, but yet I am unable to find an easy solution. Yes, I am aware of the traceback() function, but (a) it is a hassle to call it every time there is an error, and (b) the output is massive and not that helpful.

How to enable breakpoint inside a function body when I debug the main Rscript

I have a main R script which does a multiplication using a function called Multiply().
debugSource("C:/Users/R_debug_breakpoint/myFunction.R")
a<-1
b<-2
mult<-Multiply(a,b)
I write Multiply() in a different R script called "myFunction.R"
"myFunction.R" looks like this:
Multiply<-function(a,b){
c<-a+b
e<-a/b
d<-a*b
return(d)
}
If I want to set a breakpoint inside Multiply() function at the line:
d<-a*b and enter source mode to see what the value of c,e.
However, when I press Source option in my main.R, the debugger does not halt at the breakpoint I set inside the Muliply function. It just runs all the codes in main.R.
I search this problem in many webpages, and the most close one is
How to set a breakpoint in function body in R-studio?
However, I use debugSource here but it still fails.
Another blog about the breakpoint I found out useful is:
https://support.rstudio.com/hc/en-us/articles/200534337-Breakpoint-Troubleshooting
It says:In order to hit breakpoints during sourcing, you need to use the special debug sourcing command debugSource included in RStudio. If you are calling source manually on your file, breakpoints will still work, but will not be enabled until after all the code in the file has been executed.
I think this is reason why it doesn't work, but it doesn't mention a work around for it.
Much appreciated if any help from you guys.

Read line causing a wait for input R

I'm a total noob at R and I may have bitten off a little more than I can chew but if you can help me I will appreciate it.
So what i'm trying to do is retrieve the top trending from twitter (working) and then use them as part of a URL to try pull back their definitions. My issue is the readline function seems to wait for me to hit return before it attempts the URL and i'm looking for a way to make it do the rest automagically, please find my code below
definitions <- ""
lapply(X=hashtags,FUN=function(X){
tagdef <- c(tagdefurl,X[[dfPointer]])
tagdef<- paste(tagdef,collapse=" ")
tagdef <- stringr::str_replace(string=tagdef,pattern=" ", replacement="")
definitions <- tryCatch(readline(tagdef),silent=F)
})
tagdef is defined as is supposed to be the list to store the returned definitions in
I've checked all my OAuth nonsense and everything on that side is fine, i'm getting the trends back without issue. Can anyone give me some pointers?
Unfortunately, you might have just stumbled on a case of "user error due to similarly named functions". In R, there is both readline (which reads a line from the terminal (in interactive use)) and readLines (which is used to *read some or all text lines from a connection).
The former expects user input, and the first argument is "prompt", hence the waiting for input.
Remember also that cApItaLiZation matters in R.

Suppressing some messages in R but leaving others?

I'm an R newbie using RScaLAPACK and every time I spawn a new process grid I get a message.
> sla.gridInit(2)
[1] "RScaLAPACK:Process Grid Initialized "
I'm going to put this line in a function and I don't want my function to be spitting out this message. However- I don't want to just sink("/dev/null") the call because for all I know, something somewhere could go wrong and then I'd be suppressing useful output. Basically, I want it to be silent when it succeeds and loud if it fails. What is the best way to accomplish this?
Any thoughts, including design considerations, are welcome.
edit:
sla.gridInit() isn't returning anything. The code for sla.gridInit just calls print().
edit:
I suppose capturing output is best like in suppress messages displayed by "print" instead of "message" or "warning" in R . At least I will have the output if I want to do something with it.
You can wrap this function in one of the suppress* functions, suppressMessages, suppressWarnings or suppressPackageStartupMessages. See the help pages of those functions for more details.

Resources