# Saving information to a log file

**URL:** <https://discourse.julialang.org/t/saving-information-to-a-log-file/110869>\
**Category:** General Usage\
**Created:** [February 28, 2024, 3:38am UTC](https://discourse.julialang.org/t/saving-information-to-a-log-file/110869 "2024-02-28T03:38:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpeters2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpeters2/32/202247_2.png) [@mpeters2](https://discourse.julialang.org/u/mpeters2)\
**Post date:** [February 28, 2024, 3:38am UTC](https://discourse.julialang.org/t/saving-information-to-a-log-file/110869/1 "2024-02-28T03:38:14Z")

</div>

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:

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

```

And this is my extremely simple logging function:

```julia
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.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [February 28, 2024, 4:42am UTC](https://discourse.julialang.org/t/saving-information-to-a-log-file/110869/2 "2024-02-28T04:42:37Z")

</div>

```julia
julia> g = 56::Int64
56

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

```

Please use lower case for string

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 28, 2024, 6:57am UTC](https://discourse.julialang.org/t/saving-information-to-a-log-file/110869/3 "2024-02-28T06:57:22Z")

</div>

You may be interested in the following.

[https://julialogging.github.io/how-to/log-to-file/](https://julialogging.github.io/how-to/log-to-file/)

---

<div class="post-metadata">

**Author:** ![mpeters2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpeters2/32/202247_2.png) [@mpeters2](https://discourse.julialang.org/u/mpeters2)\
**Post date:** [February 28, 2024, 10:02pm UTC](https://discourse.julialang.org/t/saving-information-to-a-log-file/110869/4 "2024-02-28T22:02:44Z")

</div>

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:

```julia
		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…
