Customization of log file

I need to execute a function saving a logfile.
More in detail, I need to add some custom logs into the diary logfile
Currently Im using the following instruction:
eval(['diary ', logfile])
%my function script
%... STEP 1
%... STEP 2
%... STEP3
% 0 (function end without error)
diary off
So, here after the following question:
  1. how add to the logfile created using the diary function some custom logs such as for example: STEP 1 completed, STEP 2 completed, STEP 3 Completed
  2. how capture the "0" end of running section, meaning that the function works without errors, and this value for closing my Logfile.
RESOLVED by using fopen, fprintf, and fclose for generating and append logs to my logfile.
I used a try - catch statment like following::
try
%
%DO SOMETHING
%
fprintf(fid, '%d\n', 0); %Right execution set = 0
catch ME
fprintf(fid, '%s\n', ME.identifier);
fprintf(fid, '%s\n', ME.message);
fprintf(fid, '%d\n', -1); %Error catched and appended to the logfile
end

2 Comments

Do you have to use a diary? Can't you just use fopen and fprintf (and fclose) to write to a log file?
I'm updating my functions on the FEX to allow exactly that: redirect errors and warnings to a log file instead of the command window.
You should post your answer as an answer. Then you can accept your answer.
If you want I can post the code I use to redirect errors and warnings. Those two functions are intended to be drop in replacements for any calls to warning() and error().

Sign in to comment.

Answers (0)

Categories

Products

Release

R2021b

Asked:

on 19 Nov 2021

Commented:

Rik
on 19 Nov 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!