Saving information to a log file

I’m trying to debug some code that crashes Julia (yet, ironically, works perfectly fine in the debugger!). My problem is that it crashes the REPL, so I can’t see what’s going on. I went so far as to use QuickTime player to make a screen recording so that I could slow down the movie of the REPL before it crashed…

So I got the idea of writing some log functions to write information to disk so that I could inspect it post-crash. That’s when I stumbled across some problems.

For example, for the life of me, I can not figure out how to:

(1) Save a variable’s type as string. typeof() returns a variables type, and I have not figured out how to typecast it as a string.
(2) Save a pointer’s address.

This is what I have tried:

		logOut(logFile, String(typeof(surfaceStructPtr)) )		
		logOut(logFile, String(pointer(surfaceStructPtr)) )

And this is my extremely simple logging function:

function logOut(logFile, message)
    dt = now()
    dtString = Dates.format(dt, "yyyy-mm-dd HH:MM:SS")
    message = dtString * ": " * message
    write(logFile,message)
    flush(logFile)
    println(message)
end

P.S. I also tried to find a way to stream the REPL to disk, but none of the suggestions worked.

julia> g = 56::Int64
56

julia> string(typeof(g))
"Int64"

Please use lower case for string

You may be interested in the following.

https://julialogging.github.io/how-to/log-to-file/

1 Like

StevenSiew: doh! I think its a bad-habit I have from type-casting in C.

I also figured out that I didn’t need to print the pointer’s memory address, I could just do something like this to see if it is a legitimate pointer:

		if surfaceStructPtr == C_NULL
			logOut(logFile, "surfaceStructPtr is C_NULL" )
		else
			logOut(logFile, "surfaceStructPtr is NOT C_NULL" )
		end

Now I just need to figure out why it runs in the debugger (meaning it does the task and does not crash), but segfaults when I run it from the REPL…