@@ -477,50 +477,15 @@ function write_env_usage(source_file::AbstractString, usage_filepath::AbstractSt
477477 # Ensure that log dir exists
478478 ! ispath (logdir ()) && mkpath (logdir ())
479479
480- usage_file = joinpath (logdir (), usage_filepath)
481- timestamp = now ()
482-
483- # # Atomically write usage file
484- while true
485- # read existing usage file
486- usage = if isfile (usage_file)
487- TOML. parsefile (usage_file)
488- else
489- Dict {String, Any} ()
490- end
491-
492- # record new usage
493- usage[source_file] = [Dict (" time" => timestamp)]
494-
495- # keep only latest usage info
496- for k in keys (usage)
497- times = map (d -> Dates. DateTime (d[" time" ]), usage[k])
498- usage[k] = [Dict (" time" => maximum (times))]
499- end
500-
501- # Write to a temp file in the same directory as the destination
502- temp_usage_file = tempname (logdir ())
503- open (temp_usage_file, " w" ) do io
504- TOML. print (io, usage, sorted= true )
505- end
506-
507- # Move the temp file into place, replacing the original
508- mv (temp_usage_file, usage_file, force = true )
480+ # Generate entire entry as a string first
481+ entry = sprint () do io
482+ TOML. print (io, Dict (source_file => [Dict (" time" => now ())]))
483+ end
509484
510- # Check that the new file has what we want in it
511- new_usage = if isfile (usage_file)
512- TOML. parsefile (usage_file)
513- else
514- Dict {String, Any} ()
515- end
516- if haskey (new_usage, source_file)
517- for e in new_usage[source_file]
518- if Dates. DateTime (e[" time" ]) >= timestamp
519- return
520- end
521- end
522- end
523- # If not, try again
485+ # Append entry to log file in one chunk
486+ usage_file = joinpath (logdir (), usage_filepath)
487+ open (usage_file, append= true ) do io
488+ write (io, entry)
524489 end
525490end
526491
0 commit comments